Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add collectionId field to commit field #1235

Merged
merged 7 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions client/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type Store interface {

GetCollectionByName(context.Context, string) (Collection, error)
GetCollectionBySchemaID(context.Context, string) (Collection, error)
GetCollectionByVersionID(context.Context, string) (Collection, error)
GetAllCollections(context.Context) ([]Collection, error)

ExecRequest(context.Context, string) *RequestResult
Expand Down
2 changes: 2 additions & 0 deletions client/request/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const (
HeightFieldName = "height"
CidFieldName = "cid"
DockeyFieldName = "dockey"
CollectionIDFieldName = "collectionID"
SchemaVersionIDFieldName = "schemaVersionId"
DeltaFieldName = "delta"

Expand Down Expand Up @@ -88,6 +89,7 @@ var (
HeightFieldName,
CidFieldName,
DockeyFieldName,
CollectionIDFieldName,
SchemaVersionIDFieldName,
DeltaFieldName,
}
Expand Down
18 changes: 9 additions & 9 deletions core/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type Key interface {

// DataStoreKey is a type that represents a key in the database.
type DataStoreKey struct {
CollectionId string
CollectionID string
InstanceType InstanceType
DocKey string
FieldId string
Expand Down Expand Up @@ -144,7 +144,7 @@ func NewDataStoreKey(key string) (DataStoreKey, error) {
return dataStoreKey, ErrInvalidKey
}

dataStoreKey.CollectionId = elements[0]
dataStoreKey.CollectionID = elements[0]
dataStoreKey.InstanceType = InstanceType(elements[1])
dataStoreKey.DocKey = elements[2]
if numberOfElements == 4 {
Expand Down Expand Up @@ -272,8 +272,8 @@ func (k HeadStoreKey) WithFieldId(fieldId string) HeadStoreKey {
func (k DataStoreKey) ToString() string {
var result string

if k.CollectionId != "" {
result = result + "/" + k.CollectionId
if k.CollectionID != "" {
result = result + "/" + k.CollectionID
}
if k.InstanceType != "" {
result = result + "/" + string(k.InstanceType)
Expand All @@ -297,22 +297,22 @@ func (k DataStoreKey) ToDS() ds.Key {
}

func (k DataStoreKey) Equal(other DataStoreKey) bool {
return k.CollectionId == other.CollectionId &&
return k.CollectionID == other.CollectionID &&
k.DocKey == other.DocKey &&
k.FieldId == other.FieldId &&
k.InstanceType == other.InstanceType
}

func (k DataStoreKey) ToPrimaryDataStoreKey() PrimaryDataStoreKey {
return PrimaryDataStoreKey{
CollectionId: k.CollectionId,
CollectionId: k.CollectionID,
DocKey: k.DocKey,
}
}

func (k PrimaryDataStoreKey) ToDataStoreKey() DataStoreKey {
return DataStoreKey{
CollectionId: k.CollectionId,
CollectionID: k.CollectionId,
DocKey: k.DocKey,
}
}
Expand Down Expand Up @@ -506,8 +506,8 @@ func (k DataStoreKey) PrefixEnd() DataStoreKey {
newKey.InstanceType = InstanceType(bytesPrefixEnd([]byte(k.InstanceType)))
return newKey
}
if k.CollectionId != "" {
newKey.CollectionId = string(bytesPrefixEnd([]byte(k.CollectionId)))
if k.CollectionID != "" {
newKey.CollectionID = string(bytesPrefixEnd([]byte(k.CollectionID)))
return newKey
}
return newKey
Expand Down
4 changes: 2 additions & 2 deletions core/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestNewDataStoreKey_ReturnsCollectionIdAndIndexIdAndDocKeyAndFieldIdAndInst
assert.Equal(
t,
DataStoreKey{
CollectionId: collectionId,
CollectionID: collectionId,
DocKey: docKey,
FieldId: fieldId,
InstanceType: InstanceType(instanceType)},
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestNewDataStoreKey_GivenAShortObjectMarker(t *testing.T) {
assert.Equal(
t,
DataStoreKey{
CollectionId: collectionId,
CollectionID: collectionId,
DocKey: docKey,
InstanceType: InstanceType(instanceType)},
result)
Expand Down
4 changes: 2 additions & 2 deletions db/base/collection_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import (
// MakeIndexPrefix generates a key prefix for the given collection/index descriptions
func MakeCollectionKey(col client.CollectionDescription) core.DataStoreKey {
return core.DataStoreKey{
CollectionId: col.IDString(),
CollectionID: col.IDString(),
}
}

// MakeIndexKey generates a key for the target dockey, using the collection/index description
func MakeDocKey(col client.CollectionDescription, docKey string) core.DataStoreKey {
return core.DataStoreKey{
CollectionId: col.IDString(),
CollectionID: col.IDString(),
DocKey: docKey,
}
}
Expand Down
12 changes: 6 additions & 6 deletions db/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func (db *db) validateUpdateCollection(
// getCollectionByVersionId returns the [*collection] at the given [schemaVersionId] version.
//
// Will return an error if the given key is empty, or not found.
func (db *db) getCollectionByVersionId(
func (db *db) getCollectionByVersionID(
ctx context.Context,
txn datastore.Txn,
schemaVersionId string,
Expand Down Expand Up @@ -406,7 +406,7 @@ func (db *db) getCollectionByName(ctx context.Context, txn datastore.Txn, name s
}

schemaVersionId := string(buf)
return db.getCollectionByVersionId(ctx, txn, schemaVersionId)
return db.getCollectionByVersionID(ctx, txn, schemaVersionId)
}

// getCollectionBySchemaID returns an existing collection using the schema hash ID.
Expand All @@ -426,7 +426,7 @@ func (db *db) getCollectionBySchemaID(
}

schemaVersionId := string(buf)
return db.getCollectionByVersionId(ctx, txn, schemaVersionId)
return db.getCollectionByVersionID(ctx, txn, schemaVersionId)
}

// getAllCollections gets all the currently defined collections.
Expand All @@ -452,7 +452,7 @@ func (db *db) getAllCollections(ctx context.Context, txn datastore.Txn) ([]clien
}

schemaVersionId := string(res.Value)
col, err := db.getCollectionByVersionId(ctx, txn, schemaVersionId)
col, err := db.getCollectionByVersionID(ctx, txn, schemaVersionId)
if err != nil {
return nil, NewErrFailedToGetCollection(schemaVersionId, err)
}
Expand Down Expand Up @@ -1082,7 +1082,7 @@ func (c *collection) getPrimaryKeyFromDocKey(docKey client.DocKey) core.PrimaryD

func (c *collection) getDSKeyFromDockey(docKey client.DocKey) core.DataStoreKey {
return core.DataStoreKey{
CollectionId: fmt.Sprint(c.colID),
CollectionID: fmt.Sprint(c.colID),
DocKey: docKey.String(),
InstanceType: core.ValueKey,
}
Expand All @@ -1095,7 +1095,7 @@ func (c *collection) tryGetFieldKey(key core.PrimaryDataStoreKey, fieldName stri
}

return core.DataStoreKey{
CollectionId: key.CollectionId,
CollectionID: key.CollectionId,
DocKey: key.DocKey,
FieldId: strconv.FormatUint(uint64(fieldId), 10),
}, true
Expand Down
20 changes: 20 additions & 0 deletions db/txn_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,26 @@ func (db *explicitTxnDB) GetCollectionBySchemaID(
return db.getCollectionBySchemaID(ctx, db.txn, schemaID)
}

// GetCollectionByVersionID returns an existing collection using the schema version hash ID.
func (db *implicitTxnDB) GetCollectionByVersionID(
ctx context.Context, schemaVersionID string,
) (client.Collection, error) {
txn, err := db.NewTxn(ctx, true)
if err != nil {
return nil, err
}
defer txn.Discard(ctx)

return db.getCollectionByVersionID(ctx, txn, schemaVersionID)
}

// GetCollectionByVersionID returns an existing collection using the schema version hash ID.
func (db *explicitTxnDB) GetCollectionByVersionID(
ctx context.Context, schemaVersionID string,
) (client.Collection, error) {
return db.getCollectionByVersionID(ctx, db.txn, schemaVersionID)
}

// AddP2PCollection adds the given collection ID that the P2P system
// subscribes to to the the persisted list. It will error if the provided
// collection ID is invalid.
Expand Down
22 changes: 16 additions & 6 deletions planner/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,12 @@ func (n *dagScanNode) dagBlockToNodeDoc(block blocks.Block) (core.Doc, []*ipld.L

schemaVersionId, ok := delta["SchemaVersionID"].(string)
if ok {
n.commitSelect.DocumentMapping.SetFirstOfName(&commit, "schemaVersionId", schemaVersionId)
n.commitSelect.DocumentMapping.SetFirstOfName(&commit,
request.SchemaVersionIDFieldName, schemaVersionId)
}

n.commitSelect.DocumentMapping.SetFirstOfName(&commit, "height", int64(prio))
n.commitSelect.DocumentMapping.SetFirstOfName(&commit, "delta", delta["Data"])
n.commitSelect.DocumentMapping.SetFirstOfName(&commit, request.HeightFieldName, int64(prio))
n.commitSelect.DocumentMapping.SetFirstOfName(&commit, request.DeltaFieldName, delta["Data"])

dockey, ok := delta["DocKey"].([]byte)
if !ok {
Expand All @@ -302,7 +303,16 @@ func (n *dagScanNode) dagBlockToNodeDoc(block blocks.Block) (core.Doc, []*ipld.L
if err != nil {
return core.Doc{}, nil, err
}
n.commitSelect.DocumentMapping.SetFirstOfName(&commit, "dockey", dockeyObj.DocKey)
n.commitSelect.DocumentMapping.SetFirstOfName(&commit,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: I think people here do prefer the below, instead of the current (no need to change now, but in future PRs consider this):

n.commitSelect.DocumentMapping.SetFirstOfName(
    &commit,
    request.DockeyFieldName, 
    dockeyObj.DocKey,
)

request.DockeyFieldName, dockeyObj.DocKey)

collection, err := n.planner.db.GetCollectionByVersionID(n.planner.ctx, schemaVersionId)
if err != nil {
return core.Doc{}, nil, err
}

n.commitSelect.DocumentMapping.SetFirstOfName(&commit,
request.CollectionIDFieldName, int64(collection.ID()))

heads := make([]*ipld.Link, 0)

Expand All @@ -315,8 +325,8 @@ func (n *dagScanNode) dagBlockToNodeDoc(block blocks.Block) (core.Doc, []*ipld.L

for i, l := range nd.Links() {
link := linksMapping.NewDoc()
linksMapping.SetFirstOfName(&link, "name", l.Name)
linksMapping.SetFirstOfName(&link, "cid", l.Cid.String())
linksMapping.SetFirstOfName(&link, request.LinksNameFieldName, l.Name)
linksMapping.SetFirstOfName(&link, request.LinksCidFieldName, l.Cid.String())

links[i] = link
}
Expand Down
16 changes: 12 additions & 4 deletions request/graphql/schema/types/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ var (
// Height: Int
// CID: String
// Dockey: String
// CollectionID: Int
// SchemaVersionID: String
// Delta: String
// Previous: [Commit]
// Links: [Commit]
// }
//
// Any self referential type needs to be initalized
// Any self referential type needs to be initialized
// inside the init() func
CommitObject = gql.NewObject(gql.ObjectConfig{
Name: request.CommitTypeName,
Expand All @@ -50,6 +51,9 @@ var (
"dockey": &gql.Field{
Type: gql.String,
},
"collectionID": &gql.Field{
Type: gql.Int,
},
"schemaVersionId": &gql.Field{
Type: gql.String,
},
Expand Down Expand Up @@ -110,6 +114,9 @@ var (
"dockey": &gql.InputObjectFieldConfig{
Type: OrderingEnum,
},
"collectionID": &gql.InputObjectFieldConfig{
Type: OrderingEnum,
},
},
},
)
Expand All @@ -118,9 +125,10 @@ var (
gql.EnumConfig{
Name: "commitFields",
Values: gql.EnumValueConfigMap{
"height": &gql.EnumValueConfig{Value: "height"},
"cid": &gql.EnumValueConfig{Value: "cid"},
"dockey": &gql.EnumValueConfig{Value: "dockey"},
"height": &gql.EnumValueConfig{Value: "height"},
"cid": &gql.EnumValueConfig{Value: "cid"},
"dockey": &gql.EnumValueConfig{Value: "dockey"},
"collectionID": &gql.EnumValueConfig{Value: "collectionID"},
},
},
)
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/query/commits/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,20 @@ const userCollectionGQLSchema = (`
}
`)

const companiesCollectionGQLSchema = (`
type companies {
Name: String
}
`)

func updateUserCollectionSchema() testUtils.SchemaUpdate {
return testUtils.SchemaUpdate{
Schema: userCollectionGQLSchema,
}
}

func updateCompaniesCollectionSchema() testUtils.SchemaUpdate {
return testUtils.SchemaUpdate{
Schema: companiesCollectionGQLSchema,
}
}
Loading