-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡ ♻️ redesign field indexes for better performance
- only work with keys within the queried time-window - reduce memory footprint of the filtering function
- Loading branch information
Showing
5 changed files
with
86 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,44 @@ | ||
package storage | ||
|
||
import ( | ||
"encoding/json" | ||
"encoding/base64" | ||
"fmt" | ||
|
||
"github.com/dgraph-io/badger/v3" | ||
) | ||
|
||
type fieldIndex struct { | ||
txn *badger.Txn | ||
key []byte | ||
txn *badger.Txn | ||
keyPrefix []byte | ||
} | ||
|
||
func newFieldIndex(txn *badger.Txn, stream, field, value string) *fieldIndex { | ||
encodedValue := base64.StdEncoding.EncodeToString([]byte(value)) | ||
|
||
return &fieldIndex{ | ||
txn: txn, | ||
key: []byte(fmt.Sprintf("index:%s:field:%s:%s", stream, field, value)), | ||
keyPrefix: []byte(fmt.Sprintf( | ||
"index:%s:field:%s:%s:", | ||
stream, field, encodedValue, | ||
)), | ||
} | ||
} | ||
|
||
func (index *fieldIndex) AddKey(entryKey []byte) error { | ||
fieldKeys, err := index.load() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fieldKeys = append(fieldKeys, string(entryKey)) | ||
|
||
return index.store(fieldKeys) | ||
} | ||
|
||
func (index *fieldIndex) GetKeys() ([]string, error) { | ||
return index.load() | ||
} | ||
|
||
func (index *fieldIndex) load() ([]string, error) { | ||
fieldKeys := []string{} | ||
|
||
fieldIndexValue, err := index.txn.Get(index.key) | ||
if err == badger.ErrKeyNotFound { | ||
return fieldKeys, nil | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = fieldIndexValue.Value(func(val []byte) error { | ||
return json.Unmarshal(val, &fieldKeys) | ||
}) | ||
if err != nil { | ||
return nil, &UnmarshalError{Reason: err} | ||
} | ||
|
||
return fieldKeys, nil | ||
indexKey := []byte(fmt.Sprintf("%s%s", index.keyPrefix, entryKey)) | ||
return index.txn.Set(indexKey, []byte{}) | ||
} | ||
|
||
func (index *fieldIndex) store(fieldKeys []string) error { | ||
fieldKeysEncoded, err := json.Marshal(fieldKeys) | ||
if err != nil { | ||
return &MarshalError{Reason: err} | ||
} | ||
|
||
if err := index.txn.Set(index.key, fieldKeysEncoded); err != nil { | ||
return err | ||
func (index *fieldIndex) IterKeys(fn func(key string)) { | ||
opts := badger.DefaultIteratorOptions | ||
opts.PrefetchValues = false | ||
opts.Prefix = index.keyPrefix | ||
it := index.txn.NewIterator(opts) | ||
defer it.Close() | ||
|
||
for it.Rewind(); it.Valid(); it.Next() { | ||
indexKey := it.Item().Key() | ||
entryKey := string(indexKey[len(index.keyPrefix):]) | ||
fn(entryKey) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters