Class DataSynchronizer
- java.lang.Object
-
- com.mongodb.stitch.core.services.mongodb.remote.sync.internal.DataSynchronizer
-
- All Implemented Interfaces:
NetworkMonitor.StateListener
public class DataSynchronizer extends Object implements NetworkMonitor.StateListener
DataSynchronizer handles the bidirectional synchronization of documents between a local MongoDB and a remote MongoDB (via Stitch). It also expose CRUD operations to interact with synchronized documents.
-
-
Field Summary
Fields Modifier and Type Field Description static String
DOCUMENT_VERSION_FIELD
-
Constructor Summary
Constructors Constructor Description DataSynchronizer(String instanceKey, CoreStitchServiceClient service, MongoClient localClient, CoreRemoteMongoClient remoteClient, NetworkMonitor networkMonitor, AuthMonitor authMonitor, Dispatcher dispatcher)
-
Method Summary
Modifier and Type Method Description void
addWatcher(MongoNamespace namespace, Callback<CompactChangeEvent<BsonDocument>,Object> watcher)
Queues up a callback to be removed and invoked on the next change event.boolean
areAllStreamsOpen()
void
close()
Stops the background data synchronization thread and releases the local client.void
configure(MongoNamespace namespace, SyncConfiguration syncConfiguration)
void
disableListeners()
void
disableSyncThread()
boolean
doSyncPass()
Performs a single synchronization pass in both the local and remote directions; the order of which does not matter.<T> Collection<T>
find(MongoNamespace namespace, BsonDocument filter, int limit, BsonDocument projection, BsonDocument sort, Class<T> resultClass, CodecRegistry codecRegistry)
<T> T
findOne(MongoNamespace namespace, BsonDocument filter, BsonDocument projection, BsonDocument sort, Class<T> resultClass, CodecRegistry codecRegistry)
Set<BsonValue>
getPausedDocumentIds(MongoNamespace namespace)
Return the set of synchronized document _ids in a namespace that have been paused due to an irrecoverable error.com.mongodb.stitch.core.services.mongodb.remote.sync.internal.InstanceSynchronizationConfig
getSyncConfig()
Set<CoreDocumentSynchronizationConfig>
getSynchronizedDocuments(MongoNamespace namespace)
Returns the set of synchronized documents in a namespace.Set<MongoNamespace>
getSynchronizedNamespaces()
Returns the set of synchronized namespaces.boolean
hasNamespaceListener(MongoNamespace namespace)
boolean
isNamespaceListenerOpen(MongoNamespace namespace)
boolean
isRunning()
Whether or not the DataSynchronizer is running in the background.void
onNetworkStateChanged()
void
reinitialize(MongoClient localClient)
void
removeWatcher(MongoNamespace namespace, Callback<CompactChangeEvent<BsonDocument>,Object> watcher)
void
start()
Starts data synchronization in a background thread.void
stop()
Stops the background data synchronization thread.void
syncDocumentsFromRemote(MongoNamespace namespace, BsonValue... documentIds)
Requests that a document be synchronized by the given _id.void
wipeInMemorySettings()
Reloads the synchronization config.
-
-
-
Field Detail
-
DOCUMENT_VERSION_FIELD
public static final String DOCUMENT_VERSION_FIELD
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
DataSynchronizer
public DataSynchronizer(String instanceKey, CoreStitchServiceClient service, MongoClient localClient, CoreRemoteMongoClient remoteClient, NetworkMonitor networkMonitor, AuthMonitor authMonitor, Dispatcher dispatcher)
-
-
Method Detail
-
getSyncConfig
public com.mongodb.stitch.core.services.mongodb.remote.sync.internal.InstanceSynchronizationConfig getSyncConfig()
-
onNetworkStateChanged
public void onNetworkStateChanged()
- Specified by:
onNetworkStateChanged
in interfaceNetworkMonitor.StateListener
-
reinitialize
public void reinitialize(MongoClient localClient)
-
wipeInMemorySettings
public void wipeInMemorySettings()
Reloads the synchronization config. This wipes all in-memory synchronization settings.
-
hasNamespaceListener
public boolean hasNamespaceListener(@Nonnull MongoNamespace namespace)
-
isNamespaceListenerOpen
public boolean isNamespaceListenerOpen(@Nonnull MongoNamespace namespace)
-
configure
public void configure(@Nonnull MongoNamespace namespace, @Nonnull SyncConfiguration syncConfiguration)
-
start
public void start()
Starts data synchronization in a background thread.
-
disableSyncThread
public void disableSyncThread()
-
disableListeners
public void disableListeners()
-
stop
public void stop()
Stops the background data synchronization thread.
-
close
public void close()
Stops the background data synchronization thread and releases the local client.
-
doSyncPass
public boolean doSyncPass()
Performs a single synchronization pass in both the local and remote directions; the order of which does not matter. If switching the order produces different results after one pass, then there is a bug.- Returns:
- whether or not the synchronization pass was successful.
-
addWatcher
public void addWatcher(MongoNamespace namespace, Callback<CompactChangeEvent<BsonDocument>,Object> watcher)
Queues up a callback to be removed and invoked on the next change event.
-
removeWatcher
public void removeWatcher(MongoNamespace namespace, Callback<CompactChangeEvent<BsonDocument>,Object> watcher)
-
getSynchronizedNamespaces
public Set<MongoNamespace> getSynchronizedNamespaces()
Returns the set of synchronized namespaces.- Returns:
- the set of synchronized namespaces.
-
getSynchronizedDocuments
public Set<CoreDocumentSynchronizationConfig> getSynchronizedDocuments(MongoNamespace namespace)
Returns the set of synchronized documents in a namespace.- Parameters:
namespace
- the namespace to get synchronized documents for.- Returns:
- the set of synchronized documents in a namespace.
-
getPausedDocumentIds
public Set<BsonValue> getPausedDocumentIds(MongoNamespace namespace)
Return the set of synchronized document _ids in a namespace that have been paused due to an irrecoverable error.- Parameters:
namespace
- the namespace to get paused document _ids for.- Returns:
- the set of paused document _ids in a namespace
-
syncDocumentsFromRemote
public void syncDocumentsFromRemote(MongoNamespace namespace, BsonValue... documentIds)
Requests that a document be synchronized by the given _id. Actual synchronization of the document will happen later in adoSyncPass()
iteration.- Parameters:
namespace
- the namespace to put the document in.documentIds
- the _ids of the documents.
-
findOne
public <T> T findOne(MongoNamespace namespace, BsonDocument filter, BsonDocument projection, BsonDocument sort, Class<T> resultClass, CodecRegistry codecRegistry)
-
find
public <T> Collection<T> find(MongoNamespace namespace, BsonDocument filter, int limit, BsonDocument projection, BsonDocument sort, Class<T> resultClass, CodecRegistry codecRegistry)
-
isRunning
public boolean isRunning()
Whether or not the DataSynchronizer is running in the background.- Returns:
- true if running, false if not
-
areAllStreamsOpen
public boolean areAllStreamsOpen()
-
-