diff options
author | Christian Pointner <equinox@spreadspace.org> | 2017-07-02 19:30:59 +0200 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2017-07-02 19:30:59 +0200 |
commit | 4137eede579ee69cfc495227149858bfd19ec0b6 (patch) | |
tree | 8c15d731e082c19fd07a10ab5819ab9312834234 /src | |
parent | move tlsconfig to seperate package (diff) |
config type for store
Diffstat (limited to 'src')
-rw-r--r-- | src/hub/src/spreadspace.org/sfive/s5srv.go | 2 | ||||
-rw-r--r-- | src/hub/src/spreadspace.org/sfive/s5store.go | 14 | ||||
-rw-r--r-- | src/hub/src/spreadspace.org/sfive/s5store_test.go | 52 |
3 files changed, 34 insertions, 34 deletions
diff --git a/src/hub/src/spreadspace.org/sfive/s5srv.go b/src/hub/src/spreadspace.org/sfive/s5srv.go index dcd8df0..f6f4bc8 100644 --- a/src/hub/src/spreadspace.org/sfive/s5srv.go +++ b/src/hub/src/spreadspace.org/sfive/s5srv.go @@ -150,7 +150,7 @@ func (srv Server) Close() { func NewServer(dbPath string, readOnly, anonymize bool, anonKeyfile, geoipDB string) (srv *Server, err error) { // TODO: read configuration and create instance with correct settings srv = &Server{} - if srv.store, err = NewStore(dbPath, readOnly); err != nil { + if srv.store, err = NewStore(StoreConfig{dbPath, readOnly}); err != nil { return } diff --git a/src/hub/src/spreadspace.org/sfive/s5store.go b/src/hub/src/spreadspace.org/sfive/s5store.go index ebe83ec..d852618 100644 --- a/src/hub/src/spreadspace.org/sfive/s5store.go +++ b/src/hub/src/spreadspace.org/sfive/s5store.go @@ -146,31 +146,31 @@ func createDB(dbPath string) (db *bolt.DB, version int, hubUUID string, err erro return } -func NewStore(dbPath string, readOnly bool) (*Store, error) { - db, version, hubid, err := openDB(dbPath, readOnly) +func NewStore(cfg StoreConfig) (*Store, error) { + db, version, hubid, err := openDB(cfg.dbPath, cfg.readOnly) if err != nil { return nil, err } if db != nil { - if readOnly { + if cfg.readOnly { s5l.Printf("store: opened read-only (UUID: %s)", hubid) } else { s5l.Printf("store: opened (UUID: %s)", hubid) } - return &Store{version, hubid, db, readOnly}, nil + return &Store{version, hubid, db, cfg.readOnly}, nil } - if readOnly { + if cfg.readOnly { return nil, errors.New("store: failed to open, requested read-only mode but store file does not exist.") } - db, version, hubid, err = createDB(dbPath) + db, version, hubid, err = createDB(cfg.dbPath) if err != nil { return nil, err } s5l.Printf("store: initialized (UUID: %s)", hubid) - return &Store{version, hubid, db, readOnly}, nil + return &Store{version, hubid, db, cfg.readOnly}, nil } func (st *Store) Close() { diff --git a/src/hub/src/spreadspace.org/sfive/s5store_test.go b/src/hub/src/spreadspace.org/sfive/s5store_test.go index 3f5cd37..ae52a0a 100644 --- a/src/hub/src/spreadspace.org/sfive/s5store_test.go +++ b/src/hub/src/spreadspace.org/sfive/s5store_test.go @@ -123,7 +123,7 @@ func TestMain(m *testing.M) { func TestOpen(t *testing.T) { // non-existing directory - if _, err := NewStore("/nonexistend/db.bolt", false); err == nil { + if _, err := NewStore(StoreConfig{"/nonexistend/db.bolt", false}); err == nil { t.Fatalf("opening store in nonexisting directory should throw an error") } @@ -132,7 +132,7 @@ func TestOpen(t *testing.T) { if err := os.MkdirAll(testBoltPath, 0700); err != nil { t.Fatalf("unexpected error: %v", err) } - if _, err := NewStore(testBoltPath, false); err == nil { + if _, err := NewStore(StoreConfig{testBoltPath, false}); err == nil { t.Fatalf("opening store using a directory should throw an error") } @@ -144,7 +144,7 @@ func TestOpen(t *testing.T) { io.WriteString(f, "this is not a bolt db.") f.Close() } - if _, err := NewStore(testBoltPath, false); err == nil { + if _, err := NewStore(StoreConfig{testBoltPath, false}); err == nil { t.Fatalf("opening store using a invalid database should throw an error") } @@ -158,7 +158,7 @@ func TestOpen(t *testing.T) { t.Fatalf("unexpected error: %v", err) } } - if _, err := NewStore(testBoltPath, false); err == nil { + if _, err := NewStore(StoreConfig{testBoltPath, false}); err == nil { t.Fatalf("opening store without HubInfo Bucket should throw an error") } @@ -179,7 +179,7 @@ func TestOpen(t *testing.T) { t.Fatalf("unexpected error: %v", err) } } - if _, err := NewStore(testBoltPath, false); err == nil { + if _, err := NewStore(StoreConfig{testBoltPath, false}); err == nil { t.Fatalf("opening store without a database version should throw an error") } @@ -201,7 +201,7 @@ func TestOpen(t *testing.T) { t.Fatalf("unexpected error: %v", err) } } - if _, err := NewStore(testBoltPath, false); err == nil { + if _, err := NewStore(StoreConfig{testBoltPath, false}); err == nil { t.Fatalf("opening store with wrong database version should throw an error") } @@ -223,7 +223,7 @@ func TestOpen(t *testing.T) { t.Fatalf("unexpected error: %v", err) } } - if _, err := NewStore(testBoltPath, false); err == nil { + if _, err := NewStore(StoreConfig{testBoltPath, false}); err == nil { t.Fatalf("opening store without a database UUID should throw an error") } @@ -245,7 +245,7 @@ func TestOpen(t *testing.T) { t.Fatalf("unexpected error: %v", err) } } - if _, err := NewStore(testBoltPath, false); err == nil { + if _, err := NewStore(StoreConfig{testBoltPath, false}); err == nil { t.Fatalf("opening store with empty UUID should throw an error") } @@ -267,20 +267,20 @@ func TestOpen(t *testing.T) { t.Fatalf("unexpected error: %v", err) } } - if _, err := NewStore(testBoltPath, false); err == nil { + if _, err := NewStore(StoreConfig{testBoltPath, false}); err == nil { t.Fatalf("opening store with (some) buckets missing should throw an error") } // create new bolt-db and reopen it os.Remove(testBoltPath) - store, err := NewStore(testBoltPath, false) + store, err := NewStore(StoreConfig{testBoltPath, false}) if err != nil { t.Fatalf("creating new store failed: %v", err) } createdUUID := store.hubUUID store.Close() - store, err = NewStore(testBoltPath, false) + store, err = NewStore(StoreConfig{testBoltPath, false}) if err != nil { t.Fatalf("re-opening existing store failed: %v", err) } @@ -288,7 +288,7 @@ func TestOpen(t *testing.T) { t.Fatalf("UUID of opened store differs from the one previously generated: '%s' != '%s'", createdUUID, store.hubUUID) } - if _, err := NewStore(testBoltPath, false); err == nil { + if _, err := NewStore(StoreConfig{testBoltPath, false}); err == nil { t.Fatalf("opening already opened database should throw an error") } store.Close() @@ -296,7 +296,7 @@ func TestOpen(t *testing.T) { func TestAppendAndFetch(t *testing.T) { os.Remove(testBoltPath) - store, err := NewStore(testBoltPath, false) + store, err := NewStore(StoreConfig{testBoltPath, false}) if err != nil { t.Fatalf("unexpected error: %v", err) } @@ -364,12 +364,12 @@ func TestAppendAndFetch(t *testing.T) { func TestReadOnly(t *testing.T) { // create read-only db from not-existing file must fail os.Remove(testBoltPath) - if _, err := NewStore(testBoltPath, true); err == nil { + if _, err := NewStore(StoreConfig{testBoltPath, true}); err == nil { t.Fatalf("creating a read-only database should throw an error") } // prepare a store with one data-update - store, err := NewStore(testBoltPath, false) + store, err := NewStore(StoreConfig{testBoltPath, false}) if err != nil { t.Fatalf("unexpected error: %v", err) } @@ -384,7 +384,7 @@ func TestReadOnly(t *testing.T) { store.Close() // read-only db from existing file must succeed - store, err = NewStore(testBoltPath, true) + store, err = NewStore(StoreConfig{testBoltPath, true}) if err != nil { t.Fatalf("opening existing store in read-only mode failed: %v", err) } @@ -404,7 +404,7 @@ func TestReadOnly(t *testing.T) { func TestGetUpdatesAfter(t *testing.T) { // prepare a store with 3 data-updates os.Remove(testBoltPath) - store, err := NewStore(testBoltPath, false) + store, err := NewStore(StoreConfig{testBoltPath, false}) if err != nil { t.Fatalf("unexpected error: %v", err) } @@ -500,7 +500,7 @@ func TestGetUpdatesAfter(t *testing.T) { func TestForwardedUpdates(t *testing.T) { // prepare a new store os.Remove(testBoltPath) - store, err := NewStore(testBoltPath, false) + store, err := NewStore(StoreConfig{testBoltPath, false}) if err != nil { t.Fatalf("unexpected error: %v", err) } @@ -638,28 +638,28 @@ func checkForwardedUpdates2(t *testing.T, src1Store, src2Store, fwdStore, finalS func TestForwardedUpdates2(t *testing.T) { // prepare 4 new stores os.Remove(testBoltPath) - src1Store, err := NewStore(testBoltPath, false) + src1Store, err := NewStore(StoreConfig{testBoltPath, false}) if err != nil { t.Fatalf("unexpected error: %v", err) } defer src1Store.Close() os.Remove(testBoltPath2) - src2Store, err := NewStore(testBoltPath2, false) + src2Store, err := NewStore(StoreConfig{testBoltPath2, false}) if err != nil { t.Fatalf("unexpected error: %v", err) } defer src2Store.Close() os.Remove(testBoltPathFwd) - fwdStore, err := NewStore(testBoltPathFwd, false) + fwdStore, err := NewStore(StoreConfig{testBoltPathFwd, false}) if err != nil { t.Fatalf("unexpected error: %v", err) } defer fwdStore.Close() os.Remove(testBoltPathFinal) - finalStore, err := NewStore(testBoltPathFinal, false) + finalStore, err := NewStore(StoreConfig{testBoltPathFinal, false}) if err != nil { t.Fatalf("unexpected error: %v", err) } @@ -743,7 +743,7 @@ func TestForwardedUpdates2(t *testing.T) { func TestGetSources(t *testing.T) { // prepare a new store os.Remove(testBoltPath) - store, err := NewStore(testBoltPath, false) + store, err := NewStore(StoreConfig{testBoltPath, false}) if err != nil { t.Fatalf("unexpected error: %v", err) } @@ -778,7 +778,7 @@ func TestGetSources(t *testing.T) { func TestGetClients(t *testing.T) { // prepare a new store os.Remove(testBoltPath) - store, err := NewStore(testBoltPath, false) + store, err := NewStore(StoreConfig{testBoltPath, false}) if err != nil { t.Fatalf("unexpected error: %v", err) } @@ -816,7 +816,7 @@ func TestGetClients(t *testing.T) { func BenchmarkAppendMany(b *testing.B) { os.Remove(testBoltPath) - store, err := NewStore(testBoltPath, false) + store, err := NewStore(StoreConfig{testBoltPath, false}) if err != nil { b.Fatalf("unexpected error: %v", err) } @@ -832,7 +832,7 @@ func BenchmarkAppendMany(b *testing.B) { func BenchmarkGetUpdatesAfter(b *testing.B) { os.Remove(testBoltPath) - store, err := NewStore(testBoltPath, false) + store, err := NewStore(StoreConfig{testBoltPath, false}) if err != nil { b.Fatalf("unexpected error: %v", err) } |