Example usage for com.mongodb.client MongoCollection getNamespace

List of usage examples for com.mongodb.client MongoCollection getNamespace

Introduction

In this page you can find the example usage for com.mongodb.client MongoCollection getNamespace.

Prototype

MongoNamespace getNamespace();

Source Link

Document

Gets the namespace of this collection.

Usage

From source file:org.radarcns.mongo.util.MongoHelper.java

License:Apache License

/**
 * Creates given index on the collection, if it is not already created for given collection.
 * Having indexes created in background prevents other operations being blocked.
 * @param collection mongoCollection to index.
 * @param index index to be created.//ww w  .j a va2s . c  o  m
 */
private static void createIndexIfNotAvailable(MongoCollection<Document> collection, Bson index) {
    if (indexMap.containsKey(collection.getNamespace().getCollectionName())) {
        List<String> availableIndexes = indexMap.get(collection.getNamespace().getCollectionName());
        if (!availableIndexes.contains(index.toString())) {
            collection.createIndex(index, new IndexOptions().background(true));
            availableIndexes.add(index.toString());
        }

    } else {
        collection.createIndex(index, new IndexOptions().background(true));
        List<String> indexList = new ArrayList<>();
        indexList.add(index.toString());
        indexMap.put(collection.getNamespace().getCollectionName(), indexList);
    }
}

From source file:rapture.lock.mongodb.MongoLockHandler2.java

License:Open Source License

