Struct dittolive_ditto::store::Store
source · pub struct Store { /* private fields */ }
Expand description
Store
provides access to Collection
s and a
write transaction API.
Implementations§
source§impl Store
impl Store
sourcepub fn collection(
&self,
collection_name: &str
) -> Result<Collection, DittoError>
pub fn collection( &self, collection_name: &str ) -> Result<Collection, DittoError>
Returns a Collection
with the provided name.
A collection name is valid if :
- its length is less than 100
- it is not empty
- it does not contain the char ‘\0’
- it does not begin with “$TS_”
sourcepub fn collections(&self) -> PendingCollectionsOperation<'_>
pub fn collections(&self) -> PendingCollectionsOperation<'_>
Returns an object that lets you fetch or observe the collections in the store.
sourcepub fn with_batched_write<F>(
&self,
f: F
) -> Result<Vec<WriteTransactionResult>, DittoError>where
for<'batch> F: FnOnce(ScopedStore<'batch>) -> Action<'batch>,
pub fn with_batched_write<F>( &self, f: F ) -> Result<Vec<WriteTransactionResult>, DittoError>where for<'batch> F: FnOnce(ScopedStore<'batch>) -> Action<'batch>,
Allows you to group multiple operations together that affect multiple documents, potentially across multiple collections, without auto-committing on each operation.
At the end of the batch of operations, either
batch.commit_changes
or
batch.revert_changes
must be called.
Example
ditto.store().with_batched_write(|batch| {
let mut foo_coll = batch.collection("foo");
foo_coll.find...().remove();
let mut bar_coll = batch.collection("bar");
// Expensive multi-mutation op:
for _ in 0 .. 10_000 {
let doc = ...;
bar_coll.insert(doc, None, false);
}
// At this point, we must say whether we commit or revert
// these changes:
batch.commit_changes()
})
sourcepub fn collection_names(&self) -> Result<Vec<String>, DittoError>
pub fn collection_names(&self) -> Result<Vec<String>, DittoError>
Returns a list of the names of collections in the local store.
sourcepub fn queries_hash(
&self,
live_queries: &[LiveQuery]
) -> Result<u64, DittoError>
pub fn queries_hash( &self, live_queries: &[LiveQuery] ) -> Result<u64, DittoError>
Returns a hash representing the current version of the given queries. When a document matching such queries gets mutated, the hash will change as well.
Please note that the hash depends on how queries are constructed, so you should make sure to always compare hashes generated with the same set of queries.
sourcepub fn queries_hash_mnemonic(
&self,
live_queries: &[LiveQuery]
) -> Result<String, DittoError>
pub fn queries_hash_mnemonic( &self, live_queries: &[LiveQuery] ) -> Result<String, DittoError>
Returns a sequence of English words representing the current version of the given queries. When a document matching such queries gets mutated, the words will change as well.
Please note that the resulting sequence of words depends on how queries are constructed, so you should make sure to always compare hashes generated with the same set of queries.
sourcepub fn start_all_live_query_webhooks(&self) -> Result<(), DittoError>
pub fn start_all_live_query_webhooks(&self) -> Result<(), DittoError>
Start all live query webhooks.
sourcepub fn start_live_query_webhook_by_id(
&self,
doc_id: DocumentId
) -> Result<(), DittoError>
pub fn start_live_query_webhook_by_id( &self, doc_id: DocumentId ) -> Result<(), DittoError>
Start a live query webhooks by its id.
sourcepub fn register_live_query_webhook(
&self,
collection_name: &str,
query: &str,
url: &str
) -> Result<DocumentId, DittoError>
pub fn register_live_query_webhook( &self, collection_name: &str, query: &str, url: &str ) -> Result<DocumentId, DittoError>
Register a new live query webhook
sourcepub fn live_query_webhook_generate_new_api_secret(
&self
) -> Result<(), DittoError>
pub fn live_query_webhook_generate_new_api_secret( &self ) -> Result<(), DittoError>
Generate a new API secret for live query webhook
sourcepub fn timeseries(&self, ts_name: &str) -> Result<TimeSeries, DittoError>
pub fn timeseries(&self, ts_name: &str) -> Result<TimeSeries, DittoError>
Returns a TimeSeries
with the provided name.
sourcepub fn disk_usage(&self) -> &DiskUsage
pub fn disk_usage(&self) -> &DiskUsage
sourcepub fn register_observer<Q, F>(
&self,
query: Q,
query_args: Option<QueryArguments>,
on_change: F
) -> Result<Arc<StoreObserver>, DittoError>where
Q: TryInto<Query, Error = DittoError>,
F: ChangeHandler,
pub fn register_observer<Q, F>( &self, query: Q, query_args: Option<QueryArguments>, on_change: F ) -> Result<Arc<StoreObserver>, DittoError>where Q: TryInto<Query, Error = DittoError>, F: ChangeHandler,
Register a handler to be called whenever a query’s results change in the local store.
The returned StoreObserver
must be kept in scope for as long as the change handler
should be called with new change events.
The given query may not modify any documents and must be a valid Ditto Query Language query.
If no on_error
argument is provided, errors that are thrown in the
provided change handler will be logged to the console.
sourcepub fn observers(&self) -> impl '_ + Deref<Target = SetArc<StoreObserver>>
pub fn observers(&self) -> impl '_ + Deref<Target = SetArc<StoreObserver>>
Gets temporary access to the set of currently registered observers.
A (read) lock is held until the return value is dropped: this means
that neither Self::register_observer()
nor
StoreObserver::cancel()
can make progress until this read
lock is released.
sourcepub async fn execute<Q>(
&self,
query: Q,
query_args: Option<QueryArguments>
) -> Result<QueryResult, DittoError>where
Q: TryInto<Query, Error = DittoError>,
pub async fn execute<Q>( &self, query: Q, query_args: Option<QueryArguments> ) -> Result<QueryResult, DittoError>where Q: TryInto<Query, Error = DittoError>,
Executes the given query in the local store and returns the result.
Use placeholders to incorporate values from the optional query_args
parameter into the query. The keys of the QueryArguments
object must
match the placeholders used within the query. You can not use placeholders
in the FROM
clause.
Limitations:
- Supports
SELECT * FROM <collection name>
with optionalWHERE <expression>
- No transactions
sourcepub async fn new_attachment(
&self,
filepath: &(impl ?Sized + AsRef<Path>),
user_data: HashMap<String, String>
) -> Result<DittoAttachment, DittoError>
pub async fn new_attachment( &self, filepath: &(impl ?Sized + AsRef<Path>), user_data: HashMap<String, String> ) -> Result<DittoAttachment, DittoError>
Creates a new attachment, which can then be inserted into a document.
The file residing at the provided path will be copied into the Ditto’s store. The
DittoAttachment
object that is returned is what you can
then use to insert an attachment into a document.
You can provide custom user data about the attachment, which will be replicated to other peers alongside the file attachment.
sourcepub fn fetch_attachment(
&self,
attachment_token: impl DittoAttachmentTokenLike,
on_fetch_event: impl 'static + Send + Sync + Fn(DittoAttachmentFetchEvent)
) -> Result<DittoAttachmentFetcher<'static, V2>, DittoError>
pub fn fetch_attachment( &self, attachment_token: impl DittoAttachmentTokenLike, on_fetch_event: impl 'static + Send + Sync + Fn(DittoAttachmentFetchEvent) ) -> Result<DittoAttachmentFetcher<'static, V2>, DittoError>
Fetches the attachment corresponding to the provided attachment token.
-
attachment_token
: can be either aDittoAttachmentToken
, or a&BTreeMap<CborValue, CborValue>
, that is, the output of aQueryResultItem::value()
once casted.as_object()
. -
on_fetch_event
: A closure that will be called when the status of the request to fetch the attachment has changed. If the attachment is already available then this will be called almost immediately with a completed status value.
The returned DittoAttachmentFetcher
is a handle which is safe to discard, unless you
wish to be able to .cancel()
the fetching operation.
When not explicitly cancelled, the fetching operation will remain active until it either
completes, the attachment is deleted, or the owning Ditto
object is dropped.
sourcepub fn attachment_fetchers(&self) -> Vec<DittoAttachmentFetcher<'static, V2>>
pub fn attachment_fetchers(&self) -> Vec<DittoAttachmentFetcher<'static, V2>>
Gets a copy of the set of currently registered attachment fetchers.
A (read) lock is held during the copy: this contends with Self::fetch_attachment()
and
with DittoAttachmentFetcher::cancel()
.