-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(keyvalue): add badger as keyvalue store
KeyValue store will be used by Incidents, so that the functions can decide based on history not just on the event that triggered them.
- Loading branch information
1 parent
e77764c
commit bb1ba5e
Showing
9 changed files
with
142 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ node_modules/ | |
|
||
# Database | ||
zdravko.db* | ||
zdravko_kv.db* | ||
temporal.db* | ||
|
||
# Keys | ||
|
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
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package kv | ||
|
||
import ( | ||
"time" | ||
|
||
badger "github.com/dgraph-io/badger/v4" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type BadgerKeyValueStore struct { | ||
db *badger.DB | ||
} | ||
|
||
func NewBadgerKeyValueStore(path string) (*BadgerKeyValueStore, error) { | ||
db, err := badger.Open(badger.DefaultOptions(path)) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to open badger db") | ||
} | ||
return &BadgerKeyValueStore{db: db}, nil | ||
} | ||
|
||
func (b *BadgerKeyValueStore) Close() error { | ||
return b.db.Close() | ||
} | ||
|
||
func (b *BadgerKeyValueStore) Set(key string, value []byte, ttl time.Duration) error { | ||
return b.db.Update(func(txn *badger.Txn) error { | ||
e := badger.NewEntry([]byte(key), value).WithTTL(ttl) | ||
return txn.SetEntry(e) | ||
}) | ||
} | ||
|
||
func (b *BadgerKeyValueStore) Increment(key string) (int, error) { | ||
var value int | ||
return value, b.db.Update(func(txn *badger.Txn) error { | ||
item, err := txn.Get([]byte(key)) | ||
if err != nil { | ||
return err | ||
} | ||
valCopy, err := item.ValueCopy(nil) | ||
if err != nil { | ||
return err | ||
} | ||
value = int(valCopy[0]) + 1 | ||
return txn.Set([]byte(key), []byte{byte(value)}) | ||
}) | ||
} | ||
|
||
func (b *BadgerKeyValueStore) Get(key string) ([]byte, error) { | ||
var value []byte | ||
return value, b.db.View(func(txn *badger.Txn) error { | ||
item, err := txn.Get([]byte(key)) | ||
if err != nil { | ||
return err | ||
} | ||
valCopy, err := item.ValueCopy(value) | ||
if err != nil { | ||
return err | ||
} | ||
value = valCopy | ||
return nil | ||
}) | ||
} | ||
|
||
func (b *BadgerKeyValueStore) Delete(key string) error { | ||
return b.db.Update(func(txn *badger.Txn) error { | ||
return txn.Delete([]byte(key)) | ||
}) | ||
} | ||
|
||
func (b *BadgerKeyValueStore) Keys(prefix string) ([]string, error) { | ||
var keys []string | ||
return keys, b.db.View(func(txn *badger.Txn) error { | ||
itr := txn.NewIterator(badger.DefaultIteratorOptions) | ||
defer itr.Close() | ||
for itr.Seek([]byte(prefix)); itr.ValidForPrefix([]byte(prefix)); itr.Next() { | ||
item := itr.Item() | ||
keys = append(keys, string(item.Key())) | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package kv | ||
|
||
import "time" | ||
|
||
type KeyValueStore interface { | ||
Close() error | ||
|
||
Get(key string) ([]byte, error) | ||
Set(key string, value []byte, ttl time.Duration) error | ||
Increment(key string) (int, error) | ||
Delete(key string) error | ||
Keys(prefix string) ([]string, error) | ||
} |
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