-
Notifications
You must be signed in to change notification settings - Fork 44
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
refactor: Move instance type to start of key #316
Conversation
Can you expand on what you mean by this a bit? Messy how? |
If the only components of the key are CollectionId and InstanceType PrefixEnd alters the InstanceType component which did odd things - it was easier to put it at the front than track down what exactly was going on. Didn't see any harm in doing so either |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall in favor, some questions/clarifications. Mainly around generating/passing keys (primary vs Datastore)
The one larger point I wanted to make is keeping everything prefixed by CollectionID
. I think its cleanest if both instance and pk
are stored after it. This means a single iterator could be used to syncing/exporting/etc. Without having to worry about 3 different prefixes (pk
, v
, p
).
Ive throw together a POC to make the instancetype work after the collection ID, few random places that needed attention beyond PrefixEnd(): https://github.com/sourcenetwork/defradb/tree/jsimnz/refactor/i313A-instance-type.
Note: perf is the same between both versions (instance type before and after)
core/key.go
Outdated
func (k PrimaryDataStoreKey) ToString() string { | ||
result := "/" + PRIMARY_KEY | ||
|
||
if k.CollectionId != "" { | ||
result = result + "/" + k.CollectionId | ||
} | ||
if k.DocKey != "" { | ||
result = result + "/" + k.DocKey | ||
} | ||
|
||
return result | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel more comfortable having everything scoped under the CollectionID
prefix. Creates the cleanest seperation of k/v pairs. A single iteration under the CollectionID
prefix can be used for exporting, syncing, etc. Instead of now 3 different prefixes to capture all the data related to a given Collection.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those are good reasons, although the code will be a bit messier than in your POC (to avoid the mutation in the getters lol).
This change only affects the last commit - would you mind quickly approving #315 so I can work off develop instead?
- CollectionId first
- Including pk
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Managed to sort out collection id in a fairly localized fashion - 2 new commits Handle range filters in iterable shim and FIXUP - Keep collectionId first in key
db/collection.go
Outdated
func (c *collection) getPrimaryKeyFromDocKey(docKey client.DocKey) core.PrimaryDataStoreKey { | ||
return core.PrimaryDataStoreKey{ | ||
CollectionId: fmt.Sprint(c.colID), | ||
DocKey: docKey.String(), | ||
} | ||
} | ||
|
||
func (c *collection) getDataStoreKeyFrom(key core.PrimaryDataStoreKey) core.DataStoreKey { | ||
return core.DataStoreKey{ | ||
CollectionId: fmt.Sprint(c.colID), | ||
IndexId: fmt.Sprint(c.PrimaryIndex().ID), | ||
DocKey: key.DocKey, | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should there be a direct method to go from DocKey to HeadStore key, instead of having to call
c.getDataStoreKeyFrom(c.getPrimaryKeyFromDocKey(dockey))
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EDIT: I cant actual see what you have described above anywhere - could you clarify please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a comment below :)
ae374f6
to
dd263e4
Compare
Is not doing anything, and is conceptually incorrect
9d7120d
to
25663f2
Compare
Codecov Report
@@ Coverage Diff @@
## develop #316 +/- ##
===========================================
- Coverage 65.07% 65.01% -0.07%
===========================================
Files 80 80
Lines 8979 8974 -5
===========================================
- Hits 5843 5834 -9
- Misses 2520 2528 +8
+ Partials 616 612 -4
|
25663f2
to
ce227c8
Compare
db/collection.go
Outdated
@@ -559,14 +548,14 @@ func (c *collection) save(ctx context.Context, txn datastore.Txn, doc *client.Do | |||
// Loop through doc values | |||
// => instantiate MerkleCRDT objects | |||
// => Set/Publish new CRDT values | |||
dockey := core.DataStoreKeyFromDocKey(doc.Key()) | |||
primaryKey := c.getPrimaryKeyFromDocKey(doc.Key()).ToDataStoreKey() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
== COMMENT HERE ABOUT WEIRD CALL ==
hi :) welcome traveler :)
Anyway, yea this is what I think I was referring to with the other comment you asked clarification on.
Seems like a lot of run around happening when basically we want to go from docKey to DataStoreKey.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
option: c.getDataStoreKeyFromDocKey(...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah okay lol - I don't think I really register call chains in this style as weird :) Super common in C#/JS/Rust(?) lol and I prefer chaining them together instead of risking overly specific helper functions that can obfuscate what is actually happening.
In this case - I do think the current setup is clearer in it's intent - primaryKey only is a datastoreKey here as it needs to have a fieldId tagged on within the subsquent loop. This variable is really a PrimaryKey. Would prefer to change it to primaryKey := c.getPrimaryKeyFromDocKey(doc.Key())
and modify getFieldKey
to make the cast (golangs lack of overloading might make this messy though but will see).
EDIT: code modified as per above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
ce227c8
to
05aabc1
Compare
* Add key type checks * Add PrimaryDataStoreKey * Use primary key over data key in collection * Remove unwanted c.getPrimaryIndexDocKey call Is not doing anything, and is conceptually incorrect * Correct update pk usage * Correct save pk usage * Remove index from key * Handle range filters in iterable shim * Move InstanceType to start of key
Closes #313
Moves InstanceType to base of key (
/[InstanceType]/[CollectionId]/[DocKey]/[FieldId]
), earlier talk suggested it woul go after CollectionId, but that proved messy with the PrefixEnd logic so I moved it to the start. Based off #315 for convenience. Benches suggest 40% improvement vs parent branch (~45% vs dev).