-
Notifications
You must be signed in to change notification settings - Fork 2
Add key-only ScanKeys interface #234
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,6 +95,34 @@ func (s *boltStore) Scan(ctx context.Context, start []byte, end []byte, limit in | |
| return res, errors.WithStack(err) | ||
| } | ||
|
|
||
| func (s *boltStore) ScanKeys(ctx context.Context, start []byte, end []byte, limit int) ([][]byte, error) { | ||
| s.log.InfoContext(ctx, "ScanKeys", | ||
| slog.String("start", string(start)), | ||
| slog.String("end", string(end)), | ||
| slog.Int("limit", limit), | ||
| ) | ||
|
|
||
| var res [][]byte | ||
|
|
||
| err := s.bbolt.View(func(tx *bbolt.Tx) error { | ||
| b := tx.Bucket(defaultBucket) | ||
| if b == nil { | ||
| return nil | ||
| } | ||
|
|
||
| c := b.Cursor() | ||
| for k, _ := c.Seek(start); k != nil && (end == nil || bytes.Compare(k, end) < 0); k, _ = c.Next() { | ||
| res = append(res, k) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The key keyCopy := make([]byte, len(k))
copy(keyCopy, k)
res = append(res, keyCopy)Style Guide ReferencesFootnotes
|
||
| if len(res) >= limit { | ||
|
Comment on lines
+113
to
+116
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P1] Copy Bolt scan keys before returning them The Bolt implementation of Useful? React with 👍 / 👎. |
||
| break | ||
| } | ||
| } | ||
| return nil | ||
| }) | ||
|
|
||
| return res, errors.WithStack(err) | ||
| } | ||
|
|
||
| func (s *boltStore) Put(ctx context.Context, key []byte, value []byte) error { | ||
| s.log.InfoContext(ctx, "put", | ||
| slog.String("key", string(key)), | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci] reported by reviewdog 🐶 elastickv/store/rb_memory_store.go Line 287 in 77b6072
|
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -128,6 +128,40 @@ func (s *rbMemoryStore) Scan(ctx context.Context, start []byte, end []byte, limi | |||||
| return result, nil | ||||||
| } | ||||||
|
|
||||||
| func (s *rbMemoryStore) ScanKeys(ctx context.Context, start []byte, end []byte, limit int) ([][]byte, error) { | ||||||
| s.mtx.RLock() | ||||||
| defer s.mtx.RUnlock() | ||||||
|
|
||||||
| var result [][]byte | ||||||
|
|
||||||
| it := s.tree.Iterator() | ||||||
|
|
||||||
| var ok bool | ||||||
| if start != nil { | ||||||
| it.Begin() | ||||||
| ok = it.NextTo(func(key, _ interface{}) bool { | ||||||
| k, _ := key.([]byte) | ||||||
| return bytes.Compare(k, start) >= 0 | ||||||
| }) | ||||||
| } else { | ||||||
| ok = it.First() | ||||||
| } | ||||||
|
Comment on lines
+140
to
+148
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use of While the |
||||||
|
|
||||||
| for ; ok && len(result) < limit; ok = it.Next() { | ||||||
| k, _ := it.Key().([]byte) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The blank identifier
Suggested change
|
||||||
|
|
||||||
| if end != nil && bytes.Compare(k, end) > 0 { | ||||||
| break | ||||||
| } | ||||||
|
|
||||||
| keyCopy := make([]byte, len(k)) | ||||||
| copy(keyCopy, k) | ||||||
| result = append(result, keyCopy) | ||||||
| } | ||||||
|
|
||||||
| return result, nil | ||||||
| } | ||||||
|
|
||||||
| func (s *rbMemoryStore) Put(ctx context.Context, key []byte, value []byte) error { | ||||||
| s.mtx.Lock() | ||||||
| defer s.mtx.Unlock() | ||||||
|
|
||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci] reported by reviewdog 🐶 elastickv/store/rb_memory_store_test.go Line 280 in 77b6072
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The BoltDB cursor returns slices that reference the underlying database memory. These keys need to be copied to prevent data corruption when the transaction ends, similar to how the memory store implementation copies keys.