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

Cleanup the core messages now that v0 is gone #652

Merged
merged 1 commit into from
Jun 20, 2022
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
21 changes: 8 additions & 13 deletions internal/datastore/common/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,21 +247,16 @@ func NewPGXExecutor(txSource TxFactory) ExecuteQueryFunc {
var tuples []*core.RelationTuple
for rows.Next() {
nextTuple := &core.RelationTuple{
ObjectAndRelation: &core.ObjectAndRelation{},
User: &core.User{
UserOneof: &core.User_Userset{
Userset: &core.ObjectAndRelation{},
},
},
ResourceAndRelation: &core.ObjectAndRelation{},
Subject: &core.ObjectAndRelation{},
Comment on lines +250 to +251
Copy link
Contributor

Choose a reason for hiding this comment

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

If a Subject does also have a Relation, which isn't it named SubjectAndRelation, or perhaps why not naming ResourceAndRelation just Relation?

Copy link
Member Author

Choose a reason for hiding this comment

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

The real change would be to break the relation out from both, but that is a major refactor, so I left it using ObjectAndRelation. Since I did so, I wanted to be clear that the relation for the operation is in the first ONR

}
userset := nextTuple.User.GetUserset()
err := rows.Scan(
&nextTuple.ObjectAndRelation.Namespace,
&nextTuple.ObjectAndRelation.ObjectId,
&nextTuple.ObjectAndRelation.Relation,
&userset.Namespace,
&userset.ObjectId,
&userset.Relation,
&nextTuple.ResourceAndRelation.Namespace,
&nextTuple.ResourceAndRelation.ObjectId,
&nextTuple.ResourceAndRelation.Relation,
&nextTuple.Subject.Namespace,
&nextTuple.Subject.ObjectId,
&nextTuple.Subject.Relation,
)
if err != nil {
return nil, fmt.Errorf(errUnableToQueryTuples, err)
Expand Down
14 changes: 5 additions & 9 deletions internal/datastore/crdb/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,15 @@ func (cds *crdbDatastore) Watch(ctx context.Context, afterRevision datastore.Rev

oneChange := &core.RelationTupleUpdate{
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit / For a follow up: the direct proto instantiation tends to be rather verbose, would it make sense to create helper methods to help with readability?

Copy link
Member Author

Choose a reason for hiding this comment

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

We do have some helper methods defined in various places, so we could in theory grab one, but I like that everything is explicitly named here

Tuple: &core.RelationTuple{
ObjectAndRelation: &core.ObjectAndRelation{
ResourceAndRelation: &core.ObjectAndRelation{
Namespace: pkValues[0],
ObjectId: pkValues[1],
Relation: pkValues[2],
},
User: &core.User{
UserOneof: &core.User_Userset{
Userset: &core.ObjectAndRelation{
Namespace: pkValues[3],
ObjectId: pkValues[4],
Relation: pkValues[5],
},
},
Subject: &core.ObjectAndRelation{
Namespace: pkValues[3],
ObjectId: pkValues[4],
Relation: pkValues[5],
},
},
}
Expand Down
6 changes: 3 additions & 3 deletions internal/datastore/memdb/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ func (r relationship) Relationship() *v1.Relationship {

func (r relationship) RelationTuple() *core.RelationTuple {
return &core.RelationTuple{
ObjectAndRelation: &core.ObjectAndRelation{
ResourceAndRelation: &core.ObjectAndRelation{
Namespace: r.namespace,
ObjectId: r.resourceID,
Relation: r.relation,
},
User: &core.User{UserOneof: &core.User_Userset{Userset: &core.ObjectAndRelation{
Subject: &core.ObjectAndRelation{
Namespace: r.subjectNamespace,
ObjectId: r.subjectObjectID,
Relation: r.subjectRelation,
}}},
},
}
}

Expand Down
22 changes: 9 additions & 13 deletions internal/datastore/mysql/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,21 +337,17 @@ func newMySQLExecutor(tx querier) common.ExecuteQueryFunc {
var tuples []*core.RelationTuple
for rows.Next() {
nextTuple := &core.RelationTuple{
ObjectAndRelation: &core.ObjectAndRelation{},
User: &core.User{
UserOneof: &core.User_Userset{
Userset: &core.ObjectAndRelation{},
},
},
ResourceAndRelation: &core.ObjectAndRelation{},
Subject: &core.ObjectAndRelation{},
}
userset := nextTuple.User.GetUserset()

err := rows.Scan(
&nextTuple.ObjectAndRelation.Namespace,
&nextTuple.ObjectAndRelation.ObjectId,
&nextTuple.ObjectAndRelation.Relation,
&userset.Namespace,
&userset.ObjectId,
&userset.Relation,
&nextTuple.ResourceAndRelation.Namespace,
&nextTuple.ResourceAndRelation.ObjectId,
&nextTuple.ResourceAndRelation.Relation,
&nextTuple.Subject.Namespace,
&nextTuple.Subject.ObjectId,
&nextTuple.Subject.Relation,
)
if err != nil {
return nil, fmt.Errorf(errUnableToQueryTuples, err)
Expand Down
18 changes: 9 additions & 9 deletions internal/datastore/mysql/datastore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,16 @@ func GarbageCollectionTest(t *testing.T, ds datastore.Datastore) {
// Write a relationship.

tpl := &corev1.RelationTuple{
ObjectAndRelation: &corev1.ObjectAndRelation{
ResourceAndRelation: &corev1.ObjectAndRelation{
Namespace: "resource",
ObjectId: "someresource",
Relation: "reader",
},
User: &corev1.User{UserOneof: &corev1.User_Userset{Userset: &corev1.ObjectAndRelation{
Subject: &corev1.ObjectAndRelation{
Namespace: "user",
ObjectId: "someuser",
Relation: "...",
}}},
},
}
relationship := tuple.ToRelationship(tpl)

Expand Down Expand Up @@ -321,16 +321,16 @@ func GarbageCollectionByTimeTest(t *testing.T, ds datastore.Datastore) {

// Write a relationship.
tpl := &corev1.RelationTuple{
ObjectAndRelation: &corev1.ObjectAndRelation{
ResourceAndRelation: &corev1.ObjectAndRelation{
Namespace: "resource",
ObjectId: "someresource",
Relation: "reader",
},
User: &corev1.User{UserOneof: &corev1.User_Userset{Userset: &corev1.ObjectAndRelation{
Subject: &corev1.ObjectAndRelation{
Namespace: "user",
ObjectId: "someuser",
Relation: "...",
}}},
},
}
relationship := tuple.ToRelationship(tpl)

Expand Down Expand Up @@ -406,16 +406,16 @@ func ChunkedGarbageCollectionTest(t *testing.T, ds datastore.Datastore) {
var tuples []*corev1.RelationTuple
for i := 0; i < chunkRelationshipCount; i++ {
tpl := &corev1.RelationTuple{
ObjectAndRelation: &corev1.ObjectAndRelation{
ResourceAndRelation: &corev1.ObjectAndRelation{
Namespace: "resource",
ObjectId: fmt.Sprintf("resource-%d", i),
Relation: "reader",
},
User: &corev1.User{UserOneof: &corev1.User_Userset{Userset: &corev1.ObjectAndRelation{
Subject: &corev1.ObjectAndRelation{
Namespace: "user",
ObjectId: "someuser",
Relation: "...",
}}},
},
}
tuples = append(tuples, tpl)
}
Expand Down
27 changes: 11 additions & 16 deletions internal/datastore/mysql/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,20 @@ func (mds *Datastore) loadChanges(
stagedChanges := common.NewChanges()

for rows.Next() {
userset := &core.ObjectAndRelation{}
tpl := &core.RelationTuple{
ObjectAndRelation: &core.ObjectAndRelation{},
User: &core.User{
UserOneof: &core.User_Userset{
Userset: userset,
},
},
nextTuple := &core.RelationTuple{
ResourceAndRelation: &core.ObjectAndRelation{},
Subject: &core.ObjectAndRelation{},
}

var createdTxn uint64
var deletedTxn uint64
err = rows.Scan(
&tpl.ObjectAndRelation.Namespace,
&tpl.ObjectAndRelation.ObjectId,
&tpl.ObjectAndRelation.Relation,
&userset.Namespace,
&userset.ObjectId,
&userset.Relation,
&nextTuple.ResourceAndRelation.Namespace,
&nextTuple.ResourceAndRelation.ObjectId,
&nextTuple.ResourceAndRelation.Relation,
&nextTuple.Subject.Namespace,
&nextTuple.Subject.ObjectId,
&nextTuple.Subject.Relation,
&createdTxn,
&deletedTxn,
)
Expand All @@ -140,11 +135,11 @@ func (mds *Datastore) loadChanges(
}

if createdTxn > afterRevision && createdTxn <= newRevision {
stagedChanges.AddChange(ctx, revisionFromTransaction(createdTxn), tpl, core.RelationTupleUpdate_TOUCH)
stagedChanges.AddChange(ctx, revisionFromTransaction(createdTxn), nextTuple, core.RelationTupleUpdate_TOUCH)
}

if deletedTxn > afterRevision && deletedTxn <= newRevision {
stagedChanges.AddChange(ctx, revisionFromTransaction(deletedTxn), tpl, core.RelationTupleUpdate_DELETE)
stagedChanges.AddChange(ctx, revisionFromTransaction(deletedTxn), nextTuple, core.RelationTupleUpdate_DELETE)
}
}
if err = rows.Err(); err != nil {
Expand Down
20 changes: 10 additions & 10 deletions internal/datastore/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,16 @@ func GarbageCollectionTest(t *testing.T, ds datastore.Datastore) {

// Write a relationship.
tpl := &core.RelationTuple{
ObjectAndRelation: &core.ObjectAndRelation{
ResourceAndRelation: &core.ObjectAndRelation{
Namespace: "resource",
ObjectId: "someresource",
Relation: "reader",
},
User: &core.User{UserOneof: &core.User_Userset{Userset: &core.ObjectAndRelation{
Subject: &core.ObjectAndRelation{
Namespace: "user",
ObjectId: "someuser",
Relation: "...",
}}},
},
}
relationship := tuple.ToRelationship(tpl)

Expand Down Expand Up @@ -312,16 +312,16 @@ func GarbageCollectionByTimeTest(t *testing.T, ds datastore.Datastore) {

// Write a relationship.
tpl := &core.RelationTuple{
ObjectAndRelation: &core.ObjectAndRelation{
ResourceAndRelation: &core.ObjectAndRelation{
Namespace: "resource",
ObjectId: "someresource",
Relation: "reader",
},
User: &core.User{UserOneof: &core.User_Userset{Userset: &core.ObjectAndRelation{
Subject: &core.ObjectAndRelation{
Namespace: "user",
ObjectId: "someuser",
Relation: "...",
}}},
},
}
relationship := tuple.ToRelationship(tpl)

Expand Down Expand Up @@ -396,16 +396,16 @@ func ChunkedGarbageCollectionTest(t *testing.T, ds datastore.Datastore) {
var tpls []*core.RelationTuple
for i := 0; i < chunkRelationshipCount; i++ {
tpl := &core.RelationTuple{
ObjectAndRelation: &core.ObjectAndRelation{
ResourceAndRelation: &core.ObjectAndRelation{
Namespace: "resource",
ObjectId: fmt.Sprintf("resource-%d", i),
Relation: "reader",
},
User: &core.User{UserOneof: &core.User_Userset{Userset: &core.ObjectAndRelation{
Subject: &core.ObjectAndRelation{
Namespace: "user",
ObjectId: "someuser",
Relation: "...",
}}},
},
}
tpls = append(tpls, tpl)
}
Expand Down Expand Up @@ -611,7 +611,7 @@ func BenchmarkPostgresQuery(b *testing.B) {
defer iter.Close()

for tpl := iter.Next(); tpl != nil; tpl = iter.Next() {
require.Equal(testfixtures.DocumentNS.Name, tpl.ObjectAndRelation.Namespace)
require.Equal(testfixtures.DocumentNS.Name, tpl.ResourceAndRelation.Namespace)
}
require.NoError(iter.Err())
}
Expand Down
27 changes: 11 additions & 16 deletions internal/datastore/postgres/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,25 +118,20 @@ func (pgd *pgDatastore) loadChanges(
stagedChanges := common.NewChanges()

for rows.Next() {
userset := &core.ObjectAndRelation{}
tpl := &core.RelationTuple{
ObjectAndRelation: &core.ObjectAndRelation{},
User: &core.User{
UserOneof: &core.User_Userset{
Userset: userset,
},
},
nextTuple := &core.RelationTuple{
ResourceAndRelation: &core.ObjectAndRelation{},
Subject: &core.ObjectAndRelation{},
}

var createdTxn uint64
var deletedTxn uint64
err = rows.Scan(
&tpl.ObjectAndRelation.Namespace,
&tpl.ObjectAndRelation.ObjectId,
&tpl.ObjectAndRelation.Relation,
&userset.Namespace,
&userset.ObjectId,
&userset.Relation,
&nextTuple.ResourceAndRelation.Namespace,
&nextTuple.ResourceAndRelation.ObjectId,
&nextTuple.ResourceAndRelation.Relation,
&nextTuple.Subject.Namespace,
&nextTuple.Subject.ObjectId,
&nextTuple.Subject.Relation,
&createdTxn,
&deletedTxn,
)
Expand All @@ -145,11 +140,11 @@ func (pgd *pgDatastore) loadChanges(
}

if createdTxn > afterRevision && createdTxn <= newRevision {
stagedChanges.AddChange(ctx, revisionFromTransaction(createdTxn), tpl, core.RelationTupleUpdate_TOUCH)
stagedChanges.AddChange(ctx, revisionFromTransaction(createdTxn), nextTuple, core.RelationTupleUpdate_TOUCH)
}

if deletedTxn > afterRevision && deletedTxn <= newRevision {
stagedChanges.AddChange(ctx, revisionFromTransaction(deletedTxn), tpl, core.RelationTupleUpdate_DELETE)
stagedChanges.AddChange(ctx, revisionFromTransaction(deletedTxn), nextTuple, core.RelationTupleUpdate_DELETE)
}
}
if err = rows.Err(); err != nil {
Expand Down
14 changes: 5 additions & 9 deletions internal/datastore/proxy/hedging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,19 +295,15 @@ func TestDatastoreE2E(t *testing.T) {

expectedTuples := []*core.RelationTuple{
{
ObjectAndRelation: &core.ObjectAndRelation{
ResourceAndRelation: &core.ObjectAndRelation{
Namespace: "test",
ObjectId: "test",
Relation: "test",
},
User: &core.User{
UserOneof: &core.User_Userset{
Userset: &core.ObjectAndRelation{
Namespace: "test",
ObjectId: "test",
Relation: "test",
},
},
Subject: &core.ObjectAndRelation{
Namespace: "test",
ObjectId: "test",
Relation: "test",
},
},
}
Expand Down
21 changes: 8 additions & 13 deletions internal/datastore/spanner/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,16 @@ func queryExecutor(txSource txFactory) common.ExecuteQueryFunc {

if err := iter.Do(func(row *spanner.Row) error {
nextTuple := &core.RelationTuple{
ObjectAndRelation: &core.ObjectAndRelation{},
User: &core.User{
UserOneof: &core.User_Userset{
Userset: &core.ObjectAndRelation{},
},
},
ResourceAndRelation: &core.ObjectAndRelation{},
Subject: &core.ObjectAndRelation{},
}
userset := nextTuple.User.GetUserset()
err := row.Columns(
&nextTuple.ObjectAndRelation.Namespace,
&nextTuple.ObjectAndRelation.ObjectId,
&nextTuple.ObjectAndRelation.Relation,
&userset.Namespace,
&userset.ObjectId,
&userset.Relation,
&nextTuple.ResourceAndRelation.Namespace,
&nextTuple.ResourceAndRelation.ObjectId,
&nextTuple.ResourceAndRelation.Relation,
&nextTuple.Subject.Namespace,
&nextTuple.Subject.ObjectId,
&nextTuple.Subject.Relation,
)
if err != nil {
return err
Expand Down
Loading