Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding scan support #2

Merged
merged 1 commit into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions store/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Cache interface {
DeleteExpired()
OnEvicted(func(string, interface{}))
CloneItems() map[string]Item
Scan(func([]byte, []byte) error)
ItemCount() int
}

Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion store/disk/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 2 additions & 3 deletions store/disk/leveldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"errors"
"strconv"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -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
}
}
Expand Down
12 changes: 12 additions & 0 deletions store/hybrid/hybrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down