From 1ae825032c5f98b74b65d02e97b282f28071a0d3 Mon Sep 17 00:00:00 2001 From: ben-toogood Date: Fri, 21 Aug 2020 09:35:53 +0100 Subject: [PATCH] store: remove write TTL & expiry options (#1960) --- store/cockroach/cockroach.go | 8 +---- store/file/file.go | 7 ---- store/memory/memory.go | 7 ---- store/options.go | 19 ----------- store/test/store_test.go | 64 ++++++++---------------------------- 5 files changed, 14 insertions(+), 91 deletions(-) diff --git a/store/cockroach/cockroach.go b/store/cockroach/cockroach.go index 8a15c4da..5e31cc7d 100644 --- a/store/cockroach/cockroach.go +++ b/store/cockroach/cockroach.go @@ -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) diff --git a/store/file/file.go b/store/file/file.go index d6633295..5a963156 100644 --- a/store/file/file.go +++ b/store/file/file.go @@ -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 } diff --git a/store/memory/memory.go b/store/memory/memory.go index bc20030b..66b8ef2d 100644 --- a/store/memory/memory.go +++ b/store/memory/memory.go @@ -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 } diff --git a/store/options.go b/store/options.go index 27224731..ba2f3be5 100644 --- a/store/options.go +++ b/store/options.go @@ -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 diff --git a/store/test/store_test.go b/store/test/store_test.go index 7cea5084..6593f984 100644 --- a/store/test/store_test.go +++ b/store/test/store_test.go @@ -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) {