Esempio n. 1
0
func NewLevelDB(filename string, create bool) (interfaces.IDatabase, error) {
	db := new(LevelDB)
	var err error

	var tlDB *leveldb.DB

	if create == true {
		err = os.MkdirAll(filename, 0750)
		if err != nil {
			return nil, err
		}
	} else {
		_, err = os.Stat(filename)
		if err != nil {
			return nil, err
		}
	}

	opts := &opt.Options{
		Compression: opt.NoCompression,
	}

	tlDB, err = leveldb.OpenFile(filename, opts)
	if err != nil {
		return nil, err
	}
	db.lDB = tlDB

	return db, nil
}
Esempio n. 2
0
// This program is used to compare two databases - one from server and the other one from client
func main() {

	ro := &opt.ReadOptions{}
	//wo := &opt.WriteOptions{}
	opts := &opt.Options{}

	ldb, err := leveldb.OpenFile(dbpath, opts)
	fmt.Println("started db from ", dbpath)
	if err != nil {
		fmt.Printf("db open failed %v\n", err)
		return
	}

	ldb2, err := leveldb.OpenFile(dbpath2, opts)
	fmt.Println("started db2 from ", dbpath2)
	if err != nil {
		fmt.Printf("db2 open failed %v\n", err)
		return
	}

	elice := make([]*tst, 0, 10)

	iter := ldb.NewIterator(&util.Range{Start: nil, Limit: nil}, ro)

	for iter.Next() {
		key := iter.Key()
		value := iter.Value()
		t := new(tst)

		value2, _ := ldb2.Get(key, ro)

		if value2 == nil {
			fmt.Printf("key:%v\n", common.EncodeBinary(&key))
		} else if bytes.Compare(value, value2) != 0 {
			fmt.Printf("Key with different values:%v\n", common.EncodeBinary(&key))
			fmt.Printf("value1:%v\n", common.EncodeBinary(&value))
			fmt.Printf("value2:%v\n", common.EncodeBinary(&value2))
		}

		elice = append(elice, t)

	}

	fmt.Printf("len(elice):%v", len(elice))
	fmt.Printf("completed\n")
	ldb.Close()
}
Esempio n. 3
0
func main() {

	ro := &opt.ReadOptions{}
	//wo := &opt.WriteOptions{}
	opts := &opt.Options{}

	ldb, err := leveldb.OpenFile(dbpath, opts)
	fmt.Println("started db from ", dbpath)
	if err != nil {
		fmt.Printf("db open failed %v\n", err)
		return
	}

	elice := make([]*tst, 0, 10)

	//var fromkey [] byte = []byte{byte(2)} 		  	// Table Name (4 bytes)
	//var tokey [] byte = []byte{byte(51)} 		  	// Table Name (4 bytes)
	iter := ldb.NewIterator(&util.Range{Start: nil, Limit: nil}, ro)

	for iter.Next() {
		key := iter.Key()

		fmt.Printf("key: %v\n", common.EncodeBinary(&key))
		//		fmt.Println("  value:%v", iter.Value())

		buf := iter.Value()
		var buf2 []byte

		buf2 = buf

		fmt.Printf("value: ")
		//		fmt.Println(spew.Sdump(iter.Value()))
		spew.Dump(buf2)

		t := new(tst)

		//t.key = binary.BigEndian.Uint32(key[:4])
		//buf := bytes.NewBuffer(key)
		//binary.Read(buf, binary.BigEndian, &t.key)

		//fmt.Println("t.key:%v", t.key)
		elice = append(elice, t)

	}

	fmt.Printf("len(elice):%v", len(elice))
	fmt.Printf("completed\n")
	ldb.Close()
}
Esempio n. 4
0
func openDB(dbpath string, create bool) (pbdb database.Db, err error) {
	var db LevelDb
	var tlDb *leveldb.DB
	var dbversion int32

	defer func() {
		if err == nil {
			db.lDb = tlDb

			// Initialize db
			db.lastDirBlkHeight = -1

			pbdb = &db
		}
	}()

	if create == true {
		err = os.MkdirAll(dbpath, 0750)
		if err != nil {
			log.Println("mkdir failed %v %v", dbpath, err)
			return
		}
	} else {
		_, err = os.Stat(dbpath)
		if err != nil {
			return
		}
	}

	needVersionFile := false
	verfile := dbpath + ".ver"
	fi, ferr := os.Open(verfile)
	if ferr == nil {
		defer fi.Close()

		ferr = binary.Read(fi, binary.BigEndian, &dbversion)
		if ferr != nil {
			dbversion = ^0
		}
	} else {
		if create == true {
			needVersionFile = true
			dbversion = CurrentDBVersion
		}
	}

	opts := &opt.Options{
		Compression: opt.NoCompression,
	}

	switch dbversion {
	case 0:
		opts = &opt.Options{}
	case 1:
		// uses defaults from above
	default:
		err = fmt.Errorf("unsupported db version %v", dbversion)
		return
	}

	tlDb, err = leveldb.OpenFile(dbpath, opts)
	if err != nil {
		return
	}

	// If we opened the database successfully on 'create'
	// update the
	if needVersionFile {
		fo, ferr := os.Create(verfile)
		if ferr != nil {
			// TODO(design) close and delete database?
			err = ferr
			return
		}
		defer fo.Close()
		err = binary.Write(fo, binary.BigEndian, dbversion)
		if err != nil {
			return
		}
	}

	return
}