com.mongodb.MongoNamespace Java Examples

The following examples show how to use com.mongodb.MongoNamespace. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NamespaceChangeStreamListener.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
NamespaceChangeStreamListener(
    final MongoNamespace namespace,
    final NamespaceSynchronizationConfig nsConfig,
    final CoreStitchServiceClient service,
    final NetworkMonitor networkMonitor,
    final AuthMonitor authMonitor,
    final ReadWriteLock nsLock
) {
  this.namespace = namespace;
  this.nsConfig = nsConfig;
  this.service = service;
  this.networkMonitor = networkMonitor;
  this.authMonitor = authMonitor;
  this.events = new HashMap<>();
  this.nsLock = nsLock;
  this.logger =
      Loggers.getLogger(
          String.format("NamespaceChangeStreamListener-%s", namespace.toString()));
  this.watchers = new HashSet<>();
}
 
Example #2
Source File: NamespaceSynchronizationConfig.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
static NamespaceSynchronizationConfig fromBsonDocument(final BsonDocument document) {
  keyPresent(ConfigCodec.Fields.NAMESPACE_FIELD, document);
  keyPresent(ConfigCodec.Fields.SCHEMA_VERSION_FIELD, document);

  final int schemaVersion =
      document.getNumber(ConfigCodec.Fields.SCHEMA_VERSION_FIELD).intValue();
  if (schemaVersion != 1) {
    throw new IllegalStateException(
        String.format(
            "unexpected schema version '%d' for %s",
            schemaVersion,
            CoreDocumentSynchronizationConfig.class.getSimpleName()));
  }

  return new NamespaceSynchronizationConfig(
      new MongoNamespace(document.getString(ConfigCodec.Fields.NAMESPACE_FIELD).getValue()));
}
 
Example #3
Source File: ChangeEvent.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs a change event.
 *
 * @param id The id of the change event.
 * @param operationType The operation type represented by the change event.
 * @param fullDocument The full document at some point after the change is applied.
 * @param ns The namespace (database and collection) of the document.
 * @param documentKey The id if the underlying document that changed.
 * @param updateDescription The description of what has changed (for updates only).
 * @param hasUncommittedWrites Whether this represents a local uncommitted write.
 */
public ChangeEvent(
    final BsonDocument id,
    final OperationType operationType,
    final DocumentT fullDocument,
    final MongoNamespace ns,
    final BsonDocument documentKey,
    final UpdateDescription updateDescription,
    final boolean hasUncommittedWrites
) {
  super(
      operationType, fullDocument, documentKey, updateDescription, hasUncommittedWrites
  );

  this.id = id;
  this.ns = ns;
}
 
Example #4
Source File: DataSynchronizer.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
public void configure(@Nonnull final MongoNamespace namespace,
                      @Nonnull final SyncConfiguration syncConfiguration) {
  this.waitUntilInitialized();

  final NamespaceSynchronizationConfig nsConfig = this.syncConfig.getNamespaceConfig(namespace);
  final SyncFrequency prevFrequency = nsConfig.getSyncFrequency() != null
      ? nsConfig.getSyncFrequency()
      : SyncFrequency.onDemand(); // serves as default prev freq since it has no streams or timers

  nsConfig.configure(syncConfiguration);

  syncLock.lock();
  try {
    this.exceptionListener = syncConfiguration.getExceptionListener();
    this.isConfigured = true;
    this.configureSyncFrequency(namespace, syncConfiguration.getSyncFrequency(), prevFrequency);
  } finally {
    syncLock.unlock();
  }

  if (!isRunning) {
    this.start();
  }
}
 
Example #5
Source File: InstanceChangeStreamListenerImpl.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the latest change events for a given namespace.
 *
 * @param namespace the namespace to get events for.
 * @return the latest change events for a given namespace.
 */
public Map<BsonValue, CompactChangeEvent<BsonDocument>> getEventsForNamespace(
    final MongoNamespace namespace
) {
  this.instanceLock.readLock().lock();
  final NamespaceChangeStreamListener streamer;
  try {
    streamer = nsStreamers.get(namespace);
  } finally {
    this.instanceLock.readLock().unlock();
  }
  if (streamer == null) {
    return new HashMap<>();
  }
  return streamer.getEvents();
}
 
Example #6
Source File: ChangeEvents.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Generates a change event for a local replacement of a document in the given namespace referring
 * to the given document _id.
 *
 * @param namespace the namespace where the document was inserted.
 * @param documentId the _id of the document that was updated.
 * @param document the replacement document.
 * @return a change event for a local replacement of a document in the given namespace referring
 *         to the given document _id.
 */
