forked from sourcenetwork/defradb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
12669e4
commit 5998a2d
Showing
17 changed files
with
827 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// 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 db | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/sourcenetwork/defradb/client" | ||
"github.com/sourcenetwork/defradb/internal/db/description" | ||
) | ||
|
||
// CollectionRetriever is a helper struct that retrieves a collection from a document ID. | ||
type CollectionRetriever struct { | ||
db *db | ||
} | ||
|
||
// NewCollectionRetriever creates a new CollectionRetriever. | ||
func NewCollectionRetriever(database client.DB) *CollectionRetriever { | ||
internalDB, ok := database.(*db) | ||
if !ok { | ||
return nil | ||
} | ||
return &CollectionRetriever{ | ||
db: internalDB, | ||
} | ||
} | ||
|
||
// RetrieveCollectionFromDocID retrieves a collection from a document ID. | ||
func (r *CollectionRetriever) RetrieveCollectionFromDocID( | ||
ctx context.Context, | ||
docID string, | ||
) (client.Collection, error) { | ||
ctx, txn, err := ensureContextTxn(ctx, r.db, false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer txn.Discard(ctx) | ||
|
||
headIterator, err := NewHeadBlocksIteratorFromTxn(ctx, txn, docID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
hasValue, err := headIterator.Next() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !hasValue { | ||
return nil, NewErrDocIDNotFound(docID) | ||
} | ||
|
||
schema, err := description.GetSchemaVersion(ctx, txn, headIterator.CurrentBlock().Delta.GetSchemaVersionID()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
col, err := getCollectionFromRootSchema(ctx, r.db, schema.Root) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return col, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// 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 db | ||
|
||
import ( | ||
"context" | ||
"io" | ||
|
||
"github.com/ipfs/go-cid" | ||
|
||
"github.com/sourcenetwork/defradb/datastore" | ||
"github.com/sourcenetwork/defradb/internal/core" | ||
coreblock "github.com/sourcenetwork/defradb/internal/core/block" | ||
"github.com/sourcenetwork/defradb/internal/merkle/clock" | ||
) | ||
|
||
// DocHeadBlocksIterator is an iterator that iterates over the head blocks of a document. | ||
type DocHeadBlocksIterator struct { | ||
ctx context.Context | ||
blockstore datastore.Blockstore | ||
cids []cid.Cid | ||
|
||
currentCid cid.Cid | ||
currentBlock *coreblock.Block | ||
currentRawBlock []byte | ||
} | ||
|
||
var _ io.Closer = (*DocHeadBlocksIterator)(nil) | ||
|
||
func (h *DocHeadBlocksIterator) Close() error { | ||
return nil | ||
} | ||
|
||
// NewHeadBlocksIterator creates a new DocHeadBlocksIterator. | ||
func NewHeadBlocksIterator( | ||
ctx context.Context, | ||
headstore datastore.DSReaderWriter, | ||
blockstore datastore.Blockstore, | ||
docID string, | ||
) (*DocHeadBlocksIterator, error) { | ||
headStoreKey := core.HeadStoreKey{ | ||
DocID: docID, | ||
FieldID: core.COMPOSITE_NAMESPACE, | ||
} | ||
headset := clock.NewHeadSet(headstore, headStoreKey) | ||
cids, _, err := headset.List(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &DocHeadBlocksIterator{ | ||
ctx: ctx, | ||
blockstore: blockstore, | ||
cids: cids, | ||
}, nil | ||
} | ||
|
||
// NewHeadBlocksIteratorFromTxn creates a new DocHeadBlocksIterator from a transaction. | ||
func NewHeadBlocksIteratorFromTxn( | ||
ctx context.Context, | ||
txn datastore.Txn, | ||
docID string, | ||
) (*DocHeadBlocksIterator, error) { | ||
return NewHeadBlocksIterator(ctx, txn.Headstore(), txn.Blockstore(), docID) | ||
} | ||
|
||
// Next advances the iterator to the next block. | ||
func (h *DocHeadBlocksIterator) Next() (bool, error) { | ||
if len(h.cids) == 0 { | ||
return false, nil | ||
} | ||
nextCid := h.cids[0] | ||
h.cids = h.cids[1:] | ||
|
||
rawBlock, err := h.blockstore.Get(h.ctx, nextCid) | ||
if err != nil { | ||
return false, err | ||
} | ||
blk, err := coreblock.GetFromBytes(rawBlock.RawData()) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
h.currentCid = nextCid | ||
h.currentBlock = blk | ||
h.currentRawBlock = rawBlock.RawData() | ||
return true, nil | ||
} | ||
|
||
// CurrentCid returns the CID of the current block. | ||
func (h *DocHeadBlocksIterator) CurrentCid() cid.Cid { | ||
return h.currentCid | ||
} | ||
|
||
// CurrentBlock returns the current block. | ||
func (h *DocHeadBlocksIterator) CurrentBlock() *coreblock.Block { | ||
return h.currentBlock | ||
} | ||
|
||
// CurrentRawBlock returns the raw data of the current block. | ||
func (h *DocHeadBlocksIterator) CurrentRawBlock() []byte { | ||
return h.currentRawBlock | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.