mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-06-08 05:50:47 +00:00
Fetch by prefix from db.
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/syndtr/goleveldb/leveldb"
|
"github.com/syndtr/goleveldb/leveldb"
|
||||||
"github.com/syndtr/goleveldb/leveldb/filter"
|
"github.com/syndtr/goleveldb/leveldb/filter"
|
||||||
@@ -17,6 +18,7 @@ var (
|
|||||||
type Storage interface {
|
type Storage interface {
|
||||||
Get(key []byte) ([]byte, error)
|
Get(key []byte) ([]byte, error)
|
||||||
Put(key []byte, value []byte) error
|
Put(key []byte, value []byte) error
|
||||||
|
FetchByPrefix(prefix []byte) [][]byte
|
||||||
Close() error
|
Close() error
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,6 +60,25 @@ func (l *levelDB) Put(key []byte, value []byte) error {
|
|||||||
return l.db.Put(key, value, nil)
|
return l.db.Put(key, value, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *levelDB) FetchByPrefix(prefix []byte) [][]byte {
|
||||||
|
result := make([][]byte, 0, 20)
|
||||||
|
|
||||||
|
iterator := l.db.NewIterator(nil)
|
||||||
|
if iterator.Seek(prefix) {
|
||||||
|
for bytes.HasPrefix(iterator.Key(), prefix) {
|
||||||
|
val := iterator.Value()
|
||||||
|
valc := make([]byte, len(val))
|
||||||
|
copy(valc, val)
|
||||||
|
result = append(result, valc)
|
||||||
|
if !iterator.Next() {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
func (l *levelDB) Close() error {
|
func (l *levelDB) Close() error {
|
||||||
return l.db.Close()
|
return l.db.Close()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,3 +44,20 @@ func (s *LevelDBSuite) TestGetPut(c *C) {
|
|||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
c.Assert(result, DeepEquals, value)
|
c.Assert(result, DeepEquals, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *LevelDBSuite) TestFetchByPrefix(c *C) {
|
||||||
|
c.Check(s.db.FetchByPrefix([]byte{0x80}), DeepEquals, [][]byte{})
|
||||||
|
|
||||||
|
s.db.Put([]byte{0x80, 0x01}, []byte{0x01})
|
||||||
|
s.db.Put([]byte{0x80, 0x03}, []byte{0x03})
|
||||||
|
s.db.Put([]byte{0x80, 0x02}, []byte{0x02})
|
||||||
|
c.Check(s.db.FetchByPrefix([]byte{0x80}), DeepEquals, [][]byte{[]byte{0x01}, []byte{0x02}, []byte{0x03}})
|
||||||
|
|
||||||
|
s.db.Put([]byte{0x90, 0x01}, []byte{0x04})
|
||||||
|
c.Check(s.db.FetchByPrefix([]byte{0x80}), DeepEquals, [][]byte{[]byte{0x01}, []byte{0x02}, []byte{0x03}})
|
||||||
|
|
||||||
|
s.db.Put([]byte{0x00, 0x01}, []byte{0x05})
|
||||||
|
c.Check(s.db.FetchByPrefix([]byte{0x80}), DeepEquals, [][]byte{[]byte{0x01}, []byte{0x02}, []byte{0x03}})
|
||||||
|
|
||||||
|
c.Check(s.db.FetchByPrefix([]byte{0xa0}), DeepEquals, [][]byte{})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user