use of com.eightkdata.mongowp.utils.BsonDocumentBuilder 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.eightkdata.mongowp.utils.BsonDocumentBuilder in project torodb by torodb.
the class OplogTestParser method getOps.
private static List<OplogOperation> getOps(BsonDocument doc) {
BsonValue<?> oplogValue = doc.get("oplog");
if (oplogValue == null) {
throw new AssertionError("Does not contain oplog");
}
AtomicInteger tsFactory = new AtomicInteger();
AtomicInteger tFactory = new AtomicInteger();
BsonInt32 twoInt32 = DefaultBsonValues.newInt(2);
return oplogValue.asArray().asList().stream().map(BsonValue::asDocument).map(child -> {
BsonDocumentBuilder builder = new BsonDocumentBuilder(child);
if (child.get("ts") == null) {
builder.appendUnsafe("ts", DefaultBsonValues.newTimestamp(tsFactory.incrementAndGet(), tFactory.incrementAndGet()));
}
if (child.get("h") == null) {
builder.appendUnsafe("h", DefaultBsonValues.INT32_ONE);
}
if (child.get("v") == null) {
builder.appendUnsafe("v", twoInt32);
}
return builder.build();
}).map(child -> {
try {
return OplogOperationParser.fromBson(child);
} catch (MongoException ex) {
throw new AssertionError("Invalid oplog operation", ex);
}
}).collect(Collectors.toList());
}
use of com.eightkdata.mongowp.utils.BsonDocumentBuilder in project torodb by torodb.
the class CollectionOptions method marshall.
public BsonDocument marshall() {
BsonDocumentBuilder builder = new BsonDocumentBuilder();
marshall(builder);
return builder.build();
}
use of com.eightkdata.mongowp.utils.BsonDocumentBuilder in project torodb by torodb.
the class HeartbeatInfo method marshall.
public BsonDocument marshall() {
BsonDocumentBuilder builder = new BsonDocumentBuilder();
builder.append(SET_NAME_FIELD_NAME, setName);
builder.append(PROTOCOL_VERSION_FIELD_NAME, protocolVersion);
builder.append(CONFIG_VERSION_FIELD_NAME, configVersion);
if (senderHost != null) {
builder.append(SENDER_HOST_FIELD_NAME, senderHost.toString());
} else {
builder.append(SENDER_HOST_FIELD_NAME, "");
}
if (senderId != null) {
builder.append(SENDER_ID_FIELD_NAME, senderId);
}
if (checkEmpty != null) {
builder.append(CHECK_EMPTY_FIELD_NAME, checkEmpty);
}
return builder.build();
}
use of com.eightkdata.mongowp.utils.BsonDocumentBuilder in project torodb by torodb.
the class WriteConcernError method marshall.
public BsonValue<?> marshall() {
BsonDocumentBuilder bsonWriteConcernError = new BsonDocumentBuilder();
bsonWriteConcernError.append(CODE_FIELD, code);
bsonWriteConcernError.append(ERR_MSG_FIELD, errMsg);
return bsonWriteConcernError.build();
}
Aggregations