From b71516b4422a655238832835a7d4bddc47594611 Mon Sep 17 00:00:00 2001 From: Samuele Pedroni Date: Fri, 8 Jan 2016 10:54:24 +0100 Subject: wip: refine Backstore interface --- asserts/database.go | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/asserts/database.go b/asserts/database.go index 4b0223fbfa..f39f3b191f 100644 --- a/asserts/database.go +++ b/asserts/database.go @@ -30,22 +30,20 @@ import ( ) // Backstore is a backstore for assertions. It can store and retrieve -// assertions by type under primary paths (tuples of strings). Plus it -// supports more general searches. +// assertions by type under unique primary keys. Plus it +// supports searching by headers. type Backstore interface { - // Put stores an assertion under the given unique primaryPath. + // Put stores an assertion for the given primaryKey field names forming a unique key. // It is responsible for checking that assert is newer than a // previously stored revision. - Put(assertType AssertionType, primaryPath []string, assert Assertion) error - // Get loads an assertion with the given unique primaryPath. + Put(assertType AssertionType, primaryKey []string, assert Assertion) error + // Get loads the assertion with the given unique keyValues for the primaryKey fields. // If none is present it returns ErrNotFound. - Get(assertType AssertionType, primaryPath []string) (Assertion, error) + Get(assertType AssertionType, primaryKey, keyValues []string) (Assertion, error) // Search searches for assertions matching the given headers. // It invokes foundCb for each found assertion. - // pathHint is an incomplete primary path pattern (with "" - // representing omitted components) that covers a superset of - // the results, it can be used for the search if helpful. - Search(assertType AssertionType, headers map[string]string, pathHint []string, foundCb func(Assertion)) error + // As hint the primaryKey field names forming a unique key are also given. + Search(assertType AssertionType, primaryKey []string, headers map[string]string, foundCb func(Assertion)) error } // KeypairManager is a manager and backstore for private/public key pairs. -- cgit v1.2.3 From e1129e3ce4b88f480d723b061a61c84fc160c9bf Mon Sep 17 00:00:00 2001 From: Samuele Pedroni Date: Fri, 8 Jan 2016 11:19:42 +0100 Subject: actually use/implement refined Backstore --- asserts/database.go | 34 +++++++++++++--------------------- asserts/fsbackstore.go | 22 ++++++++++++++-------- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/asserts/database.go b/asserts/database.go index f39f3b191f..18dd4dfa05 100644 --- a/asserts/database.go +++ b/asserts/database.go @@ -207,14 +207,13 @@ func (db *Database) findAccountKeys(authorityID, keyID string) ([]*AccountKey, e } } // consider stored account keys - foundKeyCb := func(a Assertion) { + a, err := db.bs.Get(AccountKeyType, []string{"account-id", "public-key-id"}, []string{authorityID, keyID}) + switch err { + case nil: res = append(res, a.(*AccountKey)) - } - err := db.bs.Search(AccountKeyType, map[string]string{ - "account-id": authorityID, - "public-key-id": keyID, - }, []string{authorityID, keyID}, foundKeyCb) - if err != nil { + case ErrNotFound: + // nothing to do + default: return nil, err } return res, nil @@ -267,15 +266,12 @@ func (db *Database) Add(assert Assertion) error { if err != nil { return err } - primaryKey := make([]string, len(reg.primaryKey)) - for i, k := range reg.primaryKey { - keyVal := assert.Header(k) - if keyVal == "" { + for _, k := range reg.primaryKey { + if assert.Header(k) == "" { return fmt.Errorf("missing primary key header: %v", k) } - primaryKey[i] = keyVal } - return db.bs.Put(assert.Type(), primaryKey, assert) + return db.bs.Put(assert.Type(), reg.primaryKey, assert) } func searchMatch(assert Assertion, expectedHeaders map[string]string) bool { @@ -296,15 +292,15 @@ func (db *Database) Find(assertionType AssertionType, headers map[string]string) if err != nil { return nil, err } - primaryKey := make([]string, len(reg.primaryKey)) + keyValues := make([]string, len(reg.primaryKey)) for i, k := range reg.primaryKey { keyVal := headers[k] if keyVal == "" { return nil, fmt.Errorf("must provide primary key: %v", k) } - primaryKey[i] = keyVal + keyValues[i] = keyVal } - assert, err := db.bs.Get(assertionType, primaryKey) + assert, err := db.bs.Get(assertionType, reg.primaryKey, keyValues) if err != nil { return nil, err } @@ -322,15 +318,11 @@ func (db *Database) FindMany(assertionType AssertionType, headers map[string]str return nil, err } res := []Assertion{} - primaryKey := make([]string, len(reg.primaryKey)) - for i, k := range reg.primaryKey { - primaryKey[i] = headers[k] - } foundCb := func(assert Assertion) { res = append(res, assert) } - err = db.bs.Search(assertionType, headers, primaryKey, foundCb) + err = db.bs.Search(assertionType, reg.primaryKey, headers, foundCb) if err != nil { return nil, err } diff --git a/asserts/fsbackstore.go b/asserts/fsbackstore.go index c6a12e78b3..b9bba51579 100644 --- a/asserts/fsbackstore.go +++ b/asserts/fsbackstore.go @@ -70,7 +70,12 @@ func buildDiskPrimaryPath(primaryPath []string) string { return filepath.Join(comps...) } -func (fsbs *filesystemBackstore) Put(assertType AssertionType, primaryPath []string, assert Assertion) error { +func (fsbs *filesystemBackstore) Put(assertType AssertionType, primaryKey []string, assert Assertion) error { + primaryPath := make([]string, len(primaryKey)) + for i, k := range primaryKey { + primaryPath[i] = assert.Header(k) + } + diskPrimaryPath := buildDiskPrimaryPath(primaryPath) curAssert, err := fsbs.readAssertion(assertType, diskPrimaryPath) if err == nil { @@ -90,8 +95,8 @@ func (fsbs *filesystemBackstore) Put(assertType AssertionType, primaryPath []str return nil } -func (fsbs *filesystemBackstore) Get(assertType AssertionType, primaryPath []string) (Assertion, error) { - return fsbs.readAssertion(assertType, buildDiskPrimaryPath(primaryPath)) +func (fsbs *filesystemBackstore) Get(assertType AssertionType, primaryKey []string, keyValues []string) (Assertion, error) { + return fsbs.readAssertion(assertType, buildDiskPrimaryPath(keyValues)) } func (fsbs *filesystemBackstore) search(assertType AssertionType, diskPattern []string, foundCb func(Assertion)) error { @@ -114,13 +119,14 @@ func (fsbs *filesystemBackstore) search(assertType AssertionType, diskPattern [] return nil } -func (fsbs *filesystemBackstore) Search(assertType AssertionType, headers map[string]string, pathHint []string, foundCb func(Assertion)) error { - diskPattern := make([]string, len(pathHint)) - for i, comp := range pathHint { - if comp == "" { +func (fsbs *filesystemBackstore) Search(assertType AssertionType, primaryKey []string, headers map[string]string, foundCb func(Assertion)) error { + diskPattern := make([]string, len(primaryKey)) + for i, k := range primaryKey { + keyVal := headers[k] + if keyVal == "" { diskPattern[i] = "*" } else { - diskPattern[i] = url.QueryEscape(comp) + diskPattern[i] = url.QueryEscape(keyVal) } } -- cgit v1.2.3 From 8b8f911a2a012124141b8715db0e989b8d56be93 Mon Sep 17 00:00:00 2001 From: Samuele Pedroni Date: Fri, 8 Jan 2016 13:05:26 +0100 Subject: as per atomatt try to be less ambiguous here, price is a bit of a mouthful of arg name --- asserts/database.go | 15 +++++++-------- asserts/fsbackstore.go | 16 ++++++++-------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/asserts/database.go b/asserts/database.go index 18dd4dfa05..26b282327d 100644 --- a/asserts/database.go +++ b/asserts/database.go @@ -30,20 +30,19 @@ import ( ) // Backstore is a backstore for assertions. It can store and retrieve -// assertions by type under unique primary keys. Plus it -// supports searching by headers. +// assertions by type under unique primary key headers. Plus it supports searching by headers. type Backstore interface { - // Put stores an assertion for the given primaryKey field names forming a unique key. + // Put stores an assertion for the given primaryKeyHeaders forming a unique key. // It is responsible for checking that assert is newer than a // previously stored revision. - Put(assertType AssertionType, primaryKey []string, assert Assertion) error - // Get loads the assertion with the given unique keyValues for the primaryKey fields. + Put(assertType AssertionType, primaryKeyHeaders []string, assert Assertion) error + // Get loads the assertion with the given unique key for the primaryKeyHeaders. // If none is present it returns ErrNotFound. - Get(assertType AssertionType, primaryKey, keyValues []string) (Assertion, error) + Get(assertType AssertionType, primaryKeyHeaders, key []string) (Assertion, error) // Search searches for assertions matching the given headers. // It invokes foundCb for each found assertion. - // As hint the primaryKey field names forming a unique key are also given. - Search(assertType AssertionType, primaryKey []string, headers map[string]string, foundCb func(Assertion)) error + // As hint the primaryKeyHeaders forming a unique key are also given. + Search(assertType AssertionType, primaryKeyHeaders []string, headers map[string]string, foundCb func(Assertion)) error } // KeypairManager is a manager and backstore for private/public key pairs. diff --git a/asserts/fsbackstore.go b/asserts/fsbackstore.go index b9bba51579..abdd1d80f5 100644 --- a/asserts/fsbackstore.go +++ b/asserts/fsbackstore.go @@ -70,9 +70,9 @@ func buildDiskPrimaryPath(primaryPath []string) string { return filepath.Join(comps...) } -func (fsbs *filesystemBackstore) Put(assertType AssertionType, primaryKey []string, assert Assertion) error { - primaryPath := make([]string, len(primaryKey)) - for i, k := range primaryKey { +func (fsbs *filesystemBackstore) Put(assertType AssertionType, primaryKeyHeaders []string, assert Assertion) error { + primaryPath := make([]string, len(primaryKeyHeaders)) + for i, k := range primaryKeyHeaders { primaryPath[i] = assert.Header(k) } @@ -95,8 +95,8 @@ func (fsbs *filesystemBackstore) Put(assertType AssertionType, primaryKey []stri return nil } -func (fsbs *filesystemBackstore) Get(assertType AssertionType, primaryKey []string, keyValues []string) (Assertion, error) { - return fsbs.readAssertion(assertType, buildDiskPrimaryPath(keyValues)) +func (fsbs *filesystemBackstore) Get(assertType AssertionType, primaryKeyHeaders, key []string) (Assertion, error) { + return fsbs.readAssertion(assertType, buildDiskPrimaryPath(key)) } func (fsbs *filesystemBackstore) search(assertType AssertionType, diskPattern []string, foundCb func(Assertion)) error { @@ -119,9 +119,9 @@ func (fsbs *filesystemBackstore) search(assertType AssertionType, diskPattern [] return nil } -func (fsbs *filesystemBackstore) Search(assertType AssertionType, primaryKey []string, headers map[string]string, foundCb func(Assertion)) error { - diskPattern := make([]string, len(primaryKey)) - for i, k := range primaryKey { +func (fsbs *filesystemBackstore) Search(assertType AssertionType, primaryKeyHeaders []string, headers map[string]string, foundCb func(Assertion)) error { + diskPattern := make([]string, len(primaryKeyHeaders)) + for i, k := range primaryKeyHeaders { keyVal := headers[k] if keyVal == "" { diskPattern[i] = "*" -- cgit v1.2.3 From 42a30068cc9f9cc000d9dd7f08ceff17452fd4ff Mon Sep 17 00:00:00 2001 From: Samuele Pedroni Date: Tue, 12 Jan 2016 14:47:33 +0100 Subject: declunkify doc comments a little bit --- asserts/database.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/asserts/database.go b/asserts/database.go index 26b282327d..ff9bb8a45e 100644 --- a/asserts/database.go +++ b/asserts/database.go @@ -29,23 +29,23 @@ import ( "time" ) -// Backstore is a backstore for assertions. It can store and retrieve +// A Backstore stores assertions. It can store and retrieve // assertions by type under unique primary key headers. Plus it supports searching by headers. type Backstore interface { // Put stores an assertion for the given primaryKeyHeaders forming a unique key. // It is responsible for checking that assert is newer than a // previously stored revision. Put(assertType AssertionType, primaryKeyHeaders []string, assert Assertion) error - // Get loads the assertion with the given unique key for the primaryKeyHeaders. + // Get returns the assertion with the given unique key for the primaryKeyHeaders. // If none is present it returns ErrNotFound. Get(assertType AssertionType, primaryKeyHeaders, key []string) (Assertion, error) - // Search searches for assertions matching the given headers. + // Search returns assertions matching the given headers. // It invokes foundCb for each found assertion. // As hint the primaryKeyHeaders forming a unique key are also given. Search(assertType AssertionType, primaryKeyHeaders []string, headers map[string]string, foundCb func(Assertion)) error } -// KeypairManager is a manager and backstore for private/public key pairs. +// A KeypairManager is a manager and backstore for private/public key pairs. type KeypairManager interface { // Put stores the given private/public key pair for identity, // making sure it can be later retrieved by authority-id and -- cgit v1.2.3