Skip to content

Commit

Permalink
implemented a method KeysWithFilters to return keys matching specifie…
Browse files Browse the repository at this point in the history
…d filters and ListKeysWithFilters to provide an iterable list of keys (as suggested). And additionally defined KeyIterator for iterating over keys and a utility function matchesFilters to check if a key meets all filter criteria.
  • Loading branch information
Darshan174 committed Jun 20, 2024
1 parent 32215ca commit 2f728c4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
57 changes: 57 additions & 0 deletions jetstream/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,63 @@ func (js *jetStream) CreateKeyValue(ctx context.Context, cfg KeyValueConfig) (Ke
return nil, err
}

// KeysWithFilters returns a filtered list of keys in the bucket.
func (js *jetStream) KeysWithFilters(filter []string) ([]string, error) {
// Fetch all keys.
allKeys := []string{/* list of all keys */}
var filteredKeys []string

for _, key := range allKeys {
if matchesFilters(key, filter) {
filteredKeys = append(filteredKeys, key)
}
}

return filteredKeys, nil
}

// ListKeysWithFilters returns an iterable list of keys in the bucket that match the given filters.
func (js *jetStream) ListKeysWithFilters(filter []string) (KeyLister, error) {
keys, err := js.KeysWithFilters(filter)
if err != nil {
return nil, err
}

return NewKeyIterator(keys), nil
}

// matchesFilters checks if a key matches all provided filters.
func matchesFilters(key string, filters []string) bool {
for _, filter := range filters {
if !strings.Contains(key, filter) {
return false
}
}
return true
}

// KeyIterator is a simple iterator for keys.
type KeyIterator struct {
keys []string
index int
}

// NewKeyIterator creates a new KeyIterator.
func NewKeyIterator(keys []string) *KeyIterator {
return &KeyIterator{keys: keys, index: -1}
}

// Next advances the iterator and returns the next key.
func (it *KeyIterator) Next() (string, bool) {
it.index++
if it.index >= len(it.keys) {
return "", false
}
return it.keys[it.index], true
}



stream, err := js.CreateStream(ctx, scfg)
if err != nil {
if errors.Is(err, ErrStreamNameAlreadyInUse) {
Expand Down
8 changes: 0 additions & 8 deletions kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,6 @@ type KeyValue interface {
PurgeDeletes(opts ...PurgeOpt) error
// Status retrieves the status and configuration of a bucket
Status() (KeyValueStatus, error)
// KeysWithFilter returns a filtered list of keys in the bucket.
KeysWithFilter(filter string) ([]string, error)
// KeysWithFilters returns a filtered list of keys in the bucket.
KeysWithFilters(filter []string) ([]string, error)
// And for the new, iterable API:
ListKeysWithFilter(filter string) (KeyLister, error)
ListKeysWithFilters(filter []string) (KeyLister, error)

}

// KeyValueStatus is run-time status about a Key-Value bucket
Expand Down

0 comments on commit 2f728c4

Please sign in to comment.