Exemple #1
0
func (db *LevelDB) GetAll(bucket []byte, sample interfaces.BinaryMarshallableAndCopyable) ([]interfaces.BinaryMarshallableAndCopyable, error) {
	db.dbLock.Lock()
	defer db.dbLock.Unlock()

	var fromKey []byte = bucket[:]
	var toKey []byte = bucket[:]
	toKey = addOneToByteArray(toKey)

	iter := db.lDB.NewIterator(&util.Range{Start: fromKey, Limit: toKey}, db.ro)

	answer := []interfaces.BinaryMarshallableAndCopyable{}

	for iter.Next() {
		v := iter.Value()
		tmp := sample.New()
		err := tmp.UnmarshalBinary(v)
		if err != nil {
			return nil, err
		}
		answer = append(answer, tmp)
	}
	iter.Release()
	err := iter.Error()
	if err != nil {
		return nil, err
	}

	return answer, nil
}
Exemple #2
0
func (db *LevelDB) GetAll(bucket []byte, sample interfaces.BinaryMarshallableAndCopyable) ([]interfaces.BinaryMarshallableAndCopyable, [][]byte, error) {
	db.dbLock.RLock()
	defer db.dbLock.RUnlock()

	ldbKey := ExtendBucket(bucket)

	var fromKey []byte = ldbKey[:]
	var toKey []byte = ldbKey[:]
	toKey = addOneToByteArray(toKey)

	iter := db.lDB.NewIterator(&util.Range{Start: fromKey, Limit: toKey}, db.ro)

	answer := []interfaces.BinaryMarshallableAndCopyable{}
	keys := [][]byte{}
	for iter.Next() {
		v := iter.Value()
		vCopy := make([]byte, len(v))
		copy(vCopy, v)
		tmp := sample.New()
		err := tmp.UnmarshalBinary(vCopy)
		if err != nil {
			return nil, nil, err
		}
		keys = append(keys, iter.Key()[len(ldbKey):])
		answer = append(answer, tmp)
	}
	iter.Release()
	err := iter.Error()
	if err != nil {
		return nil, nil, err
	}

	return answer, keys, nil
}
Exemple #3
0
func (db *BoltDB) GetAll(bucket []byte, sample interfaces.BinaryMarshallableAndCopyable) ([]interfaces.BinaryMarshallableAndCopyable, [][]byte, error) {
	db.Sem.Lock()
	defer db.Sem.Unlock()

	answer := []interfaces.BinaryMarshallableAndCopyable{}
	keys := [][]byte{}
	err := db.db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(bucket))
		if b == nil {
			//fmt.Println("bucket 0x" + hex.EncodeToString(bucket) + " not found")
		} else {
			b.ForEach(func(k, v []byte) error {
				tmp := sample.New()
				err := tmp.UnmarshalBinary(v)
				if err != nil {
					return err
				}
				keys = append(keys, k)
				answer = append(answer, tmp)
				return nil
			})
			return nil
		}
		return nil
	})
	if err != nil {
		return nil, nil, err
	}
	return answer, keys, nil
}
Exemple #4
0
func (db *MapDB) GetAll(bucket []byte, sample interfaces.BinaryMarshallableAndCopyable) ([]interfaces.BinaryMarshallableAndCopyable, [][]byte, error) {
	db.createCache(bucket)

	db.Sem.RLock()
	defer db.Sem.RUnlock()

	if db.Cache == nil {
		db.Cache = map[string]map[string][]byte{}
	}
	_, ok := db.Cache[string(bucket)]
	if ok == false {
		db.Cache[string(bucket)] = map[string][]byte{}
	}

	keys, err := db.ListAllKeys(bucket)
	if err != nil {
		return nil, nil, err
	}

	answer := []interfaces.BinaryMarshallableAndCopyable{}
	for _, k := range keys {
		tmp := sample.New()
		v := db.Cache[string(bucket)][string(k)]
		err := tmp.UnmarshalBinary(v)
		if err != nil {
			return nil, nil, err
		}
		answer = append(answer, tmp)
	}
	return answer, keys, nil
}