From c940e8324b51ac2d32a65c0cc1a7e36f7b7989cf Mon Sep 17 00:00:00 2001 From: AndrewSisley Date: Thu, 14 Apr 2022 12:48:31 -0400 Subject: [PATCH] refactor: Remove dead code from client package and document remaining (#356) * Remove unneeded no-lint comment * Extract errors to own file We should declare all our public errors here * Declare IndexNotFound error * Declare ErrDocumentNotFound error No good declaring it internally if it is publicly accessable * Declare ErrInvalidUpdateTarget error * Declare ErrInvalidUpdater error * Declare ErrInvalidDeleteTarget error * Make dockey version constant private * Make namespaceSDNDocKeyV0 private * Make versionToNamespace private * Remove Undef DocKey I cant spot anywhere where we would want users to provide one of these, and don't want to encourage it * Remove unused CreateOpt struct * Remove unused UpdateOpt struct * Remove unused DeleteOpt struct * Move DB interface to own file core.go is ambiguous with the newer types added to the package, and this file will get hard to read once documention expands the line count * Rename core.go to collection.go Having moved the DB interface out, this name makes sense * Remove unimplemented CreateIndex function from public interface This function is unlikely to be implemented for quite a while and as it modifies an existing collection, it comes with a load of synchronization questions that should be answered properly before we can assume it will exist in the currently declared form * Return result from UpdateWith Note: this function is untested * Return result from DeleteWith * Add package level documentation * Document CType consts * Document errors * Document Collection interface * Document DocKeysResult * Document UpdateResult * Document DeleteResult * Tweak error messages --- client/collection.go | 168 ++++++++++++++++++++++++++++++++++ client/core.go | 100 -------------------- client/ctype.go | 5 +- client/db.go | 45 +++++++++ db/errors.go => client/doc.go | 14 +-- client/dockey.go | 20 ++-- client/document.go | 7 -- client/errors.go | 28 ++++++ db/collection.go | 13 +-- db/collection_delete.go | 47 ++++------ db/collection_get.go | 2 +- db/collection_update.go | 41 +++------ db/db_test.go | 4 +- 13 files changed, 301 insertions(+), 193 deletions(-) create mode 100644 client/collection.go delete mode 100644 client/core.go create mode 100644 client/db.go rename db/errors.go => client/doc.go (52%) create mode 100644 client/errors.go diff --git a/client/collection.go b/client/collection.go new file mode 100644 index 0000000000..b4d9ad2c32 --- /dev/null +++ b/client/collection.go @@ -0,0 +1,168 @@ +// Copyright 2022 Democratized Data Foundation +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +package client + +import ( + "context" + + "github.com/sourcenetwork/defradb/datastore" +) + +// Collection represents a defradb collection. +// +// A Collection is mostly analogous to a SQL table, however a collection is specific to its +// host, and many collections may share the same schema. +// +// Many functions on this object will interact with the underlying datastores. +type Collection interface { + // Description returns the CollectionDescription of this Collection. + Description() CollectionDescription + // Name returns the name of this collection. + Name() string + // Schema returns the SchemaDescription used to define this Collection. + Schema() SchemaDescription + // ID returns the ID of this Collection. + ID() uint32 + // SchemaID returns the ID of the Schema used to define this Collection. + SchemaID() string + + // Indexes returns all the indexes defined on this Collection. + Indexes() []IndexDescription + // PrimaryIndex returns the primary index for the this Collection. + PrimaryIndex() IndexDescription + // Index returns the index with the given index ID. + // + // If no index is found with the given ID an ErrIndexNotFound error will be returned. + Index(uint32) (IndexDescription, error) + + // Create a new document. + // + // Will verify the DocKey/CID to ensure that the new document is correctly formatted. + Create(context.Context, *Document) error + // CreateMany new documents. + // + // Will verify the DocKeys/CIDs to ensure that the new documents are correctly formatted. + CreateMany(context.Context, []*Document) error + // Update an existing document with the new values. + // + // Any field that needs to be removed or cleared should call doc.Clear(field) before. + // Any field that is nil/empty that hasn't called Clear will be ignored. + // + // Will return a ErrDocumentNotFound error if the given document is not found. + Update(context.Context, *Document) error + // Save the given document in the database. + // + // If a document exists with the given DocKey it will update it. Otherwise a new document + // will be created. + Save(context.Context, *Document) error + // Delete will attempt to delete a document by key. + // + // Will return true if a deletion is successful, and return false along with an error + // if it cannot. If the document doesn't exist, then it will return false and a ErrDocumentNotFound error. + // This operation will hard-delete all state relating to the given DocKey. This includes data, block, and head storage. + Delete(context.Context, DocKey) (bool, error) + // Exists checks if a given document exists with supplied DocKey. + // + // Will return true if a matching document exists, otherwise will return false. + Exists(context.Context, DocKey) (bool, error) + + // UpdateWith updates a target document using the given updater type. + // + // Target can be a Filter statement, a single docKey, a single document, + // an array of docKeys, or an array of documents. + // It is recommened to use the respective typed versions of Update + // (e.g. UpdateWithFilter or UpdateWithKey) over this function if you can. + // + // Returns an ErrInvalidUpdateTarget error if the target type is not supported. + // Returns an ErrInvalidUpdater error if the updater type is not supported. + UpdateWith(ctx context.Context, target interface{}, updater interface{}) (*UpdateResult, error) + // UpdateWithFilter updates using a filter to target documents for update. + // + // The provided updater must be a string Patch, string Merge Patch, a parsed Patch, or parsed Merge Patch + // else an ErrInvalidUpdater will be returned. + UpdateWithFilter(ctx context.Context, filter interface{}, updater interface{}) (*UpdateResult, error) + // UpdateWithKey updates using a DocKey to target a single document for update. + // + // The provided updater must be a string Patch, string Merge Patch, a parsed Patch, or parsed Merge Patch + // else an ErrInvalidUpdater will be returned. + // + // Returns an ErrDocumentNotFound if a document matching the given DocKey is not found. + UpdateWithKey(ctx context.Context, key DocKey, updater interface{}) (*UpdateResult, error) + // UpdateWithKeys updates documents matching the given DocKeys. + // + // The provided updater must be a string Patch, string Merge Patch, a parsed Patch, or parsed Merge Patch + // else an ErrInvalidUpdater will be returned. + // + // Returns an ErrDocumentNotFound if a document is not found for any given DocKey. + UpdateWithKeys(context.Context, []DocKey, interface{}) (*UpdateResult, error) + + // DeleteWith deletes a target document. + // + // Target can be a Filter statement, a single docKey, a single document, an array of docKeys, + // or an array of documents. It is recommened to use the respective typed versions of Delete + // (e.g. DeleteWithFilter or DeleteWithKey) over this function if you can. + // This operation will hard-delete all state relating to the given DocKey. This includes data, block, and head storage. + // + // Returns an ErrInvalidDeleteTarget if the target type is not supported. + DeleteWith(ctx context.Context, target interface{}) (*DeleteResult, error) + // DeleteWithFilter deletes documents matching the given filter. + // + // This operation will hard-delete all state relating to the given DocKey. This includes data, block, and head storage. + DeleteWithFilter(ctx context.Context, filter interface{}) (*DeleteResult, error) + // DeleteWithKey deletes using a DocKey to target a single document for delete. + // + // This operation will hard-delete all state relating to the given DocKey. This includes data, block, and head storage. + // + // Returns an ErrDocumentNotFound if a document matching the given DocKey is not found. + DeleteWithKey(context.Context, DocKey) (*DeleteResult, error) + // DeleteWithKeys deletes documents matching the given DocKeys. + // + // This operation will hard-delete all state relating to the given DocKey. This includes data, block, and head storage. + // + // Returns an ErrDocumentNotFound if a document is not found for any given DocKey. + DeleteWithKeys(context.Context, []DocKey) (*DeleteResult, error) + + // Get returns the document with the given DocKey. + // + // Returns an ErrDocumentNotFound if a document matching the given DocKey is not found. + Get(context.Context, DocKey) (*Document, error) + + // WithTxn returns a new instance of the collection, with a transaction + // handle instead of a raw DB handle. + WithTxn(datastore.Txn) Collection + + // GetAllDocKeys returns all the document keys that exist in the collection. + GetAllDocKeys(ctx context.Context) (<-chan DocKeysResult, error) +} + +// DocKeysResult wraps the result of an attempt at a DocKey retrieval operation. +type DocKeysResult struct { + // If a DocKey was successfully retrieved, this will be that key. + Key DocKey + // If an error was generated whilst attempting to retrieve the DocKey, this will be the error. + Err error +} + +// UpdateResult wraps the result of an update call. +type UpdateResult struct { + // Count contains the number of documents updated by the update call. + Count int64 + // DocKeys contains the DocKeys of all the documents updated by the update call. + DocKeys []string +} + +// DeleteResult wraps the result of an delete call. +type DeleteResult struct { + // Count contains the number of documents deleted by the delete call. + Count int64 + // DocKeys contains the DocKeys of all the documents deleted by the delete call. + DocKeys []string +} diff --git a/client/core.go b/client/core.go deleted file mode 100644 index 3755684bf7..0000000000 --- a/client/core.go +++ /dev/null @@ -1,100 +0,0 @@ -// Copyright 2022 Democratized Data Foundation -// -// Use of this software is governed by the Business Source License -// included in the file licenses/BSL.txt. -// -// As of the Change Date specified in that file, in accordance with -// the Business Source License, use of this software will be governed -// by the Apache License, Version 2.0, included in the file -// licenses/APL.txt. - -package client - -import ( - "context" - - "github.com/sourcenetwork/defradb/datastore" - - ds "github.com/ipfs/go-datastore" - blockstore "github.com/ipfs/go-ipfs-blockstore" -) - -type DB interface { - AddSchema(context.Context, string) error - - CreateCollection(context.Context, CollectionDescription) (Collection, error) - GetCollectionByName(context.Context, string) (Collection, error) - GetCollectionBySchemaID(context.Context, string) (Collection, error) - GetAllCollections(ctx context.Context) ([]Collection, error) - GetRelationshipIdField(fieldName, targetType, thisType string) (string, error) - - Root() ds.Batching - Blockstore() blockstore.Blockstore - - NewTxn(context.Context, bool) (datastore.Txn, error) - ExecQuery(context.Context, string) *QueryResult - ExecTransactionalQuery(ctx context.Context, query string, txn datastore.Txn) *QueryResult - Close(context.Context) - - PrintDump(ctx context.Context) -} - -type Collection interface { - Description() CollectionDescription - Name() string - Schema() SchemaDescription - ID() uint32 - SchemaID() string - - Indexes() []IndexDescription - PrimaryIndex() IndexDescription - Index(uint32) (IndexDescription, error) - CreateIndex(IndexDescription) error - - Create(context.Context, *Document) error - CreateMany(context.Context, []*Document) error - Update(context.Context, *Document) error - Save(context.Context, *Document) error - Delete(context.Context, DocKey) (bool, error) - Exists(context.Context, DocKey) (bool, error) - - UpdateWith(context.Context, interface{}, interface{}, ...UpdateOpt) error - UpdateWithFilter(context.Context, interface{}, interface{}, ...UpdateOpt) (*UpdateResult, error) - UpdateWithKey(context.Context, DocKey, interface{}, ...UpdateOpt) (*UpdateResult, error) - UpdateWithKeys(context.Context, []DocKey, interface{}, ...UpdateOpt) (*UpdateResult, error) - - DeleteWith(context.Context, interface{}, ...DeleteOpt) error - DeleteWithFilter(context.Context, interface{}, ...DeleteOpt) (*DeleteResult, error) - DeleteWithKey(context.Context, DocKey, ...DeleteOpt) (*DeleteResult, error) - DeleteWithKeys(context.Context, []DocKey, ...DeleteOpt) (*DeleteResult, error) - - Get(context.Context, DocKey) (*Document, error) - - WithTxn(datastore.Txn) Collection - - GetAllDocKeys(ctx context.Context) (<-chan DocKeysResult, error) -} - -type DocKeysResult struct { - Key DocKey - Err error -} - -type UpdateOpt struct{} -type CreateOpt struct{} -type DeleteOpt struct{} - -type UpdateResult struct { - Count int64 - DocKeys []string -} - -type DeleteResult struct { - Count int64 - DocKeys []string -} - -type QueryResult struct { - Errors []interface{} `json:"errors,omitempty"` - Data interface{} `json:"data"` -} diff --git a/client/ctype.go b/client/ctype.go index b1a116b3ad..9caf63ec96 100644 --- a/client/ctype.go +++ b/client/ctype.go @@ -13,8 +13,11 @@ package client // CType indicates CRDT type type CType byte +// Available CRDT types. +// +// If a CRDT type is not declared here, we do not support it! CRDT types here +// may not be valid in all contexts. const ( - //no lint NONE_CRDT = CType(iota) // reserved none type LWW_REGISTER OBJECT diff --git a/client/db.go b/client/db.go new file mode 100644 index 0000000000..e40459a088 --- /dev/null +++ b/client/db.go @@ -0,0 +1,45 @@ +// Copyright 2022 Democratized Data Foundation +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +package client + +import ( + "context" + + "github.com/sourcenetwork/defradb/datastore" + + ds "github.com/ipfs/go-datastore" + blockstore "github.com/ipfs/go-ipfs-blockstore" +) + +type DB interface { + AddSchema(context.Context, string) error + + CreateCollection(context.Context, CollectionDescription) (Collection, error) + GetCollectionByName(context.Context, string) (Collection, error) + GetCollectionBySchemaID(context.Context, string) (Collection, error) + GetAllCollections(ctx context.Context) ([]Collection, error) + GetRelationshipIdField(fieldName, targetType, thisType string) (string, error) + + Root() ds.Batching + Blockstore() blockstore.Blockstore + + NewTxn(context.Context, bool) (datastore.Txn, error) + ExecQuery(context.Context, string) *QueryResult + ExecTransactionalQuery(ctx context.Context, query string, txn datastore.Txn) *QueryResult + Close(context.Context) + + PrintDump(ctx context.Context) +} + +type QueryResult struct { + Errors []interface{} `json:"errors,omitempty"` + Data interface{} `json:"data"` +} diff --git a/db/errors.go b/client/doc.go similarity index 52% rename from db/errors.go rename to client/doc.go index 9df0f9cc99..587ab7e4d5 100644 --- a/db/errors.go +++ b/client/doc.go @@ -8,11 +8,11 @@ // by the Apache License, Version 2.0, included in the file // licenses/APL.txt. -package db +/* +The client package provides public members for interacting with a Defra DB instance. -import "errors" - -// errors -var ( - ErrDocumentNotFound = errors.New("No document for the given key exists") -) +Only calls made via the `DB` and `Collection` interfaces interact with the underlying datastores. +Currently the only provided implementation of `DB` is found in the `defra/db` package and can be +instantiated via the `NewDB` function. +*/ +package client diff --git a/client/dockey.go b/client/dockey.go index 307da7c7aa..6ebcde8c69 100644 --- a/client/dockey.go +++ b/client/dockey.go @@ -24,11 +24,11 @@ import ( // Key Versions const ( - V0 = 0x01 + v0 = 0x01 ) var validVersions = map[uint16]bool{ - V0: true, + v0: true, } var ( @@ -36,12 +36,13 @@ var ( // Design a more appropriate system for future proofing doc key versions, ensuring // backwards compatability. RE: CID // *At the moment this is an random uuidV4 - NamespaceSDNDocKeyV0 = uuid.Must(uuid.FromString("c94acbfa-dd53-40d0-97f3-29ce16c333fc")) + namespaceSDNDocKeyV0 = uuid.Must(uuid.FromString("c94acbfa-dd53-40d0-97f3-29ce16c333fc")) ) -// VersionToNamespace is a convenience for mapping between Version number and its UUID Namespace -var VersionToNamespace = map[uint16]uuid.UUID{ - V0: NamespaceSDNDocKeyV0, +// versionToNamespace is a convenience for mapping between Version number and its UUID Namespace +// nolint +var versionToNamespace = map[uint16]uuid.UUID{ + v0: namespaceSDNDocKeyV0, } // DocKey is the root key identifier for documents in DefraDB @@ -51,16 +52,13 @@ type DocKey struct { cid cid.Cid } -// Undef can be defined to be a nil like DocKey -var Undef = DocKey{} - // NewDocKeyV0 creates a new doc key identified by the root data CID, peer ID, and // namespaced by the versionNS // TODO: Parameterize namespace Version func NewDocKeyV0(dataCID cid.Cid) DocKey { return DocKey{ - version: V0, - uuid: uuid.NewV5(NamespaceSDNDocKeyV0, dataCID.String()), + version: v0, + uuid: uuid.NewV5(namespaceSDNDocKeyV0, dataCID.String()), cid: dataCID, } } diff --git a/client/document.go b/client/document.go index 5942ff8518..4ed9f9aa0b 100644 --- a/client/document.go +++ b/client/document.go @@ -36,13 +36,6 @@ import ( // of complex interactions with the underlying KV Datastore, as well as the // Merkle CRDT semantics. -// errors -var ( - ErrFieldNotExist = errors.New("The given field does not exist") - ErrFieldNotObject = errors.New("Trying to access field on a non object type") - ErrValueTypeMismatch = errors.New("Value does not match indicated type") -) - // Document is a generalized struct referring to a stored document in the database. // // It *can* have a reference to a enforced schema, which is enforced at the time diff --git a/client/errors.go b/client/errors.go new file mode 100644 index 0000000000..bda02147c3 --- /dev/null +++ b/client/errors.go @@ -0,0 +1,28 @@ +// Copyright 2022 Democratized Data Foundation +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +package client + +import "errors" + +// Errors returnable from this package. +// +// This list is incomplete and undefined errors may also be returned. +// Errors returned from this package may be tested against these errors with errors.Is. +var ( + ErrFieldNotExist = errors.New("The given field does not exist") + ErrFieldNotObject = errors.New("Trying to access field on a non object type") + ErrValueTypeMismatch = errors.New("Value does not match indicated type") + ErrIndexNotFound = errors.New("No index found for given ID") + ErrDocumentNotFound = errors.New("No document for the given key exists") + ErrInvalidUpdateTarget = errors.New("The target document to update is of invalid type") + ErrInvalidUpdater = errors.New("The updater of a document is of invalid type") + ErrInvalidDeleteTarget = errors.New("The target document to delete is of invalid type") +) diff --git a/db/collection.go b/db/collection.go index e20b1fcb56..cf807588d2 100644 --- a/db/collection.go +++ b/db/collection.go @@ -390,14 +390,7 @@ func (c *collection) Index(id uint32) (client.IndexDescription, error) { } } - return client.IndexDescription{}, errors.New("No index found for given ID") -} - -// CreateIndex creates a new index on the collection. Custom indexes -// are always "Secondary indexes". Primary indexes are automatically created -// on Collection creation, and cannot be changed. -func (c *collection) CreateIndex(idesc client.IndexDescription) error { - panic("not implemented") + return client.IndexDescription{}, client.ErrIndexNotFound } func (c *collection) SchemaID() string { @@ -511,7 +504,7 @@ func (c *collection) Update(ctx context.Context, doc *client.Document) error { return err } if !exists { - return ErrDocumentNotFound + return client.ErrDocumentNotFound } err = c.update(ctx, txn, doc) @@ -654,7 +647,7 @@ func (c *collection) Delete(ctx context.Context, key client.DocKey) (bool, error return false, err } if !exists { - return false, ErrDocumentNotFound + return false, client.ErrDocumentNotFound } // run delete, commit if successful diff --git a/db/collection_delete.go b/db/collection_delete.go index 120be5d94e..e7811f1b9e 100644 --- a/db/collection_delete.go +++ b/db/collection_delete.go @@ -30,9 +30,8 @@ import ( ) var ( - ErrInvalidDeleteTarget = errors.New("The doc delete targeter is an unknown type") - ErrDeleteTargetEmpty = errors.New("The doc delete targeter cannot be empty") - ErrDeleteEmpty = errors.New("The doc delete cannot be empty") + ErrDeleteTargetEmpty = errors.New("The doc delete targeter cannot be empty") + ErrDeleteEmpty = errors.New("The doc delete cannot be empty") ) // DeleteWith deletes a target document. Target can be a Filter statement, @@ -42,24 +41,16 @@ var ( func (c *collection) DeleteWith( ctx context.Context, target interface{}, - opts ...client.DeleteOpt) error { - +) (*client.DeleteResult, error) { switch t := target.(type) { - case string, map[string]interface{}, *parser.Filter: - _, err := c.DeleteWithFilter(ctx, t, opts...) - return err - + return c.DeleteWithFilter(ctx, t) case client.DocKey: - _, err := c.DeleteWithKey(ctx, t, opts...) - return err - + return c.DeleteWithKey(ctx, t) case []client.DocKey: - _, err := c.DeleteWithKeys(ctx, t, opts...) - return err + return c.DeleteWithKeys(ctx, t) default: - return ErrInvalidDeleteTarget - + return nil, client.ErrInvalidDeleteTarget } } @@ -67,7 +58,7 @@ func (c *collection) DeleteWith( func (c *collection) DeleteWithKey( ctx context.Context, key client.DocKey, - opts ...client.DeleteOpt) (*client.DeleteResult, error) { +) (*client.DeleteResult, error) { txn, err := c.getTxn(ctx, false) if err != nil { @@ -77,7 +68,7 @@ func (c *collection) DeleteWithKey( defer c.discardImplicitTxn(ctx, txn) dsKey := c.getPrimaryKeyFromDocKey(key) - res, err := c.deleteWithKey(ctx, txn, dsKey, opts...) + res, err := c.deleteWithKey(ctx, txn, dsKey) if err != nil { return nil, err } @@ -89,7 +80,7 @@ func (c *collection) DeleteWithKey( func (c *collection) DeleteWithKeys( ctx context.Context, keys []client.DocKey, - opts ...client.DeleteOpt) (*client.DeleteResult, error) { +) (*client.DeleteResult, error) { txn, err := c.getTxn(ctx, false) if err != nil { @@ -98,7 +89,7 @@ func (c *collection) DeleteWithKeys( defer c.discardImplicitTxn(ctx, txn) - res, err := c.deleteWithKeys(ctx, txn, keys, opts...) + res, err := c.deleteWithKeys(ctx, txn, keys) if err != nil { return nil, err } @@ -110,7 +101,7 @@ func (c *collection) DeleteWithKeys( func (c *collection) DeleteWithFilter( ctx context.Context, filter interface{}, - opts ...client.DeleteOpt) (*client.DeleteResult, error) { +) (*client.DeleteResult, error) { txn, err := c.getTxn(ctx, false) if err != nil { @@ -119,7 +110,7 @@ func (c *collection) DeleteWithFilter( defer c.discardImplicitTxn(ctx, txn) - res, err := c.deleteWithFilter(ctx, txn, filter, opts...) + res, err := c.deleteWithFilter(ctx, txn, filter) if err != nil { return nil, err } @@ -132,7 +123,7 @@ func (c *collection) deleteWithKey( ctx context.Context, txn datastore.Txn, key core.PrimaryDataStoreKey, - opts ...client.DeleteOpt) (*client.DeleteResult, error) { +) (*client.DeleteResult, error) { // Check the docKey we have been given to delete with actually has a corresponding // document (i.e. document actually exists in the collection). found, err := c.exists(ctx, txn, key) @@ -140,7 +131,7 @@ func (c *collection) deleteWithKey( return nil, err } if !found { - return nil, ErrDocumentNotFound + return nil, client.ErrDocumentNotFound } // Apply the function that will perform the full deletion of the document. @@ -162,7 +153,7 @@ func (c *collection) deleteWithKeys( ctx context.Context, txn datastore.Txn, keys []client.DocKey, - opts ...client.DeleteOpt) (*client.DeleteResult, error) { +) (*client.DeleteResult, error) { results := &client.DeleteResult{ DocKeys: make([]string, 0), @@ -178,7 +169,7 @@ func (c *collection) deleteWithKeys( return nil, err } if !found { - return nil, ErrDocumentNotFound + return nil, client.ErrDocumentNotFound } // Apply the function that will perform the full deletion of this document. @@ -201,7 +192,7 @@ func (c *collection) deleteWithFilter( ctx context.Context, txn datastore.Txn, filter interface{}, - opts ...client.DeleteOpt) (*client.DeleteResult, error) { +) (*client.DeleteResult, error) { // Do a selection query to scan through documents using the given filter. query, err := c.makeSelectionQuery(ctx, txn, filter) @@ -290,7 +281,7 @@ func (c *collection) applyFullDelete( return err } if !found { - return ErrDocumentNotFound + return client.ErrDocumentNotFound } // 1. =========================== Delete blockstore state =========================== diff --git a/db/collection_get.go b/db/collection_get.go index 1cb30c2159..ec83c0147d 100644 --- a/db/collection_get.go +++ b/db/collection_get.go @@ -34,7 +34,7 @@ func (c *collection) Get(ctx context.Context, key client.DocKey) (*client.Docume return nil, err } if !found { - return nil, ErrDocumentNotFound + return nil, client.ErrDocumentNotFound } doc, err := c.get(ctx, txn, dsKey) diff --git a/db/collection_update.go b/db/collection_update.go index 60b7bbb59b..a281670ca9 100644 --- a/db/collection_update.go +++ b/db/collection_update.go @@ -28,9 +28,7 @@ import ( ) var ( - ErrInvalidUpdateTarget = errors.New("The doc update targeter is an unknown type") ErrUpdateTargetEmpty = errors.New("The doc update targeter cannot be empty") - ErrInvalidUpdater = errors.New("The doc updater is an unknown type") ErrUpdateEmpty = errors.New("The doc update cannot be empty") ErrInvalidMergeValueType = errors.New( "The type of value in the merge patch doesn't match the schema", @@ -46,20 +44,16 @@ func (c *collection) UpdateWith( ctx context.Context, target interface{}, updater interface{}, - opts ...client.UpdateOpt, -) error { +) (*client.UpdateResult, error) { switch t := target.(type) { case string, map[string]interface{}, *parser.Filter: - _, err := c.UpdateWithFilter(ctx, t, updater, opts...) - return err + return c.UpdateWithFilter(ctx, t, updater) case client.DocKey: - _, err := c.UpdateWithKey(ctx, t, updater, opts...) - return err + return c.UpdateWithKey(ctx, t, updater) case []client.DocKey: - _, err := c.UpdateWithKeys(ctx, t, updater, opts...) - return err + return c.UpdateWithKeys(ctx, t, updater) default: - return ErrInvalidUpdateTarget + return nil, client.ErrInvalidUpdateTarget } } @@ -70,14 +64,13 @@ func (c *collection) UpdateWithFilter( ctx context.Context, filter interface{}, updater interface{}, - opts ...client.UpdateOpt, ) (*client.UpdateResult, error) { txn, err := c.getTxn(ctx, false) if err != nil { return nil, err } defer c.discardImplicitTxn(ctx, txn) - res, err := c.updateWithFilter(ctx, txn, filter, updater, opts...) + res, err := c.updateWithFilter(ctx, txn, filter, updater) if err != nil { return nil, err } @@ -91,14 +84,13 @@ func (c *collection) UpdateWithKey( ctx context.Context, key client.DocKey, updater interface{}, - opts ...client.UpdateOpt, ) (*client.UpdateResult, error) { txn, err := c.getTxn(ctx, false) if err != nil { return nil, err } defer c.discardImplicitTxn(ctx, txn) - res, err := c.updateWithKey(ctx, txn, key, updater, opts...) + res, err := c.updateWithKey(ctx, txn, key, updater) if err != nil { return nil, err } @@ -113,14 +105,13 @@ func (c *collection) UpdateWithKeys( ctx context.Context, keys []client.DocKey, updater interface{}, - opts ...client.UpdateOpt, ) (*client.UpdateResult, error) { txn, err := c.getTxn(ctx, false) if err != nil { return nil, err } defer c.discardImplicitTxn(ctx, txn) - res, err := c.updateWithKeys(ctx, txn, keys, updater, opts...) + res, err := c.updateWithKeys(ctx, txn, keys, updater) if err != nil { return nil, err } @@ -133,7 +124,6 @@ func (c *collection) updateWithKey( txn datastore.Txn, key client.DocKey, updater interface{}, - opts ...client.UpdateOpt, ) (*client.UpdateResult, error) { patch, err := parseUpdater(updater) if err != nil { @@ -147,7 +137,7 @@ func (c *collection) updateWithKey( case map[string]interface{}: isPatch = false default: - return nil, ErrInvalidUpdater + return nil, client.ErrInvalidUpdater } doc, err := c.Get(ctx, key) @@ -180,7 +170,6 @@ func (c *collection) updateWithKeys( txn datastore.Txn, keys []client.DocKey, updater interface{}, - opts ...client.UpdateOpt, ) (*client.UpdateResult, error) { patch, err := parseUpdater(updater) if err != nil { @@ -194,7 +183,7 @@ func (c *collection) updateWithKeys( case map[string]interface{}: isPatch = false default: - return nil, ErrInvalidUpdater + return nil, client.ErrInvalidUpdater } results := &client.UpdateResult{ @@ -230,7 +219,7 @@ func (c *collection) updateWithFilter( txn datastore.Txn, filter interface{}, updater interface{}, - opts ...client.UpdateOpt) (*client.UpdateResult, error) { +) (*client.UpdateResult, error) { patch, err := parseUpdater(updater) if err != nil { @@ -245,7 +234,7 @@ func (c *collection) updateWithFilter( case map[string]interface{}: isMerge = true default: - return nil, ErrInvalidUpdater + return nil, client.ErrInvalidUpdater } // scan through docs with filter @@ -568,7 +557,7 @@ func (c *collection) makeSelectionQuery( ctx context.Context, txn datastore.Txn, filter interface{}, - opts ...client.UpdateOpt) (planner.Query, error) { +) (planner.Query, error) { var f *parser.Filter var err error switch fval := filter.(type) { @@ -693,7 +682,7 @@ func parseUpdater(updater interface{}) (patcher, error) { case nil: return nil, ErrUpdateEmpty default: - return nil, ErrInvalidUpdater + return nil, client.ErrInvalidUpdater } } @@ -719,7 +708,7 @@ func parseUpdaterSlice(v []interface{}) (patcher, error) { for i, patch := range v { p, ok := patch.(map[string]interface{}) if !ok { - return nil, ErrInvalidUpdater + return nil, client.ErrInvalidUpdater } patches[i] = p } diff --git a/db/db_test.go b/db/db_test.go index 999899bbc5..fc9a1b1943 100644 --- a/db/db_test.go +++ b/db/db_test.go @@ -276,7 +276,7 @@ func TestDBGetNotFoundDocument(t *testing.T) { key, err := client.NewDocKeyFromString("bae-09cd7539-9b86-5661-90f6-14fbf6c1a14d") assert.NoError(t, err) _, err = col.Get(ctx, key) - assert.EqualError(t, err, ErrDocumentNotFound.Error()) + assert.EqualError(t, err, client.ErrDocumentNotFound.Error()) } func TestDBDeleteDocument(t *testing.T) { @@ -315,7 +315,7 @@ func TestDBDeleteNotFoundDocument(t *testing.T) { key, err := client.NewDocKeyFromString("bae-09cd7539-9b86-5661-90f6-14fbf6c1a14d") assert.NoError(t, err) deleted, err := col.Delete(ctx, key) - assert.EqualError(t, err, ErrDocumentNotFound.Error()) + assert.EqualError(t, err, client.ErrDocumentNotFound.Error()) assert.False(t, deleted) }