static ChangeEvent<BsonDocument> changeEventForLocalReplace(
    final MongoNamespace namespace,
    final BsonValue documentId,
    final BsonDocument document,
    final boolean writePending
) {
  return new ChangeEvent<>(
      new BsonDocument(),
      OperationType.REPLACE,
      document,
      namespace,
      new BsonDocument("_id", documentId),
      null,
      writePending);
}
 
Example #7
Source File: DataSynchronizer.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Requests that a document be synchronized by the given _id. Actual synchronization of the
 * document will happen later in a {@link DataSynchronizer#doSyncPass()} iteration.
 *
 * @param namespace  the namespace to put the document in.
 * @param documentIds the _ids of the documents.
 */
public void syncDocumentsFromRemote(
    final MongoNamespace namespace,
    final BsonValue... documentIds
) {
  this.waitUntilInitialized();

  try {
    ongoingOperationsGroup.enter();

    if (syncConfig.addSynchronizedDocuments(namespace, documentIds)) {
      checkAndInsertNamespaceListener(namespace);
    }
  } finally {
    ongoingOperationsGroup.exit();
  }
}
 
Example #8
Source File: FindOneAndModifyOperation.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Construct a new instance.
 *
 * @param namespace the database and collection namespace for the operation.
 * @param methodName the name of the findOneAndModify function to run.
 * @param filter the filter to query for the document.
 * @param update the update to apply to the resulting document.
 * @param project the projection operation to apply to the returned document.
 * @param sort the sort to use on the query before selecting the first document.
 * @param decoder the decoder for the result documents.Operations.java
 *
 */
FindOneAndModifyOperation(
        final MongoNamespace namespace,
        final String methodName,
        final BsonDocument filter,
        final BsonDocument update,
        final BsonDocument project,
        final BsonDocument sort,
        final Decoder<T> decoder) {
  notNull("namespace", namespace);
  notNull("methodName", methodName);
  notNull("filter", filter);
  notNull("update", update);
  notNull("decoder", decoder);
  this.namespace = namespace;
  this.methodName = methodName;
  this.filter = filter;
  this.update = update;
  this.project = project;
  this.sort = sort;
  this.decoder = decoder;
}
 
Example #9
Source File: DataSynchronizer.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
public <T> T findOne(
        final MongoNamespace namespace,
        final BsonDocument filter,
        final BsonDocument projection,
        final BsonDocument sort,
        final Class<T> resultClass,
        final CodecRegistry codecRegistry
) {
  this.waitUntilInitialized();

  ongoingOperationsGroup.enter();
  final Lock lock = this.syncConfig.getNamespaceConfig(namespace).getLock().writeLock();
  lock.lock();
  try {
    return getLocalCollection(namespace, resultClass, codecRegistry)
            .find(filter)
            .limit(1)
            .projection(projection)
            .sort(sort)
            .first();
  } finally {
    lock.unlock();
    ongoingOperationsGroup.exit();
  }
}
 
Example #10
Source File: SyncOperations.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
private <ResultT> SyncFindOperation<ResultT> createSyncFindOperation(
    final MongoNamespace findNamespace,
    final Bson filter,
    final Class<ResultT> resultClass,
    final RemoteFindOptions options
) {
  final BsonDocument filterDoc = filter.toBsonDocument(documentClass, codecRegistry);
  final BsonDocument projDoc = BsonUtils.toBsonDocumentOrNull(
      options.getProjection(),
      documentClass,
      codecRegistry);
  final BsonDocument sortDoc = BsonUtils.toBsonDocumentOrNull(
      options.getSort(),
      documentClass,
      codecRegistry);
  return new SyncFindOperation<>(
      findNamespace,
      resultClass,
      dataSynchronizer)
      .filter(filterDoc)
      .limit(options.getLimit())
      .projection(projDoc)
      .sort(sortDoc);
}
 
Example #11
Source File: DataSynchronizer.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
public <T> Collection<T> find(
    final MongoNamespace namespace,
    final BsonDocument filter,
    final int limit,
    final BsonDocument projection,
    final BsonDocument sort,
    final Class<T> resultClass,
    final CodecRegistry codecRegistry
) {
  this.waitUntilInitialized();

  ongoingOperationsGroup.enter();
  try {
    return getLocalCollection(namespace, resultClass, codecRegistry)
        .find(filter)
        .limit(limit)
        .projection(projection)
        .sort(sort)
        .into(new ArrayList<>());
  } finally {
    ongoingOperationsGroup.exit();
  }
}
 
Example #12
Source File: InstanceChangeStreamListenerImpl.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * If there is an unprocessed change event for a particular document ID, fetch it from the
 * appropriate namespace change stream listener, and remove it. By reading the event here, we are
 * assuming it will be processed by the consumer.
 *
 * @return the latest unprocessed change event for the given document ID and namespace, or null
 *         if none exists.
 */
public @Nullable CompactChangeEvent<BsonDocument> getUnprocessedEventForDocumentId(
        final MongoNamespace namespace,
        final BsonValue documentId
) {
  this.instanceLock.readLock().lock();
  final NamespaceChangeStreamListener streamer;
  try {
    streamer = nsStreamers.get(namespace);
  } finally {
    this.instanceLock.readLock().unlock();
  }

  if (streamer == null) {
    return null;
  }

  return streamer.getUnprocessedEventForDocumentId(documentId);
}
 
Example #13
Source File: MongoCopyDataManager.java    From mongo-kafka with Apache License 2.0 6 votes vote down vote up
MongoCopyDataManager(final MongoSourceConfig sourceConfig, final MongoClient mongoClient) {
  this.sourceConfig = sourceConfig;
  this.mongoClient = mongoClient;

  String database = sourceConfig.getString(DATABASE_CONFIG);
  String collection = sourceConfig.getString(COLLECTION_CONFIG);

  List<MongoNamespace> namespaces;
  if (database.isEmpty()) {
    namespaces = getCollections(mongoClient);
  } else if (collection.isEmpty()) {
    namespaces = getCollections(mongoClient, database);
  } else {
    namespaces = singletonList(createNamespace(database, collection));
  }
  LOGGER.info("Copying existing data on the following namespaces: {}", namespaces);
  namespacesToCopy = new AtomicInteger(namespaces.size());
  queue = new ArrayBlockingQueue<>(sourceConfig.getInt(COPY_EXISTING_QUEUE_SIZE_CONFIG));
  executor =
      Executors.newFixedThreadPool(
          Math.max(
              1,
              Math.min(
                  namespaces.size(), sourceConfig.getInt(COPY_EXISTING_MAX_THREADS_CONFIG))));
  namespaces.forEach(n -> executor.submit(() -> copyDataFrom(n)));
}
 
Example #14
Source File: DataSynchronizer.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Deletes a single synchronized document by its given id. No deletion will occur if the _id is
 * not being synchronized.
 *
 * @param nsConfig   the namespace synchronization config of the namespace where the document
 *                   lives.
 * @param documentId the _id of the document.
 */
@CheckReturnValue
@Nullable
private LocalSyncWriteModelContainer deleteOneFromRemote(
    final NamespaceSynchronizationConfig nsConfig,
    final BsonValue documentId
) {
  final MongoNamespace namespace = nsConfig.getNamespace();
  final Lock lock = this.syncConfig.getNamespaceConfig(namespace).getLock().writeLock();
  lock.lock();
  final CoreDocumentSynchronizationConfig config;
  try {
    config = syncConfig.getSynchronizedDocument(namespace, documentId);
    if (config == null) {
      return null;
    }
  } finally {
    lock.unlock();
  }

  final LocalSyncWriteModelContainer container = desyncDocumentsFromRemote(nsConfig, documentId);
  container.addLocalChangeEvent(
      ChangeEvents.changeEventForLocalDelete(namespace, documentId, false));
  return container;
}
 
Example #15
Source File: CoreRemoteMongoCollectionUnitTests.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetNamespace() {
  final CoreRemoteMongoCollection<Document> coll1 = getCollection();
  assertEquals(new MongoNamespace("dbName1", "collName1"), coll1.getNamespace());

  final CoreRemoteMongoCollection<Document> coll2 = getCollection("collName2");
  assertEquals(new MongoNamespace("dbName1", "collName2"), coll2.getNamespace());
}
 
Example #16
Source File: CoreSyncImpl.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
public CoreSyncImpl(final MongoNamespace namespace,
                    final Class<DocumentT> documentClass,
                    final DataSynchronizer dataSynchronizer,
                    final CoreStitchServiceClient service,
                    final SyncOperations<DocumentT> syncOperations) {
  this.namespace = namespace;
  this.documentClass = documentClass;
  this.dataSynchronizer = dataSynchronizer;
  this.syncOperations = syncOperations;
  this.service = service;
}
 
Example #17
Source File: NamespaceSynchronizationConfig.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
private NamespaceSynchronizationConfig(
    final MongoNamespace namespace
) {
  this.namespace = namespace;
  this.namespacesColl = null;
  this.docsColl = null;
  this.syncedDocuments = null;
  this.nsLock = new ReentrantReadWriteLock();
}
 
Example #18
Source File: CoreDocumentSynchronizationConfig.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
static BsonDocument getDocFilter(
    @Nonnull final MongoNamespace namespace,
    @Nonnull final BsonValue documentId
) {
  final BsonDocument filter = new BsonDocument();
  filter.put(ConfigCodec.Fields.NAMESPACE_FIELD, new BsonString(namespace.toString()));
  filter.put(ConfigCodec.Fields.DOCUMENT_ID_FIELD, documentId);
  return filter;
}
 
Example #19
Source File: CoreDocumentSynchronizationConfig.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
public MongoNamespace getNamespace() {
  docLock.readLock().lock();
  try {
    return namespace;
  } finally {
    docLock.readLock().unlock();
  }
}
 
Example #20
Source File: DataSynchronizer.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes a single synchronized document by its given id. No deletion will occur if the _id is
 * not being synchronized.
 *
 * @param nsConfig   the namespace synchronization config of the namespace where the document
 *                   lives.
 * @param documentId the _id of the document.
 */
@CheckReturnValue
private @Nullable
LocalSyncWriteModelContainer deleteOneFromResolution(
    final NamespaceSynchronizationConfig nsConfig,
    final BsonValue documentId,
    final BsonDocument atVersion
) {
  final MongoNamespace namespace = nsConfig.getNamespace();
  final ChangeEvent<BsonDocument> event;
  final Lock lock =
      this.syncConfig.getNamespaceConfig(namespace).getLock().writeLock();
  lock.lock();
  final CoreDocumentSynchronizationConfig config;
  try {
    config = syncConfig.getSynchronizedDocument(namespace, documentId);
    if (config == null) {
      return null;
    }

    event = ChangeEvents.changeEventForLocalDelete(namespace, documentId, true);
    config.setSomePendingWrites(logicalT, atVersion, 0L, event);
  } finally {
    lock.unlock();
  }

  final LocalSyncWriteModelContainer container = newWriteModelContainer(nsConfig);

  container.addDocIDs(documentId);
  container.addLocalWrite(new DeleteOneModel<>(getDocumentIdFilter(documentId)));
  container.addLocalChangeEvent(event);
  container.addConfigWrite(
      new ReplaceOneModel<>(CoreDocumentSynchronizationConfig.getDocFilter(
        namespace, config.getDocumentId()
      ), config));

  return container;
}
 
Example #21
Source File: DataSynchronizer.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Inserts a single document locally and being to synchronize it based on its _id. Inserting
 * a document with the same _id twice will result in a duplicate key exception.
 *
 * @param namespace the namespace to put the document in.
 * @param document  the document to insert.
 */
void insertOne(final MongoNamespace namespace, final BsonDocument document) {
  this.waitUntilInitialized();

  try {
    ongoingOperationsGroup.enter();
    // Remove forbidden fields from the document before inserting it into the local collection.
    final BsonDocument docForStorage = sanitizeDocument(document);

    final NamespaceSynchronizationConfig nsConfig =
        this.syncConfig.getNamespaceConfig(namespace);
    final Lock lock = nsConfig.getLock().writeLock();
    lock.lock();
    final ChangeEvent<BsonDocument> event;
    final BsonValue documentId;
    try {
      getLocalCollection(namespace).insertOne(docForStorage);
      documentId = BsonUtils.getDocumentId(docForStorage);
      event = ChangeEvents.changeEventForLocalInsert(namespace, docForStorage, true);
      final CoreDocumentSynchronizationConfig config = syncConfig.addAndGetSynchronizedDocument(
          namespace,
          documentId
      );
      config.setSomePendingWritesAndSave(logicalT, event);
    } finally {
      lock.unlock();
    }
    checkAndInsertNamespaceListener(namespace);
    eventDispatcher.emitEvent(nsConfig, event);
  } finally {
    ongoingOperationsGroup.exit();
  }
}
 
Example #22
Source File: AggregateOperation.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
AggregateOperation(
    final MongoNamespace namespace,
    final DataSynchronizer dataSynchronizer,
    final List<? extends Bson> pipeline,
    final Class<T> resultClass
) {
  this.namespace = namespace;
  this.dataSynchronizer = dataSynchronizer;
  this.pipeline = pipeline;
  this.resultClass = resultClass;
}
 
Example #23
Source File: UpdateManyOperation.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
UpdateManyOperation(
    final MongoNamespace namespace,
    final BsonDocument filter,
    final BsonDocument update
) {
  this.namespace = namespace;
  this.filter = filter;
  this.update = update;
}
 
Example #24
Source File: UpdateOneOperation.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
UpdateOneOperation(
    final MongoNamespace namespace,
    final Bson filter,
    final BsonDocument update,
    final DataSynchronizer dataSynchronizer,
    final SyncUpdateOptions syncUpdateOptions
) {
  this.namespace = namespace;
  this.filter = filter;
  this.update = update;
  this.dataSynchronizer = dataSynchronizer;
  this.syncUpdateOptions = syncUpdateOptions;
}
 
Example #25
Source File: DataSynchronizer.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
private void initialize() {
  this.configDb =
      localClient.getDatabase("sync_config" + instanceKey)
          .withCodecRegistry(CodecRegistries.fromRegistries(
              CodecRegistries.fromCodecs(
                  InstanceSynchronizationConfig.configCodec,
                  NamespaceSynchronizationConfig.configCodec,
                  CoreDocumentSynchronizationConfig.configCodec),
              BsonUtils.DEFAULT_CODEC_REGISTRY));

  this.instancesColl = configDb
      .getCollection("instances", InstanceSynchronizationConfig.class);

  if (instancesColl.countDocuments() == 0) {
    this.syncConfig = new InstanceSynchronizationConfig(configDb);
    instancesColl.insertOne(this.syncConfig);
  } else {
    if (instancesColl.find().first() == null) {
      throw new IllegalStateException("expected to find instance configuration");
    }
    this.syncConfig = new InstanceSynchronizationConfig(configDb);
  }
  this.instanceChangeStreamListener = new InstanceChangeStreamListenerImpl(
      syncConfig,
      service,
      networkMonitor,
      authMonitor);
  for (final MongoNamespace ns : this.syncConfig.getSynchronizedNamespaces()) {
    this.instanceChangeStreamListener.addNamespace(ns);
  }
}
 
Example #26
Source File: DeleteOneOperation.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
DeleteOneOperation(
    final MongoNamespace namespace,
    final Bson filter,
    final DataSynchronizer dataSynchronizer
) {
  this.namespace = namespace;
  this.filter = filter;
  this.dataSynchronizer = dataSynchronizer;
}
 
Example #27
Source File: CoreRemoteMongoCollectionImpl.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
CoreRemoteMongoCollectionImpl(final MongoNamespace namespace,
                              final Class<DocumentT> documentClass,
                              final CoreStitchServiceClient service,
                              @Nullable final DataSynchronizer dataSynchronizer,
                              @Nullable final NetworkMonitor networkMonitor) {
  notNull("namespace", namespace);
  notNull("documentClass", documentClass);
  this.namespace = namespace;
  this.documentClass = documentClass;
  this.service = service;
  this.operations = new Operations<>(namespace, documentClass, service.getCodecRegistry());

  this.dataSynchronizer = dataSynchronizer;
  this.networkMonitor = networkMonitor;

  if (dataSynchronizer != null && this.networkMonitor != null) {
    this.coreSync = new CoreSyncImpl<>(
        getNamespace(),
        getDocumentClass(),
        dataSynchronizer,
        service,
        new SyncOperations<>(
            getNamespace(),
            getDocumentClass(),
            dataSynchronizer,
            getCodecRegistry())
    );
  } else {
    this.coreSync = null;
  }
}
 
Example #28
Source File: FindOperation.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a new instance.
 *
 * @param namespace the database and collection namespace for the operation.
 * @param decoder the decoder for the result documents.
 */
FindOperation(final MongoNamespace namespace, final Decoder<T> decoder) {
  notNull("namespace", namespace);
  notNull("decoder", decoder);
  this.namespace = namespace;
  this.decoder = decoder;
}
 
Example #29
Source File: WatchOperation.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor id-based watch.
 */
WatchOperation(
    final MongoNamespace namespace,
    final Set<BsonValue> ids,
    final boolean useCompactEvents,
    final Codec<DocumentT> fullDocumentCodec
) {
  this.namespace = namespace;
  this.matchFilter = null;
  this.ids = ids;
  this.useCompactEvents = useCompactEvents;
  this.fullDocumentCodec = fullDocumentCodec;
}
 
Example #30
Source File: UpdateManyOperation.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
UpdateManyOperation(
    final MongoNamespace namespace,
    final Bson filter,
    final BsonDocument update,
    final DataSynchronizer dataSynchronizer,
    final SyncUpdateOptions syncUpdateOptions
) {
  this.namespace = namespace;
  this.filter = filter;
  this.update = update;
  this.dataSynchronizer = dataSynchronizer;
  this.syncUpdateOptions = syncUpdateOptions;
}