List of usage examples for com.mongodb.client MongoCollection withWriteConcern
MongoCollection<TDocument> withWriteConcern(WriteConcern writeConcern);
From source file:com.yahoo.ycsb.db3.MongoDbClient.java
License:Open Source License
/** * Delete a record from the database.// w ww . j av a 2s . c om * * @param table * The name of the table * @param key * The record key of the record to delete. * @return Zero on success, a non-zero error code on error. See the {@link DB} * class's description for a discussion of error codes. */ @Override public Status delete(String table, String key) { try { MongoCollection<Document> collection = database.getCollection(table); Document query = new Document("_id", key); DeleteResult result = collection.withWriteConcern(writeConcern).deleteOne(query); if (result.wasAcknowledged() && result.getDeletedCount() == 0) { System.err.println("Nothing deleted for key " + key); return Status.NOT_FOUND; } return Status.OK; } catch (Exception e) { System.err.println(e.toString()); return Status.ERROR; } }
From source file:org.apache.metamodel.mongodb.mongo3.MongoDbInsertionBuilder.java
License:Apache License
@Override public void execute() throws MetaModelException { final Column[] columns = getColumns(); final Object[] values = getValues(); final Document doc = new Document(); for (int i = 0; i < values.length; i++) { Object value = values[i]; if (value != null) { doc.put(columns[i].getName(), value); }// w w w . j av a 2 s . c o m } final MongoDbUpdateCallback updateCallback = getUpdateCallback(); final MongoCollection<Document> collection = updateCallback.getCollection(getTable().getName()); final WriteConcern writeConcern = updateCallback.getWriteConcernAdvisor().adviceInsert(collection, doc); collection.withWriteConcern(writeConcern); collection.insertOne(doc); logger.info("Document has been inserted"); }
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 2s .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; }