From b40add3036b1dfd0d20a13888e0d693e846651f2 Mon Sep 17 00:00:00 2001 From: Andrew Sisley Date: Mon, 7 Mar 2022 11:02:37 -0500 Subject: [PATCH 01/16] Rename var to collection name Is clearer --- net/peer.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/net/peer.go b/net/peer.go index d0a41a1f57..8efbdc59a2 100644 --- a/net/peer.go +++ b/net/peer.go @@ -229,11 +229,11 @@ func (p *Peer) RegisterNewDocument(ctx context.Context, dockey key.DocKey, c cid } // AddReplicator adds a target peer node as a replication destination for documents in our DB -func (p *Peer) AddReplicator(ctx context.Context, collection string, paddr ma.Multiaddr) (peer.ID, error) { +func (p *Peer) AddReplicator(ctx context.Context, collectionName string, paddr ma.Multiaddr) (peer.ID, error) { var pid peer.ID // verify collection - col, err := p.db.GetCollection(ctx, collection) + col, err := p.db.GetCollection(ctx, collectionName) if err != nil { return pid, fmt.Errorf("Failed to get collection for replicator: %w", err) } @@ -259,7 +259,7 @@ func (p *Peer) AddReplicator(ctx context.Context, collection string, paddr ma.Mu defer p.mu.Unlock() if reps, exists := p.replicators[col.SchemaID()]; exists { if _, exists := reps[pid]; exists { - return pid, fmt.Errorf("Replicator already exists for %s with ID %s", collection, pid) + return pid, fmt.Errorf("Replicator already exists for %s with ID %s", collectionName, pid) } } else { p.replicators[col.SchemaID()] = make(map[peer.ID]struct{}) @@ -290,7 +290,7 @@ func (p *Peer) AddReplicator(ctx context.Context, collection string, paddr ma.Mu keysCh, err := col.GetAllDocKeys(ctx) if err != nil { txn.Discard(ctx) - return pid, fmt.Errorf("Failed to get dockey for replicator %s on %s: %w", pid, collection, err) + return pid, fmt.Errorf("Failed to get dockey for replicator %s on %s: %w", pid, collectionName, err) } // async @@ -314,7 +314,7 @@ func (p *Peer) AddReplicator(ctx context.Context, collection string, paddr ma.Mu err, logging.NewKV("DocKey", dockey), logging.NewKV("Pid", pid), - logging.NewKV("Collection", collection)) + logging.NewKV("Collection", collectionName)) continue } // loop over heads, get block, make the required logs, and send @@ -324,7 +324,7 @@ func (p *Peer) AddReplicator(ctx context.Context, collection string, paddr ma.Mu log.ErrorE(p.ctx, "Failed to get block", err, logging.NewKV("Cid", c), logging.NewKV("Pid", pid), - logging.NewKV("Collection", collection)) + logging.NewKV("Collection", collectionName)) continue } From 13e44c10a8590bbc6a95df8f23d369373e03eb2f Mon Sep 17 00:00:00 2001 From: Andrew Sisley Date: Mon, 7 Mar 2022 11:04:19 -0500 Subject: [PATCH 02/16] Rename GetCollection=>GetCollectionByName Previous fn name was a bit ambigious, particularly due to the existance of GetCollectionBySchemaID, which has the same method signature. --- bench/bench_util.go | 2 +- client/core.go | 2 +- db/collection.go | 6 +++--- db/collection_test.go | 12 ++++++------ db/tests/utils.go | 4 ++-- net/peer.go | 2 +- query/graphql/planner/create.go | 2 +- query/graphql/planner/delete.go | 2 +- query/graphql/planner/update.go | 2 +- 9 files changed, 17 insertions(+), 17 deletions(-) diff --git a/bench/bench_util.go b/bench/bench_util.go index 309bc58f99..54dbae5bb6 100644 --- a/bench/bench_util.go +++ b/bench/bench_util.go @@ -90,7 +90,7 @@ func SetupCollections(b *testing.B, ctx context.Context, db *defradb.DB, fixture // loop to get collections for i := 0; i < numTypes; i++ { - col, err := db.GetCollection(ctx, fixture.TypeName(i)) + col, err := db.GetCollectionByName(ctx, fixture.TypeName(i)) if err != nil { return nil, fmt.Errorf("Couldn't get the collection %v: %w", fixture.TypeName(i), err) } diff --git a/client/core.go b/client/core.go index ab58408ea8..fdaffaab56 100644 --- a/client/core.go +++ b/client/core.go @@ -27,7 +27,7 @@ import ( type DB interface { // Collections CreateCollection(context.Context, base.CollectionDescription) (Collection, error) - GetCollection(context.Context, string) (Collection, error) + GetCollectionByName(context.Context, string) (Collection, error) GetCollectionBySchemaID(context.Context, string) (Collection, error) ExecQuery(context.Context, string) *QueryResult SchemaManager() *schema.SchemaManager diff --git a/db/collection.go b/db/collection.go index d4983a15f4..aefaa0e4cd 100644 --- a/db/collection.go +++ b/db/collection.go @@ -171,7 +171,7 @@ func (db *DB) CreateCollection(ctx context.Context, desc base.CollectionDescript } // GetCollection returns an existing collection within the database -func (db *DB) GetCollection(ctx context.Context, name string) (client.Collection, error) { +func (db *DB) GetCollectionByName(ctx context.Context, name string) (client.Collection, error) { if name == "" { return nil, errors.New("Collection name can't be empty") } @@ -227,7 +227,7 @@ func (db *DB) GetCollectionBySchemaID(ctx context.Context, schemaID string) (cli } name := string(buf) - return db.GetCollection(ctx, name) + return db.GetCollectionByName(ctx, name) } // GetAllCollections gets all the currently defined collections in the @@ -255,7 +255,7 @@ func (db *DB) GetAllCollections(ctx context.Context) ([]client.Collection, error } colName := ds.NewKey(res.Key).BaseNamespace() - col, err := db.GetCollection(ctx, colName) + col, err := db.GetCollectionByName(ctx, colName) if err != nil { return nil, fmt.Errorf("Failed to get collection (%s): %w", colName, err) } diff --git a/db/collection_test.go b/db/collection_test.go index f1ade65519..eb8f8f69b8 100644 --- a/db/collection_test.go +++ b/db/collection_test.go @@ -261,7 +261,7 @@ func TestNewCollectionReturnsErrorGivenFieldWithNoType(t *testing.T) { assert.EqualError(t, err, "Collection schema field missing CRDT type") } -func TestGetCollection(t *testing.T) { +func TestGetCollectionByName(t *testing.T) { ctx := context.Background() db, err := newMemoryDB(ctx) assert.NoError(t, err) @@ -269,7 +269,7 @@ func TestGetCollection(t *testing.T) { _, err = newTestCollectionWithSchema(ctx, db) assert.NoError(t, err) - col, err := db.GetCollection(ctx, "users") + col, err := db.GetCollectionByName(ctx, "users") assert.NoError(t, err) schema := col.Schema() @@ -289,20 +289,20 @@ func TestGetCollection(t *testing.T) { } } -func TestGetCollectionReturnsErrorGivenNonExistantCollection(t *testing.T) { +func TestGetCollectionByNameReturnsErrorGivenNonExistantCollection(t *testing.T) { ctx := context.Background() db, err := newMemoryDB(ctx) assert.NoError(t, err) - _, err = db.GetCollection(ctx, "doesNotExist") + _, err = db.GetCollectionByName(ctx, "doesNotExist") assert.EqualError(t, err, "datastore: key not found") } -func TestGetCollectionReturnsErrorGivenEmptyString(t *testing.T) { +func TestGetCollectionByNameReturnsErrorGivenEmptyString(t *testing.T) { ctx := context.Background() db, err := newMemoryDB(ctx) assert.NoError(t, err) - _, err = db.GetCollection(ctx, "") + _, err = db.GetCollectionByName(ctx, "") assert.EqualError(t, err, "Collection name can't be empty") } diff --git a/db/tests/utils.go b/db/tests/utils.go index 5bb308f5cc..a49e967159 100644 --- a/db/tests/utils.go +++ b/db/tests/utils.go @@ -428,7 +428,7 @@ func setupDatabase(ctx context.Context, t *testing.T, dbi databaseInfo, schema s collections := []client.Collection{} for _, collectionName := range collectionNames { - col, err := db.GetCollection(ctx, collectionName) + col, err := db.GetCollectionByName(ctx, collectionName) if assertError(t, test.Description, err, test.ExpectedError) { return } @@ -498,7 +498,7 @@ func setupDatabaseUsingTargetBranch(ctx context.Context, t *testing.T, dbi datab panic(err) } - _, err = refreshedDb.db.GetCollection(ctx, collectionNames[0]) + _, err = refreshedDb.db.GetCollectionByName(ctx, collectionNames[0]) if err != nil { if err.Error() == "datastore: key not found" { // If collection is not found - this must be a new test and doesn't exist in the target branch, so we pass it diff --git a/net/peer.go b/net/peer.go index 8efbdc59a2..51252df9d3 100644 --- a/net/peer.go +++ b/net/peer.go @@ -233,7 +233,7 @@ func (p *Peer) AddReplicator(ctx context.Context, collectionName string, paddr m var pid peer.ID // verify collection - col, err := p.db.GetCollection(ctx, collectionName) + col, err := p.db.GetCollectionByName(ctx, collectionName) if err != nil { return pid, fmt.Errorf("Failed to get collection for replicator: %w", err) } diff --git a/query/graphql/planner/create.go b/query/graphql/planner/create.go index adccd386e9..1280964713 100644 --- a/query/graphql/planner/create.go +++ b/query/graphql/planner/create.go @@ -96,7 +96,7 @@ func (p *Planner) CreateDoc(parsed *parser.Mutation) (planNode, error) { } // get collection - col, err := p.db.GetCollection(p.ctx, parsed.Schema) + col, err := p.db.GetCollectionByName(p.ctx, parsed.Schema) if err != nil { return nil, err } diff --git a/query/graphql/planner/delete.go b/query/graphql/planner/delete.go index 8af16c9e14..6ea68b8cd6 100644 --- a/query/graphql/planner/delete.go +++ b/query/graphql/planner/delete.go @@ -124,7 +124,7 @@ func (p *Planner) DeleteDocs(parsed *parser.Mutation) (planNode, error) { } // get collection - col, err := p.db.GetCollection(p.ctx, parsed.Schema) + col, err := p.db.GetCollectionByName(p.ctx, parsed.Schema) if err != nil { return nil, err } diff --git a/query/graphql/planner/update.go b/query/graphql/planner/update.go index cb386562d9..317d796046 100644 --- a/query/graphql/planner/update.go +++ b/query/graphql/planner/update.go @@ -144,7 +144,7 @@ func (p *Planner) UpdateDocs(parsed *parser.Mutation) (planNode, error) { } // get collection - col, err := p.db.GetCollection(p.ctx, parsed.Schema) + col, err := p.db.GetCollectionByName(p.ctx, parsed.Schema) if err != nil { return nil, err } From a42645ef89c8dde6c9cc6ccb9267c14d1493e7a7 Mon Sep 17 00:00:00 2001 From: Andrew Sisley Date: Mon, 7 Mar 2022 11:40:12 -0500 Subject: [PATCH 03/16] Remove schemaManager from DB interface Exposes a large amount of internal generator types and is not nice on a public interface --- client/core.go | 3 +-- db/db.go | 14 +++++++++++++ db/schema.go | 7 ------- query/graphql/planner/type_join.go | 32 ++++++++---------------------- 4 files changed, 23 insertions(+), 33 deletions(-) diff --git a/client/core.go b/client/core.go index fdaffaab56..4ec768addb 100644 --- a/client/core.go +++ b/client/core.go @@ -17,7 +17,6 @@ import ( "github.com/sourcenetwork/defradb/db/base" "github.com/sourcenetwork/defradb/document" "github.com/sourcenetwork/defradb/document/key" - "github.com/sourcenetwork/defradb/query/graphql/schema" blocks "github.com/ipfs/go-block-format" cid "github.com/ipfs/go-cid" @@ -30,7 +29,7 @@ type DB interface { GetCollectionByName(context.Context, string) (Collection, error) GetCollectionBySchemaID(context.Context, string) (Collection, error) ExecQuery(context.Context, string) *QueryResult - SchemaManager() *schema.SchemaManager + GetRelationshipIdField(fieldName, targetType, thisType string) (string, error) AddSchema(context.Context, string) error PrintDump(ctx context.Context) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error) diff --git a/db/db.go b/db/db.go index 1094a0ef7e..cf894df167 100644 --- a/db/db.go +++ b/db/db.go @@ -13,6 +13,7 @@ package db import ( "context" "errors" + "fmt" "sync" "github.com/sourcenetwork/defradb/client" @@ -196,6 +197,19 @@ func (db *DB) Executor() *planner.QueryExecutor { return db.queryExecutor } +func (db *DB) GetRelationshipIdField(fieldName, targetType, thisType string) (string, error) { + rm := db.schema.Relations + rel := rm.GetRelationByDescription(fieldName, targetType, thisType) + if rel == nil { + return "", fmt.Errorf("Relation does not exists") + } + subtypefieldname, _, ok := rel.GetFieldFromSchemaType(targetType) + if !ok { + return "", fmt.Errorf("Relation is missing referenced field") + } + return subtypefieldname, nil +} + // Close is called when we are shutting down the database. // This is the place for any last minute cleanup or releaseing // of resources (IE: Badger instance) diff --git a/db/schema.go b/db/schema.go index b3311466af..d3efbe24ca 100644 --- a/db/schema.go +++ b/db/schema.go @@ -15,7 +15,6 @@ import ( dsq "github.com/ipfs/go-datastore/query" "github.com/sourcenetwork/defradb/core" - "github.com/sourcenetwork/defradb/query/graphql/schema" "github.com/graphql-go/graphql/language/ast" ) @@ -74,9 +73,3 @@ func (db *DB) saveSchema(ctx context.Context, astdoc *ast.Document) error { } return nil } - -// func (db *DB) LoadSchemaIfNotExists(schema string) error { return nil } - -func (db *DB) SchemaManager() *schema.SchemaManager { - return db.schema -} diff --git a/query/graphql/planner/type_join.go b/query/graphql/planner/type_join.go index 3f70e454e5..683cb43ef0 100644 --- a/query/graphql/planner/type_join.go +++ b/query/graphql/planner/type_join.go @@ -334,17 +334,6 @@ func (p *Planner) makeTypeJoinOne(parent *selectNode, source planNode, subType * return nil, fmt.Errorf("couldn't find subtype field description for typeJoin node") } - // get relation - rm := p.db.SchemaManager().Relations - rel := rm.GetRelationByDescription(subType.Name, subTypeFieldDesc.Schema, desc.Name) - if rel == nil { - return nil, fmt.Errorf("Relation does not exists") - } - subtypefieldname, _, ok := rel.GetFieldFromSchemaType(subTypeFieldDesc.Schema) - if !ok { - return nil, fmt.Errorf("Relation is missing referenced field") - } - subType.CollectionName = subTypeFieldDesc.Schema selectPlan, err := p.SubSelect(subType) @@ -354,7 +343,10 @@ func (p *Planner) makeTypeJoinOne(parent *selectNode, source planNode, subType * typeJoin.subType = selectPlan typeJoin.subTypeName = subTypeFieldDesc.Name - typeJoin.subTypeFieldName = subtypefieldname + typeJoin.subTypeFieldName, err = p.db.GetRelationshipIdField(subType.Name, subTypeFieldDesc.Schema, desc.Name) + if err != nil { + return nil, err + } // split filter if scan, ok := source.(*scanNode); ok { @@ -513,24 +505,16 @@ func (p *Planner) makeTypeJoinMany(parent *selectNode, source planNode, subType } subType.CollectionName = subTypeFieldDesc.Schema - // get relation - rm := p.db.SchemaManager().Relations - rel := rm.GetRelationByDescription(subType.Name, subTypeFieldDesc.Schema, desc.Name) - if rel == nil { - return nil, fmt.Errorf("Relation does not exists") - } - subTypeLookupFieldName, _, ok := rel.GetFieldFromSchemaType(subTypeFieldDesc.Schema) - if !ok { - return nil, fmt.Errorf("Relation is missing referenced field") - } - selectPlan, err := p.SubSelect(subType) if err != nil { return nil, err } typeJoin.subType = selectPlan typeJoin.subTypeName = subTypeFieldDesc.Name - typeJoin.rootName = subTypeLookupFieldName + typeJoin.rootName, err = p.db.GetRelationshipIdField(subType.Name, subTypeFieldDesc.Schema, desc.Name) + if err != nil { + return nil, err + } // split filter if scan, ok := source.(*scanNode); ok { From 8d69a8897636d4eb8e447c6e55569b94659b1234 Mon Sep 17 00:00:00 2001 From: Andrew Sisley Date: Mon, 7 Mar 2022 11:57:33 -0500 Subject: [PATCH 04/16] Use internal multistore instead of public fn Pub fn is an oddity that might be removed, this is one of two refs to it --- db/blockstore.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/blockstore.go b/db/blockstore.go index dd317eb840..589c0f1cb6 100644 --- a/db/blockstore.go +++ b/db/blockstore.go @@ -18,5 +18,5 @@ import ( ) func (db *DB) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error) { - return db.DAGstore().Get(ctx, c) + return db.multistore.DAGstore().Get(ctx, c) } From 7a36c25a5574cc8d35571603d54a58d1ef5704d1 Mon Sep 17 00:00:00 2001 From: Andrew Sisley Date: Mon, 7 Mar 2022 12:21:06 -0500 Subject: [PATCH 05/16] Use blockstore over dagstore for public interfaces Removes the defra-only DagStore type/name from the db interface - blockstore concept appears to be larger than defra, and there seems no sensible way to remove it from the interfaces (p2p lib requires one). If Cid and Block should be considered standard, then blockstore should be too. --- client/core.go | 6 ++---- db/db.go | 5 +++-- db/db_test.go | 4 ++-- node/node.go | 2 +- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/client/core.go b/client/core.go index 4ec768addb..7d45682c43 100644 --- a/client/core.go +++ b/client/core.go @@ -21,6 +21,7 @@ import ( blocks "github.com/ipfs/go-block-format" cid "github.com/ipfs/go-cid" ds "github.com/ipfs/go-datastore" + blockstore "github.com/ipfs/go-ipfs-blockstore" ) type DB interface { @@ -34,10 +35,7 @@ type DB interface { PrintDump(ctx context.Context) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error) Root() ds.Batching - // Rootstore() core.DSReaderWriter - // Headstore() core.DSReaderWriter - // Datastore() core.DSReaderWriter - DAGstore() core.DAGStore + Blockstore() blockstore.Blockstore NewTxn(context.Context, bool) (core.Txn, error) GetAllCollections(ctx context.Context) ([]Collection, error) diff --git a/db/db.go b/db/db.go index cf894df167..0dd748dcf5 100644 --- a/db/db.go +++ b/db/db.go @@ -27,6 +27,7 @@ import ( ds "github.com/ipfs/go-datastore" "github.com/ipfs/go-datastore/query" dsq "github.com/ipfs/go-datastore/query" + blockstore "github.com/ipfs/go-ipfs-blockstore" "github.com/sourcenetwork/defradb/logging" ) @@ -142,8 +143,8 @@ func (db *DB) Datastore() core.DSReaderWriter { return db.multistore.Datastore() } -// DAGstore returns the internal DAG store which contains IPLD blocks -func (db *DB) DAGstore() core.DAGStore { +// Blockstore returns the internal DAG store which contains IPLD blocks +func (db *DB) Blockstore() blockstore.Blockstore { return db.multistore.DAGstore() } diff --git a/db/db_test.go b/db/db_test.go index 79abe516d6..f348fa894c 100644 --- a/db/db_test.go +++ b/db/db_test.go @@ -343,7 +343,7 @@ func TestDocumentMerkleDAG(t *testing.T) { reg := corecrdt.LWWRegister{} for _, c := range cids { - b, errGet := db.DAGstore().Get(ctx, c) + b, errGet := db.Blockstore().Get(ctx, c) assert.NoError(t, errGet) nd, errDecode := dag.DecodeProtobuf(b.RawData()) @@ -373,7 +373,7 @@ func TestDocumentMerkleDAG(t *testing.T) { assert.NoError(t, err) for _, c := range cids { - b, err := db.DAGstore().Get(ctx, c) + b, err := db.Blockstore().Get(ctx, c) assert.NoError(t, err) nd, err := dag.DecodeProtobuf(b.RawData()) diff --git a/node/node.go b/node/node.go index 2bfb69fcaa..b311b9a802 100644 --- a/node/node.go +++ b/node/node.go @@ -108,7 +108,7 @@ func NewNode(ctx context.Context, db client.DB, bs *broadcast.Broadcaster, opts return nil, fin.Cleanup(err) } - bstore := db.DAGstore() + bstore := db.Blockstore() lite, err := ipfslite.New(ctx, rootstore, bstore, h, d, nil) if err != nil { return nil, fin.Cleanup(err) From 5d6867e032fdcddf550f5f481071b739fe7efa65 Mon Sep 17 00:00:00 2001 From: Andrew Sisley Date: Mon, 7 Mar 2022 12:22:30 -0500 Subject: [PATCH 06/16] Remove printDebugDb Is a duplicate of the public PrintDump --- db/db.go | 4 ---- db/db_test.go | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/db/db.go b/db/db.go index 0dd748dcf5..7cb3c121a1 100644 --- a/db/db.go +++ b/db/db.go @@ -186,10 +186,6 @@ func (db *DB) initialize(ctx context.Context) error { return nil } -func (db *DB) printDebugDB(ctx context.Context) { - printStore(ctx, db.Rootstore()) -} - func (db *DB) PrintDump(ctx context.Context) { printStore(ctx, db.Rootstore()) } diff --git a/db/db_test.go b/db/db_test.go index f348fa894c..4d8385ba24 100644 --- a/db/db_test.go +++ b/db/db_test.go @@ -418,7 +418,7 @@ func TestDBSchemaSaveSimpleDocument(t *testing.T) { assert.Equal(t, "John", name) assert.Equal(t, int64(21), age) - db.printDebugDB(ctx) + db.PrintDump(ctx) } func TestDBUpdateDocWithFilter(t *testing.T) { From 5234a8401995e6e812d7d5b860be8f4939e783c4 Mon Sep 17 00:00:00 2001 From: Andrew Sisley Date: Mon, 7 Mar 2022 12:27:34 -0500 Subject: [PATCH 07/16] Remove public XYZStore accessors None of these need to be public. Leaves us with the (standard-typed, standard-concept) Root and Block stores only. --- db/collection.go | 12 ++++++------ db/db.go | 23 ++++------------------- db/db_test.go | 2 +- db/schema.go | 4 ++-- db/sequence.go | 4 ++-- 5 files changed, 15 insertions(+), 30 deletions(-) diff --git a/db/collection.go b/db/collection.go index aefaa0e4cd..485cef18ab 100644 --- a/db/collection.go +++ b/db/collection.go @@ -114,7 +114,7 @@ func (db *DB) newCollection(desc base.CollectionDescription) (*Collection, error func (db *DB) CreateCollection(ctx context.Context, desc base.CollectionDescription) (client.Collection, error) { // check if collection by this name exists cKey := core.NewCollectionKey(desc.Name) - exists, err := db.Systemstore().Has(ctx, cKey.ToDS()) + exists, err := db.systemstore().Has(ctx, cKey.ToDS()) if err != nil { return nil, err } @@ -144,7 +144,7 @@ func (db *DB) CreateCollection(ctx context.Context, desc base.CollectionDescript key := core.NewCollectionKey(col.desc.Name) // write the collection metadata to the system store - err = db.Systemstore().Put(ctx, key.ToDS(), buf) + err = db.systemstore().Put(ctx, key.ToDS(), buf) if err != nil { return nil, err } @@ -165,7 +165,7 @@ func (db *DB) CreateCollection(ctx context.Context, desc base.CollectionDescript col.schemaID = cid.String() csKey := core.NewCollectionSchemaKey(cid.String()) - err = db.Systemstore().Put(ctx, csKey.ToDS(), []byte(desc.Name)) + err = db.systemstore().Put(ctx, csKey.ToDS(), []byte(desc.Name)) log.Debug(ctx, "Created collection", logging.NewKV("Name", col.Name()), logging.NewKV("Id", col.SchemaID)) return col, err } @@ -177,7 +177,7 @@ func (db *DB) GetCollectionByName(ctx context.Context, name string) (client.Coll } key := core.NewCollectionKey(name) - buf, err := db.Systemstore().Get(ctx, key.ToDS()) + buf, err := db.systemstore().Get(ctx, key.ToDS()) if err != nil { return nil, err } @@ -221,7 +221,7 @@ func (db *DB) GetCollectionBySchemaID(ctx context.Context, schemaID string) (cli } key := core.NewCollectionSchemaKey(schemaID) - buf, err := db.Systemstore().Get(ctx, key.ToDS()) + buf, err := db.systemstore().Get(ctx, key.ToDS()) if err != nil { return nil, err } @@ -235,7 +235,7 @@ func (db *DB) GetCollectionBySchemaID(ctx context.Context, schemaID string) (cli func (db *DB) GetAllCollections(ctx context.Context) ([]client.Collection, error) { // create collection system prefix query prefix := core.NewCollectionKey("") - q, err := db.Systemstore().Query(ctx, query.Query{ + q, err := db.systemstore().Query(ctx, query.Query{ Prefix: prefix.ToString(), KeysOnly: true, }) diff --git a/db/db.go b/db/db.go index 7cb3c121a1..eb572eb32c 100644 --- a/db/db.go +++ b/db/db.go @@ -128,27 +128,12 @@ func (db *DB) Root() ds.Batching { return db.rootstore } -// Rootstore gets the internal rootstore handle -func (db *DB) Rootstore() core.DSReaderWriter { - return db.multistore.Rootstore() -} - -// Headstore returns the internal index store for DAG Heads -func (db *DB) Headstore() core.DSReaderWriter { - return db.multistore.Headstore() -} - -// Datastore returns the internal index store for DAG Heads -func (db *DB) Datastore() core.DSReaderWriter { - return db.multistore.Datastore() -} - // Blockstore returns the internal DAG store which contains IPLD blocks func (db *DB) Blockstore() blockstore.Blockstore { return db.multistore.DAGstore() } -func (db *DB) Systemstore() core.DSReaderWriter { +func (db *DB) systemstore() client.DSReaderWriter { return db.multistore.Systemstore() } @@ -159,7 +144,7 @@ func (db *DB) initialize(ctx context.Context) error { defer db.glock.Unlock() log.Debug(ctx, "Checking if db has already been initialized...") - exists, err := db.Systemstore().Has(ctx, ds.NewKey("init")) + exists, err := db.systemstore().Has(ctx, ds.NewKey("init")) if err != nil && err != ds.ErrNotFound { return err } @@ -178,7 +163,7 @@ func (db *DB) initialize(ctx context.Context) error { return err } - err = db.Systemstore().Put(ctx, ds.NewKey("init"), []byte{1}) + err = db.systemstore().Put(ctx, ds.NewKey("init"), []byte{1}) if err != nil { return err } @@ -187,7 +172,7 @@ func (db *DB) initialize(ctx context.Context) error { } func (db *DB) PrintDump(ctx context.Context) { - printStore(ctx, db.Rootstore()) + printStore(ctx, db.multistore.Rootstore()) } func (db *DB) Executor() *planner.QueryExecutor { diff --git a/db/db_test.go b/db/db_test.go index 4d8385ba24..70cef34c73 100644 --- a/db/db_test.go +++ b/db/db_test.go @@ -336,7 +336,7 @@ func TestDocumentMerkleDAG(t *testing.T) { err = col.Save(ctx, doc) assert.NoError(t, err) - clk := clock.NewMerkleClock(db.Headstore(), nil, core.HeadStoreKey{}.WithDocKey("bae-09cd7539-9b86-5661-90f6-14fbf6c1a14d").WithFieldId("Name"), nil) + clk := clock.NewMerkleClock(db.multistore.Headstore(), nil, core.HeadStoreKey{}.WithDocKey("bae-09cd7539-9b86-5661-90f6-14fbf6c1a14d").WithFieldId("Name"), nil) heads := clk.(*clock.MerkleClock).Heads() cids, _, err := heads.List(ctx) assert.NoError(t, err) diff --git a/db/schema.go b/db/schema.go index d3efbe24ca..ab06aa6304 100644 --- a/db/schema.go +++ b/db/schema.go @@ -45,7 +45,7 @@ func (db *DB) loadSchema(ctx context.Context) error { q := dsq.Query{ Prefix: "/schema", } - res, err := db.Systemstore().Query(ctx, q) + res, err := db.systemstore().Query(ctx, q) if err != nil { return err } @@ -66,7 +66,7 @@ func (db *DB) saveSchema(ctx context.Context, astdoc *ast.Document) error { case *ast.ObjectDefinition: body := defType.Loc.Source.Body[defType.Loc.Start:defType.Loc.End] key := core.NewSchemaKey(defType.Name.Value) - if err := db.Systemstore().Put(ctx, key.ToDS(), body); err != nil { + if err := db.systemstore().Put(ctx, key.ToDS(), body); err != nil { return err } } diff --git a/db/sequence.go b/db/sequence.go index 14a7a45b65..cf5462fc7b 100644 --- a/db/sequence.go +++ b/db/sequence.go @@ -50,7 +50,7 @@ func (db *DB) getSequence(ctx context.Context, key string) (*sequence, error) { } func (seq *sequence) get(ctx context.Context) (uint64, error) { - val, err := seq.db.Systemstore().Get(ctx, seq.key.ToDS()) + val, err := seq.db.systemstore().Get(ctx, seq.key.ToDS()) if err != nil { return 0, err } @@ -62,7 +62,7 @@ func (seq *sequence) get(ctx context.Context) (uint64, error) { func (seq *sequence) update(ctx context.Context) error { var buf [8]byte binary.BigEndian.PutUint64(buf[:], seq.val) - if err := seq.db.Systemstore().Put(ctx, seq.key.ToDS(), buf[:]); err != nil { + if err := seq.db.systemstore().Put(ctx, seq.key.ToDS(), buf[:]); err != nil { return err } From 2cd69323f79eac2335c42187ca362a754bfb2bc0 Mon Sep 17 00:00:00 2001 From: Andrew Sisley Date: Mon, 7 Mar 2022 12:41:56 -0500 Subject: [PATCH 08/16] Move store and txn interfaces into client txn is dependent on store, and txn is on the public db+collection interfaces --- client/core.go | 5 ++--- {core => client}/store.go | 8 +++---- {core => client}/txn.go | 2 +- core/crdt/base.go | 5 +++-- core/crdt/base_test.go | 5 +++-- core/crdt/composite.go | 3 ++- core/crdt/lwwreg.go | 3 ++- core/crdt/lwwreg_test.go | 3 ++- db/collection.go | 26 +++++++++++----------- db/collection_delete.go | 12 +++++------ db/collection_get.go | 3 ++- db/collection_update.go | 20 ++++++++--------- db/db.go | 6 +++--- db/fetcher/dag.go | 5 +++-- db/fetcher/fetcher.go | 7 +++--- db/fetcher/versioned.go | 7 +++--- db/query.go | 3 +-- db/tests/utils.go | 3 +-- merkle/clock/clock.go | 7 +++--- merkle/clock/heads.go | 7 +++--- merkle/crdt/composite.go | 5 +++-- merkle/crdt/factory.go | 35 +++++++++++++++--------------- merkle/crdt/factory_test.go | 9 ++++---- merkle/crdt/lwwreg.go | 5 +++-- merkle/crdt/merklecrdt.go | 2 +- merkle/crdt/merklecrdt_test.go | 5 +++-- net/process.go | 2 +- query/graphql/planner/executor.go | 7 +++--- query/graphql/planner/planner.go | 4 ++-- store/blockstore.go | 11 +++++----- store/dag.go | 6 +++--- store/multi.go | 36 +++++++++++++++---------------- store/shimstore.go | 6 +++--- store/txn.go | 18 ++++++++-------- store/wrappedStore.go | 9 ++++---- 35 files changed, 155 insertions(+), 145 deletions(-) rename {core => client}/store.go (87%) rename {core => client}/txn.go (98%) diff --git a/client/core.go b/client/core.go index 7d45682c43..54a8e5e985 100644 --- a/client/core.go +++ b/client/core.go @@ -13,7 +13,6 @@ package client import ( "context" - "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/db/base" "github.com/sourcenetwork/defradb/document" "github.com/sourcenetwork/defradb/document/key" @@ -37,7 +36,7 @@ type DB interface { Root() ds.Batching Blockstore() blockstore.Blockstore - NewTxn(context.Context, bool) (core.Txn, error) + NewTxn(context.Context, bool) (Txn, error) GetAllCollections(ctx context.Context) ([]Collection, error) } @@ -74,7 +73,7 @@ type Collection interface { Get(context.Context, key.DocKey) (*document.Document, error) - WithTxn(core.Txn) Collection + WithTxn(Txn) Collection GetAllDocKeys(ctx context.Context) (<-chan DocKeysResult, error) } diff --git a/core/store.go b/client/store.go similarity index 87% rename from core/store.go rename to client/store.go index 0265fcb18c..e6cbe8c5ff 100644 --- a/core/store.go +++ b/client/store.go @@ -8,7 +8,7 @@ // by the Apache License, Version 2.0, included in the file // licenses/APL.txt. -package core +package client import ( ds "github.com/ipfs/go-datastore" @@ -40,10 +40,10 @@ type MultiStore interface { } // DSReaderWriter simplifies the interface that is exposed by a -// core.DSReaderWriter into its subcomponents Reader and Writer. -// Using this simplified interface means that both core.DSReaderWriter +// client.DSReaderWriter into its subcomponents Reader and Writer. +// Using this simplified interface means that both client.DSReaderWriter // and ds.Txn satisfy the interface. Due to go-datastore#113 and -// go-datastore#114 ds.Txn no longer implements core.DSReaderWriter +// go-datastore#114 ds.Txn no longer implements client.DSReaderWriter // Which means we can't swap between the two for Datastores that // support TxnDatastore. type DSReaderWriter interface { diff --git a/core/txn.go b/client/txn.go similarity index 98% rename from core/txn.go rename to client/txn.go index b44254a34a..bf1a5f1333 100644 --- a/core/txn.go +++ b/client/txn.go @@ -8,7 +8,7 @@ // by the Apache License, Version 2.0, included in the file // licenses/APL.txt. -package core +package client import "context" diff --git a/core/crdt/base.go b/core/crdt/base.go index 192e158d23..b17d883486 100644 --- a/core/crdt/base.go +++ b/core/crdt/base.go @@ -16,6 +16,7 @@ import ( "errors" ds "github.com/ipfs/go-datastore" + "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" ) @@ -24,12 +25,12 @@ import ( // duplication, and better manage the overhead // tasks that all the CRDTs need to implement anyway type baseCRDT struct { - store core.DSReaderWriter + store client.DSReaderWriter key core.DataStoreKey } // @TODO paramaterize ns/suffix -func newBaseCRDT(store core.DSReaderWriter, key core.DataStoreKey) baseCRDT { +func newBaseCRDT(store client.DSReaderWriter, key core.DataStoreKey) baseCRDT { return baseCRDT{ store: store, key: key, diff --git a/core/crdt/base_test.go b/core/crdt/base_test.go index 305e30e3b1..482e987b40 100644 --- a/core/crdt/base_test.go +++ b/core/crdt/base_test.go @@ -15,15 +15,16 @@ import ( "testing" ds "github.com/ipfs/go-datastore" + "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/store" ) -func newDS() core.DSReaderWriter { +func newDS() client.DSReaderWriter { return store.AsDSReaderWriter(ds.NewMapDatastore()) } -func newSeededDS() core.DSReaderWriter { +func newSeededDS() client.DSReaderWriter { return newDS() } diff --git a/core/crdt/composite.go b/core/crdt/composite.go index 2ab7805166..3cfce49915 100644 --- a/core/crdt/composite.go +++ b/core/crdt/composite.go @@ -17,6 +17,7 @@ import ( "sort" "strings" + "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" ipld "github.com/ipfs/go-ipld-format" @@ -80,7 +81,7 @@ type CompositeDAG struct { schemaID string } -func NewCompositeDAG(store core.DSReaderWriter, schemaID string, namespace core.Key, key string) CompositeDAG { +func NewCompositeDAG(store client.DSReaderWriter, schemaID string, namespace core.Key, key string) CompositeDAG { return CompositeDAG{ key: key, schemaID: schemaID, diff --git a/core/crdt/lwwreg.go b/core/crdt/lwwreg.go index a763552acb..30082aece9 100644 --- a/core/crdt/lwwreg.go +++ b/core/crdt/lwwreg.go @@ -19,6 +19,7 @@ import ( "errors" + "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" ipld "github.com/ipfs/go-ipld-format" @@ -80,7 +81,7 @@ type LWWRegister struct { } // NewLWWRegister returns a new instance of the LWWReg with the given ID -func NewLWWRegister(store core.DSReaderWriter, key core.DataStoreKey) LWWRegister { +func NewLWWRegister(store client.DSReaderWriter, key core.DataStoreKey) LWWRegister { return LWWRegister{ baseCRDT: newBaseCRDT(store, key), // id: id, diff --git a/core/crdt/lwwreg_test.go b/core/crdt/lwwreg_test.go index 06fa528739..b5d48ae246 100644 --- a/core/crdt/lwwreg_test.go +++ b/core/crdt/lwwreg_test.go @@ -17,6 +17,7 @@ import ( "github.com/ipfs/go-cid" ds "github.com/ipfs/go-datastore" + "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/store" @@ -27,7 +28,7 @@ import ( mh "github.com/multiformats/go-multihash" ) -func newMockStore() core.DSReaderWriter { +func newMockStore() client.DSReaderWriter { return store.AsDSReaderWriter(ds.NewMapDatastore()) } diff --git a/db/collection.go b/db/collection.go index 485cef18ab..bfd32bc7cb 100644 --- a/db/collection.go +++ b/db/collection.go @@ -46,7 +46,7 @@ var _ client.Collection = (*Collection)(nil) // together under a collection name. This is analogous to SQL Tables. type Collection struct { db *DB - txn core.Txn + txn client.Txn colID uint32 @@ -278,7 +278,7 @@ func (c *Collection) GetAllDocKeys(ctx context.Context) (<-chan client.DocKeysRe return c.getAllDocKeysChan(ctx, txn) } -func (c *Collection) getAllDocKeysChan(ctx context.Context, txn core.Txn) (<-chan client.DocKeysResult, error) { +func (c *Collection) getAllDocKeysChan(ctx context.Context, txn client.Txn) (<-chan client.DocKeysResult, error) { prefix := c.getPrimaryIndexDocKey(core.DataStoreKey{}) // empty path for all keys prefix q, err := txn.Datastore().Query(ctx, query.Query{ Prefix: prefix.ToString(), @@ -398,7 +398,7 @@ func (c *Collection) SchemaID() string { // WithTxn returns a new instance of the collection, with a transaction // handle instead of a raw DB handle -func (c *Collection) WithTxn(txn core.Txn) client.Collection { +func (c *Collection) WithTxn(txn client.Txn) client.Collection { return &Collection{ db: c.db, txn: txn, @@ -442,7 +442,7 @@ func (c *Collection) CreateMany(ctx context.Context, docs []*document.Document) return c.commitImplicitTxn(ctx, txn) } -func (c *Collection) create(ctx context.Context, txn core.Txn, doc *document.Document) error { +func (c *Collection) create(ctx context.Context, txn client.Txn, doc *document.Document) error { // DocKey verification buf, err := doc.Bytes() if err != nil { @@ -519,7 +519,7 @@ func (c *Collection) Update(ctx context.Context, doc *document.Document) error { // or, just update everything regardless. // Should probably be smart about the update due to the MerkleCRDT overhead, shouldn't // add to the bloat. -func (c *Collection) update(ctx context.Context, txn core.Txn, doc *document.Document) error { +func (c *Collection) update(ctx context.Context, txn client.Txn, doc *document.Document) error { _, err := c.save(ctx, txn, doc) if err != nil { return err @@ -554,7 +554,7 @@ func (c *Collection) Save(ctx context.Context, doc *document.Document) error { return c.commitImplicitTxn(ctx, txn) } -func (c *Collection) save(ctx context.Context, txn core.Txn, doc *document.Document) (cid.Cid, error) { +func (c *Collection) save(ctx context.Context, txn client.Txn, doc *document.Document) (cid.Cid, error) { // New batch transaction/store (optional/todo) // Ensute/Set doc object marker // Loop through doc values @@ -648,7 +648,7 @@ func (c *Collection) Delete(ctx context.Context, key key.DocKey) (bool, error) { // at the moment, delete only does data storage delete. // Dag, and head store will soon follow. -func (c *Collection) delete(ctx context.Context, txn core.Txn, key core.DataStoreKey) (bool, error) { +func (c *Collection) delete(ctx context.Context, txn client.Txn, key core.DataStoreKey) (bool, error) { q := query.Query{ Prefix: c.getPrimaryIndexDocKey(key).ToString(), KeysOnly: true, @@ -686,11 +686,11 @@ func (c *Collection) Exists(ctx context.Context, key key.DocKey) (bool, error) { } // check if a document exists with the given key -func (c *Collection) exists(ctx context.Context, txn core.Txn, key core.DataStoreKey) (bool, error) { +func (c *Collection) exists(ctx context.Context, txn client.Txn, key core.DataStoreKey) (bool, error) { return txn.Datastore().Has(ctx, c.getPrimaryIndexDocKey(key.WithValueFlag()).ToDS()) } -func (c *Collection) saveDocValue(ctx context.Context, txn core.Txn, key core.DataStoreKey, val document.Value) (cid.Cid, error) { +func (c *Collection) saveDocValue(ctx context.Context, txn client.Txn, key core.DataStoreKey, val document.Value) (cid.Cid, error) { switch val.Type() { case core.LWW_REGISTER: wval, ok := val.(document.WriteableValue) @@ -715,7 +715,7 @@ func (c *Collection) saveDocValue(ctx context.Context, txn core.Txn, key core.Da func (c *Collection) saveValueToMerkleCRDT( ctx context.Context, - txn core.Txn, + txn client.Txn, key core.DataStoreKey, ctype core.CType, args ...interface{}) (cid.Cid, error) { @@ -768,7 +768,7 @@ func (c *Collection) saveValueToMerkleCRDT( // getTxn gets or creates a new transaction from the underlying db. // If the collection already has a txn, return the existing one. // Otherwise, create a new implicit transaction. -func (c *Collection) getTxn(ctx context.Context, readonly bool) (core.Txn, error) { +func (c *Collection) getTxn(ctx context.Context, readonly bool) (client.Txn, error) { if c.txn != nil { return c.txn, nil } @@ -779,13 +779,13 @@ func (c *Collection) getTxn(ctx context.Context, readonly bool) (core.Txn, error // function only if its an implicit transaction. // Implicit transactions are transactions that are created *during* an operation execution as a side effect. // Explicit transactions are provided to the collection object via the "WithTxn(...)" function. -func (c *Collection) discardImplicitTxn(ctx context.Context, txn core.Txn) { +func (c *Collection) discardImplicitTxn(ctx context.Context, txn client.Txn) { if c.txn == nil { txn.Discard(ctx) } } -func (c *Collection) commitImplicitTxn(ctx context.Context, txn core.Txn) error { +func (c *Collection) commitImplicitTxn(ctx context.Context, txn client.Txn) error { if c.txn == nil { return txn.Commit(ctx) } diff --git a/db/collection_delete.go b/db/collection_delete.go index a746f4bb73..6d3eed5ea9 100644 --- a/db/collection_delete.go +++ b/db/collection_delete.go @@ -138,7 +138,7 @@ func (c *Collection) DeleteWithFilter( func (c *Collection) deleteWithKey( ctx context.Context, - txn core.Txn, + txn client.Txn, key core.DataStoreKey, opts ...client.DeleteOpt) (*client.DeleteResult, error) { // Check the docKey we have been given to delete with actually has a corresponding @@ -168,7 +168,7 @@ func (c *Collection) deleteWithKey( func (c *Collection) deleteWithKeys( ctx context.Context, - txn core.Txn, + txn client.Txn, keys []key.DocKey, opts ...client.DeleteOpt) (*client.DeleteResult, error) { @@ -207,7 +207,7 @@ func (c *Collection) deleteWithKeys( func (c *Collection) deleteWithFilter( ctx context.Context, - txn core.Txn, + txn client.Txn, filter interface{}, opts ...client.DeleteOpt) (*client.DeleteResult, error) { @@ -265,10 +265,10 @@ func (c *Collection) deleteWithFilter( } type dagDeleter struct { - bstore core.DAGStore + bstore client.DAGStore } -func newDagDeleter(bstore core.DAGStore) dagDeleter { +func newDagDeleter(bstore client.DAGStore) dagDeleter { return dagDeleter{ bstore: bstore, } @@ -286,7 +286,7 @@ func newDagDeleter(bstore core.DAGStore) dagDeleter { // 3) Deleting headstore state. func (c *Collection) applyFullDelete( ctx context.Context, - txn core.Txn, dockey core.DataStoreKey) error { + txn client.Txn, dockey core.DataStoreKey) error { // Check the docKey we have been given to delete with actually has a corresponding // document (i.e. document actually exists in the collection). diff --git a/db/collection_get.go b/db/collection_get.go index a315c175cd..9931c7d8e2 100644 --- a/db/collection_get.go +++ b/db/collection_get.go @@ -13,6 +13,7 @@ package db import ( "context" + "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/db/base" "github.com/sourcenetwork/defradb/db/fetcher" @@ -44,7 +45,7 @@ func (c *Collection) Get(ctx context.Context, key key.DocKey) (*document.Documen return doc, c.commitImplicitTxn(ctx, txn) } -func (c *Collection) get(ctx context.Context, txn core.Txn, key core.DataStoreKey) (*document.Document, error) { +func (c *Collection) get(ctx context.Context, txn client.Txn, key core.DataStoreKey) (*document.Document, error) { // create a new document fetcher df := new(fetcher.DocumentFetcher) desc := &c.desc diff --git a/db/collection_update.go b/db/collection_update.go index a08786afb2..b54fa73aa7 100644 --- a/db/collection_update.go +++ b/db/collection_update.go @@ -129,7 +129,7 @@ func (c *Collection) UpdateWithDocs(docs []*document.SimpleDocument, updater int return nil } -func (c *Collection) updateWithKey(ctx context.Context, txn core.Txn, key key.DocKey, updater interface{}, opts ...client.UpdateOpt) (*client.UpdateResult, error) { +func (c *Collection) updateWithKey(ctx context.Context, txn client.Txn, key key.DocKey, updater interface{}, opts ...client.UpdateOpt) (*client.UpdateResult, error) { patch, err := parseUpdater(updater) if err != nil { return nil, err @@ -170,7 +170,7 @@ func (c *Collection) updateWithKey(ctx context.Context, txn core.Txn, key key.Do return results, nil } -func (c *Collection) updateWithKeys(ctx context.Context, txn core.Txn, keys []key.DocKey, updater interface{}, opts ...client.UpdateOpt) (*client.UpdateResult, error) { +func (c *Collection) updateWithKeys(ctx context.Context, txn client.Txn, keys []key.DocKey, updater interface{}, opts ...client.UpdateOpt) (*client.UpdateResult, error) { patch, err := parseUpdater(updater) if err != nil { return nil, err @@ -216,7 +216,7 @@ func (c *Collection) updateWithKeys(ctx context.Context, txn core.Txn, keys []ke func (c *Collection) updateWithFilter( ctx context.Context, - txn core.Txn, + txn client.Txn, filter interface{}, updater interface{}, opts ...client.UpdateOpt) (*client.UpdateResult, error) { @@ -287,7 +287,7 @@ func (c *Collection) updateWithFilter( return results, nil } -func (c *Collection) applyPatch(txn core.Txn, doc map[string]interface{}, patch []map[string]interface{}) error { +func (c *Collection) applyPatch(txn client.Txn, doc map[string]interface{}, patch []map[string]interface{}) error { for _, op := range patch { path, ok := op["path"].(string) if !ok { @@ -313,11 +313,11 @@ func (c *Collection) applyPatch(txn core.Txn, doc map[string]interface{}, patch return nil } -func (c *Collection) applyPatchOp(txn core.Txn, dockey string, field string, currentVal interface{}, patchOp map[string]interface{}) error { +func (c *Collection) applyPatchOp(txn client.Txn, dockey string, field string, currentVal interface{}, patchOp map[string]interface{}) error { return nil } -func (c *Collection) applyMerge(ctx context.Context, txn core.Txn, doc map[string]interface{}, merge map[string]interface{}) error { +func (c *Collection) applyMerge(ctx context.Context, txn client.Txn, doc map[string]interface{}, merge map[string]interface{}) error { keyStr, ok := doc["_key"].(string) if !ok { return errors.New("Document is missing key") @@ -507,7 +507,7 @@ func validateFieldSchema(val interface{}, field base.FieldDescription) (interfac } func (c *Collection) applyMergePatchOp( //nolint:unused - txn core.Txn, + txn client.Txn, docKey string, field string, currentVal interface{}, @@ -521,7 +521,7 @@ func (c *Collection) applyMergePatchOp( //nolint:unused // Additionally it only queries for the root scalar fields of the object func (c *Collection) makeSelectionQuery( ctx context.Context, - txn core.Txn, + txn client.Txn, filter interface{}, opts ...client.UpdateOpt) (planner.Query, error) { var f *parser.Filter @@ -576,13 +576,13 @@ func (c *Collection) makeSelectLocal(filter *parser.Filter) (*parser.Select, err // May need to query the database for other schema types // which requires a db transaction. It is recommended // to use collection.WithTxn(txn) for this function call. -func (c *Collection) getCollectionForPatchOpPath(txn core.Txn, path string) (col *Collection, isArray bool, err error) { +func (c *Collection) getCollectionForPatchOpPath(txn client.Txn, path string) (col *Collection, isArray bool, err error) { return nil, false, nil } // getTargetKeyForPatchPath walks through the given doc and Patch path. // It returns the -func (c *Collection) getTargetKeyForPatchPath(txn core.Txn, doc map[string]interface{}, path string) (string, error) { +func (c *Collection) getTargetKeyForPatchPath(txn client.Txn, doc map[string]interface{}, path string) (string, error) { _, length := splitPatchPath(path) if length == 0 { return "", errors.New("Invalid patch op path") diff --git a/db/db.go b/db/db.go index eb572eb32c..6d4e297a7f 100644 --- a/db/db.go +++ b/db/db.go @@ -52,7 +52,7 @@ type DB struct { glock sync.RWMutex rootstore ds.Batching - multistore core.MultiStore + multistore client.MultiStore crdtFactory *crdt.Factory @@ -120,7 +120,7 @@ func NewDB(ctx context.Context, rootstore ds.Batching, options ...Option) (*DB, return db, nil } -func (db *DB) NewTxn(ctx context.Context, readonly bool) (core.Txn, error) { +func (db *DB) NewTxn(ctx context.Context, readonly bool) (client.Txn, error) { return store.NewTxnFrom(ctx, db.rootstore, readonly) } @@ -204,7 +204,7 @@ func (db *DB) Close(ctx context.Context) { log.Info(ctx, "Successfully closed running process") } -func printStore(ctx context.Context, store core.DSReaderWriter) { +func printStore(ctx context.Context, store client.DSReaderWriter) { q := query.Query{ Prefix: "", KeysOnly: false, diff --git a/db/fetcher/dag.go b/db/fetcher/dag.go index 0d9dff200b..136c8b4afc 100644 --- a/db/fetcher/dag.go +++ b/db/fetcher/dag.go @@ -16,6 +16,7 @@ import ( "sort" "strings" + "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" "github.com/ipfs/go-cid" @@ -32,7 +33,7 @@ type BlockFetcher struct { type HeadFetcher struct { // Commented because this code is not used yet according to the linter. - // txn core.Txn + // txn client.Txn // key core.Key // curSpanIndex int @@ -45,7 +46,7 @@ type HeadFetcher struct { kvEnd bool } -func (hf *HeadFetcher) Start(ctx context.Context, txn core.Txn, spans core.Spans) error { +func (hf *HeadFetcher) Start(ctx context.Context, txn client.Txn, spans core.Spans) error { numspans := len(spans) if numspans == 0 { return errors.New("HeadFetcher must have at least one span") diff --git a/db/fetcher/fetcher.go b/db/fetcher/fetcher.go index 956d0a4e67..23094f8371 100644 --- a/db/fetcher/fetcher.go +++ b/db/fetcher/fetcher.go @@ -16,6 +16,7 @@ import ( "errors" dsq "github.com/ipfs/go-datastore/query" + "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/datastores/iterable" "github.com/sourcenetwork/defradb/core" @@ -29,7 +30,7 @@ import ( // encoding. type Fetcher interface { Init(col *base.CollectionDescription, index *base.IndexDescription, fields []*base.FieldDescription, reverse bool) error - Start(ctx context.Context, txn core.Txn, spans core.Spans) error + Start(ctx context.Context, txn client.Txn, spans core.Spans) error FetchNext(ctx context.Context) (*document.EncodedDocument, error) FetchNextDecoded(ctx context.Context) (*document.Document, error) FetchNextMap(ctx context.Context) ([]byte, map[string]interface{}, error) @@ -45,7 +46,7 @@ type DocumentFetcher struct { index *base.IndexDescription reverse bool - txn core.Txn + txn client.Txn spans core.Spans order []dsq.Order uniqueSpans map[core.Span]struct{} // nolint:structcheck,unused @@ -101,7 +102,7 @@ func (df *DocumentFetcher) Init(col *base.CollectionDescription, index *base.Ind } // Start implements DocumentFetcher -func (df *DocumentFetcher) Start(ctx context.Context, txn core.Txn, spans core.Spans) error { +func (df *DocumentFetcher) Start(ctx context.Context, txn client.Txn, spans core.Spans) error { if df.col == nil { return errors.New("DocumentFetcher cannot be started without a CollectionDescription") } diff --git a/db/fetcher/versioned.go b/db/fetcher/versioned.go index e8c2b9e7db..56e0fbb8b4 100644 --- a/db/fetcher/versioned.go +++ b/db/fetcher/versioned.go @@ -15,6 +15,7 @@ import ( "context" "fmt" + "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/db/base" "github.com/sourcenetwork/defradb/merkle/crdt" @@ -79,12 +80,12 @@ type VersionedFetcher struct { // embed the regular doc fetcher *DocumentFetcher - txn core.Txn + txn client.Txn ctx context.Context // Transient version store root ds.Datastore - store core.Txn + store client.Txn key core.DataStoreKey version cid.Cid @@ -112,7 +113,7 @@ func (vf *VersionedFetcher) Init(col *base.CollectionDescription, index *base.In } // Start serializes the correct state accoriding to the Key and CID -func (vf *VersionedFetcher) Start(ctx context.Context, txn core.Txn, spans core.Spans) error { +func (vf *VersionedFetcher) Start(ctx context.Context, txn client.Txn, spans core.Spans) error { if vf.col == nil { return errors.New("VersionedFetcher cannot be started without a CollectionDescription") } diff --git a/db/query.go b/db/query.go index 6b04e3844f..e80ddf57e8 100644 --- a/db/query.go +++ b/db/query.go @@ -15,7 +15,6 @@ import ( "strings" "github.com/sourcenetwork/defradb/client" - "github.com/sourcenetwork/defradb/core" gql "github.com/graphql-go/graphql" ) @@ -49,7 +48,7 @@ func (db *DB) ExecQuery(ctx context.Context, query string) *client.QueryResult { return res } -func (db *DB) ExecTransactionalQuery(ctx context.Context, query string, txn core.Txn) *client.QueryResult { +func (db *DB) ExecTransactionalQuery(ctx context.Context, query string, txn client.Txn) *client.QueryResult { res := &client.QueryResult{} // check if its Introspection query if strings.Contains(query, "IntrospectionQuery") { diff --git a/db/tests/utils.go b/db/tests/utils.go index a49e967159..8d64aae1c6 100644 --- a/db/tests/utils.go +++ b/db/tests/utils.go @@ -27,7 +27,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/sourcenetwork/defradb/client" - "github.com/sourcenetwork/defradb/core" badgerds "github.com/sourcenetwork/defradb/datastores/badger/v3" "github.com/sourcenetwork/defradb/db" "github.com/sourcenetwork/defradb/document" @@ -279,7 +278,7 @@ func ExecuteQueryTestCase(t *testing.T, schema string, collectionNames []string, } // Create the transactions before executing and queries - transactions := make([]core.Txn, 0, len(test.TransactionalQueries)) + transactions := make([]client.Txn, 0, len(test.TransactionalQueries)) erroredQueries := make([]bool, len(test.TransactionalQueries)) for i, tq := range test.TransactionalQueries { if len(transactions) < tq.TransactionId { diff --git a/merkle/clock/clock.go b/merkle/clock/clock.go index d76bb19fa0..9259b237b5 100644 --- a/merkle/clock/clock.go +++ b/merkle/clock/clock.go @@ -14,6 +14,7 @@ import ( "context" "fmt" + "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" cid "github.com/ipfs/go-cid" @@ -27,8 +28,8 @@ var ( ) type MerkleClock struct { - headstore core.DSReaderWriter - dagstore core.DAGStore + headstore client.DSReaderWriter + dagstore client.DAGStore // dagSyncer headset *heads crdt core.ReplicatedData @@ -36,7 +37,7 @@ type MerkleClock struct { // NewMerkleClock returns a new merkle clock to read/write events (deltas) to // the clock -func NewMerkleClock(headstore core.DSReaderWriter, dagstore core.DAGStore, namespace core.HeadStoreKey, crdt core.ReplicatedData) core.MerkleClock { +func NewMerkleClock(headstore client.DSReaderWriter, dagstore client.DAGStore, namespace core.HeadStoreKey, crdt core.ReplicatedData) core.MerkleClock { return &MerkleClock{ headstore: headstore, dagstore: dagstore, diff --git a/merkle/clock/heads.go b/merkle/clock/heads.go index cae1b7f939..8fc69d1233 100644 --- a/merkle/clock/heads.go +++ b/merkle/clock/heads.go @@ -19,6 +19,7 @@ import ( "errors" + "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/logging" @@ -29,15 +30,15 @@ import ( // heads manages the current Merkle-CRDT heads. type heads struct { - store core.DSReaderWriter + store client.DSReaderWriter namespace core.HeadStoreKey } -func NewHeadSet(store core.DSReaderWriter, namespace core.HeadStoreKey) *heads { +func NewHeadSet(store client.DSReaderWriter, namespace core.HeadStoreKey) *heads { return newHeadset(store, namespace) } -func newHeadset(store core.DSReaderWriter, namespace core.HeadStoreKey) *heads { +func newHeadset(store client.DSReaderWriter, namespace core.HeadStoreKey) *heads { return &heads{ store: store, namespace: namespace, diff --git a/merkle/crdt/composite.go b/merkle/crdt/composite.go index df6d1fa7b1..17986a54d6 100644 --- a/merkle/crdt/composite.go +++ b/merkle/crdt/composite.go @@ -13,6 +13,7 @@ package crdt import ( "context" + "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" corecrdt "github.com/sourcenetwork/defradb/core/crdt" corenet "github.com/sourcenetwork/defradb/core/net" @@ -22,7 +23,7 @@ import ( ) var ( - compFactoryFn = MerkleCRDTFactory(func(mstore core.MultiStore, schemaID string, bs corenet.Broadcaster) MerkleCRDTInitFn { + compFactoryFn = MerkleCRDTFactory(func(mstore client.MultiStore, schemaID string, bs corenet.Broadcaster) MerkleCRDTInitFn { return func(key core.DataStoreKey) MerkleCRDT { return NewMerkleCompositeDAG(mstore.Datastore(), mstore.Headstore(), mstore.DAGstore(), schemaID, bs, core.DataStoreKey{}, key) } @@ -46,7 +47,7 @@ type MerkleCompositeDAG struct { // NewMerkleCompositeDAG creates a new instance (or loaded from DB) of a MerkleCRDT // backed by a CompositeDAG CRDT -func NewMerkleCompositeDAG(datastore core.DSReaderWriter, headstore core.DSReaderWriter, dagstore core.DAGStore, schemaID string, bs corenet.Broadcaster, ns, key core.DataStoreKey) *MerkleCompositeDAG { +func NewMerkleCompositeDAG(datastore client.DSReaderWriter, headstore client.DSReaderWriter, dagstore client.DAGStore, schemaID string, bs corenet.Broadcaster, ns, key core.DataStoreKey) *MerkleCompositeDAG { compositeDag := corecrdt.NewCompositeDAG(datastore, schemaID, ns, key.ToString() /* stuff like namespace and ID */) clock := clock.NewMerkleClock(headstore, dagstore, key.ToHeadStoreKey(), compositeDag) diff --git a/merkle/crdt/factory.go b/merkle/crdt/factory.go index c608ba459c..58a8d59766 100644 --- a/merkle/crdt/factory.go +++ b/merkle/crdt/factory.go @@ -13,6 +13,7 @@ package crdt import ( "errors" + "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" corenet "github.com/sourcenetwork/defradb/core/net" ) @@ -26,14 +27,14 @@ type MerkleCRDTInitFn func(core.DataStoreKey) MerkleCRDT // MerkleCRDTFactory instantiates a MerkleCRDTInitFn with a MultiStore // returns a MerkleCRDTInitFn with all the necessary stores set -type MerkleCRDTFactory func(mstore core.MultiStore, schemaID string, bs corenet.Broadcaster) MerkleCRDTInitFn +type MerkleCRDTFactory func(mstore client.MultiStore, schemaID string, bs corenet.Broadcaster) MerkleCRDTInitFn // Factory is a helper utility for instantiating new MerkleCRDTs. // It removes some of the overhead of having to coordinate all the various // store parameters on every single new MerkleCRDT creation type Factory struct { crdts map[core.CType]*MerkleCRDTFactory - multistore core.MultiStore + multistore client.MultiStore } var ( @@ -45,7 +46,7 @@ var ( // NewFactory returns a newly instanciated factory object with the assigned stores // It may be called with all stores set to nil -func NewFactory(multistore core.MultiStore) *Factory { +func NewFactory(multistore client.MultiStore) *Factory { return &Factory{ crdts: make(map[core.CType]*MerkleCRDTFactory), multistore: multistore, @@ -60,7 +61,7 @@ func (factory *Factory) Register(t core.CType, fn *MerkleCRDTFactory) error { } // Instance and execute the registered factory function for a given MerkleCRDT type -// supplied with all the current stores (passed in as a core.MultiStore object) +// supplied with all the current stores (passed in as a client.MultiStore object) func (factory Factory) Instance(schemaID string, bs corenet.Broadcaster, t core.CType, key core.DataStoreKey) (MerkleCRDT, error) { // get the factory function for the given MerkleCRDT type // and pass in the current factory state as a MultiStore parameter @@ -72,8 +73,8 @@ func (factory Factory) Instance(schemaID string, bs corenet.Broadcaster, t core. } // InstanceWithStore executes the registered factory function for the given MerkleCRDT type -// with the additional supplied core.MultiStore instead of the saved one on the main Factory. -func (factory Factory) InstanceWithStores(store core.MultiStore, schemaID string, bs corenet.Broadcaster, t core.CType, key core.DataStoreKey) (MerkleCRDT, error) { +// with the additional supplied client.MultiStore instead of the saved one on the main Factory. +func (factory Factory) InstanceWithStores(store client.MultiStore, schemaID string, bs corenet.Broadcaster, t core.CType, key core.DataStoreKey) (MerkleCRDT, error) { fn, err := factory.getRegisteredFactory(t) if err != nil { return nil, err @@ -91,48 +92,48 @@ func (factory Factory) getRegisteredFactory(t core.CType) (*MerkleCRDTFactory, e } // SetStores sets all the current stores on the Factory in one call -func (factory *Factory) SetStores(multistore core.MultiStore) error { +func (factory *Factory) SetStores(multistore client.MultiStore) error { factory.multistore = multistore return nil } // WithStores returns a new instance of the Factory with all the stores set -func (factory Factory) WithStores(multistore core.MultiStore) Factory { +func (factory Factory) WithStores(multistore client.MultiStore) Factory { factory.multistore = multistore return factory } // Rootstore impements MultiStore -func (factory Factory) Rootstore() core.DSReaderWriter { +func (factory Factory) Rootstore() client.DSReaderWriter { return nil } -// Data implements core.MultiStore and returns the current Datastore -func (factory Factory) Datastore() core.DSReaderWriter { +// Data implements client.MultiStore and returns the current Datastore +func (factory Factory) Datastore() client.DSReaderWriter { if factory.multistore == nil { return nil } return factory.multistore.Datastore() } -// Head implements core.MultiStore and returns the current Headstore -func (factory Factory) Headstore() core.DSReaderWriter { +// Head implements client.MultiStore and returns the current Headstore +func (factory Factory) Headstore() client.DSReaderWriter { if factory.multistore == nil { return nil } return factory.multistore.Headstore() } -// Head implements core.MultiStore and returns the current Headstore -func (factory Factory) Systemstore() core.DSReaderWriter { +// Head implements client.MultiStore and returns the current Headstore +func (factory Factory) Systemstore() client.DSReaderWriter { if factory.multistore == nil { return nil } return factory.multistore.Systemstore() } -// Dag implements core.MultiStore and returns the current Dagstore -func (factory Factory) DAGstore() core.DAGStore { +// Dag implements client.MultiStore and returns the current Dagstore +func (factory Factory) DAGstore() client.DAGStore { if factory.multistore == nil { return nil } diff --git a/merkle/crdt/factory_test.go b/merkle/crdt/factory_test.go index 0d0e7a8262..b2b502487a 100644 --- a/merkle/crdt/factory_test.go +++ b/merkle/crdt/factory_test.go @@ -17,11 +17,12 @@ import ( ds "github.com/ipfs/go-datastore" "github.com/stretchr/testify/assert" + "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/store" ) -func newStores() core.MultiStore { +func newStores() client.MultiStore { root := ds.NewMapDatastore() rw := store.AsDSReaderWriter(root) return store.MultiStoreFrom(rw) @@ -55,7 +56,7 @@ func TestFactoryMultiStoreInterface(t *testing.T) { } // check interface implement - var _ core.MultiStore = f + var _ client.MultiStore = f // ms = f // check interface functions @@ -183,7 +184,7 @@ func TestFullFactoryInstanceCompositeRegister(t *testing.T) { func TestLWWRegisterFactoryFn(t *testing.T) { ctx := context.Background() m := newStores() - f := NewFactory(m) // here factory is only needed to satisfy core.MultiStore interface + f := NewFactory(m) // here factory is only needed to satisfy client.MultiStore interface crdt := lwwFactoryFn(f, "", nil)(core.NewDataStoreKey("/1/0/MyKey")) lwwreg, ok := crdt.(*MerkleLWWRegister) @@ -196,7 +197,7 @@ func TestLWWRegisterFactoryFn(t *testing.T) { func TestCompositeRegisterFactoryFn(t *testing.T) { ctx := context.Background() m := newStores() - f := NewFactory(m) // here factory is only needed to satisfy core.MultiStore interface + f := NewFactory(m) // here factory is only needed to satisfy client.MultiStore interface crdt := compFactoryFn(f, "", nil)(core.NewDataStoreKey("/1/0/MyKey")) merkleReg, ok := crdt.(*MerkleCompositeDAG) diff --git a/merkle/crdt/lwwreg.go b/merkle/crdt/lwwreg.go index fb60ec1e67..07f5ebe20c 100644 --- a/merkle/crdt/lwwreg.go +++ b/merkle/crdt/lwwreg.go @@ -13,6 +13,7 @@ package crdt import ( "context" + "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" corecrdt "github.com/sourcenetwork/defradb/core/crdt" corenet "github.com/sourcenetwork/defradb/core/net" @@ -24,7 +25,7 @@ import ( ) var ( - lwwFactoryFn = MerkleCRDTFactory(func(mstore core.MultiStore, _ string, _ corenet.Broadcaster) MerkleCRDTInitFn { + lwwFactoryFn = MerkleCRDTFactory(func(mstore client.MultiStore, _ string, _ corenet.Broadcaster) MerkleCRDTInitFn { return func(key core.DataStoreKey) MerkleCRDT { return NewMerkleLWWRegister(mstore.Datastore(), mstore.Headstore(), mstore.DAGstore(), core.DataStoreKey{}, key) } @@ -49,7 +50,7 @@ type MerkleLWWRegister struct { // NewMerkleLWWRegister creates a new instance (or loaded from DB) of a MerkleCRDT // backed by a LWWRegister CRDT -func NewMerkleLWWRegister(datastore core.DSReaderWriter, headstore core.DSReaderWriter, dagstore core.DAGStore, ns, key core.DataStoreKey) *MerkleLWWRegister { +func NewMerkleLWWRegister(datastore client.DSReaderWriter, headstore client.DSReaderWriter, dagstore client.DAGStore, ns, key core.DataStoreKey) *MerkleLWWRegister { register := corecrdt.NewLWWRegister(datastore, key /* stuff like namespace and ID */) clk := clock.NewMerkleClock(headstore, dagstore, key.ToHeadStoreKey(), register) diff --git a/merkle/crdt/merklecrdt.go b/merkle/crdt/merklecrdt.go index 1f2977f1ce..3c388fde8c 100644 --- a/merkle/crdt/merklecrdt.go +++ b/merkle/crdt/merklecrdt.go @@ -36,7 +36,7 @@ type MerkleCRDT interface { } // type MerkleCRDTInitFn func(core.Key) MerkleCRDT -// type MerkleCRDTFactory func(store core.DSReaderWriter, namespace core.Key) MerkleCRDTInitFn +// type MerkleCRDTFactory func(store client.DSReaderWriter, namespace core.Key) MerkleCRDTInitFn // Type indicates MerkleCRDT type // type Type byte diff --git a/merkle/crdt/merklecrdt_test.go b/merkle/crdt/merklecrdt_test.go index 95bf7478d1..2ec7f32a1b 100644 --- a/merkle/crdt/merklecrdt_test.go +++ b/merkle/crdt/merklecrdt_test.go @@ -14,6 +14,7 @@ import ( "context" "testing" + "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" corecrdt "github.com/sourcenetwork/defradb/core/crdt" "github.com/sourcenetwork/defradb/merkle/clock" @@ -29,7 +30,7 @@ func newDS() ds.Datastore { return ds.NewMapDatastore() } -func newTestBaseMerkleCRDT() (*baseMerkleCRDT, core.DSReaderWriter) { +func newTestBaseMerkleCRDT() (*baseMerkleCRDT, client.DSReaderWriter) { s := newDS() rw := store.AsDSReaderWriter(s) multistore := store.MultiStoreFrom(rw) @@ -60,7 +61,7 @@ func TestMerkleCRDTPublish(t *testing.T) { printStore(ctx, store) } -func printStore(ctx context.Context, store core.DSReaderWriter) { +func printStore(ctx context.Context, store client.DSReaderWriter) { q := query.Query{ Prefix: "", KeysOnly: false, diff --git a/net/process.go b/net/process.go index 82ba428955..e510a48f28 100644 --- a/net/process.go +++ b/net/process.go @@ -87,7 +87,7 @@ func (p *Peer) processLog( return cids, txn.Commit(ctx) } -func initCRDTForType(ctx context.Context, txn core.MultiStore, col client.Collection, docKey core.DataStoreKey, field string) (crdt.MerkleCRDT, error) { +func initCRDTForType(ctx context.Context, txn client.MultiStore, col client.Collection, docKey core.DataStoreKey, field string) (crdt.MerkleCRDT, error) { var key core.DataStoreKey var ctype core.CType if field == "" { // empty field name implies composite type diff --git a/query/graphql/planner/executor.go b/query/graphql/planner/executor.go index 16d0a7d0f7..0664953288 100644 --- a/query/graphql/planner/executor.go +++ b/query/graphql/planner/executor.go @@ -15,7 +15,6 @@ import ( "fmt" "github.com/sourcenetwork/defradb/client" - "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/query/graphql/parser" "github.com/sourcenetwork/defradb/query/graphql/schema" @@ -59,7 +58,7 @@ func NewQueryExecutor(manager *schema.SchemaManager) (*QueryExecutor, error) { // } -func (e *QueryExecutor) MakeSelectQuery(ctx context.Context, db client.DB, txn core.Txn, selectStmt *parser.Select) (Query, error) { +func (e *QueryExecutor) MakeSelectQuery(ctx context.Context, db client.DB, txn client.Txn, selectStmt *parser.Select) (Query, error) { if selectStmt == nil { return nil, fmt.Errorf("Cannot create query without a selection") } @@ -67,7 +66,7 @@ func (e *QueryExecutor) MakeSelectQuery(ctx context.Context, db client.DB, txn c return planner.makePlan(selectStmt) } -func (e *QueryExecutor) ExecQuery(ctx context.Context, db client.DB, txn core.Txn, query string, args ...interface{}) ([]map[string]interface{}, error) { +func (e *QueryExecutor) ExecQuery(ctx context.Context, db client.DB, txn client.Txn, query string, args ...interface{}) ([]map[string]interface{}, error) { q, err := e.ParseQueryString(query) if err != nil { return nil, err @@ -77,7 +76,7 @@ func (e *QueryExecutor) ExecQuery(ctx context.Context, db client.DB, txn core.Tx return planner.queryDocs(ctx, q) } -func (e *QueryExecutor) MakePlanFromParser(ctx context.Context, db client.DB, txn core.Txn, query *parser.Query) (planNode, error) { +func (e *QueryExecutor) MakePlanFromParser(ctx context.Context, db client.DB, txn client.Txn, query *parser.Query) (planNode, error) { planner := makePlanner(ctx, db, txn) return planner.makePlan(query) } diff --git a/query/graphql/planner/planner.go b/query/graphql/planner/planner.go index 66080edfe6..5aa064b495 100644 --- a/query/graphql/planner/planner.go +++ b/query/graphql/planner/planner.go @@ -93,7 +93,7 @@ type Statement struct { // Planner combines session state and database state to // produce a query plan, which is run by the execution context. type Planner struct { - txn core.Txn + txn client.Txn db client.DB ctx context.Context @@ -103,7 +103,7 @@ type Planner struct { } -func makePlanner(ctx context.Context, db client.DB, txn core.Txn) *Planner { +func makePlanner(ctx context.Context, db client.DB, txn client.Txn) *Planner { return &Planner{ txn: txn, db: db, diff --git a/store/blockstore.go b/store/blockstore.go index 1da9c6e3c2..160ee1c7ef 100644 --- a/store/blockstore.go +++ b/store/blockstore.go @@ -14,20 +14,19 @@ import ( "context" "errors" - "github.com/sourcenetwork/defradb/core" - blocks "github.com/ipfs/go-block-format" "github.com/ipfs/go-cid" ds "github.com/ipfs/go-datastore" dsq "github.com/ipfs/go-datastore/query" blockstore "github.com/ipfs/go-ipfs-blockstore" dshelp "github.com/ipfs/go-ipfs-ds-help" + "github.com/sourcenetwork/defradb/client" ) // Blockstore implementation taken from https://github.com/ipfs/go-ipfs-blockstore/blob/master/blockstore.go // Needed a custom implementation that didn't rely on the ds.Batching interface. // -// All datastore operations in DefraDB are interfaced by core.DSReaderWriter. This simplifies the interface to just +// All datastore operations in DefraDB are interfaced by client.DSReaderWriter. This simplifies the interface to just // that of read/write operations, leaving the management of the datastore to the parent objects. This also allows // us to swap between a regular ds.Datastore, and a ds.Txn which as of https://github.com/ipfs/go-datastore/issues/114 // no longer implements ds.Datastore. @@ -35,7 +34,7 @@ import ( // The original blockstore.Blockstore implementation relied on ds.Batching, so it could internally use store.Batch() // to optimize the PutMany function. However, in DefraDB, since we rely on a single rootstore for all our various // substores (data, heads, blocks), which includes a Txn/Batch system already, our respective substores don't need -// to optimize or worry about Batching/Txn. Hence the simplified core.DSReaderWriter. +// to optimize or worry about Batching/Txn. Hence the simplified client.DSReaderWriter. // ErrHashMismatch is an error returned when the hash of a block // is different than expected. @@ -48,14 +47,14 @@ var ErrNotFound = errors.New("blockstore: block not found") // NewBlockstore returns a default Blockstore implementation // using the provided datastore.Batching backend. -func NewBlockstore(store core.DSReaderWriter) blockstore.Blockstore { +func NewBlockstore(store client.DSReaderWriter) blockstore.Blockstore { return &bstore{ store: store, } } type bstore struct { - store core.DSReaderWriter + store client.DSReaderWriter rehash bool } diff --git a/store/dag.go b/store/dag.go index 4fd46f97fc..4b97e94cb7 100644 --- a/store/dag.go +++ b/store/dag.go @@ -11,7 +11,7 @@ package store import ( - "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/client" blockstore "github.com/ipfs/go-ipfs-blockstore" ) @@ -19,14 +19,14 @@ import ( // DAGStore is the interface to the underlying BlockStore and BlockService type dagStore struct { blockstore.Blockstore // become a Blockstore - store core.DSReaderWriter + store client.DSReaderWriter // bstore blockstore.Blockstore // bserv blockservice.BlockService } // NewDAGStore creates a new DAGStore with the supplied // Batching datastore -func NewDAGStore(store core.DSReaderWriter) core.DAGStore { +func NewDAGStore(store client.DSReaderWriter) client.DAGStore { dstore := &dagStore{ Blockstore: NewBlockstore(store), store: store, diff --git a/store/multi.go b/store/multi.go index 0ae8f86081..1c687eeea9 100644 --- a/store/multi.go +++ b/store/multi.go @@ -11,22 +11,22 @@ package store import ( - "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/db/base" ) type multistore struct { - root core.DSReaderWriter - data core.DSReaderWriter - head core.DSReaderWriter - system core.DSReaderWriter - // block core.DSReaderWriter - dag core.DAGStore + root client.DSReaderWriter + data client.DSReaderWriter + head client.DSReaderWriter + system client.DSReaderWriter + // block client.DSReaderWriter + dag client.DAGStore } -var _ core.MultiStore = (*multistore)(nil) +var _ client.MultiStore = (*multistore)(nil) -func MultiStoreFrom(rootstore core.DSReaderWriter) core.MultiStore { +func MultiStoreFrom(rootstore client.DSReaderWriter) client.MultiStore { block := prefix(rootstore, base.BlockStoreKey) ms := &multistore{ root: rootstore, @@ -39,26 +39,26 @@ func MultiStoreFrom(rootstore core.DSReaderWriter) core.MultiStore { return ms } -// Datastore implements core.MultiStore -func (ms multistore) Datastore() core.DSReaderWriter { +// Datastore implements client.MultiStore +func (ms multistore) Datastore() client.DSReaderWriter { return ms.data } -// Headstore implements core.MultiStore -func (ms multistore) Headstore() core.DSReaderWriter { +// Headstore implements client.MultiStore +func (ms multistore) Headstore() client.DSReaderWriter { return ms.head } -// DAGstore implements core.MultiStore -func (ms multistore) DAGstore() core.DAGStore { +// DAGstore implements client.MultiStore +func (ms multistore) DAGstore() client.DAGStore { return ms.dag } -// Rootstore implements core.MultiStore -func (ms multistore) Rootstore() core.DSReaderWriter { +// Rootstore implements client.MultiStore +func (ms multistore) Rootstore() client.DSReaderWriter { return ms.root } -func (ms multistore) Systemstore() core.DSReaderWriter { +func (ms multistore) Systemstore() client.DSReaderWriter { return ms.system } diff --git a/store/shimstore.go b/store/shimstore.go index 5ab0f2f59f..0727a4ccf9 100644 --- a/store/shimstore.go +++ b/store/shimstore.go @@ -11,11 +11,11 @@ package store import ( ds "github.com/ipfs/go-datastore" - "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/datastores/iterable" ) -func AsDSReaderWriter(store ds.Datastore) core.DSReaderWriter { +func AsDSReaderWriter(store ds.Datastore) client.DSReaderWriter { switch typedStore := store.(type) { case iterable.IterableDatastore: return typedStore @@ -32,4 +32,4 @@ type shim struct { iterable.Iterable } -var _ core.DSReaderWriter = (*shim)(nil) +var _ client.DSReaderWriter = (*shim)(nil) diff --git a/store/txn.go b/store/txn.go index 7f1506dae0..3f302c9b38 100644 --- a/store/txn.go +++ b/store/txn.go @@ -15,22 +15,22 @@ import ( ds "github.com/ipfs/go-datastore" - "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/datastores/iterable" ) type txn struct { t ds.Txn - core.MultiStore + client.MultiStore isBatch bool successFns []func() errorFns []func() } -var _ core.Txn = (*txn)(nil) +var _ client.Txn = (*txn)(nil) -func NewTxnFrom(ctx context.Context, rootstore ds.Batching, readonly bool) (core.Txn, error) { +func NewTxnFrom(ctx context.Context, rootstore ds.Batching, readonly bool) (client.Txn, error) { // check if our datastore natively supports iterable transaction, transactions or batching if iterableTxnStore, ok := rootstore.(iterable.IterableTxnDatastore); ok { rootTxn, err := iterableTxnStore.NewIterableTransaction(ctx, readonly) @@ -121,28 +121,28 @@ func (txn *txn) runSuccessFns(ctx context.Context) { /* // Systemstore returns the txn wrapped as a systemstore under the /system namespace -func (t *txn) Systemstore() core.DSReaderWriter { +func (t *txn) Systemstore() client.DSReaderWriter { return t.systemstore } // Datastore returns the txn wrapped as a datastore under the /data namespace -func (t *txn) Datastore() core.DSReaderWriter { +func (t *txn) Datastore() client.DSReaderWriter { return t.datastore } // Headstore returns the txn wrapped as a headstore under the /heads namespace -func (t *txn) Headstore() core.DSReaderWriter { +func (t *txn) Headstore() client.DSReaderWriter { return t.headstore } // DAGstore returns the txn wrapped as a blockstore for a DAGStore under the /blocks namespace -func (t *txn) DAGstore() core.DAGStore { +func (t *txn) DAGstore() client.DAGStore { return t.dagstore } // Rootstore returns the underlying txn as a DSReaderWriter to implement // the MultiStore interface -func (t *txn) Rootstore() core.DSReaderWriter { +func (t *txn) Rootstore() client.DSReaderWriter { return t.IterableTxn }*/ diff --git a/store/wrappedStore.go b/store/wrappedStore.go index 2945831d3a..6994df8fe3 100644 --- a/store/wrappedStore.go +++ b/store/wrappedStore.go @@ -12,23 +12,22 @@ package store import ( "context" - "github.com/sourcenetwork/defradb/core" - ds "github.com/ipfs/go-datastore" ktds "github.com/ipfs/go-datastore/keytransform" "github.com/ipfs/go-datastore/query" dsq "github.com/ipfs/go-datastore/query" + "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/datastores/iterable" ) type wrappedStore struct { transform ktds.KeyTransform - store core.DSReaderWriter + store client.DSReaderWriter } -var _ core.DSReaderWriter = (*wrappedStore)(nil) +var _ client.DSReaderWriter = (*wrappedStore)(nil) -func prefix(root core.DSReaderWriter, prefix ds.Key) core.DSReaderWriter { +func prefix(root client.DSReaderWriter, prefix ds.Key) client.DSReaderWriter { return &wrappedStore{ transform: ktds.PrefixTransform{Prefix: prefix}, store: root, From 703f98e19880bccefc6d6d4a6280ba3280392fb8 Mon Sep 17 00:00:00 2001 From: Andrew Sisley Date: Mon, 7 Mar 2022 12:54:15 -0500 Subject: [PATCH 09/16] Reorder the DB interface --- client/core.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/client/core.go b/client/core.go index 54a8e5e985..1212c2d67c 100644 --- a/client/core.go +++ b/client/core.go @@ -24,20 +24,22 @@ import ( ) type DB interface { - // Collections + AddSchema(context.Context, string) error + CreateCollection(context.Context, base.CollectionDescription) (Collection, error) GetCollectionByName(context.Context, string) (Collection, error) GetCollectionBySchemaID(context.Context, string) (Collection, error) - ExecQuery(context.Context, string) *QueryResult + GetAllCollections(ctx context.Context) ([]Collection, error) GetRelationshipIdField(fieldName, targetType, thisType string) (string, error) - AddSchema(context.Context, string) error - PrintDump(ctx context.Context) - GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error) + Root() ds.Batching Blockstore() blockstore.Blockstore + GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error) NewTxn(context.Context, bool) (Txn, error) - GetAllCollections(ctx context.Context) ([]Collection, error) + ExecQuery(context.Context, string) *QueryResult + + PrintDump(ctx context.Context) } type Sequence interface{} From 1566645ec2a9f4f2cea91eaacaabcfdbb85d936b Mon Sep 17 00:00:00 2001 From: Andrew Sisley Date: Mon, 7 Mar 2022 12:55:48 -0500 Subject: [PATCH 10/16] Remove unused sequence interface --- client/core.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/client/core.go b/client/core.go index 1212c2d67c..b32ef47036 100644 --- a/client/core.go +++ b/client/core.go @@ -42,8 +42,6 @@ type DB interface { PrintDump(ctx context.Context) } -type Sequence interface{} - type Collection interface { Description() base.CollectionDescription Name() string From aab419aab25d43ae4f8d127d70d67f06e9937775 Mon Sep 17 00:00:00 2001 From: Andrew Sisley Date: Mon, 7 Mar 2022 13:27:12 -0500 Subject: [PATCH 11/16] Remove schema from document Is not used, and creates extra dependency headaches when moving public items into client package. --- db/fetcher/fetcher.go | 1 - document/document.go | 12 +++--------- document/encoded.go | 4 ---- 3 files changed, 3 insertions(+), 14 deletions(-) diff --git a/db/fetcher/fetcher.go b/db/fetcher/fetcher.go index 23094f8371..dd36c95644 100644 --- a/db/fetcher/fetcher.go +++ b/db/fetcher/fetcher.go @@ -79,7 +79,6 @@ func (df *DocumentFetcher) Init(col *base.CollectionDescription, index *base.Ind df.initialized = true df.isReadingDocument = false df.doc = new(document.EncodedDocument) - df.doc.Schema = &col.Schema if df.kvResultsIter != nil { if err := df.kvResultsIter.Close(); err != nil { diff --git a/document/document.go b/document/document.go index 5c5f0d3b0f..0f9968787a 100644 --- a/document/document.go +++ b/document/document.go @@ -22,7 +22,6 @@ import ( mh "github.com/multiformats/go-multihash" "github.com/sourcenetwork/defradb/core" - "github.com/sourcenetwork/defradb/db/base" "github.com/sourcenetwork/defradb/document/key" ) @@ -64,7 +63,6 @@ var ( // @body: A document interface can be implemented by both a TypedDocument and a // UnTypedDocument, which use a schema and schemaless approach respectively. type Document struct { - schema base.SchemaDescription key key.DocKey fields map[string]Field values map[Field]Value @@ -88,17 +86,13 @@ func newEmptyDoc() *Document { } } -func NewFromMap(data map[string]interface{}, schema ...base.SchemaDescription) (*Document, error) { +func NewFromMap(data map[string]interface{}) (*Document, error) { var err error doc := &Document{ fields: make(map[string]Field), values: make(map[Field]Value), } - if len(schema) > 0 { - doc.schema = schema[0] - } - // check if document contains special _key field k, hasKey := data["_key"] if hasKey { @@ -143,14 +137,14 @@ func NewFromMap(data map[string]interface{}, schema ...base.SchemaDescription) ( } // NewFromJSON creates a new instance of a Document from a raw JSON object byte array -func NewFromJSON(obj []byte, schema ...base.SchemaDescription) (*Document, error) { +func NewFromJSON(obj []byte) (*Document, error) { data := make(map[string]interface{}) err := json.Unmarshal(obj, &data) if err != nil { return nil, err } - return NewFromMap(data, schema...) + return NewFromMap(data) } func (doc *Document) Head() cid.Cid { diff --git a/document/encoded.go b/document/encoded.go index 62e6eaf68b..0762809e31 100644 --- a/document/encoded.go +++ b/document/encoded.go @@ -109,7 +109,6 @@ func (e EncProperty) Decode() (core.CType, interface{}, error) { // @todo: Implement Encoded Document type type EncodedDocument struct { Key []byte - Schema *base.SchemaDescription Properties map[base.FieldDescription]*EncProperty } @@ -126,9 +125,6 @@ func (encdoc *EncodedDocument) Decode() (*Document, error) { return nil, err } doc := NewWithKey(key) - if encdoc.Schema != nil { - doc.schema = *encdoc.Schema - } for fieldDesc, prop := range encdoc.Properties { ctype, val, err := prop.Decode() if err != nil { From 121e65a62ec88697be7dbc3fed930180ab3adb70 Mon Sep 17 00:00:00 2001 From: Andrew Sisley Date: Thu, 10 Mar 2022 11:36:13 -0500 Subject: [PATCH 12/16] Remove blockstore.GetBlock --- api/http/api.go | 2 +- client/core.go | 3 --- db/blockstore.go | 22 ---------------------- net/peer.go | 2 +- 4 files changed, 2 insertions(+), 27 deletions(-) delete mode 100644 db/blockstore.go diff --git a/api/http/api.go b/api/http/api.go index 88f267a0d9..351fd355e7 100644 --- a/api/http/api.go +++ b/api/http/api.go @@ -179,7 +179,7 @@ func (s *Server) getBlock(w http.ResponseWriter, r *http.Request) { c = cid.NewCidV1(cid.Raw, hash) } - block, err := s.db.GetBlock(ctx, c) + block, err := s.db.Blockstore().Get(ctx, c) if err != nil { result.Errors = []interface{}{err.Error()} diff --git a/client/core.go b/client/core.go index b32ef47036..1b7a6d224c 100644 --- a/client/core.go +++ b/client/core.go @@ -17,8 +17,6 @@ import ( "github.com/sourcenetwork/defradb/document" "github.com/sourcenetwork/defradb/document/key" - blocks "github.com/ipfs/go-block-format" - cid "github.com/ipfs/go-cid" ds "github.com/ipfs/go-datastore" blockstore "github.com/ipfs/go-ipfs-blockstore" ) @@ -34,7 +32,6 @@ type DB interface { Root() ds.Batching Blockstore() blockstore.Blockstore - GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error) NewTxn(context.Context, bool) (Txn, error) ExecQuery(context.Context, string) *QueryResult diff --git a/db/blockstore.go b/db/blockstore.go deleted file mode 100644 index 589c0f1cb6..0000000000 --- a/db/blockstore.go +++ /dev/null @@ -1,22 +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 db - -import ( - "context" - - blocks "github.com/ipfs/go-block-format" - cid "github.com/ipfs/go-cid" -) - -func (db *DB) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error) { - return db.multistore.DAGstore().Get(ctx, c) -} diff --git a/net/peer.go b/net/peer.go index 51252df9d3..e7030b2ca2 100644 --- a/net/peer.go +++ b/net/peer.go @@ -200,7 +200,7 @@ func (p *Peer) handleBroadcastLoop() { func (p *Peer) RegisterNewDocument(ctx context.Context, dockey key.DocKey, c cid.Cid, schemaID string) error { log.Debug(p.ctx, "Registering a new document for our peer node", logging.NewKV("DocKey", dockey.String())) - block, err := p.db.GetBlock(ctx, c) + block, err := p.db.Blockstore().Get(ctx, c) if err != nil { log.ErrorE(p.ctx, "Failed to get document cid", err) return err From 111ce4a528cd8c1cba48e7d860db3dad778cf08e Mon Sep 17 00:00:00 2001 From: Andrew Sisley Date: Thu, 10 Mar 2022 20:38:33 -0500 Subject: [PATCH 13/16] Rename datastores to datastore --- cli/defradb/cmd/config.go | 2 +- cli/defradb/cmd/serverdump.go | 2 +- cli/defradb/cmd/start.go | 2 +- client/store.go | 2 +- {datastores => datastore}/badger/v3/compat_logger.go | 0 {datastores => datastore}/badger/v3/datastore.go | 2 +- {datastores => datastore}/badger/v3/iterator.go | 2 +- {datastores => datastore}/iterable/iterable.go | 0 {datastores => datastore}/iterable/iterable_transaction_shim.go | 0 db/db_test.go | 2 +- db/fetcher/fetcher.go | 2 +- db/tests/utils.go | 2 +- store/shimstore.go | 2 +- store/txn.go | 2 +- store/wrappedStore.go | 2 +- 15 files changed, 12 insertions(+), 12 deletions(-) rename {datastores => datastore}/badger/v3/compat_logger.go (100%) rename {datastores => datastore}/badger/v3/datastore.go (99%) rename {datastores => datastore}/badger/v3/iterator.go (99%) rename {datastores => datastore}/iterable/iterable.go (100%) rename {datastores => datastore}/iterable/iterable_transaction_shim.go (100%) diff --git a/cli/defradb/cmd/config.go b/cli/defradb/cmd/config.go index ca8669fe9e..14498bb086 100644 --- a/cli/defradb/cmd/config.go +++ b/cli/defradb/cmd/config.go @@ -11,7 +11,7 @@ package cmd import ( - badgerds "github.com/sourcenetwork/defradb/datastores/badger/v3" + badgerds "github.com/sourcenetwork/defradb/datastore/badger/v3" "github.com/sourcenetwork/defradb/logging" ) diff --git a/cli/defradb/cmd/serverdump.go b/cli/defradb/cmd/serverdump.go index 264f0454c3..985b685d4f 100644 --- a/cli/defradb/cmd/serverdump.go +++ b/cli/defradb/cmd/serverdump.go @@ -16,7 +16,7 @@ import ( "os/signal" ds "github.com/ipfs/go-datastore" - badgerds "github.com/sourcenetwork/defradb/datastores/badger/v3" + badgerds "github.com/sourcenetwork/defradb/datastore/badger/v3" "github.com/sourcenetwork/defradb/logging" "github.com/spf13/cobra" diff --git a/cli/defradb/cmd/start.go b/cli/defradb/cmd/start.go index c846f27384..878e267745 100644 --- a/cli/defradb/cmd/start.go +++ b/cli/defradb/cmd/start.go @@ -20,7 +20,7 @@ import ( "time" ma "github.com/multiformats/go-multiaddr" - badgerds "github.com/sourcenetwork/defradb/datastores/badger/v3" + badgerds "github.com/sourcenetwork/defradb/datastore/badger/v3" "github.com/sourcenetwork/defradb/db" netapi "github.com/sourcenetwork/defradb/net/api" netpb "github.com/sourcenetwork/defradb/net/api/pb" diff --git a/client/store.go b/client/store.go index e6cbe8c5ff..3b861632de 100644 --- a/client/store.go +++ b/client/store.go @@ -13,7 +13,7 @@ package client import ( ds "github.com/ipfs/go-datastore" blockstore "github.com/ipfs/go-ipfs-blockstore" - "github.com/sourcenetwork/defradb/datastores/iterable" + "github.com/sourcenetwork/defradb/datastore/iterable" ) // MultiStore is an interface wrapper around the 3 main types of stores needed for diff --git a/datastores/badger/v3/compat_logger.go b/datastore/badger/v3/compat_logger.go similarity index 100% rename from datastores/badger/v3/compat_logger.go rename to datastore/badger/v3/compat_logger.go diff --git a/datastores/badger/v3/datastore.go b/datastore/badger/v3/datastore.go similarity index 99% rename from datastores/badger/v3/datastore.go rename to datastore/badger/v3/datastore.go index e4bbaed0a7..885bf7d731 100644 --- a/datastores/badger/v3/datastore.go +++ b/datastore/badger/v3/datastore.go @@ -18,7 +18,7 @@ import ( dsq "github.com/ipfs/go-datastore/query" logger "github.com/ipfs/go-log/v2" goprocess "github.com/jbenet/goprocess" - "github.com/sourcenetwork/defradb/datastores/iterable" + "github.com/sourcenetwork/defradb/datastore/iterable" "go.uber.org/zap" ) diff --git a/datastores/badger/v3/iterator.go b/datastore/badger/v3/iterator.go similarity index 99% rename from datastores/badger/v3/iterator.go rename to datastore/badger/v3/iterator.go index ff6b7686fd..6d884959a0 100644 --- a/datastores/badger/v3/iterator.go +++ b/datastore/badger/v3/iterator.go @@ -20,7 +20,7 @@ import ( ds "github.com/ipfs/go-datastore" dsq "github.com/ipfs/go-datastore/query" goprocess "github.com/jbenet/goprocess" - "github.com/sourcenetwork/defradb/datastores/iterable" + "github.com/sourcenetwork/defradb/datastore/iterable" ) type BadgerIterator struct { diff --git a/datastores/iterable/iterable.go b/datastore/iterable/iterable.go similarity index 100% rename from datastores/iterable/iterable.go rename to datastore/iterable/iterable.go diff --git a/datastores/iterable/iterable_transaction_shim.go b/datastore/iterable/iterable_transaction_shim.go similarity index 100% rename from datastores/iterable/iterable_transaction_shim.go rename to datastore/iterable/iterable_transaction_shim.go diff --git a/db/db_test.go b/db/db_test.go index 70cef34c73..89ef5cbca1 100644 --- a/db/db_test.go +++ b/db/db_test.go @@ -25,7 +25,7 @@ import ( badger "github.com/dgraph-io/badger/v3" ds "github.com/ipfs/go-datastore" dag "github.com/ipfs/go-merkledag" - badgerds "github.com/sourcenetwork/defradb/datastores/badger/v3" + badgerds "github.com/sourcenetwork/defradb/datastore/badger/v3" "github.com/stretchr/testify/assert" ) diff --git a/db/fetcher/fetcher.go b/db/fetcher/fetcher.go index dd36c95644..0d7704979d 100644 --- a/db/fetcher/fetcher.go +++ b/db/fetcher/fetcher.go @@ -17,7 +17,7 @@ import ( dsq "github.com/ipfs/go-datastore/query" "github.com/sourcenetwork/defradb/client" - "github.com/sourcenetwork/defradb/datastores/iterable" + "github.com/sourcenetwork/defradb/datastore/iterable" "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/db/base" diff --git a/db/tests/utils.go b/db/tests/utils.go index 8d64aae1c6..7d018a48ce 100644 --- a/db/tests/utils.go +++ b/db/tests/utils.go @@ -27,7 +27,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/sourcenetwork/defradb/client" - badgerds "github.com/sourcenetwork/defradb/datastores/badger/v3" + badgerds "github.com/sourcenetwork/defradb/datastore/badger/v3" "github.com/sourcenetwork/defradb/db" "github.com/sourcenetwork/defradb/document" "github.com/sourcenetwork/defradb/logging" diff --git a/store/shimstore.go b/store/shimstore.go index 0727a4ccf9..d570f43a6d 100644 --- a/store/shimstore.go +++ b/store/shimstore.go @@ -12,7 +12,7 @@ package store import ( ds "github.com/ipfs/go-datastore" "github.com/sourcenetwork/defradb/client" - "github.com/sourcenetwork/defradb/datastores/iterable" + "github.com/sourcenetwork/defradb/datastore/iterable" ) func AsDSReaderWriter(store ds.Datastore) client.DSReaderWriter { diff --git a/store/txn.go b/store/txn.go index 3f302c9b38..a338bc72b1 100644 --- a/store/txn.go +++ b/store/txn.go @@ -16,7 +16,7 @@ import ( ds "github.com/ipfs/go-datastore" "github.com/sourcenetwork/defradb/client" - "github.com/sourcenetwork/defradb/datastores/iterable" + "github.com/sourcenetwork/defradb/datastore/iterable" ) type txn struct { diff --git a/store/wrappedStore.go b/store/wrappedStore.go index 6994df8fe3..9e116fac20 100644 --- a/store/wrappedStore.go +++ b/store/wrappedStore.go @@ -17,7 +17,7 @@ import ( "github.com/ipfs/go-datastore/query" dsq "github.com/ipfs/go-datastore/query" "github.com/sourcenetwork/defradb/client" - "github.com/sourcenetwork/defradb/datastores/iterable" + "github.com/sourcenetwork/defradb/datastore/iterable" ) type wrappedStore struct { From 785061ca0351325ee6cc7b04a7344d3a304f148b Mon Sep 17 00:00:00 2001 From: Andrew Sisley Date: Thu, 10 Mar 2022 20:43:57 -0500 Subject: [PATCH 14/16] Move store package to datastore dir --- core/crdt/base_test.go | 4 ++-- core/crdt/lwwreg_test.go | 4 ++-- {store => datastore}/blockstore.go | 2 +- {store => datastore}/dag.go | 2 +- {store => datastore}/multi.go | 2 +- {store => datastore}/shimstore.go | 2 +- {store => datastore}/store.go | 2 +- {store => datastore}/txn.go | 2 +- {store => datastore}/wrappedStore.go | 2 +- db/db.go | 8 ++++---- db/fetcher/versioned.go | 4 ++-- merkle/clock/clock_test.go | 10 +++++----- merkle/clock/heads_test.go | 4 ++-- merkle/crdt/factory_test.go | 6 +++--- merkle/crdt/lwwreg.go | 2 +- merkle/crdt/merklecrdt_test.go | 6 +++--- 16 files changed, 31 insertions(+), 31 deletions(-) rename {store => datastore}/blockstore.go (99%) rename {store => datastore}/dag.go (99%) rename {store => datastore}/multi.go (98%) rename {store => datastore}/shimstore.go (97%) rename {store => datastore}/store.go (96%) rename {store => datastore}/txn.go (99%) rename {store => datastore}/wrappedStore.go (99%) diff --git a/core/crdt/base_test.go b/core/crdt/base_test.go index 482e987b40..01775d1996 100644 --- a/core/crdt/base_test.go +++ b/core/crdt/base_test.go @@ -17,11 +17,11 @@ import ( ds "github.com/ipfs/go-datastore" "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" - "github.com/sourcenetwork/defradb/store" + "github.com/sourcenetwork/defradb/datastore" ) func newDS() client.DSReaderWriter { - return store.AsDSReaderWriter(ds.NewMapDatastore()) + return datastore.AsDSReaderWriter(ds.NewMapDatastore()) } func newSeededDS() client.DSReaderWriter { diff --git a/core/crdt/lwwreg_test.go b/core/crdt/lwwreg_test.go index b5d48ae246..82de6cae17 100644 --- a/core/crdt/lwwreg_test.go +++ b/core/crdt/lwwreg_test.go @@ -19,7 +19,7 @@ import ( ds "github.com/ipfs/go-datastore" "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" - "github.com/sourcenetwork/defradb/store" + "github.com/sourcenetwork/defradb/datastore" "github.com/ugorji/go/codec" @@ -29,7 +29,7 @@ import ( ) func newMockStore() client.DSReaderWriter { - return store.AsDSReaderWriter(ds.NewMapDatastore()) + return datastore.AsDSReaderWriter(ds.NewMapDatastore()) } func setupLWWRegister() LWWRegister { diff --git a/store/blockstore.go b/datastore/blockstore.go similarity index 99% rename from store/blockstore.go rename to datastore/blockstore.go index 160ee1c7ef..a0183d030f 100644 --- a/store/blockstore.go +++ b/datastore/blockstore.go @@ -8,7 +8,7 @@ // by the Apache License, Version 2.0, included in the file // licenses/APL.txt. -package store +package datastore import ( "context" diff --git a/store/dag.go b/datastore/dag.go similarity index 99% rename from store/dag.go rename to datastore/dag.go index 4b97e94cb7..597ed468ec 100644 --- a/store/dag.go +++ b/datastore/dag.go @@ -8,7 +8,7 @@ // by the Apache License, Version 2.0, included in the file // licenses/APL.txt. -package store +package datastore import ( "github.com/sourcenetwork/defradb/client" diff --git a/store/multi.go b/datastore/multi.go similarity index 98% rename from store/multi.go rename to datastore/multi.go index 1c687eeea9..a30533bf0e 100644 --- a/store/multi.go +++ b/datastore/multi.go @@ -8,7 +8,7 @@ // by the Apache License, Version 2.0, included in the file // licenses/APL.txt. -package store +package datastore import ( "github.com/sourcenetwork/defradb/client" diff --git a/store/shimstore.go b/datastore/shimstore.go similarity index 97% rename from store/shimstore.go rename to datastore/shimstore.go index d570f43a6d..45982a1519 100644 --- a/store/shimstore.go +++ b/datastore/shimstore.go @@ -7,7 +7,7 @@ // 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 store +package datastore import ( ds "github.com/ipfs/go-datastore" diff --git a/store/store.go b/datastore/store.go similarity index 96% rename from store/store.go rename to datastore/store.go index 49779db110..e9fb868137 100644 --- a/store/store.go +++ b/datastore/store.go @@ -8,7 +8,7 @@ // by the Apache License, Version 2.0, included in the file // licenses/APL.txt. -package store +package datastore import ( "github.com/sourcenetwork/defradb/logging" diff --git a/store/txn.go b/datastore/txn.go similarity index 99% rename from store/txn.go rename to datastore/txn.go index a338bc72b1..e260e42e1b 100644 --- a/store/txn.go +++ b/datastore/txn.go @@ -8,7 +8,7 @@ // by the Apache License, Version 2.0, included in the file // licenses/APL.txt. -package store +package datastore import ( "context" diff --git a/store/wrappedStore.go b/datastore/wrappedStore.go similarity index 99% rename from store/wrappedStore.go rename to datastore/wrappedStore.go index 9e116fac20..703f0d7dcc 100644 --- a/store/wrappedStore.go +++ b/datastore/wrappedStore.go @@ -7,7 +7,7 @@ // 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 store +package datastore import ( "context" diff --git a/db/db.go b/db/db.go index 6d4e297a7f..4aef198957 100644 --- a/db/db.go +++ b/db/db.go @@ -19,10 +19,10 @@ import ( "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" corenet "github.com/sourcenetwork/defradb/core/net" + "github.com/sourcenetwork/defradb/datastore" "github.com/sourcenetwork/defradb/merkle/crdt" "github.com/sourcenetwork/defradb/query/graphql/planner" "github.com/sourcenetwork/defradb/query/graphql/schema" - "github.com/sourcenetwork/defradb/store" ds "github.com/ipfs/go-datastore" "github.com/ipfs/go-datastore/query" @@ -77,8 +77,8 @@ func WithBroadcaster(bs corenet.Broadcaster) Option { // NewDB creates a new instance of the DB using the given options func NewDB(ctx context.Context, rootstore ds.Batching, options ...Option) (*DB, error) { log.Debug(ctx, "loading: internal datastores") - root := store.AsDSReaderWriter(rootstore) - multistore := store.MultiStoreFrom(root) + root := datastore.AsDSReaderWriter(rootstore) + multistore := datastore.MultiStoreFrom(root) crdtFactory := crdt.DefaultFactory.WithStores(multistore) log.Debug(ctx, "loading: schema manager") @@ -121,7 +121,7 @@ func NewDB(ctx context.Context, rootstore ds.Batching, options ...Option) (*DB, } func (db *DB) NewTxn(ctx context.Context, readonly bool) (client.Txn, error) { - return store.NewTxnFrom(ctx, db.rootstore, readonly) + return datastore.NewTxnFrom(ctx, db.rootstore, readonly) } func (db *DB) Root() ds.Batching { diff --git a/db/fetcher/versioned.go b/db/fetcher/versioned.go index 56e0fbb8b4..bed58c71e4 100644 --- a/db/fetcher/versioned.go +++ b/db/fetcher/versioned.go @@ -17,9 +17,9 @@ import ( "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/datastore" "github.com/sourcenetwork/defradb/db/base" "github.com/sourcenetwork/defradb/merkle/crdt" - "github.com/sourcenetwork/defradb/store" "github.com/ipfs/go-cid" ds "github.com/ipfs/go-datastore" @@ -148,7 +148,7 @@ func (vf *VersionedFetcher) Start(ctx context.Context, txn client.Txn, spans cor // create store root := ds.NewMapDatastore() vf.root = root - vf.store, err = store.NewTxnFrom(ctx, root, false) // were going to discard and nuke this later + vf.store, err = datastore.NewTxnFrom(ctx, root, false) // were going to discard and nuke this later if err != nil { return err } diff --git a/merkle/clock/clock_test.go b/merkle/clock/clock_test.go index d7dccd17ce..7a089111a9 100644 --- a/merkle/clock/clock_test.go +++ b/merkle/clock/clock_test.go @@ -16,7 +16,7 @@ import ( "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/core/crdt" - "github.com/sourcenetwork/defradb/store" + "github.com/sourcenetwork/defradb/datastore" cid "github.com/ipfs/go-cid" ds "github.com/ipfs/go-datastore" @@ -30,16 +30,16 @@ func newDS() ds.Datastore { func newTestMerkleClock() *MerkleClock { s := newDS() - rw := store.AsDSReaderWriter(s) - multistore := store.MultiStoreFrom(rw) + rw := datastore.AsDSReaderWriter(s) + multistore := datastore.MultiStoreFrom(rw) reg := crdt.NewLWWRegister(rw, core.DataStoreKey{}) return NewMerkleClock(multistore.Headstore(), multistore.DAGstore(), core.HeadStoreKey{DocKey: "dockey", FieldId: "1"}, reg).(*MerkleClock) } func TestNewMerkleClock(t *testing.T) { s := newDS() - rw := store.AsDSReaderWriter(s) - multistore := store.MultiStoreFrom(rw) + rw := datastore.AsDSReaderWriter(s) + multistore := datastore.MultiStoreFrom(rw) reg := crdt.NewLWWRegister(rw, core.DataStoreKey{}) clk := NewMerkleClock(multistore.Headstore(), multistore.DAGstore(), core.HeadStoreKey{}, reg).(*MerkleClock) diff --git a/merkle/clock/heads_test.go b/merkle/clock/heads_test.go index ad05220df0..506a8454ab 100644 --- a/merkle/clock/heads_test.go +++ b/merkle/clock/heads_test.go @@ -23,7 +23,7 @@ import ( ds "github.com/ipfs/go-datastore" mh "github.com/multiformats/go-multihash" "github.com/sourcenetwork/defradb/core" - "github.com/sourcenetwork/defradb/store" + "github.com/sourcenetwork/defradb/datastore" ) func newRandomCID() cid.Cid { @@ -50,7 +50,7 @@ func newRandomCID() cid.Cid { func newHeadSet() *heads { s := newDS() - return newHeadset(store.AsDSReaderWriter(s), core.HeadStoreKey{}.WithDocKey("mydockey").WithFieldId("1")) + return newHeadset(datastore.AsDSReaderWriter(s), core.HeadStoreKey{}.WithDocKey("mydockey").WithFieldId("1")) } func TestHeadsWrite(t *testing.T) { diff --git a/merkle/crdt/factory_test.go b/merkle/crdt/factory_test.go index b2b502487a..2be2a285ea 100644 --- a/merkle/crdt/factory_test.go +++ b/merkle/crdt/factory_test.go @@ -19,13 +19,13 @@ import ( "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" - "github.com/sourcenetwork/defradb/store" + "github.com/sourcenetwork/defradb/datastore" ) func newStores() client.MultiStore { root := ds.NewMapDatastore() - rw := store.AsDSReaderWriter(root) - return store.MultiStoreFrom(rw) + rw := datastore.AsDSReaderWriter(root) + return datastore.MultiStoreFrom(rw) } func TestNewBlankFactory(t *testing.T) { diff --git a/merkle/crdt/lwwreg.go b/merkle/crdt/lwwreg.go index 07f5ebe20c..771f2e556a 100644 --- a/merkle/crdt/lwwreg.go +++ b/merkle/crdt/lwwreg.go @@ -19,7 +19,7 @@ import ( corenet "github.com/sourcenetwork/defradb/core/net" "github.com/sourcenetwork/defradb/merkle/clock" - // "github.com/sourcenetwork/defradb/store" + // "github.com/sourcenetwork/defradb/datastore" "github.com/ipfs/go-cid" ) diff --git a/merkle/crdt/merklecrdt_test.go b/merkle/crdt/merklecrdt_test.go index 2ec7f32a1b..283cc60187 100644 --- a/merkle/crdt/merklecrdt_test.go +++ b/merkle/crdt/merklecrdt_test.go @@ -17,8 +17,8 @@ import ( "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" corecrdt "github.com/sourcenetwork/defradb/core/crdt" + "github.com/sourcenetwork/defradb/datastore" "github.com/sourcenetwork/defradb/merkle/clock" - "github.com/sourcenetwork/defradb/store" "github.com/ipfs/go-cid" ds "github.com/ipfs/go-datastore" @@ -32,8 +32,8 @@ func newDS() ds.Datastore { func newTestBaseMerkleCRDT() (*baseMerkleCRDT, client.DSReaderWriter) { s := newDS() - rw := store.AsDSReaderWriter(s) - multistore := store.MultiStoreFrom(rw) + rw := datastore.AsDSReaderWriter(s) + multistore := datastore.MultiStoreFrom(rw) reg := corecrdt.NewLWWRegister(multistore.Datastore(), core.DataStoreKey{}) clk := clock.NewMerkleClock(multistore.Headstore(), multistore.DAGstore(), core.HeadStoreKey{}, reg) From b81b0767b96311b179c72839be1aa3377973de19 Mon Sep 17 00:00:00 2001 From: Andrew Sisley Date: Thu, 10 Mar 2022 20:51:34 -0500 Subject: [PATCH 15/16] Move store.go into datastore package --- client/core.go | 5 +-- client/store.go | 58 ------------------------------- client/txn.go | 33 ------------------ core/crdt/base.go | 6 ++-- core/crdt/base_test.go | 5 ++- core/crdt/composite.go | 4 +-- core/crdt/lwwreg.go | 4 +-- core/crdt/lwwreg_test.go | 3 +- datastore/blockstore.go | 9 +++-- datastore/dag.go | 6 ++-- datastore/multi.go | 35 +++++++++---------- datastore/shimstore.go | 5 ++- datastore/store.go | 44 +++++++++++++++++++++++ datastore/txn.go | 37 +++++++++++++++----- datastore/wrappedStore.go | 7 ++-- db/collection.go | 27 +++++++------- db/collection_delete.go | 13 +++---- db/collection_get.go | 4 +-- db/collection_update.go | 21 +++++------ db/db.go | 8 ++--- db/fetcher/dag.go | 6 ++-- db/fetcher/fetcher.go | 8 ++--- db/fetcher/versioned.go | 7 ++-- db/query.go | 3 +- db/tests/utils.go | 3 +- merkle/clock/clock.go | 8 ++--- merkle/clock/heads.go | 8 ++--- merkle/crdt/composite.go | 6 ++-- merkle/crdt/factory.go | 36 +++++++++---------- merkle/crdt/factory_test.go | 9 +++-- merkle/crdt/lwwreg.go | 6 ++-- merkle/crdt/merklecrdt.go | 2 +- merkle/crdt/merklecrdt_test.go | 5 ++- net/process.go | 3 +- query/graphql/planner/executor.go | 7 ++-- query/graphql/planner/planner.go | 5 +-- 36 files changed, 213 insertions(+), 243 deletions(-) delete mode 100644 client/store.go delete mode 100644 client/txn.go diff --git a/client/core.go b/client/core.go index 1b7a6d224c..a68d55e63d 100644 --- a/client/core.go +++ b/client/core.go @@ -13,6 +13,7 @@ package client import ( "context" + "github.com/sourcenetwork/defradb/datastore" "github.com/sourcenetwork/defradb/db/base" "github.com/sourcenetwork/defradb/document" "github.com/sourcenetwork/defradb/document/key" @@ -33,7 +34,7 @@ type DB interface { Root() ds.Batching Blockstore() blockstore.Blockstore - NewTxn(context.Context, bool) (Txn, error) + NewTxn(context.Context, bool) (datastore.Txn, error) ExecQuery(context.Context, string) *QueryResult PrintDump(ctx context.Context) @@ -70,7 +71,7 @@ type Collection interface { Get(context.Context, key.DocKey) (*document.Document, error) - WithTxn(Txn) Collection + WithTxn(datastore.Txn) Collection GetAllDocKeys(ctx context.Context) (<-chan DocKeysResult, error) } diff --git a/client/store.go b/client/store.go deleted file mode 100644 index 3b861632de..0000000000 --- a/client/store.go +++ /dev/null @@ -1,58 +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 ( - ds "github.com/ipfs/go-datastore" - blockstore "github.com/ipfs/go-ipfs-blockstore" - "github.com/sourcenetwork/defradb/datastore/iterable" -) - -// MultiStore is an interface wrapper around the 3 main types of stores needed for -// MerkleCRDTs -type MultiStore interface { - Rootstore() DSReaderWriter - - // Datastore is a wrapped root DSReaderWriter - // under the /data namespace - Datastore() DSReaderWriter - - // Headstore is a wrapped root DSReaderWriter - // under the /head namespace - Headstore() DSReaderWriter - - // DAGstore is a wrapped root DSReaderWriter - // as a Blockstore, embedded into a DAGStore - // under the /blocks namespace - DAGstore() DAGStore - - // Headstore is a wrapped root DSReaderWriter - // under the /system namespace - Systemstore() DSReaderWriter -} - -// DSReaderWriter simplifies the interface that is exposed by a -// client.DSReaderWriter into its subcomponents Reader and Writer. -// Using this simplified interface means that both client.DSReaderWriter -// and ds.Txn satisfy the interface. Due to go-datastore#113 and -// go-datastore#114 ds.Txn no longer implements client.DSReaderWriter -// Which means we can't swap between the two for Datastores that -// support TxnDatastore. -type DSReaderWriter interface { - ds.Read - ds.Write - iterable.Iterable -} - -// DAGStore proxies the ipld.DAGService under the /core namespace for future-proofing -type DAGStore interface { - blockstore.Blockstore -} diff --git a/client/txn.go b/client/txn.go deleted file mode 100644 index bf1a5f1333..0000000000 --- a/client/txn.go +++ /dev/null @@ -1,33 +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" - -// Txn is a common interface to the db.Txn struct -type Txn interface { - MultiStore - - IsBatch() bool - - // Commit finalizes a transaction, attempting to commit it to the Datastore. - // May return an error if the transaction has gone stale. The presence of an - // error is an indication that the data was not committed to the Datastore. - Commit(ctx context.Context) error - // Discard throws away changes recorded in a transaction without committing - // them to the underlying Datastore. Any calls made to Discard after Commit - // has been successfully called will have no effect on the transaction and - // state of the Datastore, making it safe to defer. - Discard(ctx context.Context) - - OnSuccess(fn func()) - OnError(fn func()) -} diff --git a/core/crdt/base.go b/core/crdt/base.go index b17d883486..9955354bd3 100644 --- a/core/crdt/base.go +++ b/core/crdt/base.go @@ -16,8 +16,8 @@ import ( "errors" ds "github.com/ipfs/go-datastore" - "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/datastore" ) // baseCRDT is embedded as a base layer into all @@ -25,12 +25,12 @@ import ( // duplication, and better manage the overhead // tasks that all the CRDTs need to implement anyway type baseCRDT struct { - store client.DSReaderWriter + store datastore.DSReaderWriter key core.DataStoreKey } // @TODO paramaterize ns/suffix -func newBaseCRDT(store client.DSReaderWriter, key core.DataStoreKey) baseCRDT { +func newBaseCRDT(store datastore.DSReaderWriter, key core.DataStoreKey) baseCRDT { return baseCRDT{ store: store, key: key, diff --git a/core/crdt/base_test.go b/core/crdt/base_test.go index 01775d1996..ae5519471b 100644 --- a/core/crdt/base_test.go +++ b/core/crdt/base_test.go @@ -15,16 +15,15 @@ import ( "testing" ds "github.com/ipfs/go-datastore" - "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/datastore" ) -func newDS() client.DSReaderWriter { +func newDS() datastore.DSReaderWriter { return datastore.AsDSReaderWriter(ds.NewMapDatastore()) } -func newSeededDS() client.DSReaderWriter { +func newSeededDS() datastore.DSReaderWriter { return newDS() } diff --git a/core/crdt/composite.go b/core/crdt/composite.go index 3cfce49915..ac2788c4b4 100644 --- a/core/crdt/composite.go +++ b/core/crdt/composite.go @@ -17,8 +17,8 @@ import ( "sort" "strings" - "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/datastore" ipld "github.com/ipfs/go-ipld-format" dag "github.com/ipfs/go-merkledag" @@ -81,7 +81,7 @@ type CompositeDAG struct { schemaID string } -func NewCompositeDAG(store client.DSReaderWriter, schemaID string, namespace core.Key, key string) CompositeDAG { +func NewCompositeDAG(store datastore.DSReaderWriter, schemaID string, namespace core.Key, key string) CompositeDAG { return CompositeDAG{ key: key, schemaID: schemaID, diff --git a/core/crdt/lwwreg.go b/core/crdt/lwwreg.go index 30082aece9..54b0de4c30 100644 --- a/core/crdt/lwwreg.go +++ b/core/crdt/lwwreg.go @@ -19,8 +19,8 @@ import ( "errors" - "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/datastore" ipld "github.com/ipfs/go-ipld-format" dag "github.com/ipfs/go-merkledag" @@ -81,7 +81,7 @@ type LWWRegister struct { } // NewLWWRegister returns a new instance of the LWWReg with the given ID -func NewLWWRegister(store client.DSReaderWriter, key core.DataStoreKey) LWWRegister { +func NewLWWRegister(store datastore.DSReaderWriter, key core.DataStoreKey) LWWRegister { return LWWRegister{ baseCRDT: newBaseCRDT(store, key), // id: id, diff --git a/core/crdt/lwwreg_test.go b/core/crdt/lwwreg_test.go index 82de6cae17..93b4ae3a9d 100644 --- a/core/crdt/lwwreg_test.go +++ b/core/crdt/lwwreg_test.go @@ -17,7 +17,6 @@ import ( "github.com/ipfs/go-cid" ds "github.com/ipfs/go-datastore" - "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/datastore" @@ -28,7 +27,7 @@ import ( mh "github.com/multiformats/go-multihash" ) -func newMockStore() client.DSReaderWriter { +func newMockStore() datastore.DSReaderWriter { return datastore.AsDSReaderWriter(ds.NewMapDatastore()) } diff --git a/datastore/blockstore.go b/datastore/blockstore.go index a0183d030f..5f581f0463 100644 --- a/datastore/blockstore.go +++ b/datastore/blockstore.go @@ -20,13 +20,12 @@ import ( dsq "github.com/ipfs/go-datastore/query" blockstore "github.com/ipfs/go-ipfs-blockstore" dshelp "github.com/ipfs/go-ipfs-ds-help" - "github.com/sourcenetwork/defradb/client" ) // Blockstore implementation taken from https://github.com/ipfs/go-ipfs-blockstore/blob/master/blockstore.go // Needed a custom implementation that didn't rely on the ds.Batching interface. // -// All datastore operations in DefraDB are interfaced by client.DSReaderWriter. This simplifies the interface to just +// All datastore operations in DefraDB are interfaced by DSReaderWriter. This simplifies the interface to just // that of read/write operations, leaving the management of the datastore to the parent objects. This also allows // us to swap between a regular ds.Datastore, and a ds.Txn which as of https://github.com/ipfs/go-datastore/issues/114 // no longer implements ds.Datastore. @@ -34,7 +33,7 @@ import ( // The original blockstore.Blockstore implementation relied on ds.Batching, so it could internally use store.Batch() // to optimize the PutMany function. However, in DefraDB, since we rely on a single rootstore for all our various // substores (data, heads, blocks), which includes a Txn/Batch system already, our respective substores don't need -// to optimize or worry about Batching/Txn. Hence the simplified client.DSReaderWriter. +// to optimize or worry about Batching/Txn. Hence the simplified DSReaderWriter. // ErrHashMismatch is an error returned when the hash of a block // is different than expected. @@ -47,14 +46,14 @@ var ErrNotFound = errors.New("blockstore: block not found") // NewBlockstore returns a default Blockstore implementation // using the provided datastore.Batching backend. -func NewBlockstore(store client.DSReaderWriter) blockstore.Blockstore { +func NewBlockstore(store DSReaderWriter) blockstore.Blockstore { return &bstore{ store: store, } } type bstore struct { - store client.DSReaderWriter + store DSReaderWriter rehash bool } diff --git a/datastore/dag.go b/datastore/dag.go index 597ed468ec..f0d6f4b8cb 100644 --- a/datastore/dag.go +++ b/datastore/dag.go @@ -11,22 +11,20 @@ package datastore import ( - "github.com/sourcenetwork/defradb/client" - blockstore "github.com/ipfs/go-ipfs-blockstore" ) // DAGStore is the interface to the underlying BlockStore and BlockService type dagStore struct { blockstore.Blockstore // become a Blockstore - store client.DSReaderWriter + store DSReaderWriter // bstore blockstore.Blockstore // bserv blockservice.BlockService } // NewDAGStore creates a new DAGStore with the supplied // Batching datastore -func NewDAGStore(store client.DSReaderWriter) client.DAGStore { +func NewDAGStore(store DSReaderWriter) DAGStore { dstore := &dagStore{ Blockstore: NewBlockstore(store), store: store, diff --git a/datastore/multi.go b/datastore/multi.go index a30533bf0e..c28331e47c 100644 --- a/datastore/multi.go +++ b/datastore/multi.go @@ -11,22 +11,21 @@ package datastore import ( - "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/db/base" ) type multistore struct { - root client.DSReaderWriter - data client.DSReaderWriter - head client.DSReaderWriter - system client.DSReaderWriter - // block client.DSReaderWriter - dag client.DAGStore + root DSReaderWriter + data DSReaderWriter + head DSReaderWriter + system DSReaderWriter + // block DSReaderWriter + dag DAGStore } -var _ client.MultiStore = (*multistore)(nil) +var _ MultiStore = (*multistore)(nil) -func MultiStoreFrom(rootstore client.DSReaderWriter) client.MultiStore { +func MultiStoreFrom(rootstore DSReaderWriter) MultiStore { block := prefix(rootstore, base.BlockStoreKey) ms := &multistore{ root: rootstore, @@ -39,26 +38,26 @@ func MultiStoreFrom(rootstore client.DSReaderWriter) client.MultiStore { return ms } -// Datastore implements client.MultiStore -func (ms multistore) Datastore() client.DSReaderWriter { +// Datastore implements MultiStore +func (ms multistore) Datastore() DSReaderWriter { return ms.data } -// Headstore implements client.MultiStore -func (ms multistore) Headstore() client.DSReaderWriter { +// Headstore implements MultiStore +func (ms multistore) Headstore() DSReaderWriter { return ms.head } -// DAGstore implements client.MultiStore -func (ms multistore) DAGstore() client.DAGStore { +// DAGstore implements MultiStore +func (ms multistore) DAGstore() DAGStore { return ms.dag } -// Rootstore implements client.MultiStore -func (ms multistore) Rootstore() client.DSReaderWriter { +// Rootstore implements MultiStore +func (ms multistore) Rootstore() DSReaderWriter { return ms.root } -func (ms multistore) Systemstore() client.DSReaderWriter { +func (ms multistore) Systemstore() DSReaderWriter { return ms.system } diff --git a/datastore/shimstore.go b/datastore/shimstore.go index 45982a1519..30176857d4 100644 --- a/datastore/shimstore.go +++ b/datastore/shimstore.go @@ -11,11 +11,10 @@ package datastore import ( ds "github.com/ipfs/go-datastore" - "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/datastore/iterable" ) -func AsDSReaderWriter(store ds.Datastore) client.DSReaderWriter { +func AsDSReaderWriter(store ds.Datastore) DSReaderWriter { switch typedStore := store.(type) { case iterable.IterableDatastore: return typedStore @@ -32,4 +31,4 @@ type shim struct { iterable.Iterable } -var _ client.DSReaderWriter = (*shim)(nil) +var _ DSReaderWriter = (*shim)(nil) diff --git a/datastore/store.go b/datastore/store.go index e9fb868137..5615edcff4 100644 --- a/datastore/store.go +++ b/datastore/store.go @@ -11,9 +11,53 @@ package datastore import ( + ds "github.com/ipfs/go-datastore" + blockstore "github.com/ipfs/go-ipfs-blockstore" + "github.com/sourcenetwork/defradb/datastore/iterable" "github.com/sourcenetwork/defradb/logging" ) var ( log = logging.MustNewLogger("defradb.store") ) + +// MultiStore is an interface wrapper around the 3 main types of stores needed for +// MerkleCRDTs +type MultiStore interface { + Rootstore() DSReaderWriter + + // Datastore is a wrapped root DSReaderWriter + // under the /data namespace + Datastore() DSReaderWriter + + // Headstore is a wrapped root DSReaderWriter + // under the /head namespace + Headstore() DSReaderWriter + + // DAGstore is a wrapped root DSReaderWriter + // as a Blockstore, embedded into a DAGStore + // under the /blocks namespace + DAGstore() DAGStore + + // Headstore is a wrapped root DSReaderWriter + // under the /system namespace + Systemstore() DSReaderWriter +} + +// DSReaderWriter simplifies the interface that is exposed by a +// DSReaderWriter into its subcomponents Reader and Writer. +// Using this simplified interface means that both DSReaderWriter +// and ds.Txn satisfy the interface. Due to go-datastore#113 and +// go-datastore#114 ds.Txn no longer implements DSReaderWriter +// Which means we can't swap between the two for Datastores that +// support TxnDatastore. +type DSReaderWriter interface { + ds.Read + ds.Write + iterable.Iterable +} + +// DAGStore proxies the ipld.DAGService under the /core namespace for future-proofing +type DAGStore interface { + blockstore.Blockstore +} diff --git a/datastore/txn.go b/datastore/txn.go index e260e42e1b..6f32358cab 100644 --- a/datastore/txn.go +++ b/datastore/txn.go @@ -15,22 +15,41 @@ import ( ds "github.com/ipfs/go-datastore" - "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/datastore/iterable" ) +// Txn is a common interface to the db.Txn struct +type Txn interface { + MultiStore + + IsBatch() bool + + // Commit finalizes a transaction, attempting to commit it to the Datastore. + // May return an error if the transaction has gone stale. The presence of an + // error is an indication that the data was not committed to the Datastore. + Commit(ctx context.Context) error + // Discard throws away changes recorded in a transaction without committing + // them to the underlying Datastore. Any calls made to Discard after Commit + // has been successfully called will have no effect on the transaction and + // state of the Datastore, making it safe to defer. + Discard(ctx context.Context) + + OnSuccess(fn func()) + OnError(fn func()) +} + type txn struct { t ds.Txn - client.MultiStore + MultiStore isBatch bool successFns []func() errorFns []func() } -var _ client.Txn = (*txn)(nil) +var _ Txn = (*txn)(nil) -func NewTxnFrom(ctx context.Context, rootstore ds.Batching, readonly bool) (client.Txn, error) { +func NewTxnFrom(ctx context.Context, rootstore ds.Batching, readonly bool) (Txn, error) { // check if our datastore natively supports iterable transaction, transactions or batching if iterableTxnStore, ok := rootstore.(iterable.IterableTxnDatastore); ok { rootTxn, err := iterableTxnStore.NewIterableTransaction(ctx, readonly) @@ -121,28 +140,28 @@ func (txn *txn) runSuccessFns(ctx context.Context) { /* // Systemstore returns the txn wrapped as a systemstore under the /system namespace -func (t *txn) Systemstore() client.DSReaderWriter { +func (t *txn) Systemstore() DSReaderWriter { return t.systemstore } // Datastore returns the txn wrapped as a datastore under the /data namespace -func (t *txn) Datastore() client.DSReaderWriter { +func (t *txn) Datastore() DSReaderWriter { return t.datastore } // Headstore returns the txn wrapped as a headstore under the /heads namespace -func (t *txn) Headstore() client.DSReaderWriter { +func (t *txn) Headstore() DSReaderWriter { return t.headstore } // DAGstore returns the txn wrapped as a blockstore for a DAGStore under the /blocks namespace -func (t *txn) DAGstore() client.DAGStore { +func (t *txn) DAGstore() DAGStore { return t.dagstore } // Rootstore returns the underlying txn as a DSReaderWriter to implement // the MultiStore interface -func (t *txn) Rootstore() client.DSReaderWriter { +func (t *txn) Rootstore() DSReaderWriter { return t.IterableTxn }*/ diff --git a/datastore/wrappedStore.go b/datastore/wrappedStore.go index 703f0d7dcc..dbd0f227b6 100644 --- a/datastore/wrappedStore.go +++ b/datastore/wrappedStore.go @@ -16,18 +16,17 @@ import ( ktds "github.com/ipfs/go-datastore/keytransform" "github.com/ipfs/go-datastore/query" dsq "github.com/ipfs/go-datastore/query" - "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/datastore/iterable" ) type wrappedStore struct { transform ktds.KeyTransform - store client.DSReaderWriter + store DSReaderWriter } -var _ client.DSReaderWriter = (*wrappedStore)(nil) +var _ DSReaderWriter = (*wrappedStore)(nil) -func prefix(root client.DSReaderWriter, prefix ds.Key) client.DSReaderWriter { +func prefix(root DSReaderWriter, prefix ds.Key) DSReaderWriter { return &wrappedStore{ transform: ktds.PrefixTransform{Prefix: prefix}, store: root, diff --git a/db/collection.go b/db/collection.go index bfd32bc7cb..13000ddd30 100644 --- a/db/collection.go +++ b/db/collection.go @@ -19,6 +19,7 @@ import ( "github.com/fxamacker/cbor/v2" "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/datastore" "github.com/sourcenetwork/defradb/db/base" "github.com/sourcenetwork/defradb/document" "github.com/sourcenetwork/defradb/document/key" @@ -46,7 +47,7 @@ var _ client.Collection = (*Collection)(nil) // together under a collection name. This is analogous to SQL Tables. type Collection struct { db *DB - txn client.Txn + txn datastore.Txn colID uint32 @@ -278,7 +279,7 @@ func (c *Collection) GetAllDocKeys(ctx context.Context) (<-chan client.DocKeysRe return c.getAllDocKeysChan(ctx, txn) } -func (c *Collection) getAllDocKeysChan(ctx context.Context, txn client.Txn) (<-chan client.DocKeysResult, error) { +func (c *Collection) getAllDocKeysChan(ctx context.Context, txn datastore.Txn) (<-chan client.DocKeysResult, error) { prefix := c.getPrimaryIndexDocKey(core.DataStoreKey{}) // empty path for all keys prefix q, err := txn.Datastore().Query(ctx, query.Query{ Prefix: prefix.ToString(), @@ -398,7 +399,7 @@ func (c *Collection) SchemaID() string { // WithTxn returns a new instance of the collection, with a transaction // handle instead of a raw DB handle -func (c *Collection) WithTxn(txn client.Txn) client.Collection { +func (c *Collection) WithTxn(txn datastore.Txn) client.Collection { return &Collection{ db: c.db, txn: txn, @@ -442,7 +443,7 @@ func (c *Collection) CreateMany(ctx context.Context, docs []*document.Document) return c.commitImplicitTxn(ctx, txn) } -func (c *Collection) create(ctx context.Context, txn client.Txn, doc *document.Document) error { +func (c *Collection) create(ctx context.Context, txn datastore.Txn, doc *document.Document) error { // DocKey verification buf, err := doc.Bytes() if err != nil { @@ -519,7 +520,7 @@ func (c *Collection) Update(ctx context.Context, doc *document.Document) error { // or, just update everything regardless. // Should probably be smart about the update due to the MerkleCRDT overhead, shouldn't // add to the bloat. -func (c *Collection) update(ctx context.Context, txn client.Txn, doc *document.Document) error { +func (c *Collection) update(ctx context.Context, txn datastore.Txn, doc *document.Document) error { _, err := c.save(ctx, txn, doc) if err != nil { return err @@ -554,7 +555,7 @@ func (c *Collection) Save(ctx context.Context, doc *document.Document) error { return c.commitImplicitTxn(ctx, txn) } -func (c *Collection) save(ctx context.Context, txn client.Txn, doc *document.Document) (cid.Cid, error) { +func (c *Collection) save(ctx context.Context, txn datastore.Txn, doc *document.Document) (cid.Cid, error) { // New batch transaction/store (optional/todo) // Ensute/Set doc object marker // Loop through doc values @@ -648,7 +649,7 @@ func (c *Collection) Delete(ctx context.Context, key key.DocKey) (bool, error) { // at the moment, delete only does data storage delete. // Dag, and head store will soon follow. -func (c *Collection) delete(ctx context.Context, txn client.Txn, key core.DataStoreKey) (bool, error) { +func (c *Collection) delete(ctx context.Context, txn datastore.Txn, key core.DataStoreKey) (bool, error) { q := query.Query{ Prefix: c.getPrimaryIndexDocKey(key).ToString(), KeysOnly: true, @@ -686,11 +687,11 @@ func (c *Collection) Exists(ctx context.Context, key key.DocKey) (bool, error) { } // check if a document exists with the given key -func (c *Collection) exists(ctx context.Context, txn client.Txn, key core.DataStoreKey) (bool, error) { +func (c *Collection) exists(ctx context.Context, txn datastore.Txn, key core.DataStoreKey) (bool, error) { return txn.Datastore().Has(ctx, c.getPrimaryIndexDocKey(key.WithValueFlag()).ToDS()) } -func (c *Collection) saveDocValue(ctx context.Context, txn client.Txn, key core.DataStoreKey, val document.Value) (cid.Cid, error) { +func (c *Collection) saveDocValue(ctx context.Context, txn datastore.Txn, key core.DataStoreKey, val document.Value) (cid.Cid, error) { switch val.Type() { case core.LWW_REGISTER: wval, ok := val.(document.WriteableValue) @@ -715,7 +716,7 @@ func (c *Collection) saveDocValue(ctx context.Context, txn client.Txn, key core. func (c *Collection) saveValueToMerkleCRDT( ctx context.Context, - txn client.Txn, + txn datastore.Txn, key core.DataStoreKey, ctype core.CType, args ...interface{}) (cid.Cid, error) { @@ -768,7 +769,7 @@ func (c *Collection) saveValueToMerkleCRDT( // getTxn gets or creates a new transaction from the underlying db. // If the collection already has a txn, return the existing one. // Otherwise, create a new implicit transaction. -func (c *Collection) getTxn(ctx context.Context, readonly bool) (client.Txn, error) { +func (c *Collection) getTxn(ctx context.Context, readonly bool) (datastore.Txn, error) { if c.txn != nil { return c.txn, nil } @@ -779,13 +780,13 @@ func (c *Collection) getTxn(ctx context.Context, readonly bool) (client.Txn, err // function only if its an implicit transaction. // Implicit transactions are transactions that are created *during* an operation execution as a side effect. // Explicit transactions are provided to the collection object via the "WithTxn(...)" function. -func (c *Collection) discardImplicitTxn(ctx context.Context, txn client.Txn) { +func (c *Collection) discardImplicitTxn(ctx context.Context, txn datastore.Txn) { if c.txn == nil { txn.Discard(ctx) } } -func (c *Collection) commitImplicitTxn(ctx context.Context, txn client.Txn) error { +func (c *Collection) commitImplicitTxn(ctx context.Context, txn datastore.Txn) error { if c.txn == nil { return txn.Commit(ctx) } diff --git a/db/collection_delete.go b/db/collection_delete.go index 6d3eed5ea9..3367112d42 100644 --- a/db/collection_delete.go +++ b/db/collection_delete.go @@ -24,6 +24,7 @@ import ( "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/datastore" "github.com/sourcenetwork/defradb/document" "github.com/sourcenetwork/defradb/document/key" "github.com/sourcenetwork/defradb/merkle/clock" @@ -138,7 +139,7 @@ func (c *Collection) DeleteWithFilter( func (c *Collection) deleteWithKey( ctx context.Context, - txn client.Txn, + txn datastore.Txn, key core.DataStoreKey, opts ...client.DeleteOpt) (*client.DeleteResult, error) { // Check the docKey we have been given to delete with actually has a corresponding @@ -168,7 +169,7 @@ func (c *Collection) deleteWithKey( func (c *Collection) deleteWithKeys( ctx context.Context, - txn client.Txn, + txn datastore.Txn, keys []key.DocKey, opts ...client.DeleteOpt) (*client.DeleteResult, error) { @@ -207,7 +208,7 @@ func (c *Collection) deleteWithKeys( func (c *Collection) deleteWithFilter( ctx context.Context, - txn client.Txn, + txn datastore.Txn, filter interface{}, opts ...client.DeleteOpt) (*client.DeleteResult, error) { @@ -265,10 +266,10 @@ func (c *Collection) deleteWithFilter( } type dagDeleter struct { - bstore client.DAGStore + bstore datastore.DAGStore } -func newDagDeleter(bstore client.DAGStore) dagDeleter { +func newDagDeleter(bstore datastore.DAGStore) dagDeleter { return dagDeleter{ bstore: bstore, } @@ -286,7 +287,7 @@ func newDagDeleter(bstore client.DAGStore) dagDeleter { // 3) Deleting headstore state. func (c *Collection) applyFullDelete( ctx context.Context, - txn client.Txn, dockey core.DataStoreKey) error { + txn datastore.Txn, dockey core.DataStoreKey) error { // Check the docKey we have been given to delete with actually has a corresponding // document (i.e. document actually exists in the collection). diff --git a/db/collection_get.go b/db/collection_get.go index 9931c7d8e2..ec83c77f59 100644 --- a/db/collection_get.go +++ b/db/collection_get.go @@ -13,8 +13,8 @@ package db import ( "context" - "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/datastore" "github.com/sourcenetwork/defradb/db/base" "github.com/sourcenetwork/defradb/db/fetcher" "github.com/sourcenetwork/defradb/document" @@ -45,7 +45,7 @@ func (c *Collection) Get(ctx context.Context, key key.DocKey) (*document.Documen return doc, c.commitImplicitTxn(ctx, txn) } -func (c *Collection) get(ctx context.Context, txn client.Txn, key core.DataStoreKey) (*document.Document, error) { +func (c *Collection) get(ctx context.Context, txn datastore.Txn, key core.DataStoreKey) (*document.Document, error) { // create a new document fetcher df := new(fetcher.DocumentFetcher) desc := &c.desc diff --git a/db/collection_update.go b/db/collection_update.go index b54fa73aa7..4dbafbdb5f 100644 --- a/db/collection_update.go +++ b/db/collection_update.go @@ -20,6 +20,7 @@ import ( "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/datastore" "github.com/sourcenetwork/defradb/db/base" "github.com/sourcenetwork/defradb/document" "github.com/sourcenetwork/defradb/document/key" @@ -129,7 +130,7 @@ func (c *Collection) UpdateWithDocs(docs []*document.SimpleDocument, updater int return nil } -func (c *Collection) updateWithKey(ctx context.Context, txn client.Txn, key key.DocKey, updater interface{}, opts ...client.UpdateOpt) (*client.UpdateResult, error) { +func (c *Collection) updateWithKey(ctx context.Context, txn datastore.Txn, key key.DocKey, updater interface{}, opts ...client.UpdateOpt) (*client.UpdateResult, error) { patch, err := parseUpdater(updater) if err != nil { return nil, err @@ -170,7 +171,7 @@ func (c *Collection) updateWithKey(ctx context.Context, txn client.Txn, key key. return results, nil } -func (c *Collection) updateWithKeys(ctx context.Context, txn client.Txn, keys []key.DocKey, updater interface{}, opts ...client.UpdateOpt) (*client.UpdateResult, error) { +func (c *Collection) updateWithKeys(ctx context.Context, txn datastore.Txn, keys []key.DocKey, updater interface{}, opts ...client.UpdateOpt) (*client.UpdateResult, error) { patch, err := parseUpdater(updater) if err != nil { return nil, err @@ -216,7 +217,7 @@ func (c *Collection) updateWithKeys(ctx context.Context, txn client.Txn, keys [] func (c *Collection) updateWithFilter( ctx context.Context, - txn client.Txn, + txn datastore.Txn, filter interface{}, updater interface{}, opts ...client.UpdateOpt) (*client.UpdateResult, error) { @@ -287,7 +288,7 @@ func (c *Collection) updateWithFilter( return results, nil } -func (c *Collection) applyPatch(txn client.Txn, doc map[string]interface{}, patch []map[string]interface{}) error { +func (c *Collection) applyPatch(txn datastore.Txn, doc map[string]interface{}, patch []map[string]interface{}) error { for _, op := range patch { path, ok := op["path"].(string) if !ok { @@ -313,11 +314,11 @@ func (c *Collection) applyPatch(txn client.Txn, doc map[string]interface{}, patc return nil } -func (c *Collection) applyPatchOp(txn client.Txn, dockey string, field string, currentVal interface{}, patchOp map[string]interface{}) error { +func (c *Collection) applyPatchOp(txn datastore.Txn, dockey string, field string, currentVal interface{}, patchOp map[string]interface{}) error { return nil } -func (c *Collection) applyMerge(ctx context.Context, txn client.Txn, doc map[string]interface{}, merge map[string]interface{}) error { +func (c *Collection) applyMerge(ctx context.Context, txn datastore.Txn, doc map[string]interface{}, merge map[string]interface{}) error { keyStr, ok := doc["_key"].(string) if !ok { return errors.New("Document is missing key") @@ -507,7 +508,7 @@ func validateFieldSchema(val interface{}, field base.FieldDescription) (interfac } func (c *Collection) applyMergePatchOp( //nolint:unused - txn client.Txn, + txn datastore.Txn, docKey string, field string, currentVal interface{}, @@ -521,7 +522,7 @@ func (c *Collection) applyMergePatchOp( //nolint:unused // Additionally it only queries for the root scalar fields of the object func (c *Collection) makeSelectionQuery( ctx context.Context, - txn client.Txn, + txn datastore.Txn, filter interface{}, opts ...client.UpdateOpt) (planner.Query, error) { var f *parser.Filter @@ -576,13 +577,13 @@ func (c *Collection) makeSelectLocal(filter *parser.Filter) (*parser.Select, err // May need to query the database for other schema types // which requires a db transaction. It is recommended // to use collection.WithTxn(txn) for this function call. -func (c *Collection) getCollectionForPatchOpPath(txn client.Txn, path string) (col *Collection, isArray bool, err error) { +func (c *Collection) getCollectionForPatchOpPath(txn datastore.Txn, path string) (col *Collection, isArray bool, err error) { return nil, false, nil } // getTargetKeyForPatchPath walks through the given doc and Patch path. // It returns the -func (c *Collection) getTargetKeyForPatchPath(txn client.Txn, doc map[string]interface{}, path string) (string, error) { +func (c *Collection) getTargetKeyForPatchPath(txn datastore.Txn, doc map[string]interface{}, path string) (string, error) { _, length := splitPatchPath(path) if length == 0 { return "", errors.New("Invalid patch op path") diff --git a/db/db.go b/db/db.go index 4aef198957..7b47851dba 100644 --- a/db/db.go +++ b/db/db.go @@ -52,7 +52,7 @@ type DB struct { glock sync.RWMutex rootstore ds.Batching - multistore client.MultiStore + multistore datastore.MultiStore crdtFactory *crdt.Factory @@ -120,7 +120,7 @@ func NewDB(ctx context.Context, rootstore ds.Batching, options ...Option) (*DB, return db, nil } -func (db *DB) NewTxn(ctx context.Context, readonly bool) (client.Txn, error) { +func (db *DB) NewTxn(ctx context.Context, readonly bool) (datastore.Txn, error) { return datastore.NewTxnFrom(ctx, db.rootstore, readonly) } @@ -133,7 +133,7 @@ func (db *DB) Blockstore() blockstore.Blockstore { return db.multistore.DAGstore() } -func (db *DB) systemstore() client.DSReaderWriter { +func (db *DB) systemstore() datastore.DSReaderWriter { return db.multistore.Systemstore() } @@ -204,7 +204,7 @@ func (db *DB) Close(ctx context.Context) { log.Info(ctx, "Successfully closed running process") } -func printStore(ctx context.Context, store client.DSReaderWriter) { +func printStore(ctx context.Context, store datastore.DSReaderWriter) { q := query.Query{ Prefix: "", KeysOnly: false, diff --git a/db/fetcher/dag.go b/db/fetcher/dag.go index 136c8b4afc..42c73e20c2 100644 --- a/db/fetcher/dag.go +++ b/db/fetcher/dag.go @@ -16,8 +16,8 @@ import ( "sort" "strings" - "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/datastore" "github.com/ipfs/go-cid" dsq "github.com/ipfs/go-datastore/query" @@ -33,7 +33,7 @@ type BlockFetcher struct { type HeadFetcher struct { // Commented because this code is not used yet according to the linter. - // txn client.Txn + // txn datastore.Txn // key core.Key // curSpanIndex int @@ -46,7 +46,7 @@ type HeadFetcher struct { kvEnd bool } -func (hf *HeadFetcher) Start(ctx context.Context, txn client.Txn, spans core.Spans) error { +func (hf *HeadFetcher) Start(ctx context.Context, txn datastore.Txn, spans core.Spans) error { numspans := len(spans) if numspans == 0 { return errors.New("HeadFetcher must have at least one span") diff --git a/db/fetcher/fetcher.go b/db/fetcher/fetcher.go index 0d7704979d..e582eb29d8 100644 --- a/db/fetcher/fetcher.go +++ b/db/fetcher/fetcher.go @@ -16,7 +16,7 @@ import ( "errors" dsq "github.com/ipfs/go-datastore/query" - "github.com/sourcenetwork/defradb/client" + "github.com/sourcenetwork/defradb/datastore" "github.com/sourcenetwork/defradb/datastore/iterable" "github.com/sourcenetwork/defradb/core" @@ -30,7 +30,7 @@ import ( // encoding. type Fetcher interface { Init(col *base.CollectionDescription, index *base.IndexDescription, fields []*base.FieldDescription, reverse bool) error - Start(ctx context.Context, txn client.Txn, spans core.Spans) error + Start(ctx context.Context, txn datastore.Txn, spans core.Spans) error FetchNext(ctx context.Context) (*document.EncodedDocument, error) FetchNextDecoded(ctx context.Context) (*document.Document, error) FetchNextMap(ctx context.Context) ([]byte, map[string]interface{}, error) @@ -46,7 +46,7 @@ type DocumentFetcher struct { index *base.IndexDescription reverse bool - txn client.Txn + txn datastore.Txn spans core.Spans order []dsq.Order uniqueSpans map[core.Span]struct{} // nolint:structcheck,unused @@ -101,7 +101,7 @@ func (df *DocumentFetcher) Init(col *base.CollectionDescription, index *base.Ind } // Start implements DocumentFetcher -func (df *DocumentFetcher) Start(ctx context.Context, txn client.Txn, spans core.Spans) error { +func (df *DocumentFetcher) Start(ctx context.Context, txn datastore.Txn, spans core.Spans) error { if df.col == nil { return errors.New("DocumentFetcher cannot be started without a CollectionDescription") } diff --git a/db/fetcher/versioned.go b/db/fetcher/versioned.go index bed58c71e4..cc5070ab9c 100644 --- a/db/fetcher/versioned.go +++ b/db/fetcher/versioned.go @@ -15,7 +15,6 @@ import ( "context" "fmt" - "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/datastore" "github.com/sourcenetwork/defradb/db/base" @@ -80,12 +79,12 @@ type VersionedFetcher struct { // embed the regular doc fetcher *DocumentFetcher - txn client.Txn + txn datastore.Txn ctx context.Context // Transient version store root ds.Datastore - store client.Txn + store datastore.Txn key core.DataStoreKey version cid.Cid @@ -113,7 +112,7 @@ func (vf *VersionedFetcher) Init(col *base.CollectionDescription, index *base.In } // Start serializes the correct state accoriding to the Key and CID -func (vf *VersionedFetcher) Start(ctx context.Context, txn client.Txn, spans core.Spans) error { +func (vf *VersionedFetcher) Start(ctx context.Context, txn datastore.Txn, spans core.Spans) error { if vf.col == nil { return errors.New("VersionedFetcher cannot be started without a CollectionDescription") } diff --git a/db/query.go b/db/query.go index e80ddf57e8..7bc880a4f8 100644 --- a/db/query.go +++ b/db/query.go @@ -15,6 +15,7 @@ import ( "strings" "github.com/sourcenetwork/defradb/client" + "github.com/sourcenetwork/defradb/datastore" gql "github.com/graphql-go/graphql" ) @@ -48,7 +49,7 @@ func (db *DB) ExecQuery(ctx context.Context, query string) *client.QueryResult { return res } -func (db *DB) ExecTransactionalQuery(ctx context.Context, query string, txn client.Txn) *client.QueryResult { +func (db *DB) ExecTransactionalQuery(ctx context.Context, query string, txn datastore.Txn) *client.QueryResult { res := &client.QueryResult{} // check if its Introspection query if strings.Contains(query, "IntrospectionQuery") { diff --git a/db/tests/utils.go b/db/tests/utils.go index 7d018a48ce..c86d9c5f94 100644 --- a/db/tests/utils.go +++ b/db/tests/utils.go @@ -27,6 +27,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/sourcenetwork/defradb/client" + "github.com/sourcenetwork/defradb/datastore" badgerds "github.com/sourcenetwork/defradb/datastore/badger/v3" "github.com/sourcenetwork/defradb/db" "github.com/sourcenetwork/defradb/document" @@ -278,7 +279,7 @@ func ExecuteQueryTestCase(t *testing.T, schema string, collectionNames []string, } // Create the transactions before executing and queries - transactions := make([]client.Txn, 0, len(test.TransactionalQueries)) + transactions := make([]datastore.Txn, 0, len(test.TransactionalQueries)) erroredQueries := make([]bool, len(test.TransactionalQueries)) for i, tq := range test.TransactionalQueries { if len(transactions) < tq.TransactionId { diff --git a/merkle/clock/clock.go b/merkle/clock/clock.go index 9259b237b5..26c030d2e6 100644 --- a/merkle/clock/clock.go +++ b/merkle/clock/clock.go @@ -14,8 +14,8 @@ import ( "context" "fmt" - "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/datastore" cid "github.com/ipfs/go-cid" dshelp "github.com/ipfs/go-ipfs-ds-help" @@ -28,8 +28,8 @@ var ( ) type MerkleClock struct { - headstore client.DSReaderWriter - dagstore client.DAGStore + headstore datastore.DSReaderWriter + dagstore datastore.DAGStore // dagSyncer headset *heads crdt core.ReplicatedData @@ -37,7 +37,7 @@ type MerkleClock struct { // NewMerkleClock returns a new merkle clock to read/write events (deltas) to // the clock -func NewMerkleClock(headstore client.DSReaderWriter, dagstore client.DAGStore, namespace core.HeadStoreKey, crdt core.ReplicatedData) core.MerkleClock { +func NewMerkleClock(headstore datastore.DSReaderWriter, dagstore datastore.DAGStore, namespace core.HeadStoreKey, crdt core.ReplicatedData) core.MerkleClock { return &MerkleClock{ headstore: headstore, dagstore: dagstore, diff --git a/merkle/clock/heads.go b/merkle/clock/heads.go index 8fc69d1233..5dc35d6c9d 100644 --- a/merkle/clock/heads.go +++ b/merkle/clock/heads.go @@ -19,8 +19,8 @@ import ( "errors" - "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/datastore" "github.com/sourcenetwork/defradb/logging" cid "github.com/ipfs/go-cid" @@ -30,15 +30,15 @@ import ( // heads manages the current Merkle-CRDT heads. type heads struct { - store client.DSReaderWriter + store datastore.DSReaderWriter namespace core.HeadStoreKey } -func NewHeadSet(store client.DSReaderWriter, namespace core.HeadStoreKey) *heads { +func NewHeadSet(store datastore.DSReaderWriter, namespace core.HeadStoreKey) *heads { return newHeadset(store, namespace) } -func newHeadset(store client.DSReaderWriter, namespace core.HeadStoreKey) *heads { +func newHeadset(store datastore.DSReaderWriter, namespace core.HeadStoreKey) *heads { return &heads{ store: store, namespace: namespace, diff --git a/merkle/crdt/composite.go b/merkle/crdt/composite.go index 17986a54d6..16c801d01a 100644 --- a/merkle/crdt/composite.go +++ b/merkle/crdt/composite.go @@ -13,17 +13,17 @@ package crdt import ( "context" - "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" corecrdt "github.com/sourcenetwork/defradb/core/crdt" corenet "github.com/sourcenetwork/defradb/core/net" + "github.com/sourcenetwork/defradb/datastore" "github.com/sourcenetwork/defradb/merkle/clock" "github.com/ipfs/go-cid" ) var ( - compFactoryFn = MerkleCRDTFactory(func(mstore client.MultiStore, schemaID string, bs corenet.Broadcaster) MerkleCRDTInitFn { + compFactoryFn = MerkleCRDTFactory(func(mstore datastore.MultiStore, schemaID string, bs corenet.Broadcaster) MerkleCRDTInitFn { return func(key core.DataStoreKey) MerkleCRDT { return NewMerkleCompositeDAG(mstore.Datastore(), mstore.Headstore(), mstore.DAGstore(), schemaID, bs, core.DataStoreKey{}, key) } @@ -47,7 +47,7 @@ type MerkleCompositeDAG struct { // NewMerkleCompositeDAG creates a new instance (or loaded from DB) of a MerkleCRDT // backed by a CompositeDAG CRDT -func NewMerkleCompositeDAG(datastore client.DSReaderWriter, headstore client.DSReaderWriter, dagstore client.DAGStore, schemaID string, bs corenet.Broadcaster, ns, key core.DataStoreKey) *MerkleCompositeDAG { +func NewMerkleCompositeDAG(datastore datastore.DSReaderWriter, headstore datastore.DSReaderWriter, dagstore datastore.DAGStore, schemaID string, bs corenet.Broadcaster, ns, key core.DataStoreKey) *MerkleCompositeDAG { compositeDag := corecrdt.NewCompositeDAG(datastore, schemaID, ns, key.ToString() /* stuff like namespace and ID */) clock := clock.NewMerkleClock(headstore, dagstore, key.ToHeadStoreKey(), compositeDag) diff --git a/merkle/crdt/factory.go b/merkle/crdt/factory.go index 58a8d59766..5ba541541c 100644 --- a/merkle/crdt/factory.go +++ b/merkle/crdt/factory.go @@ -13,9 +13,9 @@ package crdt import ( "errors" - "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" corenet "github.com/sourcenetwork/defradb/core/net" + "github.com/sourcenetwork/defradb/datastore" ) var ( @@ -27,14 +27,14 @@ type MerkleCRDTInitFn func(core.DataStoreKey) MerkleCRDT // MerkleCRDTFactory instantiates a MerkleCRDTInitFn with a MultiStore // returns a MerkleCRDTInitFn with all the necessary stores set -type MerkleCRDTFactory func(mstore client.MultiStore, schemaID string, bs corenet.Broadcaster) MerkleCRDTInitFn +type MerkleCRDTFactory func(mstore datastore.MultiStore, schemaID string, bs corenet.Broadcaster) MerkleCRDTInitFn // Factory is a helper utility for instantiating new MerkleCRDTs. // It removes some of the overhead of having to coordinate all the various // store parameters on every single new MerkleCRDT creation type Factory struct { crdts map[core.CType]*MerkleCRDTFactory - multistore client.MultiStore + multistore datastore.MultiStore } var ( @@ -46,7 +46,7 @@ var ( // NewFactory returns a newly instanciated factory object with the assigned stores // It may be called with all stores set to nil -func NewFactory(multistore client.MultiStore) *Factory { +func NewFactory(multistore datastore.MultiStore) *Factory { return &Factory{ crdts: make(map[core.CType]*MerkleCRDTFactory), multistore: multistore, @@ -61,7 +61,7 @@ func (factory *Factory) Register(t core.CType, fn *MerkleCRDTFactory) error { } // Instance and execute the registered factory function for a given MerkleCRDT type -// supplied with all the current stores (passed in as a client.MultiStore object) +// supplied with all the current stores (passed in as a datastore.MultiStore object) func (factory Factory) Instance(schemaID string, bs corenet.Broadcaster, t core.CType, key core.DataStoreKey) (MerkleCRDT, error) { // get the factory function for the given MerkleCRDT type // and pass in the current factory state as a MultiStore parameter @@ -73,8 +73,8 @@ func (factory Factory) Instance(schemaID string, bs corenet.Broadcaster, t core. } // InstanceWithStore executes the registered factory function for the given MerkleCRDT type -// with the additional supplied client.MultiStore instead of the saved one on the main Factory. -func (factory Factory) InstanceWithStores(store client.MultiStore, schemaID string, bs corenet.Broadcaster, t core.CType, key core.DataStoreKey) (MerkleCRDT, error) { +// with the additional supplied datastore.MultiStore instead of the saved one on the main Factory. +func (factory Factory) InstanceWithStores(store datastore.MultiStore, schemaID string, bs corenet.Broadcaster, t core.CType, key core.DataStoreKey) (MerkleCRDT, error) { fn, err := factory.getRegisteredFactory(t) if err != nil { return nil, err @@ -92,48 +92,48 @@ func (factory Factory) getRegisteredFactory(t core.CType) (*MerkleCRDTFactory, e } // SetStores sets all the current stores on the Factory in one call -func (factory *Factory) SetStores(multistore client.MultiStore) error { +func (factory *Factory) SetStores(multistore datastore.MultiStore) error { factory.multistore = multistore return nil } // WithStores returns a new instance of the Factory with all the stores set -func (factory Factory) WithStores(multistore client.MultiStore) Factory { +func (factory Factory) WithStores(multistore datastore.MultiStore) Factory { factory.multistore = multistore return factory } // Rootstore impements MultiStore -func (factory Factory) Rootstore() client.DSReaderWriter { +func (factory Factory) Rootstore() datastore.DSReaderWriter { return nil } -// Data implements client.MultiStore and returns the current Datastore -func (factory Factory) Datastore() client.DSReaderWriter { +// Data implements datastore.MultiStore and returns the current Datastore +func (factory Factory) Datastore() datastore.DSReaderWriter { if factory.multistore == nil { return nil } return factory.multistore.Datastore() } -// Head implements client.MultiStore and returns the current Headstore -func (factory Factory) Headstore() client.DSReaderWriter { +// Head implements datastore.MultiStore and returns the current Headstore +func (factory Factory) Headstore() datastore.DSReaderWriter { if factory.multistore == nil { return nil } return factory.multistore.Headstore() } -// Head implements client.MultiStore and returns the current Headstore -func (factory Factory) Systemstore() client.DSReaderWriter { +// Head implements datastore.MultiStore and returns the current Headstore +func (factory Factory) Systemstore() datastore.DSReaderWriter { if factory.multistore == nil { return nil } return factory.multistore.Systemstore() } -// Dag implements client.MultiStore and returns the current Dagstore -func (factory Factory) DAGstore() client.DAGStore { +// Dag implements datastore.MultiStore and returns the current Dagstore +func (factory Factory) DAGstore() datastore.DAGStore { if factory.multistore == nil { return nil } diff --git a/merkle/crdt/factory_test.go b/merkle/crdt/factory_test.go index 2be2a285ea..4b07796334 100644 --- a/merkle/crdt/factory_test.go +++ b/merkle/crdt/factory_test.go @@ -17,12 +17,11 @@ import ( ds "github.com/ipfs/go-datastore" "github.com/stretchr/testify/assert" - "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/datastore" ) -func newStores() client.MultiStore { +func newStores() datastore.MultiStore { root := ds.NewMapDatastore() rw := datastore.AsDSReaderWriter(root) return datastore.MultiStoreFrom(rw) @@ -56,7 +55,7 @@ func TestFactoryMultiStoreInterface(t *testing.T) { } // check interface implement - var _ client.MultiStore = f + var _ datastore.MultiStore = f // ms = f // check interface functions @@ -184,7 +183,7 @@ func TestFullFactoryInstanceCompositeRegister(t *testing.T) { func TestLWWRegisterFactoryFn(t *testing.T) { ctx := context.Background() m := newStores() - f := NewFactory(m) // here factory is only needed to satisfy client.MultiStore interface + f := NewFactory(m) // here factory is only needed to satisfy datastore.MultiStore interface crdt := lwwFactoryFn(f, "", nil)(core.NewDataStoreKey("/1/0/MyKey")) lwwreg, ok := crdt.(*MerkleLWWRegister) @@ -197,7 +196,7 @@ func TestLWWRegisterFactoryFn(t *testing.T) { func TestCompositeRegisterFactoryFn(t *testing.T) { ctx := context.Background() m := newStores() - f := NewFactory(m) // here factory is only needed to satisfy client.MultiStore interface + f := NewFactory(m) // here factory is only needed to satisfy datastore.MultiStore interface crdt := compFactoryFn(f, "", nil)(core.NewDataStoreKey("/1/0/MyKey")) merkleReg, ok := crdt.(*MerkleCompositeDAG) diff --git a/merkle/crdt/lwwreg.go b/merkle/crdt/lwwreg.go index 771f2e556a..f27d7e13d6 100644 --- a/merkle/crdt/lwwreg.go +++ b/merkle/crdt/lwwreg.go @@ -13,10 +13,10 @@ package crdt import ( "context" - "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" corecrdt "github.com/sourcenetwork/defradb/core/crdt" corenet "github.com/sourcenetwork/defradb/core/net" + "github.com/sourcenetwork/defradb/datastore" "github.com/sourcenetwork/defradb/merkle/clock" // "github.com/sourcenetwork/defradb/datastore" @@ -25,7 +25,7 @@ import ( ) var ( - lwwFactoryFn = MerkleCRDTFactory(func(mstore client.MultiStore, _ string, _ corenet.Broadcaster) MerkleCRDTInitFn { + lwwFactoryFn = MerkleCRDTFactory(func(mstore datastore.MultiStore, _ string, _ corenet.Broadcaster) MerkleCRDTInitFn { return func(key core.DataStoreKey) MerkleCRDT { return NewMerkleLWWRegister(mstore.Datastore(), mstore.Headstore(), mstore.DAGstore(), core.DataStoreKey{}, key) } @@ -50,7 +50,7 @@ type MerkleLWWRegister struct { // NewMerkleLWWRegister creates a new instance (or loaded from DB) of a MerkleCRDT // backed by a LWWRegister CRDT -func NewMerkleLWWRegister(datastore client.DSReaderWriter, headstore client.DSReaderWriter, dagstore client.DAGStore, ns, key core.DataStoreKey) *MerkleLWWRegister { +func NewMerkleLWWRegister(datastore datastore.DSReaderWriter, headstore datastore.DSReaderWriter, dagstore datastore.DAGStore, ns, key core.DataStoreKey) *MerkleLWWRegister { register := corecrdt.NewLWWRegister(datastore, key /* stuff like namespace and ID */) clk := clock.NewMerkleClock(headstore, dagstore, key.ToHeadStoreKey(), register) diff --git a/merkle/crdt/merklecrdt.go b/merkle/crdt/merklecrdt.go index 3c388fde8c..7339f332dd 100644 --- a/merkle/crdt/merklecrdt.go +++ b/merkle/crdt/merklecrdt.go @@ -36,7 +36,7 @@ type MerkleCRDT interface { } // type MerkleCRDTInitFn func(core.Key) MerkleCRDT -// type MerkleCRDTFactory func(store client.DSReaderWriter, namespace core.Key) MerkleCRDTInitFn +// type MerkleCRDTFactory func(store datastore.DSReaderWriter, namespace core.Key) MerkleCRDTInitFn // Type indicates MerkleCRDT type // type Type byte diff --git a/merkle/crdt/merklecrdt_test.go b/merkle/crdt/merklecrdt_test.go index 283cc60187..baf7a9b976 100644 --- a/merkle/crdt/merklecrdt_test.go +++ b/merkle/crdt/merklecrdt_test.go @@ -14,7 +14,6 @@ import ( "context" "testing" - "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" corecrdt "github.com/sourcenetwork/defradb/core/crdt" "github.com/sourcenetwork/defradb/datastore" @@ -30,7 +29,7 @@ func newDS() ds.Datastore { return ds.NewMapDatastore() } -func newTestBaseMerkleCRDT() (*baseMerkleCRDT, client.DSReaderWriter) { +func newTestBaseMerkleCRDT() (*baseMerkleCRDT, datastore.DSReaderWriter) { s := newDS() rw := datastore.AsDSReaderWriter(s) multistore := datastore.MultiStoreFrom(rw) @@ -61,7 +60,7 @@ func TestMerkleCRDTPublish(t *testing.T) { printStore(ctx, store) } -func printStore(ctx context.Context, store client.DSReaderWriter) { +func printStore(ctx context.Context, store datastore.DSReaderWriter) { q := query.Query{ Prefix: "", KeysOnly: false, diff --git a/net/process.go b/net/process.go index e510a48f28..d9599faa4c 100644 --- a/net/process.go +++ b/net/process.go @@ -24,6 +24,7 @@ import ( "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/datastore" "github.com/sourcenetwork/defradb/logging" "github.com/sourcenetwork/defradb/merkle/clock" "github.com/sourcenetwork/defradb/merkle/crdt" @@ -87,7 +88,7 @@ func (p *Peer) processLog( return cids, txn.Commit(ctx) } -func initCRDTForType(ctx context.Context, txn client.MultiStore, col client.Collection, docKey core.DataStoreKey, field string) (crdt.MerkleCRDT, error) { +func initCRDTForType(ctx context.Context, txn datastore.MultiStore, col client.Collection, docKey core.DataStoreKey, field string) (crdt.MerkleCRDT, error) { var key core.DataStoreKey var ctype core.CType if field == "" { // empty field name implies composite type diff --git a/query/graphql/planner/executor.go b/query/graphql/planner/executor.go index 0664953288..f494f41c58 100644 --- a/query/graphql/planner/executor.go +++ b/query/graphql/planner/executor.go @@ -15,6 +15,7 @@ import ( "fmt" "github.com/sourcenetwork/defradb/client" + "github.com/sourcenetwork/defradb/datastore" "github.com/sourcenetwork/defradb/query/graphql/parser" "github.com/sourcenetwork/defradb/query/graphql/schema" @@ -58,7 +59,7 @@ func NewQueryExecutor(manager *schema.SchemaManager) (*QueryExecutor, error) { // } -func (e *QueryExecutor) MakeSelectQuery(ctx context.Context, db client.DB, txn client.Txn, selectStmt *parser.Select) (Query, error) { +func (e *QueryExecutor) MakeSelectQuery(ctx context.Context, db client.DB, txn datastore.Txn, selectStmt *parser.Select) (Query, error) { if selectStmt == nil { return nil, fmt.Errorf("Cannot create query without a selection") } @@ -66,7 +67,7 @@ func (e *QueryExecutor) MakeSelectQuery(ctx context.Context, db client.DB, txn c return planner.makePlan(selectStmt) } -func (e *QueryExecutor) ExecQuery(ctx context.Context, db client.DB, txn client.Txn, query string, args ...interface{}) ([]map[string]interface{}, error) { +func (e *QueryExecutor) ExecQuery(ctx context.Context, db client.DB, txn datastore.Txn, query string, args ...interface{}) ([]map[string]interface{}, error) { q, err := e.ParseQueryString(query) if err != nil { return nil, err @@ -76,7 +77,7 @@ func (e *QueryExecutor) ExecQuery(ctx context.Context, db client.DB, txn client. return planner.queryDocs(ctx, q) } -func (e *QueryExecutor) MakePlanFromParser(ctx context.Context, db client.DB, txn client.Txn, query *parser.Query) (planNode, error) { +func (e *QueryExecutor) MakePlanFromParser(ctx context.Context, db client.DB, txn datastore.Txn, query *parser.Query) (planNode, error) { planner := makePlanner(ctx, db, txn) return planner.makePlan(query) } diff --git a/query/graphql/planner/planner.go b/query/graphql/planner/planner.go index 5aa064b495..1315a4aef1 100644 --- a/query/graphql/planner/planner.go +++ b/query/graphql/planner/planner.go @@ -17,6 +17,7 @@ import ( "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/datastore" "github.com/sourcenetwork/defradb/logging" "github.com/sourcenetwork/defradb/query/graphql/parser" ) @@ -93,7 +94,7 @@ type Statement struct { // Planner combines session state and database state to // produce a query plan, which is run by the execution context. type Planner struct { - txn client.Txn + txn datastore.Txn db client.DB ctx context.Context @@ -103,7 +104,7 @@ type Planner struct { } -func makePlanner(ctx context.Context, db client.DB, txn client.Txn) *Planner { +func makePlanner(ctx context.Context, db client.DB, txn datastore.Txn) *Planner { return &Planner{ txn: txn, db: db, From 958c2e76219ec08c10f8a179dfe4a9ded60dd461 Mon Sep 17 00:00:00 2001 From: Andrew Sisley Date: Thu, 10 Mar 2022 21:31:14 -0500 Subject: [PATCH 16/16] Remove unwanted comment --- document/document.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/document/document.go b/document/document.go index 0f9968787a..89b3738fd2 100644 --- a/document/document.go +++ b/document/document.go @@ -66,9 +66,8 @@ type Document struct { key key.DocKey fields map[string]Field values map[Field]Value - // @TODO: schemaInfo schema.Info - head cid.Cid - mu sync.RWMutex + head cid.Cid + mu sync.RWMutex // marks if document has unsaved changes isDirty bool }