store: remove write TTL & expiry options (#1960)

This commit is contained in:
ben-toogood 2020-08-21 09:35:53 +01:00 committed by GitHub
parent f146b52418
commit 1ae825032c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 14 additions and 91 deletions

View File

@ -433,15 +433,9 @@ func (s *sqlStore) Write(r *store.Record, opts ...store.WriteOption) error {
} }
var expiry time.Time var expiry time.Time
// expiry from options takes precedence if r.Expiry != 0 {
if !options.Expiry.IsZero() {
expiry = options.Expiry
} else if r.Expiry != 0 {
expiry = time.Now().Add(r.Expiry) expiry = time.Now().Add(r.Expiry)
} }
if options.TTL != 0 {
expiry = time.Now().Add(options.TTL)
}
if expiry.IsZero() { if expiry.IsZero() {
_, err = st.Exec(r.Key, r.Value, metadata, nil) _, err = st.Exec(r.Key, r.Value, metadata, nil)

View File

@ -330,13 +330,6 @@ func (m *fileStore) Write(r *store.Record, opts ...store.WriteOption) error {
newRecord.Metadata = make(map[string]interface{}) newRecord.Metadata = make(map[string]interface{})
newRecord.Expiry = r.Expiry 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 { for k, v := range r.Metadata {
newRecord.Metadata[k] = v newRecord.Metadata[k] = v
} }

View File

@ -238,13 +238,6 @@ func (m *memoryStore) Write(r *store.Record, opts ...store.WriteOption) error {
copy(newRecord.Value, r.Value) copy(newRecord.Value, r.Value)
newRecord.Expiry = r.Expiry 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 { for k, v := range r.Metadata {
newRecord.Metadata[k] = v newRecord.Metadata[k] = v
} }

View File

@ -2,7 +2,6 @@ package store
import ( import (
"context" "context"
"time"
) )
// Options contains configuration for the Store // Options contains configuration for the Store
@ -108,10 +107,6 @@ func ReadOffset(o uint) ReadOption {
// If Expiry and TTL are set TTL takes precedence // If Expiry and TTL are set TTL takes precedence
type WriteOptions struct { type WriteOptions struct {
Database, Table string 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 // 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 // DeleteOptions configures an individual Delete operation
type DeleteOptions struct { type DeleteOptions struct {
Database, Table string Database, Table string

View File

@ -162,20 +162,22 @@ func readTests(s store.Store, t *testing.T) {
if err := s.Write(&store.Record{ if err := s.Write(&store.Record{
Key: "foofoobarbar", Key: "foofoobarbar",
Value: []byte("something"), Value: []byte("something"),
}, store.WriteTTL(time.Millisecond*100)); err != nil { Expiry: time.Millisecond * 100,
}); err != nil {
t.Error(err) t.Error(err)
} }
if err := s.Write(&store.Record{ if err := s.Write(&store.Record{
Key: "foofoo", Key: "foofoo",
Value: []byte("something"), Value: []byte("something"),
}, store.WriteExpiry(time.Now().Add(time.Millisecond*100))); err != nil { Expiry: time.Millisecond * 100,
}); err != nil {
t.Error(err) t.Error(err)
} }
if err := s.Write(&store.Record{ if err := s.Write(&store.Record{
Key: "barbar", Key: "barbar",
Value: []byte("something"), Value: []byte("something"),
// TTL has higher precedence than expiry Expiry: time.Millisecond * 100,
}, store.WriteExpiry(time.Now().Add(time.Hour)), store.WriteTTL(time.Millisecond*100)); err != nil { }); err != nil {
t.Error(err) t.Error(err)
} }
@ -306,7 +308,7 @@ func expiryTests(s store.Store, t *testing.T) {
t.Errorf("Expected %# v, got %# v", store.ErrNotFound, err) 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: "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: "aaaa", Value: []byte("bbb"), Expiry: 1 * time.Second})
s.Write(&store.Record{Key: "aaaaa", 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 { if len(results) != 0 {
t.Fatal("Results should have returned 0 records") 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) { func suffixPrefixExpiryTests(s store.Store, t *testing.T) {