diff options
| -rw-r--r-- | asserts/database.go | 57 | ||||
| -rw-r--r-- | asserts/fsbackstore.go | 22 |
2 files changed, 37 insertions, 42 deletions
diff --git a/asserts/database.go b/asserts/database.go index 4b0223fbfa..ff9bb8a45e 100644 --- a/asserts/database.go +++ b/asserts/database.go @@ -29,26 +29,23 @@ import ( "time" ) -// 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. +// 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 under the given unique primaryPath. + // 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, primaryPath []string, assert Assertion) error - // Get loads an assertion with the given unique primaryPath. + Put(assertType AssertionType, primaryKeyHeaders []string, assert Assertion) error + // Get returns the assertion with the given unique key for the primaryKeyHeaders. // If none is present it returns ErrNotFound. - Get(assertType AssertionType, primaryPath []string) (Assertion, error) - // Search searches for assertions matching the given headers. + Get(assertType AssertionType, primaryKeyHeaders, key []string) (Assertion, error) + // Search returns 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 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 @@ -209,14 +206,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 @@ -269,15 +265,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 { @@ -298,15 +291,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 } @@ -324,15 +317,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..abdd1d80f5 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, primaryKeyHeaders []string, assert Assertion) error { + primaryPath := make([]string, len(primaryKeyHeaders)) + for i, k := range primaryKeyHeaders { + 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, primaryKeyHeaders, key []string) (Assertion, error) { + return fsbs.readAssertion(assertType, buildDiskPrimaryPath(key)) } 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, 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] = "*" } else { - diskPattern[i] = url.QueryEscape(comp) + diskPattern[i] = url.QueryEscape(keyVal) } } |
