store: remove write TTL & expiry options (#1960)
This commit is contained in:
parent
f146b52418
commit
1ae825032c
@ -433,15 +433,9 @@ func (s *sqlStore) Write(r *store.Record, opts ...store.WriteOption) error {
|
||||
}
|
||||
|
||||
var expiry time.Time
|
||||
// expiry from options takes precedence
|
||||
if !options.Expiry.IsZero() {
|
||||
expiry = options.Expiry
|
||||
} else if r.Expiry != 0 {
|
||||
if r.Expiry != 0 {
|
||||
expiry = time.Now().Add(r.Expiry)
|
||||
}
|
||||
if options.TTL != 0 {
|
||||
expiry = time.Now().Add(options.TTL)
|
||||
}
|
||||
|
||||
if expiry.IsZero() {
|
||||
_, err = st.Exec(r.Key, r.Value, metadata, nil)
|
||||
|
@ -330,13 +330,6 @@ func (m *fileStore) Write(r *store.Record, opts ...store.WriteOption) error {
|
||||
newRecord.Metadata = make(map[string]interface{})
|
||||
newRecord.Expiry = r.Expiry
|
||||
|
||||
if !writeOpts.Expiry.IsZero() {
|
||||
newRecord.Expiry = time.Until(writeOpts.Expiry)
|
||||
}
|
||||
if writeOpts.TTL != 0 {
|
||||
newRecord.Expiry = writeOpts.TTL
|
||||
}
|
||||
|
||||
for k, v := range r.Metadata {
|
||||
newRecord.Metadata[k] = v
|
||||
}
|
||||
|
@ -238,13 +238,6 @@ func (m *memoryStore) Write(r *store.Record, opts ...store.WriteOption) error {
|
||||
copy(newRecord.Value, r.Value)
|
||||
newRecord.Expiry = r.Expiry
|
||||
|
||||
if !writeOpts.Expiry.IsZero() {
|
||||
newRecord.Expiry = time.Until(writeOpts.Expiry)
|
||||
}
|
||||
if writeOpts.TTL != 0 {
|
||||
newRecord.Expiry = writeOpts.TTL
|
||||
}
|
||||
|
||||
for k, v := range r.Metadata {
|
||||
newRecord.Metadata[k] = v
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Options contains configuration for the Store
|
||||
@ -108,10 +107,6 @@ func ReadOffset(o uint) ReadOption {
|
||||
// If Expiry and TTL are set TTL takes precedence
|
||||
type WriteOptions struct {
|
||||
Database, Table string
|
||||
// Expiry is the time the record expires
|
||||
Expiry time.Time
|
||||
// TTL is the time until the record expires
|
||||
TTL time.Duration
|
||||
}
|
||||
|
||||
// WriteOption sets values in WriteOptions
|
||||
@ -125,20 +120,6 @@ func WriteTo(database, table string) WriteOption {
|
||||
}
|
||||
}
|
||||
|
||||
// WriteExpiry is the time the record expires
|
||||
func WriteExpiry(t time.Time) WriteOption {
|
||||
return func(w *WriteOptions) {
|
||||
w.Expiry = t
|
||||
}
|
||||
}
|
||||
|
||||
// WriteTTL is the time the record expires
|
||||
func WriteTTL(d time.Duration) WriteOption {
|
||||
return func(w *WriteOptions) {
|
||||
w.TTL = d
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteOptions configures an individual Delete operation
|
||||
type DeleteOptions struct {
|
||||
Database, Table string
|
||||
|
@ -160,22 +160,24 @@ func runStoreTest(s store.Store, t *testing.T) {
|
||||
func readTests(s store.Store, t *testing.T) {
|
||||
// Test Table, Suffix and WriteOptions
|
||||
if err := s.Write(&store.Record{
|
||||
Key: "foofoobarbar",
|
||||
Value: []byte("something"),
|
||||
}, store.WriteTTL(time.Millisecond*100)); err != nil {
|
||||
Key: "foofoobarbar",
|
||||
Value: []byte("something"),
|
||||
Expiry: time.Millisecond * 100,
|
||||
}); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := s.Write(&store.Record{
|
||||
Key: "foofoo",
|
||||
Value: []byte("something"),
|
||||
}, store.WriteExpiry(time.Now().Add(time.Millisecond*100))); err != nil {
|
||||
Key: "foofoo",
|
||||
Value: []byte("something"),
|
||||
Expiry: time.Millisecond * 100,
|
||||
}); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := s.Write(&store.Record{
|
||||
Key: "barbar",
|
||||
Value: []byte("something"),
|
||||
// TTL has higher precedence than expiry
|
||||
}, store.WriteExpiry(time.Now().Add(time.Hour)), store.WriteTTL(time.Millisecond*100)); err != nil {
|
||||
Key: "barbar",
|
||||
Value: []byte("something"),
|
||||
Expiry: time.Millisecond * 100,
|
||||
}); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
@ -306,7 +308,7 @@ func expiryTests(s store.Store, t *testing.T) {
|
||||
t.Errorf("Expected %# v, got %# v", store.ErrNotFound, err)
|
||||
}
|
||||
|
||||
// exercise the different ways to write an expiry, record.expiry
|
||||
// exercise the records expiry
|
||||
s.Write(&store.Record{Key: "aaa", Value: []byte("bbb"), Expiry: 1 * time.Second})
|
||||
s.Write(&store.Record{Key: "aaaa", Value: []byte("bbb"), Expiry: 1 * time.Second})
|
||||
s.Write(&store.Record{Key: "aaaaa", Value: []byte("bbb"), Expiry: 1 * time.Second})
|
||||
@ -325,46 +327,6 @@ func expiryTests(s store.Store, t *testing.T) {
|
||||
if len(results) != 0 {
|
||||
t.Fatal("Results should have returned 0 records")
|
||||
}
|
||||
|
||||
// exercise the different ways to write an expiry, WriteExpiry
|
||||
s.Write(&store.Record{Key: "bbb", Value: []byte("bbb")}, store.WriteExpiry(time.Now().Add(1*time.Second)))
|
||||
s.Write(&store.Record{Key: "bbbb", Value: []byte("bbb")}, store.WriteExpiry(time.Now().Add(1*time.Second)))
|
||||
s.Write(&store.Record{Key: "bbbbb", Value: []byte("bbb")}, store.WriteExpiry(time.Now().Add(1*time.Second)))
|
||||
results, err = s.Read("b", store.ReadPrefix())
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if len(results) != 3 {
|
||||
t.Fatalf("Results should have returned 3 records. Received %d", len(results))
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
results, err = s.Read("b", store.ReadPrefix())
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if len(results) != 0 {
|
||||
t.Fatalf("Results should have returned 0 records. Received %d", len(results))
|
||||
}
|
||||
|
||||
// exercise the different ways to write an expiry, WriteTTL
|
||||
s.Write(&store.Record{Key: "ccc", Value: []byte("bbb")}, store.WriteTTL(1*time.Second))
|
||||
s.Write(&store.Record{Key: "cccc", Value: []byte("bbb")}, store.WriteTTL(1*time.Second))
|
||||
s.Write(&store.Record{Key: "ccccc", Value: []byte("bbb")}, store.WriteTTL(1*time.Second))
|
||||
results, err = s.Read("c", store.ReadPrefix())
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if len(results) != 3 {
|
||||
t.Fatalf("Results should have returned 3 records. Received %d", len(results))
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
results, err = s.Read("c", store.ReadPrefix())
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if len(results) != 0 {
|
||||
t.Fatalf("Results should have returned 0 records. Received %d", len(results))
|
||||
}
|
||||
}
|
||||
|
||||
func suffixPrefixExpiryTests(s store.Store, t *testing.T) {
|
||||
|
Loading…
Reference in New Issue
Block a user