@Override
public LockHandle acquireLock(String lockHolder, String lockName, long secondsToWait, long secondsToHold) {
    log.debug("Mongo acquire lock2  " + lockName + ":" + secondsToHold + ":" + secondsToWait);
    long start = System.currentTimeMillis();

    MongoCollection<Document> coll = getLockCollection(lockName);
    log.debug("lock COLLECTION for " + lockName + "IS " + coll.getNamespace().getFullName());

    long bailTime = System.currentTimeMillis() + secondsToWait * 1000;
    long leaseTime = System.currentTimeMillis() + secondsToHold * 1000;

    Document lockFile = new Document();
    lockFile.put("lockName", lockName);
    lockFile.put("lockHolder", lockHolder);
    lockFile.put("lease", leaseTime);

    long myLockID = System.currentTimeMillis();
    lockFile.put("_id", "" + myLockID);
    log.debug("id is " + myLockID);
    log.debug("bailtime " + bailTime);

    while (bailTime > System.currentTimeMillis()) {
        try {/*from w w w. j  a v  a 2  s  . c  o  m*/
            myLockID = System.currentTimeMillis();
            lockFile.put("_id", "" + myLockID);
            lockFile.put("lease", myLockID + secondsToHold * 1000);

            coll.withWriteConcern(WriteConcern.ACKNOWLEDGED).insertOne(lockFile);
            log.debug("inserted file" + lockFile);

            break;
        } catch (MongoServerException e) {
            log.error(ExceptionToString.format(e));
            try {
                Thread.sleep(50);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
        }
    }

    // loop until we acquire lock or timeout
    while (bailTime > System.currentTimeMillis()) {
        // we have the lock if no lock file exists with a smaller number
        FindIterable<Document> results = coll
                .find(new Document("lease", new Document("$gt", System.currentTimeMillis())))
                .sort(new Document("_id", 1)).limit(1);

        Document lock = results.first();
        if (lock != null && ((String) lock.get("_id")).equals("" + myLockID)) {
            log.debug("* i have the lock" + lock.get("_id") + ":" + myLockID);
            LockHandle lockHandle = new LockHandle();
            lockHandle.setLockName(lockName);
            lockHandle.setHandle("" + myLockID);
            lockHandle.setLockHolder(lockHolder);
            long end = System.currentTimeMillis();
            log.debug("* NG acquired lock in " + (end - start));
            return lockHandle;
        } else {
            // log.info("* waiting for lock held by "+ lock.get("_id"));
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
    // ran out of time trying to get lock.
    log.debug("giving up " + myLockID);
    long end2 = System.currentTimeMillis();
    log.debug("denied lock in " + (end2 - start));
    return null;
}

From source file:uk.ac.ebi.eva.dbmigration.mongodb.ExtractAnnotationFromVariant.java

License:Apache License

@ChangeSet(order = "001", id = "migrateAnnotation", author = "EVA")
public void migrateAnnotation(MongoDatabase mongoDatabase) {
    final MongoCollection<Document> variantsCollection = mongoDatabase
            .getCollection(databaseParameters.getDbCollectionsVariantsName());
    final MongoCollection<Document> annotationCollection = mongoDatabase
            .getCollection(databaseParameters.getDbCollectionsAnnotationsName());
    logger.info("1) migrate annotation from collection {}", variantsCollection.getNamespace());

    long annotationsReadCount = 0;
    long annotationsWrittenCount = 0;
    BulkWriteOptions unorderedBulk = new BulkWriteOptions().ordered(false);
    Document onlyAnnotatedVariants = new Document(ANNOT_FIELD, EXISTS);
    try (MongoCursor<Document> cursor = variantsCollection.find(onlyAnnotatedVariants).iterator()) {
        while (true) {
            List<InsertOneModel<Document>> annotationsToInsert = getBatch(cursor, BULK_SIZE).stream()
                    .map(this::buildInsertionDocument).collect(toList());

            if (annotationsToInsert.isEmpty()) {
                break;
            }/*  w  w w.j a va 2 s  .c  om*/

            annotationsReadCount += annotationsToInsert.size();
            BulkWriteResult bulkInsert = annotationCollection.bulkWrite(annotationsToInsert, unorderedBulk);
            annotationsWrittenCount += bulkInsert.getInsertedCount();
        }
    }

    //before executing the next changeSet check that the count of read and written annotation documents match
    if (annotationsReadCount != annotationsWrittenCount) {
        throw new RuntimeException("The number of processed Variants (" + annotationsReadCount
                + ") is different from the number of new annotation inserted (" + annotationsWrittenCount
                + "). The '" + ANNOT_FIELD + "' field will not be removed from the "
                + variantsCollection.getNamespace() + " collection.");
    }
}

From source file:uk.ac.ebi.eva.dbmigration.mongodb.ExtractAnnotationFromVariant.java

License:Apache License

@ChangeSet(order = "002", id = "dropIndexes", author = "EVA")
public void dropIndexes(MongoDatabase mongoDatabase) {
    final MongoCollection<Document> variantsCollection = mongoDatabase
            .getCollection(databaseParameters.getDbCollectionsVariantsName());
    logger.info("2) drop indexes from annot field from collection {}", variantsCollection.getNamespace());

    variantsCollection.dropIndex(LEGACY_ANNOTATION_CT_SO_INDEX);
    variantsCollection.dropIndex(LEGACY_ANNOTATION_XREF_ID_INDEX);
}

From source file:uk.ac.ebi.eva.dbmigration.mongodb.ExtractAnnotationFromVariant.java

License:Apache License

@ChangeSet(order = "003", id = "reduceAnnotationFromVariants", author = "EVA")
public void reduceAnnotationFromVariants(MongoDatabase mongoDatabase) {
    final MongoCollection<Document> variantsCollection = mongoDatabase
            .getCollection(databaseParameters.getDbCollectionsVariantsName());
    logger.info("3) reduce annotation field from collection {}", variantsCollection.getNamespace());

    long annotationsReadCount = 0;
    long annotationsUpdatedCount = 0;
    BulkWriteOptions unorderedBulk = new BulkWriteOptions().ordered(false);
    Document onlyAnnotatedVariants = new Document(ANNOT_FIELD, EXISTS);
    try (MongoCursor<Document> cursor = variantsCollection.find(onlyAnnotatedVariants).iterator()) {
        while (true) {
            List<UpdateOneModel<Document>> annotationsToUpdate = getBatch(cursor, BULK_SIZE).stream()
                    .map(this::buildUpdateDocument).collect(toList());

            if (annotationsToUpdate.isEmpty()) {
                break;
            }// www.  j  a  v  a  2s.c  o  m
            annotationsReadCount += annotationsToUpdate.size();
            BulkWriteResult bulkInsert = variantsCollection.bulkWrite(annotationsToUpdate, unorderedBulk);
            annotationsUpdatedCount += bulkInsert.getModifiedCount();
        }
    }
    if (annotationsReadCount != annotationsUpdatedCount) {
        throw new RuntimeException("The number of processed Variants (" + annotationsReadCount
                + ") is different from the number of annotation " + "updated (" + annotationsUpdatedCount
                + ").");
    }
}

From source file:uk.ac.ebi.eva.dbmigration.mongodb.ExtractAnnotationFromVariant.java

License:Apache License

@ChangeSet(order = "004", id = "updateAnnotationMetadata", author = "EVA")
public void updateAnnotationMetadata(MongoDatabase mongoDatabase) {
    final MongoCollection<Document> annotationMetadataCollection = mongoDatabase
            .getCollection(databaseParameters.getDbCollectionsAnnotationMetadataName());
    logger.info("4) update annotation metadata in collection {}", annotationMetadataCollection.getNamespace());

    String id = databaseParameters.getVepVersion() + "_" + databaseParameters.getVepCacheVersion();
    Document metadata = new Document(ID_FIELD, id);
    if (annotationMetadataCollection.count(metadata) == 0) {
        metadata.append(VEP_VERSION_FIELD, databaseParameters.getVepVersion()).append(CACHE_VERSION_FIELD,
                databaseParameters.getVepCacheVersion());

        annotationMetadataCollection.insertOne(metadata);
    }/*from  www . j  av  a  2  s.c o m*/
}

From source file:uk.ac.ebi.eva.dbmigration.mongodb.ExtractAnnotationFromVariant.java

License:Apache License

@ChangeSet(order = "005", id = "createIndexes", author = "EVA")
public void createIndexes(MongoDatabase mongoDatabase) {
    final MongoCollection<Document> variantsCollection = mongoDatabase
            .getCollection(databaseParameters.getDbCollectionsVariantsName());
    final MongoCollection<Document> annotationsCollection = mongoDatabase
            .getCollection(databaseParameters.getDbCollectionsAnnotationsName());
    logger.info("5) create indexes collections {} and {}", annotationsCollection.getNamespace(),
            variantsCollection.getNamespace());

    IndexOptions background = new IndexOptions().background(true);
    variantsCollection.createIndex(new Document(ANNOT_FIELD + "." + XREFS_FIELD, 1), background);
    variantsCollection.createIndex(new Document(ANNOT_FIELD + "." + SO_FIELD, 1), background);

    annotationsCollection.createIndex(new Document(CONSEQUENCE_TYPE_FIELD + "." + SO_FIELD, 1), background);
    annotationsCollection.createIndex(new Document(XREFS_FIELD + "." + XREF_ID_FIELD, 1), background);
    annotationsCollection.createIndex(/* ww  w.  j a v  a  2  s  .c o m*/
            new Document(CHROMOSOME_FIELD, 1).append(START_FIELD, 1).append(END_FIELD, 1), background);
}

From source file:uk.ac.ebi.eva.dbmigration.mongodb.ExtractAnnotationFromVariant.java

License:Apache License

@ChangeSet(order = "006", id = "addDefaultVersionInAnnotationMetadata", author = "EVA")
public void addDefaultVersion(MongoDatabase mongoDatabase) {
    final MongoCollection<Document> annotationMetadataCollection = mongoDatabase
            .getCollection(databaseParameters.getDbCollectionsAnnotationMetadataName());
    logger.info("6) add default annotation version to collection {} ",
            annotationMetadataCollection.getNamespace());

    Document allVersions = new Document();
    Document setDefaultToFalse = new Document("$set", new Document(DEFAULT_VERSION_FIELD, false));
    annotationMetadataCollection.updateMany(allVersions, setDefaultToFalse);

    String id = databaseParameters.getVepVersion() + "_" + databaseParameters.getVepCacheVersion();
    Document defaultVersionDocument = new Document(ID_FIELD, id);
    Document setDefaultToTrue = new Document("$set", new Document(DEFAULT_VERSION_FIELD, true));
    UpdateResult updateResult = annotationMetadataCollection.updateOne(defaultVersionDocument,
            setDefaultToTrue);//from w  w  w  .  j a  v  a  2 s  .c om
    Assert.state(updateResult.getModifiedCount() == 1, "Only one modification was expected");
}