From d8d68394178fbc17f86e852a552930cb230e51f8 Mon Sep 17 00:00:00 2001 From: Andrew Sisley Date: Tue, 19 Nov 2024 13:31:22 -0500 Subject: [PATCH] Add support for col-level tt queries --- internal/db/fetcher/versioned.go | 7 + .../request/graphql/schema/descriptions.go | 9 +- .../query/simple/with_cid_branchable_test.go | 161 ++++++++++++++++++ 3 files changed, 172 insertions(+), 5 deletions(-) create mode 100644 tests/integration/query/simple/with_cid_branchable_test.go diff --git a/internal/db/fetcher/versioned.go b/internal/db/fetcher/versioned.go index 0003bc5383..ae20ff8d52 100644 --- a/internal/db/fetcher/versioned.go +++ b/internal/db/fetcher/versioned.go @@ -318,6 +318,13 @@ func (vf *VersionedFetcher) merge(c cid.Cid) error { var mcrdt merklecrdt.MerkleCRDT switch { + case block.Delta.IsCollection(): + mcrdt = merklecrdt.NewMerkleCollection( + vf.store, + keys.NewCollectionSchemaVersionKey(vf.col.Description().SchemaVersionID, vf.col.Description().ID), + keys.NewHeadstoreColKey(vf.col.Description().RootID), + ) + case block.Delta.IsComposite(): mcrdt = merklecrdt.NewMerkleCompositeDAG( vf.store, diff --git a/internal/request/graphql/schema/descriptions.go b/internal/request/graphql/schema/descriptions.go index 07a6873d61..b7c0373abc 100644 --- a/internal/request/graphql/schema/descriptions.go +++ b/internal/request/graphql/schema/descriptions.go @@ -73,11 +73,10 @@ An optional set of docIDs for this field. Only documents with a docID be ignored. ` cidArgDescription string = ` -An optional value that specifies the commit ID of a document to return. - This CID does not need to be the most recent for a document, if it - corresponds to an older version of a document the document will be returned - at the state it was in at the time of that commit. If a matching commit is - not found then an empty set will be returned. +An optional value that specifies the commit ID of a document or a branchable collection. + This CID does not need to be the most recent. If it corresponds to an older version + the document(s) will be returned at the state they were in at the time of that commit. + If a matching commit is not found then an empty set will be returned. ` singleFieldFilterArgDescription string = ` An optional filter for this join, if the related record does diff --git a/tests/integration/query/simple/with_cid_branchable_test.go b/tests/integration/query/simple/with_cid_branchable_test.go new file mode 100644 index 0000000000..58590a5d5a --- /dev/null +++ b/tests/integration/query/simple/with_cid_branchable_test.go @@ -0,0 +1,161 @@ +// Copyright 2024 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 simple + +import ( + "testing" + + testUtils "github.com/sourcenetwork/defradb/tests/integration" +) + +func TestQuerySimpleWithCidOfBranchableCollection_FirstCid(t *testing.T) { + test := testUtils.TestCase{ + Actions: []any{ + testUtils.SchemaUpdate{ + Schema: ` + type Users @branchable { + name: String + } + `, + }, + testUtils.CreateDoc{ + Doc: `{ + "name": "Fred" + }`, + }, + testUtils.UpdateDoc{ + Doc: `{ + "name": "Freddddd" + }`, + }, + testUtils.CreateDoc{ + Doc: `{ + "name": "John" + }`, + }, + testUtils.Request{ + Request: `query { + Users ( + cid: "bafyreiewwsnu2ld5qlntamdm77ayb7xtmxz3p5difvaaakaome7zbtpo4u" + ) { + name + } + }`, + Results: map[string]any{ + "Users": []map[string]any{ + { + "name": "Fred", + }, + }, + }, + }, + }, + } + + testUtils.ExecuteTestCase(t, test) +} + +func TestQuerySimpleWithCidOfBranchableCollection_MiddleCid(t *testing.T) { + test := testUtils.TestCase{ + Actions: []any{ + testUtils.SchemaUpdate{ + Schema: ` + type Users @branchable { + name: String + } + `, + }, + testUtils.CreateDoc{ + Doc: `{ + "name": "Fred" + }`, + }, + testUtils.UpdateDoc{ + Doc: `{ + "name": "Freddddd" + }`, + }, + testUtils.CreateDoc{ + Doc: `{ + "name": "John" + }`, + }, + testUtils.Request{ + Request: `query { + Users ( + cid: "bafyreifpamlyhcbriztgbhds5ctgi5rm6w5wcar2py7246lo6j5v7iusxm" + ) { + name + } + }`, + Results: map[string]any{ + "Users": []map[string]any{ + { + "name": "Freddddd", + }, + }, + }, + }, + }, + } + + testUtils.ExecuteTestCase(t, test) +} + +func TestQuerySimpleWithCidOfBranchableCollection_LastCid(t *testing.T) { + test := testUtils.TestCase{ + Actions: []any{ + testUtils.SchemaUpdate{ + Schema: ` + type Users @branchable { + name: String + } + `, + }, + testUtils.CreateDoc{ + Doc: `{ + "name": "Fred" + }`, + }, + testUtils.UpdateDoc{ + Doc: `{ + "name": "Freddddd" + }`, + }, + testUtils.CreateDoc{ + Doc: `{ + "name": "John" + }`, + }, + testUtils.Request{ + Request: `query { + Users ( + cid: "bafyreigmt6ytph32jjxts2bij7fkne5ntionldsnklp35vcamvvl2x3a5i" + ) { + name + } + }`, + Results: map[string]any{ + "Users": []map[string]any{ + { + "name": "Freddddd", + }, + { + "name": "John", + }, + }, + }, + }, + }, + } + + testUtils.ExecuteTestCase(t, test) +}