diff --git a/store/cache/cache.go b/store/cache/cache.go index aa89362..550c004 100644 --- a/store/cache/cache.go +++ b/store/cache/cache.go @@ -19,6 +19,7 @@ type Cache interface { DeleteExpired() OnEvicted(func(string, interface{})) CloneItems() map[string]Item + Scan(func([]byte, []byte) error) ItemCount() int } @@ -144,6 +145,15 @@ func (c *CacheMemory) OnEvicted(f func(string, interface{})) { c.onEvicted = f } +func (c *CacheMemory) Scan(f func([]byte, []byte) error) { + c.mu.Lock() + defer c.mu.Unlock() + + for k, item := range c.Items { + f([]byte(k), item.Object.([]byte)) + } +} + func (c *CacheMemory) CloneItems() map[string]Item { c.mu.RLock() defer c.mu.RUnlock() diff --git a/store/disk/kv.go b/store/disk/kv.go index 46143d9..1003f80 100644 --- a/store/disk/kv.go +++ b/store/disk/kv.go @@ -33,5 +33,5 @@ type ScannerOptions struct { FetchValues bool // the handler that handles the incoming data - Handler func(k, v string) bool + Handler func(k []byte, v []byte) error } diff --git a/store/disk/leveldb.go b/store/disk/leveldb.go index 8ceeef1..1dde1cc 100644 --- a/store/disk/leveldb.go +++ b/store/disk/leveldb.go @@ -4,7 +4,6 @@ import ( "bytes" "errors" "strconv" - "strings" "sync" "time" @@ -218,8 +217,8 @@ func (ldb *LevelDB) Scan(scannerOpt ScannerOptions) error { for iter.Next() { key := iter.Key() - val := strings.SplitN(string(iter.Value()), ";", 2)[1] - if !valid(key) || !scannerOpt.Handler(string(key), string(val)) { + val := bytes.SplitN(iter.Value(), []byte(";"), 2)[1] + if !valid(key) || scannerOpt.Handler(key, val) != nil { break } } diff --git a/store/hybrid/hybrid.go b/store/hybrid/hybrid.go index 2057973..0d1dfcd 100644 --- a/store/hybrid/hybrid.go +++ b/store/hybrid/hybrid.go @@ -166,6 +166,18 @@ func (hm *HybridMap) Del(key string) error { return nil } +func (hm *HybridMap) Scan(f func([]byte, []byte) error) { + switch hm.options.Type { + case Memory: + hm.memorymap.Scan(f) + case Hybrid: + hm.memorymap.Scan(f) + hm.diskmap.Scan(disk.ScannerOptions{Handler: f}) + case Disk: + hm.diskmap.Scan(disk.ScannerOptions{Handler: f}) + } +} + func (hm *HybridMap) Size() int64 { return 0 }