ledisdb/ledis/scan.go

107 lines
2.2 KiB
Go
Raw Normal View History

2014-08-14 11:24:41 +04:00
package ledis
import (
"bytes"
2014-08-14 11:24:41 +04:00
"errors"
"regexp"
2014-08-14 11:24:41 +04:00
)
var errDataType = errors.New("error data type")
var errMetaKey = errors.New("error meta key")
func (db *DB) scan(dataType byte, key []byte, count int, inclusive bool, match string) ([][]byte, error) {
2014-08-14 11:24:41 +04:00
var minKey, maxKey []byte
var err error
var r *regexp.Regexp
if len(match) > 0 {
if r, err = regexp.Compile(match); err != nil {
return nil, err
}
}
2014-08-26 19:21:45 +04:00
if len(key) > 0 {
2014-08-14 15:05:17 +04:00
if err = checkKeySize(key); err != nil {
2014-08-14 11:24:41 +04:00
return nil, err
}
2014-09-17 19:06:42 +04:00
if minKey, err = db.encodeScanKey(dataType, key); err != nil {
2014-08-14 11:24:41 +04:00
return nil, err
}
} else {
2014-09-17 19:06:42 +04:00
if minKey, err = db.encodeScanMinKey(dataType); err != nil {
2014-08-14 11:24:41 +04:00
return nil, err
}
}
2014-09-17 19:06:42 +04:00
if maxKey, err = db.encodeScanMaxKey(dataType); err != nil {
2014-08-14 11:24:41 +04:00
return nil, err
}
if count <= 0 {
count = defaultScanCount
}
v := make([][]byte, 0, count)
it := db.bucket.NewIterator()
it.Seek(minKey)
2014-08-14 11:24:41 +04:00
if !inclusive {
if it.Valid() && bytes.Equal(it.RawKey(), minKey) {
it.Next()
}
2014-08-14 11:24:41 +04:00
}
for i := 0; it.Valid() && i < count && bytes.Compare(it.RawKey(), maxKey) < 0; it.Next() {
2014-09-17 19:06:42 +04:00
if k, err := db.decodeScanKey(dataType, it.Key()); err != nil {
2014-08-14 11:24:41 +04:00
continue
} else if r != nil && !r.Match(k) {
continue
2014-08-14 11:24:41 +04:00
} else {
v = append(v, k)
i++
2014-08-14 11:24:41 +04:00
}
}
it.Close()
return v, nil
}
2014-09-17 19:06:42 +04:00
func (db *DB) encodeScanMinKey(dataType byte) ([]byte, error) {
return db.encodeScanKey(dataType, nil)
2014-08-14 11:24:41 +04:00
}
2014-09-17 19:06:42 +04:00
func (db *DB) encodeScanMaxKey(dataType byte) ([]byte, error) {
k, err := db.encodeScanKey(dataType, nil)
2014-08-14 15:05:17 +04:00
if err != nil {
return nil, err
}
2014-08-14 11:24:41 +04:00
k[len(k)-1] = dataType + 1
return k, nil
}
2014-09-17 19:06:42 +04:00
func (db *DB) encodeScanKey(dataType byte, key []byte) ([]byte, error) {
2014-08-14 11:24:41 +04:00
switch dataType {
case KVType:
return db.encodeKVKey(key), nil
case LMetaType:
return db.lEncodeMetaKey(key), nil
case HSizeType:
return db.hEncodeSizeKey(key), nil
case ZSizeType:
return db.zEncodeSizeKey(key), nil
case BitMetaType:
return db.bEncodeMetaKey(key), nil
2014-08-16 12:55:36 +04:00
case SSizeType:
return db.sEncodeSizeKey(key), nil
2014-08-14 11:24:41 +04:00
default:
2014-08-14 15:05:17 +04:00
return nil, errDataType
2014-08-14 11:24:41 +04:00
}
}
2014-09-17 19:06:42 +04:00
func (db *DB) decodeScanKey(dataType byte, ek []byte) ([]byte, error) {
2014-08-14 11:24:41 +04:00
if len(ek) < 2 || ek[0] != db.index || ek[1] != dataType {
return nil, errMetaKey
}
return ek[2:], nil
}