use of com.torodb.mongodb.commands.signatures.general.InsertCommand.InsertResult in project torodb by torodb.
the class InsertImplementation method apply.
@Override
public Status<InsertResult> apply(Request req, Command<? super InsertArgument, ? super InsertResult> command, InsertArgument arg, WriteMongodTransaction context) {
mongodMetrics.getInserts().mark(arg.getDocuments().size());
Stream<KvDocument> docsToInsert = arg.getDocuments().stream().map(FromBsonValueTranslator.getInstance()).map((v) -> (KvDocument) v);
try {
if (!context.getTorodTransaction().existsCollection(req.getDatabase(), arg.getCollection())) {
context.getTorodTransaction().createIndex(req.getDatabase(), arg.getCollection(), Constants.ID_INDEX, ImmutableList.<IndexFieldInfo>of(new IndexFieldInfo(new AttributeReference(Arrays.asList(new Key[] { new ObjectKey(Constants.ID) })), FieldIndexOrdering.ASC.isAscending())), true);
}
context.getTorodTransaction().insert(req.getDatabase(), arg.getCollection(), docsToInsert);
} catch (UserException ex) {
//TODO: Improve error reporting
return Status.from(ErrorCode.COMMAND_FAILED, ex.getLocalizedMessage());
}
return Status.ok(new InsertResult(arg.getDocuments().size()));
}
use of com.torodb.mongodb.commands.signatures.general.InsertCommand.InsertResult in project torodb by torodb.
the class TransactionalDbCloner method cloneCollection.
private void cloneCollection(String toDb, MongoConnection remoteConnection, WriteMongodTransaction transaction, CloneOptions opts, String collection, CollectionOptions collOptions) throws MongoException, CloningException {
String fromDb = opts.getDbToClone();
LOGGER.info("Cloning {}.{} into {}.{}", fromDb, collection, toDb, collection);
//TODO: enable exhaust?
EnumSet<QueryOption> queryFlags = EnumSet.of(QueryOption.NO_CURSOR_TIMEOUT);
if (opts.isSlaveOk()) {
queryFlags.add(QueryOption.SLAVE_OK);
}
MongoCursor<BsonDocument> cursor = remoteConnection.query(opts.getDbToClone(), collection, null, 0, 0, new QueryOptions(queryFlags), null, null);
while (!cursor.hasNext()) {
List<? extends BsonDocument> docsToInsert = cursor.fetchBatch().asList();
Status<InsertResult> insertResult = transaction.execute(new Request(toDb, null, true, null), InsertCommand.INSTANCE, new InsertArgument.Builder(collection).addDocuments(docsToInsert).setWriteConcern(WriteConcern.fsync()).setOrdered(true).build());
if (!insertResult.isOk() || insertResult.getResult().getN() != docsToInsert.size()) {
throw new CloningException("Error while inserting a cloned document");
}
}
}
use of com.torodb.mongodb.commands.signatures.general.InsertCommand.InsertResult in project torodb by torodb.
the class OplogManager method storeState.
@Locked(exclusive = true)
private void storeState(long hash, OpTime opTime) throws OplogManagerPersistException {
Preconditions.checkState(isRunning(), "The service is not running");
try {
retrier.retry(() -> {
try (WriteMongodTransaction transaction = connection.openWriteTransaction()) {
Status<Long> deleteResult = transaction.execute(new Request(OPLOG_DB, null, true, null), DeleteCommand.INSTANCE, new DeleteArgument.Builder(OPLOG_COL).addStatement(new DeleteStatement(DOC_QUERY, false)).build());
if (!deleteResult.isOk()) {
throw new RetrierAbortException(new MongoException(deleteResult));
}
//TODO: This should be stored as timestamp once TORODB-189 is resolved
long optimeAsLong = opTime.toOldBson().getMillisFromUnix();
Status<InsertResult> insertResult = transaction.execute(new Request(OPLOG_DB, null, true, null), InsertCommand.INSTANCE, new InsertArgument.Builder(OPLOG_COL).addDocument(new BsonDocumentBuilder().appendUnsafe(KEY, new BsonDocumentBuilder().appendUnsafe("hash", newLong(hash)).appendUnsafe("optime_i", DefaultBsonValues.newLong(optimeAsLong)).appendUnsafe("optime_t", newLong(opTime.getTerm())).build()).build()).build());
if (insertResult.isOk() && insertResult.getResult().getN() != 1) {
throw new RetrierAbortException(new MongoException(ErrorCode.OPERATION_FAILED, "More than one element inserted"));
}
if (!insertResult.isOk()) {
throw new RetrierAbortException(new MongoException(insertResult));
}
transaction.commit();
return Empty.getInstance();
} catch (UserException ex) {
throw new RetrierAbortException(ex);
}
}, Hint.INFREQUENT_ROLLBACK);
} catch (RetrierGiveUpException ex) {
throw new OplogManagerPersistException(ex);
}
}
use of com.torodb.mongodb.commands.signatures.general.InsertCommand.InsertResult in project torodb by torodb.
the class AkkaDbCloner method insertDocuments.
private int insertDocuments(MongodServer localServer, String toDb, String collection, List<BsonDocument> docsToInsert) throws RollbackException {
try (WriteMongodTransaction transaction = createWriteMongodTransaction(localServer)) {
Status<InsertResult> insertResult = transaction.execute(new Request(toDb, null, true, null), InsertCommand.INSTANCE, new InsertArgument.Builder(collection).addDocuments(docsToInsert).setWriteConcern(WriteConcern.fsync()).setOrdered(true).build());
if (!insertResult.isOk()) {
throw new CloningException("Error while inserting a cloned document");
}
int insertedDocs = insertResult.getResult().getN();
if (insertedDocs != docsToInsert.size()) {
throw new CloningException("Expected to insert " + docsToInsert.size() + " but " + insertResult + " were inserted");
}
transaction.commit();
return insertedDocs;
} catch (UserException ex) {
throw new CloningException("Unexpected error while cloning documents", ex);
}
}
Aggregations