diff options
Diffstat (limited to 'src/hub/src/spreadspace.org/sfive/s5store.go')
-rw-r--r-- | src/hub/src/spreadspace.org/sfive/s5store.go | 80 |
1 files changed, 41 insertions, 39 deletions
diff --git a/src/hub/src/spreadspace.org/sfive/s5store.go b/src/hub/src/spreadspace.org/sfive/s5store.go index 86a4fa0..e8aebf5 100644 --- a/src/hub/src/spreadspace.org/sfive/s5store.go +++ b/src/hub/src/spreadspace.org/sfive/s5store.go @@ -146,10 +146,10 @@ func createDB(dbPath string) (db *bolt.DB, version int, hubUUID string, err erro return } -func NewStore(dbPath string, readOnly bool) (Store, error) { +func NewStore(dbPath string, readOnly bool) (*Store, error) { db, version, hubid, err := openDB(dbPath, readOnly) if err != nil { - return Store{}, err + return nil, err } if db != nil { @@ -158,22 +158,22 @@ func NewStore(dbPath string, readOnly bool) (Store, error) { } else { s5l.Printf("store: opened (UUID: %s)", hubid) } - return Store{version, hubid, db, readOnly}, nil + return &Store{version, hubid, db, readOnly}, nil } if readOnly { - return Store{}, errors.New("store: failed to open, requested read-only mode but store file does not exist.") + return nil, errors.New("store: failed to open, requested read-only mode but store file does not exist.") } db, version, hubid, err = createDB(dbPath) if err != nil { - return Store{}, err + return nil, err } s5l.Printf("store: initialized (UUID: %s)", hubid) - return Store{version, hubid, db, readOnly}, nil + return &Store{version, hubid, db, readOnly}, nil } -func (st Store) Close() { +func (st *Store) Close() { s5l.Printf("store: closing") st.db.Close() } @@ -184,7 +184,7 @@ func (st Store) Close() { // append key-value pairs to buckets -func (st Store) insertNewHub(tx *bolt.Tx, hubUUID string) (hubID int, err error) { +func (st *Store) insertNewHub(tx *bolt.Tx, hubUUID string) (hubID int, err error) { if hubUUID == "" { return } @@ -211,7 +211,7 @@ func (st Store) insertNewHub(tx *bolt.Tx, hubUUID string) (hubID int, err error) return hubID, err } -func (st Store) insertNewSource(tx *bolt.Tx, src sourceDB) (srcID int, err error) { +func (st *Store) insertNewSource(tx *bolt.Tx, src *sourceDB) (srcID int, err error) { bf := tx.Bucket([]byte(sourcesFwdBn)) // br.FillPercent = 1.0 // these appends are not ordered (the key is the slug and not an integer id) br := tx.Bucket([]byte(sourcesRevBn)) @@ -240,7 +240,7 @@ func (st Store) insertNewSource(tx *bolt.Tx, src sourceDB) (srcID int, err error return srcID, err } -func (st Store) insertUpdate(tx *bolt.Tx, du updateDB) (duID int, err error) { +func (st *Store) insertUpdate(tx *bolt.Tx, du *updateDB) (duID int, err error) { b := tx.Bucket([]byte(updatesBn)) b.FillPercent = 1.0 // we only do appends @@ -255,7 +255,7 @@ func (st Store) insertUpdate(tx *bolt.Tx, du updateDB) (duID int, err error) { return } -func (st Store) insertNewUserAgent(tx *bolt.Tx, ua string) (uaID int, err error) { +func (st *Store) insertNewUserAgent(tx *bolt.Tx, ua string) (uaID int, err error) { bf := tx.Bucket([]byte(userAgentsFwdBn)) bf.FillPercent = 1.0 // we only do appends br := tx.Bucket([]byte(userAgentsRevBn)) @@ -278,7 +278,7 @@ func (st Store) insertNewUserAgent(tx *bolt.Tx, ua string) (uaID int, err error) return uaID, err } -func (st Store) insertClient(tx *bolt.Tx, uID int, cd []Client) error { +func (st *Store) insertClient(tx *bolt.Tx, uID int, cd []Client) error { if len(cd) == 0 { return nil } @@ -302,7 +302,7 @@ func (st Store) insertClient(tx *bolt.Tx, uID int, cd []Client) error { return b.Put(itob(uID), jsonData) } -func (st Store) setLastUpdateForUUID(tx *bolt.Tx, uuid string, uID int) error { +func (st *Store) setLastUpdateForUUID(tx *bolt.Tx, uuid string, uID int) error { b := tx.Bucket([]byte(latestUpdatesBn)) b.FillPercent = 1.0 // we only do appends @@ -315,7 +315,7 @@ func (st Store) setLastUpdateForUUID(tx *bolt.Tx, uuid string, uID int) error { // Split up the multidimensional dataupdate and append all the key-value pairs -func (st Store) appendItem(tx *bolt.Tx, uf UpdateFull) (uID int, err error) { +func (st *Store) appendItem(tx *bolt.Tx, uf *UpdateFull) (uID int, err error) { u := NewUpdateDB(uf) s := NewSourceDB(uf) @@ -343,7 +343,7 @@ func (st Store) appendItem(tx *bolt.Tx, uf UpdateFull) (uID int, err error) { // Public Append Interface -func (st Store) AppendMany(updates []UpdateFull) (err error) { +func (st *Store) AppendMany(updates []*UpdateFull) (err error) { if st.readOnly { return ErrReadOnly } @@ -357,8 +357,8 @@ func (st Store) AppendMany(updates []UpdateFull) (err error) { }) } -func (st Store) Append(update UpdateFull) (err error) { - return st.AppendMany([]UpdateFull{update}) +func (st *Store) Append(update *UpdateFull) (err error) { + return st.AppendMany([]*UpdateFull{update}) } // @@ -367,7 +367,7 @@ func (st Store) Append(update UpdateFull) (err error) { // fetch key-value pairs from buckets -func (st Store) getHub(tx *bolt.Tx, id int) string { +func (st *Store) getHub(tx *bolt.Tx, id int) string { b := tx.Bucket([]byte(hubUUIDsRevBn)) uuid := b.Get(itob(id)) if uuid == nil { @@ -376,7 +376,7 @@ func (st Store) getHub(tx *bolt.Tx, id int) string { return string(uuid) } -func (st Store) getSource(tx *bolt.Tx, id int) (source sourceDB, err error) { +func (st *Store) getSource(tx *bolt.Tx, id int) (source *sourceDB, err error) { b := tx.Bucket([]byte(sourcesRevBn)) jsonData := b.Get(itob(id)) @@ -384,13 +384,14 @@ func (st Store) getSource(tx *bolt.Tx, id int) (source sourceDB, err error) { err = ErrSourceNotFound return } - if err = json.Unmarshal(jsonData, &source); err != nil { + source = &sourceDB{} + if err = json.Unmarshal(jsonData, source); err != nil { return } return } -func (st Store) getClients(tx *bolt.Tx, id int) (clients []Client, err error) { +func (st *Store) getClients(tx *bolt.Tx, id int) (clients []Client, err error) { bc := tx.Bucket([]byte(clientDataBn)) bu := tx.Bucket([]byte(userAgentsRevBn)) @@ -415,14 +416,15 @@ func (st Store) getClients(tx *bolt.Tx, id int) (clients []Client, err error) { // fetch all the key-value pairs and merge them into the multidimensional dataupdate -func (st Store) fetchItem(tx *bolt.Tx, uID int, u updateDB) (updates UpdateFull, err error) { - updates.CopyFromUpdateDB(u, st.getHub(tx, u.SourceHubID), st.hubUUID, uID) - var src sourceDB +func (st *Store) fetchItem(tx *bolt.Tx, uID int, u *updateDB) (update *UpdateFull, err error) { + update = &UpdateFull{} + update.CopyFromUpdateDB(u, st.getHub(tx, u.SourceHubID), st.hubUUID, uID) + var src *sourceDB if src, err = st.getSource(tx, u.SourceID); err != nil { return } - updates.CopyFromSourceDB(src) - if updates.Data.Clients, err = st.getClients(tx, uID); err != nil { + update.CopyFromSourceDB(src) + if update.Data.Clients, err = st.getClients(tx, uID); err != nil { return } return @@ -430,8 +432,8 @@ func (st Store) fetchItem(tx *bolt.Tx, uID int, u updateDB) (updates UpdateFull, // Public Fetch Interface -func (st Store) GetUpdatesAfter(id, limit int) (updates []UpdateFull, err error) { - updates = []UpdateFull{} +func (st *Store) GetUpdatesAfter(id, limit int) (updates []*UpdateFull, err error) { + updates = []*UpdateFull{} if id < 0 { // TODO: interpret negative values as last x values id = 0 } @@ -451,7 +453,7 @@ func (st Store) GetUpdatesAfter(id, limit int) (updates []UpdateFull, err error) k, v = c.Next() } for ; k != nil; k, v = c.Next() { - var d updateDB + var d *updateDB if err := json.Unmarshal(v, &d); err != nil { return err } @@ -470,7 +472,7 @@ func (st Store) GetUpdatesAfter(id, limit int) (updates []UpdateFull, err error) return } -func (st Store) GetUpdate(id int) (update UpdateFull, err error) { +func (st *Store) GetUpdate(id int) (update *UpdateFull, err error) { err = st.db.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(updatesBn)) @@ -479,7 +481,7 @@ func (st Store) GetUpdate(id int) (update UpdateFull, err error) { return ErrNotFound } - var d updateDB + var d *updateDB if err := json.Unmarshal(jsonData, &d); err != nil { return err } @@ -497,11 +499,11 @@ func (st Store) GetUpdate(id int) (update UpdateFull, err error) { // Auxilliary Data // -func (st Store) GetHubUUID() string { +func (st *Store) GetHubUUID() string { return st.hubUUID } -func (st Store) GetLastUpdateID() (updateID int, err error) { +func (st *Store) GetLastUpdateID() (updateID int, err error) { err = st.db.View(func(tx *bolt.Tx) error { updateID = int(tx.Bucket([]byte(updatesBn)).Sequence()) return nil @@ -509,7 +511,7 @@ func (st Store) GetLastUpdateID() (updateID int, err error) { return } -func (st Store) GetLastUpdateIDForUUID(uuid string) (updateID int, err error) { +func (st *Store) GetLastUpdateIDForUUID(uuid string) (updateID int, err error) { if uuid == st.hubUUID { return st.GetLastUpdateID() } @@ -525,7 +527,7 @@ func (st Store) GetLastUpdateIDForUUID(uuid string) (updateID int, err error) { return } -func (st Store) GetHubs() (hubs []string, err error) { +func (st *Store) GetHubs() (hubs []string, err error) { hubs = []string{st.hubUUID} err = st.db.View(func(tx *bolt.Tx) error { c := tx.Bucket([]byte(hubUUIDsRevBn)).Cursor() @@ -537,16 +539,16 @@ func (st Store) GetHubs() (hubs []string, err error) { return } -func (st Store) GetSources() (sources []Source, err error) { - sources = []Source{} +func (st *Store) GetSources() (sources []*Source, err error) { + sources = []*Source{} err = st.db.View(func(tx *bolt.Tx) error { c := tx.Bucket([]byte(sourcesRevBn)).Cursor() for k, v := c.First(); k != nil; k, v = c.Next() { - var s sourceDB + var s *sourceDB if err := json.Unmarshal(v, &s); err != nil { return err } - var src Source + src := &Source{} src.CopyFromSourceDB(s) sources = append(sources, src) } |