-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Cody Littley <[email protected]>
- Loading branch information
1 parent
2ed86a0
commit 410419a
Showing
24 changed files
with
1,211 additions
and
1,015 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,32 @@ | ||
package kvstore | ||
|
||
import "time" | ||
|
||
// Batch is a collection of key / value pairs that will be written atomically to a database. | ||
// Although it is thread safe to modify different batches in parallel or to modify a batch while | ||
// the store is being modified, it is not thread safe to concurrently modify the same batch. | ||
type Batch[K any] interface { | ||
// Put stores the given key / value pair in the batch, overwriting any existing value for that key. | ||
// If nil is passed as the value, a byte slice of length 0 will be stored. | ||
Put(key K, value []byte) | ||
// Delete removes the key from the batch. | ||
Delete(key K) | ||
// Apply atomically writes all the key / value pairs in the batch to the database. | ||
Apply() error | ||
// Size returns the number of operations in the batch. | ||
Size() uint32 | ||
} | ||
|
||
// TTLBatch is a collection of key / value pairs that will be written atomically to a database with | ||
// time-to-live (TTL) or expiration times. Although it is thread safe to modify different batches in | ||
// parallel or to modify a batch while the store is being modified, it is not thread safe to concurrently | ||
// modify the same batch. | ||
type TTLBatch[K any] interface { | ||
Batch[K] | ||
// PutWithTTL stores the given key / value pair in the batch with a time-to-live (TTL) or expiration time. | ||
// If nil is passed as the value, a byte slice of length 0 will be stored. | ||
PutWithTTL(key K, value []byte, ttl time.Duration) | ||
// PutWithExpiration stores the given key / value pair in the batch with an expiration time. | ||
// If nil is passed as the value, a byte slice of length 0 will be stored. | ||
PutWithExpiration(key K, value []byte, expiryTime time.Time) | ||
} |
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,27 @@ | ||
package kvstore | ||
|
||
import "errors" | ||
|
||
// ErrInvalidKey is returned when a key cannot be interpreted as the requested type. | ||
var ErrInvalidKey = errors.New("invalid key") | ||
|
||
// Key represents a key in a TableStore. Each key is scoped to a specific table. | ||
type Key interface { | ||
// Bytes returns the key as a byte slice. Does not include internal metadata (i.e. the table). | ||
Bytes() []byte | ||
// Raw returns the raw byte slice that represents the key. This value | ||
// may not be equal to the byte slice that was used to create the key, and | ||
// should be treated as an opaque value. | ||
Raw() []byte | ||
// Builder returns the KeyBuilder that created this key. | ||
Builder() KeyBuilder | ||
} | ||
|
||
// KeyBuilder is used to create keys for a TableStore. Each KeyBuilder is scoped to a particular table, | ||
// and can be used to create keys that are within that table. | ||
type KeyBuilder interface { | ||
// TableName returns the name of the table that this KeyBuilder is scoped to. | ||
TableName() string | ||
// Key creates a key from a byte slice. | ||
Key(key []byte) Key | ||
} |
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,60 @@ | ||
package tablestore | ||
|
||
import "time" | ||
|
||
// StoreType describes the underlying store implementation. | ||
type StoreType int | ||
|
||
const ( | ||
// LevelDB is a LevelDB-backed store. | ||
LevelDB StoreType = iota | ||
// MapStore is an in-memory store. This store does not preserve data across restarts. | ||
MapStore | ||
) | ||
|
||
// Config is the configuration for a TableStore. | ||
type Config struct { | ||
// The type of the base store. Default is LevelDB. | ||
Type StoreType | ||
// The path to the file system directory where the store will write its data. Default is nil. | ||
// Some store implementations may ignore this field (e.g. MapStore). Other store implementations may require | ||
// this field to be set (e.g. LevelDB). | ||
Path *string | ||
// If true, the store will perform garbage collection on a background goroutine. Default is true. | ||
GarbageCollectionEnabled bool | ||
// If garbage collection is enabled, this is the interval at which it will run. Default is 5 minutes. | ||
GarbageCollectionInterval time.Duration | ||
// If garbage collection is enabled, this is the maximum number of entries to delete in a single batch during | ||
// garbage collection. Default is 1024. | ||
GarbageCollectionBatchSize uint32 | ||
// The list of tables to create on startup. Any pre-existing table not in this list will be deleted. If | ||
// this list is nil, the previous schema will be carried forward with no modifications. Default is nil. | ||
Schema []string | ||
} | ||
|
||
// DefaultConfig returns a Config with default values. | ||
func DefaultConfig() *Config { | ||
return &Config{ | ||
Type: LevelDB, | ||
Path: nil, | ||
GarbageCollectionEnabled: true, | ||
GarbageCollectionInterval: 5 * time.Minute, | ||
GarbageCollectionBatchSize: 1024, | ||
Schema: nil, | ||
} | ||
} | ||
|
||
// DefaultLevelDBConfig returns a Config with default values for a LevelDB store. | ||
func DefaultLevelDBConfig(path string) *Config { | ||
config := DefaultConfig() | ||
config.Type = LevelDB | ||
config.Path = &path | ||
return config | ||
} | ||
|
||
// DefaultMapStoreConfig returns a Config with default values for a MapStore. | ||
func DefaultMapStoreConfig() *Config { | ||
config := DefaultConfig() | ||
config.Type = MapStore | ||
return config | ||
} |
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,27 @@ | ||
package tablestore | ||
|
||
import ( | ||
tu "github.com/Layr-Labs/eigenda/common/testutils" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestGetName(t *testing.T) { | ||
tu.InitializeRandom() | ||
|
||
tableName := tu.RandomString(10) | ||
|
||
kb := newKeyBuilder(tableName, 0) | ||
assert.Equal(t, tableName, kb.TableName()) | ||
} | ||
|
||
func TestBytesRoundTrip(t *testing.T) { | ||
tu.InitializeRandom() | ||
|
||
tableName := tu.RandomString(10) | ||
b := tu.RandomBytes(10) | ||
|
||
kb := newKeyBuilder(tableName, 0) | ||
k := kb.Key(b) | ||
assert.Equal(t, b, k.Bytes()) | ||
} |
Oops, something went wrong.