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()

}
Exemple #2
0
// Builds table from src iterator.
func (t *tOps) createFrom(src iterator.Iterator) (f *tFile, n int, err error) {
	w, err := t.create()
	if err != nil {
		return
	}

	defer func() {
		if err != nil {
			w.drop()
		}
	}()

	for src.Next() {
		err = w.append(src.Key(), src.Value())
		if err != nil {
			return
		}
	}
	err = src.Error()
	if err != nil {
		return
	}

	n = w.tw.EntriesLen()
	f, err = w.finish()
	return
}
Exemple #3
0
//Function: isExpireT
//Checkes whether the file is expired or not. If expired delete the file.
//Parameters:
//    fileName - file name
//    iterT - pointer database location
// Return:
//	  true - file expired
//	  false - file alive
func isExpireT(fileName string, iterT iterator.Iterator) bool {

	var expFlag int = 0
	var flag int = 0
	var f3 DBData

	key1 := iterT.Key()

	if string(key1) == fileName {
		flag = 1
	}

	if flag == 1 {
		tmp := iterT.Value()
		json.Unmarshal(tmp, &f3)
		var flagT1 int = f3.TFlag

		if flagT1 == 1 {
			timeT := f3.Life
			now := time.Now()
			tmp := timeT.Before(now)
			if tmp {
				expFlag = 1
			}
		} else {
			expFlag = 0
		}
	}

	if expFlag == 1 {
		return true
	} else {
		return false
	}
}
Exemple #4
0
func (h *blockHarness) testKV(act string, iter iterator.Iterator, i int) {
	key, value := h.kv[i], h.kv[i+1]
	if !bytes.Equal(iter.Key(), key) {
		h.t.Fatalf("block: %s: key invalid, want=%q got=%q", act, key, iter.Key())
	}
	if !bytes.Equal(iter.Value(), value) {
		h.t.Fatalf("block: %s: value invalid, want=%q got=%q", act, value, iter.Value())
	}
}
Exemple #5
0
func (h *blockHarness) last(iter iterator.Iterator) {
	if len(h.kv) == 0 {
		if iter.Last() {
			h.t.Fatalf("block: Last: expect eoi, key=%q", iter.Key())
		}
	} else {
		if !iter.Last() {
			h.t.Fatalf("block: Last: eoi, err=%v", iter.Error())
		}
	}
}
Exemple #6
0
// reads the next node record from the iterator, skipping over other
// database entries.
func nextNode(it iterator.Iterator) *Node {
	for end := false; !end; end = !it.Next() {
		id, field := splitKey(it.Key())
		if field != nodeDBDiscoverRoot {
			continue
		}
		var n Node
		if err := rlp.DecodeBytes(it.Value(), &n); err != nil {
			if glog.V(logger.Warn) {
				glog.Errorf("invalid node %x: %v", id, err)
			}
			continue
		}
		return &n
	}
	return nil
}
Exemple #7
0
// Get lookup for given key on the table. Get returns errors.ErrNotFound if
// given key did not exist.
func (t *Reader) Get(key []byte, ro opt.ReadOptionsGetter) (rkey, rvalue []byte, err error) {
	// create an iterator of index block
	index_iter := t.indexBlock.NewIterator()
	if !index_iter.Seek(key) {
		err = index_iter.Error()
		if err == nil {
			err = errors.ErrNotFound
		}
		return
	}

	// decode data block info
	bi := new(bInfo)
	_, err = bi.decodeFrom(index_iter.Value())
	if err != nil {
		return
	}

	// get the data block
	if t.filterBlock == nil || t.filterBlock.KeyMayMatch(uint(bi.offset), key) {
		var it iterator.Iterator
		var cache cache.Object
		it, cache, err = t.getDataIter(bi, ro)
		if err != nil {
			return
		}
		if cache != nil {
			defer cache.Release()
		}

		// seek to key
		if !it.Seek(key) {
			err = it.Error()
			if err == nil {
				err = errors.ErrNotFound
			}
			return
		}
		rkey, rvalue = it.Key(), it.Value()
	} else {
		err = errors.ErrNotFound
	}
	return
}
Exemple #8
0
// Make the actual query. Sanity checks of the iterator i is expected to
// have been done before calling this function.
func safeQuery(i iter.Iterator, req QueryRequest, res chan StoredEvent) {
	defer close(res)
	for i.Valid() {
		curKey, err := newEventStoreKey(i.Key())
		if err != nil {
			log.Println("A key could not be deserialized:")
			// Panicing here, because this error most
			// certainly needs to be looked at by a
			// an operator.
			log.Panicln(string(i.Key()))
		}

		if bytes.Compare(curKey.groupKey, eventPrefix) != 0 {
			break
		}
		if bytes.Compare(curKey.key, req.Stream) != 0 {
			break
		}

		resEvent := StoredEvent{
			curKey.keyId.toBytes(),
			Event{
				curKey.key,
				[]byte(i.Value()),
			},
		}
		res <- resEvent

		keyId := curKey.keyId.toBytes()
		if req.ToId != nil && bytes.Compare(req.ToId, keyId) == 0 {
			break
		}

		i.Next()
	}
}
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")
	}

}