From 87a21838657519a4702ad21074ea76ee88172af8 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Fri, 12 Jan 2018 19:23:13 +0100 Subject: added helper functions for timestamp index --- src/hub/src/spreadspace.org/sfive/s5store.go | 9 +++++ src/hub/src/spreadspace.org/sfive/s5store_test.go | 47 +++++++++++++++++++++++ src/hub/src/spreadspace.org/sfive/s5typesStore.go | 30 +++++++++++++++ 3 files changed, 86 insertions(+) (limited to 'src') diff --git a/src/hub/src/spreadspace.org/sfive/s5store.go b/src/hub/src/spreadspace.org/sfive/s5store.go index dda9247..62b3987 100644 --- a/src/hub/src/spreadspace.org/sfive/s5store.go +++ b/src/hub/src/spreadspace.org/sfive/s5store.go @@ -348,6 +348,11 @@ func (st *Store) insertClients(tx *bolt.Tx, uID int64, cd []Client) error { return b.Put(itob(uID), jsonData) } +func (st *Store) updateTimestampIndex(tx *bolt.Tx, ts time.Time, uID int64) error { + // TODO: implement this + return nil +} + func (st *Store) setLastUpdateForUUID(tx *bolt.Tx, uuid string, uID int64) error { b := tx.Bucket([]byte(latestUpdatesBn)) b.FillPercent = 1.0 // we only do appends @@ -378,6 +383,10 @@ func (st *Store) appendItem(tx *bolt.Tx, uf *UpdateFull) (uID int64, err error) return } + if err = st.updateTimestampIndex(tx, uf.StartTime, uID); err != nil { + return + } + if uf.SourceHubUUID != "" { err = st.setLastUpdateForUUID(tx, uf.SourceHubUUID, u.SourceHubUpdateID) } diff --git a/src/hub/src/spreadspace.org/sfive/s5store_test.go b/src/hub/src/spreadspace.org/sfive/s5store_test.go index 46a959a..fd49d81 100644 --- a/src/hub/src/spreadspace.org/sfive/s5store_test.go +++ b/src/hub/src/spreadspace.org/sfive/s5store_test.go @@ -121,6 +121,53 @@ func TestMain(m *testing.M) { // Testing // +func TestIDList(t *testing.T) { + var ids idList + + b := ids.toBytes() + if len(b) != 0 { + t.Fatalf("an empty id-list should yield an empty byte array but returned: %v", b) + } + + ids = append(ids, 12, 2198319823423, 4212411244) + b = ids.toBytes() + if len(b) != 3*8 { + t.Fatalf("an id-list with 3 entries should return a 24 bytes but returned: %v", b) + } + ids2, err := idListFromBytes(b) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if !reflect.DeepEqual([]int64(ids2), []int64(ids)) { + t.Fatalf("failed to read id-list, got: %v, expected: %v", ids2, ids) + } + + b = appendToBinaryIDList(b, 23) + if ids2, err = idListFromBytes(b); err != nil { + t.Fatalf("unexpected error: %v", err) + } + ids = append(ids, 23) + if !reflect.DeepEqual([]int64(ids2), []int64(ids)) { + t.Fatalf("failed to read id-list, got: %v, expected: %v", ids2, ids) + } + + // some more toBytes tests... + if ids2, err = idListFromBytes([]byte{}); err != nil { + t.Fatalf("unexpected error: %v", err) + } + if ids2, err = idListFromBytes([]byte{124}); err == nil { + t.Fatalf("read id-list from bytes with wrong length should return an error") + } + + if ids2, err = idListFromBytes([]byte{0, 0, 0, 0, 0, 0, 0, 1}); err != nil { + t.Fatalf("unexpected error: %v", err) + } + expected := []int64{1} + if !reflect.DeepEqual([]int64(ids2), expected) { + t.Fatalf("failed to read id-list, got: %v, expected: %v", ids2, expected) + } +} + func TestOpen(t *testing.T) { // non-existing directory if _, err := NewStore(StoreConfig{"/nonexistend/db.bolt", false}); err == nil { diff --git a/src/hub/src/spreadspace.org/sfive/s5typesStore.go b/src/hub/src/spreadspace.org/sfive/s5typesStore.go index 67d38d1..b046ce9 100644 --- a/src/hub/src/spreadspace.org/sfive/s5typesStore.go +++ b/src/hub/src/spreadspace.org/sfive/s5typesStore.go @@ -207,3 +207,33 @@ func itob(v int64) []byte { func btoi(b []byte) int64 { return int64(binary.BigEndian.Uint64(b)) } + +type idList []int64 + +func idListFromBytes(data []byte) (ids idList, err error) { + l := len(data) + if l%8 != 0 { + err = errors.New("length of data must be multiples of 8") + return + } + for i := 0; i < l; i += 8 { + ids = append(ids, int64(binary.BigEndian.Uint64(data[i:i+8]))) + } + return +} + +func (ids idList) toBytes() (data []byte) { + l := len(ids) + data = make([]byte, 8*l) + for i, d := 0, 0; i < l; i, d = i+1, d+8 { + binary.BigEndian.PutUint64(data[d:d+8], uint64(ids[i])) + } + return +} + +func appendToBinaryIDList(b []byte, id int64) []byte { + l := len(b) + b = append(b, make([]byte, 8)...) + binary.BigEndian.PutUint64(b[l:l+8], uint64(id)) + return b +} -- cgit v1.2.3