-
Notifications
You must be signed in to change notification settings - Fork 193
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
TTL + table store #811
Merged
Merged
TTL + table store #811
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
6d899ae
Added TTL capability to table stores.
cody-littley f4c867a
Add TTL support to table store.
cody-littley 0e639f4
Test tweaks.
cody-littley 2392cb2
Remove TODO
cody-littley f03bf05
Use config for tablestore.
cody-littley c12f76a
Re-enabled TTL unit tests.
cody-littley 6708f47
Add test to cover multi-table TTL batches.
cody-littley a152101
lint
cody-littley d02d5e7
Made suggested changes.
cody-littley c74290e
Do not reuse table IDs.
cody-littley 4ddfa37
Made suggested change.
cody-littley 91e8ff8
Simplify batches.
cody-littley 10380e7
Refactored code, still need to fix unit tests.
cody-littley cdc173e
Fix iterators.
cody-littley 4d8a5fe
Disable tests to get things compiling.
cody-littley 56e3f65
Fix table builder bug.
cody-littley 3cf4385
Reenable remaining tests.
cody-littley f2a3b71
Fix low level store tests.
cody-littley cd4d725
Fix compile issue.
cody-littley 4510669
Add test for keys.
cody-littley 4b4547a
Made suggested change.
cody-littley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It looks this is not needed?
Table
, there isTableBatch
, operating on the raw keysTableStore
, there isTableStoreBatch
, operating on the namespaced keysAnd the ttl related interfaces are always there.
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 batch APIs have now been simplified so that they don't have generics. Are there additional simplifications you'd like to see?
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.
Note that we still need two batch interfaces if we want to support batches that can set different TTLs for operations within the same batch. The core reason is that the basic
Store
API does not have a concept of a TTL, meaning we need a batch interface that doesn't have TTLs.