From b51ba5aa3ebaee20311597fcf33fe6c64b2e8ff1 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sat, 29 Apr 2017 21:03:37 +0200 Subject: added version field to store --- src/hub/src/spreadspace.org/sfive-hub/s5hub.go | 2 +- src/hub/src/spreadspace.org/sfive/s5srv.go | 2 +- src/hub/src/spreadspace.org/sfive/s5store.go | 96 +++++++++++++++++++---- src/hub/src/spreadspace.org/sfive/s5typesStore.go | 3 +- 4 files changed, 83 insertions(+), 20 deletions(-) (limited to 'src/hub') diff --git a/src/hub/src/spreadspace.org/sfive-hub/s5hub.go b/src/hub/src/spreadspace.org/sfive-hub/s5hub.go index bd68ff2..2bb436f 100644 --- a/src/hub/src/spreadspace.org/sfive-hub/s5hub.go +++ b/src/hub/src/spreadspace.org/sfive-hub/s5hub.go @@ -70,7 +70,7 @@ func main() { srv, err := sfive.NewServer(*db) if err != nil { - s5hl.Fatalf("failed to initialize: %v", err) + s5hl.Fatalf(err.Error()) } defer srv.Close() diff --git a/src/hub/src/spreadspace.org/sfive/s5srv.go b/src/hub/src/spreadspace.org/sfive/s5srv.go index 26c620f..8db3c7e 100644 --- a/src/hub/src/spreadspace.org/sfive/s5srv.go +++ b/src/hub/src/spreadspace.org/sfive/s5srv.go @@ -174,6 +174,6 @@ func NewServer(dbPath string) (server *Server, err error) { server.getHubIdChan = make(chan getHubIdToken, 1) server.getLastUpdateIdChan = make(chan getLastUpdateIdToken, 1) go server.appendActor() - s5l.Printf("server: initialized\n") + s5l.Printf("server: started\n") return } diff --git a/src/hub/src/spreadspace.org/sfive/s5store.go b/src/hub/src/spreadspace.org/sfive/s5store.go index a54ccdd..611e720 100644 --- a/src/hub/src/spreadspace.org/sfive/s5store.go +++ b/src/hub/src/spreadspace.org/sfive/s5store.go @@ -34,6 +34,9 @@ package sfive import ( "encoding/json" + "errors" + "fmt" + "os" "time" "github.com/boltdb/bolt" @@ -42,40 +45,90 @@ import ( const ( StoreGetUpdatesLimit = 42000 + StoreVersion = 1 +) + +var ( + storeBuckets = []string{latestUpdatesBn, hubUuidsFwdBn, hubUuidsRevBn, dataUpdatesBn, + sourcesFwdBn, sourcesRevBn, clientDataBn, userAgentsFwdBn, userAgentsRevBn} ) type Store struct { + version int hubUuid string db *bolt.DB } -func initDb(dbPath string) (db *bolt.DB, hubUuid string, err error) { +func checkDb(dbPath string) (db *bolt.DB, version int, hubUuid string, err error) { + if _, err = os.Stat(dbPath); err != nil { + if os.IsNotExist(err) { + err = nil + } + return + } + + db, err = bolt.Open(dbPath, 0600, &bolt.Options{Timeout: 1 * time.Second}) + if err != nil { + return + } + + err = db.View(func(tx *bolt.Tx) error { + b := tx.Bucket([]byte(hubInfoBn)) + if b == nil { + return errors.New("store: failed to open, bucket '" + hubInfoBn + "'does not exist.") + } + bVersion := b.Get([]byte(storeVersionKey)) + if bVersion == nil { + return errors.New("store: failed to open, no hub version found.") + } + version = btoi(bVersion) + if version != StoreVersion { + return fmt.Errorf("store: failed to open, wrong hub version: %s (expected: %d)", version, StoreVersion) + } + + bHubId := b.Get([]byte(hubUUIDKey)) + if bHubId != nil { + hubUuid = string(bHubId) + } + if hubUuid == "" { + return errors.New("store: failed to open, UUID does not exist or is empty.") + } + + for _, bn := range storeBuckets { + if b := tx.Bucket([]byte(bn)); b == nil { + return errors.New("store: failed to open, bucket '" + bn + "' does not exist.") + } + } + return nil + }) + + return +} + +func initDb(dbPath string) (db *bolt.DB, version int, hubUuid string, err error) { db, err = bolt.Open(dbPath, 0600, &bolt.Options{Timeout: 1 * time.Second}) if err != nil { return } err = db.Update(func(tx *bolt.Tx) error { - buckets := []string{latestUpdatesBn, hubUuidsFwdBn, hubUuidsRevBn, dataUpdatesBn, - sourcesFwdBn, sourcesRevBn, clientDataBn, userAgentsFwdBn, userAgentsRevBn} - for _, bn := range buckets { - if _, err := tx.CreateBucketIfNotExists([]byte(bn)); err != nil { + for _, bn := range storeBuckets { + if _, err := tx.CreateBucket([]byte(bn)); err != nil { return err } } - b, err := tx.CreateBucketIfNotExists([]byte(hubInfoBn)) + b, err := tx.CreateBucket([]byte(hubInfoBn)) if err != nil { return err } - bHubId := b.Get([]byte(hubUUIDKey)) - if bHubId == nil { - hubUuid = uuid.New() - if err := b.Put([]byte(hubUUIDKey), []byte(hubUuid)); err != nil { - return err - } - } else { - hubUuid = string(bHubId) + version = StoreVersion + if err := b.Put([]byte(storeVersionKey), itob(version)); err != nil { + return err + } + hubUuid = uuid.New() + if err := b.Put([]byte(hubUUIDKey), []byte(hubUuid)); err != nil { + return err } return nil }) @@ -432,12 +485,21 @@ func (st Store) GetStoreId() string { } func NewStore(dbPath string) (Store, error) { - db, hubid, err := initDb(dbPath) + db, version, hubid, err := checkDb(dbPath) if err != nil { return Store{}, err } - s5l.Printf("store: initialized (UUID: %s)", hubid) - return Store{hubid, db}, nil + + if db != nil { + s5l.Printf("store: opened (UUID: %s)", hubid) + } else { + db, version, hubid, err = initDb(dbPath) + if err != nil { + return Store{}, err + } + s5l.Printf("store: initialized (UUID: %s)", hubid) + } + return Store{version, hubid, db}, nil } func (st Store) Close() { diff --git a/src/hub/src/spreadspace.org/sfive/s5typesStore.go b/src/hub/src/spreadspace.org/sfive/s5typesStore.go index cf6cc17..5676c1f 100644 --- a/src/hub/src/spreadspace.org/sfive/s5typesStore.go +++ b/src/hub/src/spreadspace.org/sfive/s5typesStore.go @@ -58,7 +58,8 @@ const ( userAgentsRevBn = "UserAgentsRev" // well-known keys - hubUUIDKey = "HubUUID" + hubUUIDKey = "HubUUID" + storeVersionKey = "Version" ) // stored in sourcesRevBn -- cgit v1.2.3