Search in sources :

Example 1 with CollectionOptions

use of com.torodb.mongodb.commands.pojos.CollectionOptions in project torodb by torodb.

the class TransactionalDbCloner method cloneDatabase.

/**
   *
   * @param dstDb
   * @param remoteConnection
   * @param transaction
   * @param opts
   * @throws CloningException
   * @throws NotMasterException if {@link CloneOptions#getWritePermissionSupplier()
   *                            opts.getWritePermissionSupplier().get()} is evaluated to false
   */
public void cloneDatabase(@Nonnull String dstDb, @Nonnull MongoConnection remoteConnection, @Nonnull WriteMongodTransaction transaction, @Nonnull CloneOptions opts) throws CloningException, NotMasterException, MongoException {
    if (!remoteConnection.isRemote() && opts.getDbToClone().equals(dstDb)) {
        LOGGER.warn("Trying to clone a database to itself! Ignoring it");
        return;
    }
    String fromDb = opts.getDbToClone();
    CursorResult<Entry> listCollections;
    try {
        listCollections = ListCollectionsRequester.getListCollections(remoteConnection, fromDb, null);
    } catch (MongoException ex) {
        throw new CloningException("It was impossible to get information from the remote server", ex);
    }
    if (!opts.getWritePermissionSupplier().get()) {
        throw new NotMasterException("Destiny database cannot be written");
    }
    Map<String, CollectionOptions> collsToClone = Maps.newHashMap();
    for (Iterator<Entry> iterator = listCollections.getFirstBatch(); iterator.hasNext(); ) {
        Entry collEntry = iterator.next();
        String collName = collEntry.getCollectionName();
        if (opts.getCollsToIgnore().contains(collName)) {
            LOGGER.debug("Not cloning {} because is marked as an ignored collection", collName);
            continue;
        }
        if (!NamespaceUtil.isUserWritable(fromDb, collName)) {
            LOGGER.info("Not cloning {} because is a not user writable", collName);
            continue;
        }
        if (NamespaceUtil.isNormal(fromDb, collName)) {
            LOGGER.info("Not cloning {} because it is not normal", collName);
            continue;
        }
        LOGGER.info("Collection {}.{} will be cloned", fromDb, collName);
        collsToClone.put(collName, collEntry.getCollectionOptions());
    }
    if (!opts.getWritePermissionSupplier().get()) {
        throw new NotMasterException("Destiny database cannot be written " + "after get collections info");
    }
    for (Map.Entry<String, CollectionOptions> entry : collsToClone.entrySet()) {
        dropCollection(transaction, dstDb, entry.getKey());
        createCollection(transaction, dstDb, entry.getKey(), entry.getValue());
    }
    if (opts.isCloneData()) {
        for (Map.Entry<String, CollectionOptions> entry : collsToClone.entrySet()) {
            cloneCollection(dstDb, remoteConnection, transaction, opts, entry.getKey(), entry.getValue());
        }
    }
    if (opts.isCloneIndexes()) {
        for (Map.Entry<String, CollectionOptions> entry : collsToClone.entrySet()) {
            cloneIndex(dstDb, remoteConnection, transaction, opts, entry.getKey(), entry.getValue());
        }
    }
}
Also used : Entry(com.torodb.mongodb.commands.signatures.admin.ListCollectionsCommand.ListCollectionsResult.Entry) MongoException(com.eightkdata.mongowp.exceptions.MongoException) CollectionOptions(com.torodb.mongodb.commands.pojos.CollectionOptions) NotMasterException(com.eightkdata.mongowp.exceptions.NotMasterException) Map(java.util.Map)

Aggregations

MongoException (com.eightkdata.mongowp.exceptions.MongoException)1 NotMasterException (com.eightkdata.mongowp.exceptions.NotMasterException)1 CollectionOptions (com.torodb.mongodb.commands.pojos.CollectionOptions)1 Entry (com.torodb.mongodb.commands.signatures.admin.ListCollectionsCommand.ListCollectionsResult.Entry)1 Map (java.util.Map)1