Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
import com.mongodb.stitch.android.services.mongodb.remote.RemoteMongoCollection;
import com.mongodb.stitch.core.auth.providers.serverapikey.ServerApiKeyCredential;
import com.mongodb.stitch.core.internal.common.BsonUtils;
import com.mongodb.stitch.core.services.mongodb.remote.RemoteDeleteResult;
import com.mongodb.stitch.core.services.mongodb.remote.sync.ChangeEventListener;
import com.mongodb.stitch.core.services.mongodb.remote.sync.DefaultSyncConflictResolvers;
import com.mongodb.stitch.core.services.mongodb.remote.sync.SyncDeleteResult;
import com.mongodb.stitch.core.services.mongodb.remote.sync.internal.ChangeEvent;

import java.util.ArrayList;
Expand All @@ -53,7 +53,6 @@

import org.bson.BsonObjectId;
import org.bson.BsonRegularExpression;
import org.bson.BsonString;
import org.bson.BsonValue;
import org.bson.Document;
import org.bson.codecs.configuration.CodecRegistries;
Expand Down Expand Up @@ -127,7 +126,7 @@ public void updateChecked(final ObjectId itemId, final boolean isChecked) {
updateDoc.append("$unset", new Document(TodoItem.Fields.DONE_DATE, ""));
}

items.sync().updateOneById(new BsonObjectId(itemId), updateDoc);
items.sync().updateOne(new Document("_id", itemId), updateDoc);
}

