コード例 #1
0
func list(c *cli.Context) {

	dbPath := c.GlobalString("path")

	pwd, err := os.Getwd()
	dbPath = path.Join(pwd, dbPath)

	options := &opt.Options{ErrorIfMissing: true}

	db, err := leveldb.OpenFile(dbPath, options)

	if err != nil {
		msg := fmt.Sprintf("%s %s", err, dbPath)
		panic(msg)
	}

	fmt.Println("using database:", dbPath)

	defer db.Close()

	var iter iterator.Iterator

	fmt.Println("Listing reading keys...")

	iter = db.NewIterator(&dbutil.Range{Start: []byte("/reading/RF12:"), Limit: []byte("/reading/RF12~")}, nil)
	for iter.Next() {
		key := string(iter.Key())
		fmt.Println(key)
	}
	iter.Release()

	//err = iter.Error()

}
コード例 #2
0
ファイル: db_test.go プロジェクト: baa-archieve/syncthing
func TestDb_ClosedIsClosed(t *testing.T) {
	h := newDbHarness(t)
	db := h.db

	var iter, iter2 iterator.Iterator
	var snap *Snapshot
	func() {
		defer h.close()

		h.put("k", "v")
		h.getVal("k", "v")

		iter = db.NewIterator(nil, h.ro)
		iter.Seek([]byte("k"))
		testKeyVal(t, iter, "k->v")

		var err error
		snap, err = db.GetSnapshot()
		if err != nil {
			t.Fatal("GetSnapshot: got error: ", err)
		}

		h.getValr(snap, "k", "v")

		iter2 = snap.NewIterator(nil, h.ro)
		iter2.Seek([]byte("k"))
		testKeyVal(t, iter2, "k->v")

		h.put("foo", "v2")
		h.delete("foo")

		// closing DB
		iter.Release()
		iter2.Release()
	}()

	assertErr(t, db.Put([]byte("x"), []byte("y"), h.wo), true)
	_, err := db.Get([]byte("k"), h.ro)
	assertErr(t, err, true)

	if iter.Valid() {
		t.Errorf("iter.Valid should false")
	}
	assertErr(t, iter.Error(), false)
	testKeyVal(t, iter, "->")
	if iter.Seek([]byte("k")) {
		t.Errorf("iter.Seek should false")
	}
	assertErr(t, iter.Error(), true)

	assertErr(t, iter2.Error(), false)

	_, err = snap.Get([]byte("k"), h.ro)
	assertErr(t, err, true)

	_, err = db.GetSnapshot()
	assertErr(t, err, true)

	iter3 := db.NewIterator(nil, h.ro)
	assertErr(t, iter3.Error(), true)

	iter3 = snap.NewIterator(nil, h.ro)
	assertErr(t, iter3.Error(), true)

	assertErr(t, db.Delete([]byte("k"), h.wo), true)

	_, err = db.GetProperty("leveldb.stats")
	assertErr(t, err, true)

	_, err = db.SizeOf([]util.Range{{[]byte("a"), []byte("z")}})
	assertErr(t, err, true)

	assertErr(t, db.CompactRange(util.Range{}), true)

	assertErr(t, db.Close(), true)
}
コード例 #3
0
func convertFrom(c *cli.Context) {
	success := true
	defaultBand := c.GlobalString("band")
	dbPath := c.GlobalString("path")

	pwd, err := os.Getwd()
	dbPath = path.Join(pwd, dbPath)

	options := &opt.Options{ErrorIfMissing: true}

	db, err := leveldb.OpenFile(dbPath, options)

	if err != nil {
		msg := fmt.Sprintf("%s %s", err, dbPath)
		panic(msg)
	}

	fmt.Println("using database:", dbPath)

	defer db.Close()

	var iter iterator.Iterator

	fmt.Println("Converting FROM band format...")

	iter = db.NewIterator(&dbutil.Range{Start: []byte("/reading/RF12:"), Limit: []byte("/reading/RF12~")}, nil)
	for iter.Next() {
		key := string(iter.Key())
		rval := iter.Value()

		kparts := strings.Split(key, "/")

		rfnet := kparts[len(kparts)-1:]
		rfparts := strings.Split(rfnet[0], ":")

		if len(rfparts) == 4 { //its an new format with band

			if rfparts[1] == defaultBand {
				fmt.Println("INFO:Downgrading:", key)

				//remove band
				copy(rfparts[1:], rfparts[1+1:])
				rfparts[len(rfparts)-1] = ""
				rfparts = rfparts[:len(rfparts)-1]

				//adjust the json structure
				var reading Reading
				err := json.Unmarshal(rval, &reading)
				if err == nil {
					id := fmt.Sprintf("%s", strings.Join(rfparts, ":"))
					reading.Id = id
					fmt.Println("INFO:New reading:", reading)
					data, err := json.Marshal(reading)
					if err == nil {

						//write a new key
						newkey := "/reading/" + id

						err = db.Put([]byte(newkey), []byte(data), nil)
						if err != nil {
							fmt.Println("ERR:Write failed for:", newkey)
							success = false
							continue
						}

						fmt.Println("INFO:New Reading Stored:", newkey)

						//remove old key
						err = db.Delete([]byte(key), nil)

						if err != nil {
							success = false
							fmt.Println("WARN: could not remove old key:", key)
						}

					}

				} else {
					fmt.Println("Err:Failed to unmarshal data:", err)
					success = false
				}

			}

		}

	}
	iter.Release()
	err = iter.Error()
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println("Downgrade finished")
	if !success {
		fmt.Println("problems detected, please review screen")
	}

}