pub struct Ditto { /* private fields */ }Expand description
The entrypoint for accessing all Ditto functionality.
Use the Ditto object to access all other Ditto APIs, such as:
ditto.store()to access theStoreAPI and read and write data on this peerditto.sync()to access theSyncAPI and sync data with other peersditto.presence()to access thePresenceAPI and inspect connected peersditto.small_peer_info()to access theSmallPeerInfoAPI and manage peer metadataditto.disk_usage()to access theDiskUsageAPI and inspect disk usage
Implementations§
Source§impl Ditto
impl Ditto
Sourcepub async fn open(config: DittoConfig) -> Result<Ditto, DittoError>
pub async fn open(config: DittoConfig) -> Result<Ditto, DittoError>
Open a new Ditto instance using a DittoConfig
§Example
// Load your database ID somehow, ENV is a good option
let database_id = std::env::var("DITTO_DATABASE_ID")?;
// Choose one of the following types of connection config
let connect = DittoConfigConnect::Server {
url: "https://example.com/your-server-url".parse().unwrap(),
};
let connect = DittoConfigConnect::SmallPeersOnly {
private_key: Some("https://example.com/your-server-url".bytes().collect()),
};
let connect = DittoConfigConnect::SmallPeersOnly { private_key: None };
let config = DittoConfig::new(database_id, connect);
let ditto = Ditto::open(config).await?;Sourcepub fn open_sync(config: DittoConfig) -> Result<Ditto, DittoError>
pub fn open_sync(config: DittoConfig) -> Result<Ditto, DittoError>
Open a new Ditto instance using a DittoConfig
This is a synchronous blocking variant of Ditto::open() that will wait until
initialization is complete.
§Example
// Load your database ID somehow, ENV is a good option
let database_id = std::env::var("DITTO_DATABASE_ID")?;
// Choose one of the following types of connection config
let connect = DittoConfigConnect::Server {
url: "https://example.com/your-server-url".parse().unwrap(),
};
let connect = DittoConfigConnect::SmallPeersOnly {
private_key: Some("https://example.com/your-server-url".bytes().collect()),
};
let connect = DittoConfigConnect::SmallPeersOnly { private_key: None };
let config = DittoConfig::new(database_id, connect);
let ditto = Ditto::open_sync(config)?;Source§impl Ditto
impl Ditto
Sourcepub fn set_transport_config(&self, config: TransportConfig)
pub fn set_transport_config(&self, config: TransportConfig)
Set a new TransportConfig and begin syncing over these
transports. Any change to start or stop a specific transport should proceed via providing a
modified configuration to this method.
Sourcepub fn update_transport_config(&self, update: impl FnOnce(&mut TransportConfig))
pub fn update_transport_config(&self, update: impl FnOnce(&mut TransportConfig))
Convenience method to update the current transport config of the receiver.
Invokes the block with a copy of the current transport config which you can alter to your liking. The updated transport config is then set on the receiver.
You may use this method to alter the configuration at any time.
Sync will not begin until ditto.sync().start() is invoked.
§Example
Edit the config by simply mutating the &mut TransportConfig passed
to your callback:
use dittolive_ditto::prelude::*;
// Enable the TCP listener on port 4000
ditto.update_transport_config(|config| {
config.listen.tcp.enabled = true;
config.listen.tcp.interface_ip = "0.0.0.0".to_string();
config.listen.tcp.port = 4000;
});Sourcepub fn transport_config(&self) -> TransportConfig
pub fn transport_config(&self) -> TransportConfig
Returns a snapshot of the currently configured transports.
§Example
let transport_config = ditto.transport_config();
println!("Current transport config: {transport_config:#?}");Source§impl Ditto
impl Ditto
Sourcepub fn set_offline_only_license_token(
&self,
license_token: &str,
) -> Result<(), DittoError>
pub fn set_offline_only_license_token( &self, license_token: &str, ) -> Result<(), DittoError>
Activate an offline Ditto instance by setting a license token.
You cannot initiate sync on an offline
(DittoConfigConnect::SmallPeersOnly)
Ditto instance before you have activated it.
Sourcepub fn set_license_from_env(&self, var_name: &str) -> Result<(), DittoError>
pub fn set_license_from_env(&self, var_name: &str) -> Result<(), DittoError>
Look for a license token from a given environment variable.
Source§impl Ditto
impl Ditto
Sourcepub fn sync(&self) -> &Sync
pub fn sync(&self) -> &Sync
Entrypoint to Ditto’s Sync API for syncing documents between peers.
Sourcepub fn small_peer_info(&self) -> &SmallPeerInfo
pub fn small_peer_info(&self) -> &SmallPeerInfo
Return a reference to the SmallPeerInfo object.
Sourcepub fn absolute_persistence_directory(&self) -> PathBuf
pub fn absolute_persistence_directory(&self) -> PathBuf
The absolute path to the persistence directory used by Ditto to persist data.
This returns the final, resolved absolute file path where Ditto stores its data.
The value depends on what was provided in DittoConfig::persistence_directory.
- If an absolute path was provided, it returns that path unchanged.
- If a relative path was provided, it returns the path resolved relative to the default root directory.
- If no path was provided, it returns the default path using the pattern
{default_root}/ditto-{database-id}where{default_root}corresponds to the default root directory and{database-id}is the Ditto database ID in lowercase.
This property always returns a consistent value throughout the lifetime of the Ditto instance and represents the actual directory being used for persistence.
-
Note: “Database ID” was previously referred to as “App ID” in older versions of the SDK.
-
Note: It is not recommended to directly read or write to this directory as its structure and contents are managed by Ditto and may change in future versions.
-
Note: When
DittoLoggeris enabled, logs may be written to this directory even after a Ditto instance has been deallocated. Please refer to the documentation ofDittoLoggerfor more information. -
See also:
DittoConfig::persistence_directory
Sourcepub fn config(&self) -> DittoConfig
pub fn config(&self) -> DittoConfig
Returns an owned snapshot of the effective DittoConfig as used by the core library.
- Modifying this
DittoConfighas no effect on the active Ditto configuration. - The returned
DittoConfigmay be different than the one passed toDitto::openorDitto::open_syncbecause it will have resolved details such as the absolute path to the persistence directory.
Sourcepub fn set_device_name(&self, name: &str)
pub fn set_device_name(&self, name: &str)
Set a custom identifier for the current device.
When using presence, each remote peer is represented by a
short UTF-8 “device name”. By default this will be a truncated version of the device’s
hostname. It does not need to be unique among peers. Configure the device name before
calling ditto.sync().start(). If it is too long it will be
truncated.
Sourcepub fn presence(&self) -> &Arc<Presence>
pub fn presence(&self) -> &Arc<Presence>
Return a handle to the Presence API to monitor peers’ activity in the Ditto mesh.
§Example
Use ditto.presence().graph() to request a current PresenceGraph of connected peers:
use dittolive_ditto::prelude::*;
let presence_graph: PresenceGraph = ditto.presence().graph();
println!("Ditto mesh right now: {presence_graph:#?}");§Example
Use ditto.presence().register_observer(...) to subscribe to changes in mesh presence:
use dittolive_ditto::prelude::*;
let _observer = ditto.presence().register_observer(|graph| {
println!("Ditto mesh update! {graph:#?}");
});
// The observer is cancelled when dropped.
// In a real application, hold onto it for as long as you need it alive.
drop(_observer);Sourcepub fn disk_usage(&self) -> &DiskUsage
pub fn disk_usage(&self) -> &DiskUsage
Return a DiskUsage to monitor the disk usage of the Ditto
instance. It can be used to retrieve an immediate representation of the Ditto file system:
let fs_tree = ditto.disk_usage().item();Or to bind a callback to the changes:
let handle = ditto.disk_usage().observe(|fs_tree| {
// do something with the graph
});
// The handle must be kept to keep receiving updates on the file system.
// To stop receiving update, drop the handle.Sourcepub fn auth(&self) -> Option<DittoAuthenticator>
pub fn auth(&self) -> Option<DittoAuthenticator>
Returns the current DittoAuthenticator, if it exists.
The DittoAuthenticator is available when using DittoConfigConnect::Server mode.
Sourcepub fn is_activated(&self) -> bool
pub fn is_activated(&self) -> bool
Returns true if this Ditto instance has been activated with a valid
license token.
Source§impl Ditto
impl Ditto
Sourcepub fn run_garbage_collection(&self)
pub fn run_garbage_collection(&self)
Removes all sync metadata for any remote peers which aren’t currently connected. This method shouldn’t usually be called. Manually running garbage collection often will result in slower sync times. Ditto automatically runs a garbage a collection process in the background at optimal times.
Manually running garbage collection is typically only useful during testing if large amounts of data are being generated. Alternatively, if an entire data set is to be evicted and it’s clear that maintaining this metadata isn’t necessary, then garbage collection could be run after evicting the old data.