From 75c0629e6957932c7bf485123764f070b44719ca Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Fri, 12 Jan 2018 23:13:59 +0100 Subject: added timestamp index --- src/hub/src/spreadspace.org/sfive/s5store.go | 19 ++++++++- src/hub/src/spreadspace.org/sfive/s5store_test.go | 49 +++++++++++++++++++++++ src/hub/src/spreadspace.org/sfive/s5typesStore.go | 2 +- 3 files changed, 67 insertions(+), 3 deletions(-) (limited to 'src/hub') diff --git a/src/hub/src/spreadspace.org/sfive/s5store.go b/src/hub/src/spreadspace.org/sfive/s5store.go index 62b3987..20eea10 100644 --- a/src/hub/src/spreadspace.org/sfive/s5store.go +++ b/src/hub/src/spreadspace.org/sfive/s5store.go @@ -349,8 +349,23 @@ func (st *Store) insertClients(tx *bolt.Tx, uID int64, cd []Client) error { } func (st *Store) updateTimestampIndex(tx *bolt.Tx, ts time.Time, uID int64) error { - // TODO: implement this - return nil + tsb := tx.Bucket([]byte(timestampsIdxBn)) + + ts_utc := ts.UTC() + var buf [10]byte + b, err := tsb.CreateBucketIfNotExists(ts_utc.AppendFormat(buf[:0], "2006-01-02")) + if err != nil { + return err + } + + k := itob(ts_utc.UnixNano()) + old := b.Get(k) + if old == nil { + return b.Put(k, idList{uID}.toBytes()) + } + + new := appendToBinaryIDList(old, uID) + return b.Put(k, new) } func (st *Store) setLastUpdateForUUID(tx *bolt.Tx, uuid string, uID int64) error { diff --git a/src/hub/src/spreadspace.org/sfive/s5store_test.go b/src/hub/src/spreadspace.org/sfive/s5store_test.go index fd49d81..bd6c85f 100644 --- a/src/hub/src/spreadspace.org/sfive/s5store_test.go +++ b/src/hub/src/spreadspace.org/sfive/s5store_test.go @@ -431,6 +431,55 @@ func TestAppendAndFetch(t *testing.T) { } } +func TestTimestampIndex(t *testing.T) { + os.Remove(testBoltPath) + store, err := NewStore(StoreConfig{testBoltPath, false}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + defer store.Close() + + upd := updateData + upd.StartTime = time.Date(2014, time.August, 24, 14, 35, 33, 847000000, time.UTC) + upd.Data.Clients = clientsData + in := &UpdateFull{Header{0, "", -1, "", -1}, sourceData, upd} + + if err = store.Append(in); err != nil { + t.Fatalf("failed to append update: %v", err) + } + if err = store.Append(in); err != nil { + t.Fatalf("failed to append update: %v", err) + } + in.Update.StartTime = in.Update.StartTime.Add(1 * time.Hour) + if err = store.Append(in); err != nil { + t.Fatalf("failed to append update: %v", err) + } + in.Update.StartTime = in.Update.StartTime.Add(24 * time.Hour) + if err = store.Append(in); err != nil { + t.Fatalf("failed to append update: %v", err) + } + + err = store.db.View(func(tx *bolt.Tx) error { + tsb := tx.Bucket([]byte(timestampsIdxBn)) + + db24 := tsb.Bucket([]byte("2014-08-24")) + if db24 == nil { + t.Fatalf("the append should have created an bucket for 24.8.2014") + } + db25 := tsb.Bucket([]byte("2014-08-25")) + if db25 == nil { + t.Fatalf("the append should have created an bucket for 25.8.2014") + } + + // TODO: check if db24 and db25 contain the correct entries + + return nil + }) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } +} + func TestReadOnly(t *testing.T) { // create read-only db from not-existing file must fail os.Remove(testBoltPath) diff --git a/src/hub/src/spreadspace.org/sfive/s5typesStore.go b/src/hub/src/spreadspace.org/sfive/s5typesStore.go index b046ce9..b6e9b66 100644 --- a/src/hub/src/spreadspace.org/sfive/s5typesStore.go +++ b/src/hub/src/spreadspace.org/sfive/s5typesStore.go @@ -64,7 +64,7 @@ const ( userAgentsRevBn = "UserAgentsRev" // index buckets - timestampsIdxBn = "_timestamps" + timestampsIdxBn = "timestampsIdx" // well-known keys hubUUIDKey = "HubUUID" -- cgit v1.2.3