-
Notifications
You must be signed in to change notification settings - Fork 53
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
docs: Document client interfaces in client/db.go #1305
Changes from all commits
aed4040
b6cef50
d3719aa
b31415f
73b25a8
81f633d
3a62c2f
c22364c
818ee77
0b95bc5
a8228f4
4aa0e4d
e7d6139
655650b
edaa039
43c1fb4
c7f689b
70b1419
cd5b0f5
05e8695
961ad62
818e175
63bc34f
f44c0e2
16ffc02
3001b2d
428cb45
8fdac05
a52060f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,30 +19,75 @@ import ( | |
"github.com/sourcenetwork/defradb/events" | ||
) | ||
|
||
// DB is the primary public programmatic access point to the local DefraDB instance. | ||
// | ||
// It should be constructed via the [db] package, via the [db.NewDB] function. | ||
type DB interface { | ||
// Store contains DefraDB functions protected by an internal, short-lived, transaction, allowing safe | ||
// access to common database read and write operations. | ||
Store | ||
|
||
// NewTxn returns a new transaction on the root store that may be managed externally. | ||
// | ||
// It may be used with other functions in the client package. It is not threadsafe. | ||
NewTxn(context.Context, bool) (datastore.Txn, error) | ||
|
||
// NewConcurrentTxn returns a new transaction on the root store that may be managed externally. | ||
// | ||
// It may be used with other functions in the client package. It is threadsafe and mutliple threads/Go routines | ||
// can safely operate on it concurrently. | ||
NewConcurrentTxn(context.Context, bool) (datastore.Txn, error) | ||
|
||
// WithTxn returns a new [client.Store] that respects the given transaction. | ||
WithTxn(datastore.Txn) Store | ||
|
||
// Root returns the underlying root store, within which all data managed by DefraDB is held. | ||
Root() datastore.RootStore | ||
|
||
// Blockstore returns the blockstore, within which all blocks (commits) managed by DefraDB are held. | ||
// | ||
// It sits within the rootstore returned by [Root]. | ||
Blockstore() blockstore.Blockstore | ||
|
||
// Close closes the database instance and releases any resources held. | ||
// | ||
// The behaviour of other functions in this package after this function has been called is undefined | ||
// unless explicitly stated on the function in question. | ||
// | ||
// It does not explicitly clear any data from persisted storage, and a new [DB] instance may typically | ||
// be created after calling this to resume operations on the prior data - this is however dependant on | ||
// the behaviour of the rootstore provided on database instance creation, as this function will Close | ||
// the provided rootstore. | ||
Close(context.Context) | ||
|
||
// Events returns the database event queue. | ||
// | ||
// It may be used to monitor database events - a new event will be yielded for each mutation. | ||
// Note: it does not copy the queue, just the reference to it. | ||
Comment on lines
+63
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. praise: useful note |
||
Events() events.Events | ||
|
||
// MaxTxnRetries returns the number of retries that this DefraDB instance has been configured to | ||
// make in the event of a transaction conflict in certain scenarios. | ||
// | ||
// Currently this is only used within the P2P system and will not affect operations initiated by users. | ||
MaxTxnRetries() int | ||
|
||
// PrintDump logs the entire contents of the rootstore (all the data managed by this DefraDB instance). | ||
// | ||
// It is likely unwise to call this on a large database instance. | ||
PrintDump(ctx context.Context) error | ||
} | ||
|
||
// Store contains the core DefraDB read-write operations. | ||
type Store interface { | ||
// P2P holds the P2P related methods that must be implemented by the database. | ||
P2P | ||
|
||
// AddSchema takes the provided GQL schema in SDL format, and applies it to the [Store], | ||
// creating the necessary collections, request types, etc. | ||
// | ||
// All schema types provided must not exist prior to calling this, and they may not reference existing | ||
// types previously defined. | ||
Comment on lines
+89
to
+90
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thought(out of scope): Reading this I realize that we should probably allow new schema to reference existing ones. For example, I may want to add an |
||
AddSchema(context.Context, string) error | ||
|
||
// PatchSchema takes the given JSON patch string and applies it to the set of CollectionDescriptions | ||
|
@@ -61,6 +106,10 @@ type Store interface { | |
// [FieldKindStringToEnumMapping]. | ||
PatchSchema(context.Context, string) error | ||
|
||
// CreateCollection creates a new collection using the given description. | ||
// | ||
// WARNING: It does not currently update the GQL types, and as such a database restart is required after | ||
// calling this if use of the new collection via GQL is desired (for example via [ExecRequest]). | ||
CreateCollection(context.Context, CollectionDescription) (Collection, error) | ||
Comment on lines
+109
to
113
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. though: Seeing this warning makes me think that maybe this function shouldn't be exposed publicly. Unless I'm missing something, we don't use this function anywhere internally. We only use the private version of it for the adding schemas. It feels like giving users a chance to do something they may not want to. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is still nominally useful, and my preference long term is to fix both this and UpdateCollection so that they do actually update the GQL types. That said, I think the lack of references might be quite new - I think they were referenced until the client txn rework, but I'm not 100% sure. I have just spotted that UpdateCollection is missing this warning and should have it too
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we remove them until they do update the GQL types? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont really think so, they will still work (on restart) and leaving them in documented like this should cause no harm IMO. Can even be fixed in 0.5.1 if we want I think, as it wouldnt quite be a breaking change IMO There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've merged, but happy to remove these if that is what we decide upon |
||
|
||
// UpdateCollection updates the persisted collection description matching the name of the given | ||
|
@@ -71,6 +120,9 @@ type Store interface { | |
// The collection (including the schema version ID) will only be updated if any changes have actually | ||
// been made, if the given description matches the current persisted description then no changes will be | ||
// applied. | ||
// | ||
// WARNING: It does not currently update the GQL types, and as such a database restart is required after | ||
// calling this if use of the new collection via GQL is desired (for example via [ExecRequest]). | ||
UpdateCollection(context.Context, CollectionDescription) (Collection, error) | ||
|
||
// ValidateUpdateCollection validates that the given collection description is a valid update. | ||
|
@@ -79,20 +131,51 @@ type Store interface { | |
// collection. Will return an error if it fails validation. | ||
ValidateUpdateCollection(context.Context, CollectionDescription) (bool, error) | ||
|
||
// GetCollectionByName attempts to retrieve a collection matching the given name. | ||
// | ||
// If no matching collection is found an error will be returned. | ||
GetCollectionByName(context.Context, string) (Collection, error) | ||
|
||
// GetCollectionBySchemaID attempts to retrieve a collection matching the given schema ID. | ||
// | ||
// If no matching collection is found an error will be returned. | ||
GetCollectionBySchemaID(context.Context, string) (Collection, error) | ||
|
||
// GetCollectionBySchemaID attempts to retrieve a collection matching the given schema version ID. | ||
// | ||
// If no matching collection is found an error will be returned. | ||
GetCollectionByVersionID(context.Context, string) (Collection, error) | ||
|
||
// GetAllCollections returns all the collections and their descriptions that currently exist within | ||
// this [Store]. | ||
GetAllCollections(context.Context) ([]Collection, error) | ||
|
||
// ExecRequest executes the given GQL request against the [Store]. | ||
ExecRequest(context.Context, string) *RequestResult | ||
} | ||
|
||
// GQLResult represents the immediate results of a GQL request. | ||
// | ||
// It does not handle subscription channels. This object and its children are json serializable. | ||
type GQLResult struct { | ||
// Errors contains any errors generated whilst attempting to execute the request. | ||
shahzadlone marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// | ||
// If there are values in this slice the request will likely not have run to completion | ||
// and [Data] will be nil. | ||
Errors []any `json:"errors,omitempty"` | ||
Data any `json:"data"` | ||
|
||
// Data contains the resultant data produced by the GQL request. | ||
// | ||
// It will be nil if any errors were raised during execution. | ||
Data any `json:"data"` | ||
} | ||
|
||
// RequestResult represents the results of a GQL request. | ||
type RequestResult struct { | ||
// GQL contains the immediate results of the GQL request. | ||
GQL GQLResult | ||
|
||
// Pub contains a pointer to an event stream which channels any subscription results | ||
// if the request was a GQL subscription. | ||
Pub *events.Publisher[events.Update] | ||
} |
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.
nitpick: idk if we care lol but I realize this is just American vs UK spelling diffs at this point.
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.
https://ruggedthuglife.com/canada/how-do-you-spell-behaviour-in-canada/ I'm fairly sure we never agreed to use US English, but I might be wrong.
https://www.merriam-webster.com/words-at-play/spelling-variants-dependent-vs-dependant also has similar issues :)
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.
UK English always. Not sure why Americans need to try to be special with everything...