@Override
Expand Down Expand Up @@ -358,8 +357,8 @@ private void addTodoItem(final String task) {

private void updateTodoItemTask(final ObjectId itemId, final String newTask) {
final BsonObjectId docId = new BsonObjectId(itemId);
items.sync().updateOneById(
docId,
items.sync().updateOne(
new Document("_id", docId),
new Document("$set", new Document(TodoItem.Fields.TASK, newTask)))
.addOnSuccessListener(result -> {
items.sync().find(new Document("_id", docId)).first()
Expand All @@ -375,11 +374,11 @@ private void updateTodoItemTask(final ObjectId itemId, final String newTask) {
}

private void clearCheckedTodoItems() {
final List<Task<RemoteDeleteResult>> tasks = new ArrayList<>();
final List<Task<SyncDeleteResult>> tasks = new ArrayList<>();
getItems().addOnSuccessListener(todoItems -> {
for (final TodoItem item : todoItems) {
if (item.isChecked()) {
tasks.add(items.sync().deleteOneById(new BsonObjectId(item.getId())));
tasks.add(items.sync().deleteOne(new Document("_id", item.getId())));
}
}
Tasks.whenAllComplete(tasks)
Expand All @@ -388,17 +387,17 @@ private void clearCheckedTodoItems() {
}

private void clearAllTodoItems() {
final List<Task<RemoteDeleteResult>> tasks = new ArrayList<>();
final List<Task<SyncDeleteResult>> tasks = new ArrayList<>();
getItems().addOnSuccessListener(todoItems -> {
for (final TodoItem item : todoItems) {
tasks.add(items.sync().deleteOneById(new BsonObjectId(item.getId())));
tasks.add(items.sync().deleteOne(new Document("_id", item.getId())));
}
Tasks.whenAllComplete(tasks)
.addOnCompleteListener(task -> todoAdapter.clearItems());
});
}

private void touchList() {
lists.sync().updateOneById(new BsonString(userId), new Document("$inc", new Document("i", 1)));
lists.sync().updateOne(new Document("_id", userId), new Document("$inc", new Document("i", 1)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ import com.mongodb.stitch.core.services.mongodb.remote.RemoteUpdateResult
import com.mongodb.stitch.core.services.mongodb.remote.sync.ChangeEventListener
import com.mongodb.stitch.core.services.mongodb.remote.sync.ConflictHandler
import com.mongodb.stitch.core.services.mongodb.remote.sync.ErrorListener
import com.mongodb.stitch.core.services.mongodb.remote.sync.SyncDeleteResult
import com.mongodb.stitch.core.services.mongodb.remote.sync.SyncInsertManyResult
import com.mongodb.stitch.core.services.mongodb.remote.sync.SyncInsertOneResult
import com.mongodb.stitch.core.services.mongodb.remote.sync.SyncUpdateOptions
import com.mongodb.stitch.core.services.mongodb.remote.sync.SyncUpdateResult
import com.mongodb.stitch.core.services.mongodb.remote.sync.internal.DataSynchronizer
import com.mongodb.stitch.core.testutils.BaseStitchIntTest
import com.mongodb.stitch.core.testutils.sync.ProxyRemoteMethods
Expand Down Expand Up @@ -71,20 +76,36 @@ class SyncMongoClientIntTests : BaseStitchAndroidIntTest(), SyncIntTestRunner {
sync.syncOne(id)
}

override fun insertOneAndSync(document: Document): RemoteInsertOneResult {
override fun count(filter: Bson): Long {
return Tasks.await(sync.count(filter))
}

override fun aggregate(pipeline: List<Bson>): Iterable<Document?> {
return Tasks.await(sync.aggregate(pipeline).into(mutableListOf<Document>()))
}

override fun insertOneAndSync(document: Document): SyncInsertOneResult {
return Tasks.await(sync.insertOneAndSync(document))
}

override fun findOneById(id: BsonValue): Document? {
return Tasks.await(sync.findOneById(id))
override fun insertManyAndSync(documents: List<Document>): SyncInsertManyResult {
return Tasks.await(sync.insertManyAndSync(documents))
}

override fun updateOne(filter: Bson, update: Bson, updateOptions: SyncUpdateOptions): SyncUpdateResult {
return Tasks.await(sync.updateOne(filter, update, updateOptions))
}

override fun updateOneById(documentId: BsonValue, update: Bson): RemoteUpdateResult {
return Tasks.await(sync.updateOneById(documentId, update))
override fun updateMany(filter: Bson, update: Bson, updateOptions: SyncUpdateOptions): SyncUpdateResult {
return Tasks.await(sync.updateMany(filter, update, updateOptions))
}

override fun deleteOneById(documentId: BsonValue): RemoteDeleteResult {
return Tasks.await(sync.deleteOneById(documentId))
override fun deleteOne(filter: Bson): SyncDeleteResult {
return Tasks.await(sync.deleteOne(filter))
}

override fun deleteMany(filter: Bson): SyncDeleteResult {
return Tasks.await(sync.deleteMany(filter))
}

override fun desyncOne(id: BsonValue) {
Expand Down Expand Up @@ -277,11 +298,6 @@ class SyncMongoClientIntTests : BaseStitchAndroidIntTest(), SyncIntTestRunner {
testProxy.testInsertInsertConflict()
}

@Test
override fun testPausedDocumentConfig() {
testProxy.testPausedDocumentConfig()
}

@Test
override fun testConfigure() {
}
Expand Down Expand Up @@ -321,6 +337,26 @@ class SyncMongoClientIntTests : BaseStitchAndroidIntTest(), SyncIntTestRunner {
testProxy.testResumeSyncForDocumentResumesSync()
}

@Test
override fun testReadsBeforeAndAfterSync() {
testProxy.testReadsBeforeAndAfterSync()
}

@Test
override fun testInsertManyNoConflicts() {
testProxy.testInsertManyNoConflicts()
}

@Test
override fun testUpdateManyNoConflicts() {
testProxy.testUpdateManyNoConflicts()
}

@Test
override fun testDeleteManyNoConflicts() {
testProxy.testDeleteManyNoConflicts()
}

/**
* Get the uri for where mongodb is running locally.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@
import android.support.annotation.Nullable;

import com.google.android.gms.tasks.Task;
import com.mongodb.stitch.core.services.mongodb.remote.RemoteDeleteResult;
import com.mongodb.stitch.core.services.mongodb.remote.RemoteInsertOneResult;
import com.mongodb.stitch.core.services.mongodb.remote.RemoteUpdateResult;
import com.mongodb.stitch.core.services.mongodb.remote.sync.ChangeEventListener;
import com.mongodb.stitch.core.services.mongodb.remote.sync.ConflictHandler;
import com.mongodb.stitch.core.services.mongodb.remote.sync.ErrorListener;
import com.mongodb.stitch.core.services.mongodb.remote.sync.SyncCountOptions;
import com.mongodb.stitch.core.services.mongodb.remote.sync.SyncDeleteResult;
import com.mongodb.stitch.core.services.mongodb.remote.sync.SyncInsertManyResult;
import com.mongodb.stitch.core.services.mongodb.remote.sync.SyncInsertOneResult;
import com.mongodb.stitch.core.services.mongodb.remote.sync.SyncUpdateOptions;
import com.mongodb.stitch.core.services.mongodb.remote.sync.SyncUpdateResult;

import java.util.List;
import java.util.Set;

import org.bson.BsonValue;
Expand Down Expand Up @@ -103,14 +107,40 @@ void configure(@NonNull final ConflictHandler<DocumentT> conflictHandler,
boolean resumeSyncForDocument(@NonNull final BsonValue documentId);

/**
* Finds all documents in the collection that have been synchronized from the remote.
* Counts the number of documents in the collection that have been synchronized with the remote.
*
* @return the number of documents in the collection
*/
Task<Long> count();

/**
* Counts the number of documents in the collection that have been synchronized with the remote
* according to the given options.
*
* @param filter the query filter
* @return the number of documents in the collection
*/
Task<Long> count(final Bson filter);

/**
* Counts the number of documents in the collection that have been synchronized with the remote
* according to the given options.
*
* @param filter the query filter
* @param options the options describing the count
* @return the number of documents in the collection
*/
Task<Long> count(final Bson filter, final SyncCountOptions options);

/**
* Finds all documents in the collection that have been synchronized with the remote.
*
* @return the find iterable interface
*/
SyncFindIterable<DocumentT> find();

/**
* Finds all documents in the collection that have been synchronized from the remote.
* Finds all documents in the collection that have been synchronized with the remote.
*
* @param resultClass the class to decode each document into
* @param <ResultT> the target document type of the iterable.
Expand All @@ -119,67 +149,138 @@ void configure(@NonNull final ConflictHandler<DocumentT> conflictHandler,
<ResultT> SyncFindIterable<ResultT> find(final Class<ResultT> resultClass);

/**
* Finds all documents in the collection that have been synchronized from the remote.
* Finds all documents in the collection that have been synchronized with the remote.
*
* @param filter the query filter
* @return the find iterable interface
*/
SyncFindIterable<DocumentT> find(final Bson filter);

/**
* Finds all documents in the collection that have been synchronized from the remote.
* Finds all documents in the collection that have been synchronized with the remote.
*
* @param filter the query filter
* @param resultClass the class to decode each document into
* @param <ResultT> the target document type of the iterable.
* @return the find iterable interface
*/
<ResultT> SyncFindIterable<ResultT> find(final Bson filter, final Class<ResultT> resultClass);
<ResultT> SyncFindIterable<ResultT> find(
final Bson filter,
final Class<ResultT> resultClass);


/**
* Finds a single document by the given id. It is first searched for in the local synchronized
* cache and if not found and there is internet connectivity, it is searched for remotely.
* Aggregates documents that have been synchronized with the remote
* according to the specified aggregation pipeline.
*
* @param documentId the _id of the document to search for.
* @return a task containing the document if found locally or remotely.
* @param pipeline the aggregation pipeline
* @return an iterable containing the result of the aggregation operation
*/
Task<DocumentT> findOneById(final BsonValue documentId);
SyncAggregateIterable<DocumentT> aggregate(final List<? extends Bson> pipeline);

/**
* Finds a single document by the given id. It is first searched for in the local synchronized
* cache and if not found and there is internet connectivity, it is searched for remotely.
* Aggregates documents that have been synchronized with the remote
* according to the specified aggregation pipeline.
*
* @param documentId the _id of the document to search for.
* @param pipeline the aggregation pipeline
* @param resultClass the class to decode each document into
* @param <ResultT> the target document type of the iterable.
* @return a task containing the document if found locally or remotely.
* @return an iterable containing the result of the aggregation operation
*/
<ResultT> SyncAggregateIterable<ResultT> aggregate(
final List<? extends Bson> pipeline,
final Class<ResultT> resultClass);

/**
* Inserts the provided document. If the document is missing an identifier, one will be
* generated. Begin synchronizating on the document's id.
*
* @param document the document to insert
* @return the result of the insert one operation
*/
Task<SyncInsertOneResult> insertOneAndSync(final DocumentT document);

/**
* Inserts one or more documents. If the documents are missing an identifier, they will be
* generated. Begin synchronizing on the documents' ids.
*
* @param documents the documents to insert
* @return the result of the insert many operation
*/
Task<SyncInsertManyResult> insertManyAndSync(final List<DocumentT> documents);

/**
* Removes at most one document that has been synchronized with the remote
* from the collection that matches the given filter. If no
* documents match, the collection is not modified.
*
* @param filter the query filter to apply the the delete operation
* @return the result of the remove one operation
*/
Task<SyncDeleteResult> deleteOne(final Bson filter);

/**
* Removes all documents from the collection that have been synchronized with the remote
* that match the given query filter. If no documents match, the collection is not modified.
*
* @param filter the query filter to apply the the delete operation
* @return the result of the remove many operation
*/
Task<SyncDeleteResult> deleteMany(final Bson filter);

/**
* Update a single document that has been synchronized with the remote
* in the collection according to the specified arguments. If the update results in an upsert,
* the newly upserted document will automatically become synchronized.
*
* @param filter a document describing the query filter, which may not be null.
* @param update a document describing the update, which may not be null. The update to
* apply must include only update operators.
* @return the result of the update one operation
*/
<ResultT> Task<ResultT> findOneById(final BsonValue documentId, final Class<ResultT> resultClass);
Task<SyncUpdateResult> updateOne(final Bson filter, final Bson update);

/**
* Updates a document by the given id. It is first searched for in the local synchronized cache
* and if not found and there is internet connectivity, it is searched for remotely.
* Update a single document that has been synchronized with the remote
* in the collection according to the specified arguments. If the update results in an upsert,
* the newly upserted document will automatically become synchronized.
*
* @param documentId the _id of the document to search for.
* @param update the update specifier.
* @return a task containing the result of the local or remote update.
* @param filter a document describing the query filter, which may not be null.
* @param update a document describing the update, which may not be null. The update to
* apply must include only update operators.
* @param updateOptions the options to apply to the update operation
* @return the result of the update one operation
*/
Task<RemoteUpdateResult> updateOneById(final BsonValue documentId, final Bson update);
Task<SyncUpdateResult> updateOne(
final Bson filter,
final Bson update,
final SyncUpdateOptions updateOptions);

/**
* Inserts a single document and begins to synchronize it.
* Update all documents that have been synchronized with the remote
* in the collection according to the specified arguments. If the update results in an upsert,
* the newly upserted document will automatically become synchronized.
*
* @param document the document to insert and synchronize.
* @return the result of the insertion.
* @param filter a document describing the query filter, which may not be null.
* @param update a document describing the update, which may not be null. The update to
* apply must include only update operators.
* @return the result of the update many operation
*/
Task<RemoteInsertOneResult> insertOneAndSync(final DocumentT document);
Task<SyncUpdateResult> updateMany(final Bson filter, final Bson update);

/**
* Deletes a single document by the given id. It is first searched for in the local synchronized
* cache and if not found and there is internet connectivity, it is searched for remotely.
* Update all documents that have been synchronized with the remote
* in the collection according to the specified arguments. If the update results in an upsert,
* the newly upserted document will automatically become synchronized.
*
* @param documentId the _id of the document to search for.
* @return a task containing the result of the local or remote update.
* @param filter a document describing the query filter, which may not be null.
* @param update a document describing the update, which may not be null. The update to
* apply must include only update operators.
* @param updateOptions the options to apply to the update operation
* @return the result of the update many operation
*/
Task<RemoteDeleteResult> deleteOneById(final BsonValue documentId);
Task<SyncUpdateResult> updateMany(
final Bson filter,
final Bson update,
final SyncUpdateOptions updateOptions);
}
Loading