use of com.eightkdata.mongowp.bson.BsonDocument 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.bson.BsonDocument in project torodb by torodb.
the class FindImplementation method apply.
@Override
public Status<FindResult> apply(Request req, Command<? super FindArgument, ? super FindResult> command, FindArgument arg, MongodTransaction context) {
logFindCommand(arg);
BsonDocument filter = arg.getFilter();
Cursor<BsonDocument> cursor;
switch(filter.size()) {
case 0:
{
cursor = context.getTorodTransaction().findAll(req.getDatabase(), arg.getCollection()).asDocCursor().transform(t -> t.getRoot()).transform(ToBsonDocumentTranslator.getInstance());
break;
}
case 1:
{
try {
cursor = getByAttributeCursor(context.getTorodTransaction(), req.getDatabase(), arg.getCollection(), filter).transform(ToBsonDocumentTranslator.getInstance());
} catch (CommandFailed ex) {
return Status.from(ex);
}
break;
}
default:
{
return Status.from(ErrorCode.COMMAND_FAILED, "The given query is not supported right now");
}
}
if (Long.valueOf(arg.getBatchSize()) > (long) Integer.MAX_VALUE) {
return Status.from(ErrorCode.COMMAND_FAILED, "Only batchSize equals or lower than " + Integer.MAX_VALUE + " is supported");
}
OptionalLong batchSize = arg.getEffectiveBatchSize();
List<BsonDocument> batch = cursor.getNextBatch(batchSize.isPresent() ? (int) batchSize.getAsLong() : 101);
cursor.close();
return Status.ok(new FindResult(CursorResult.createSingleBatchCursor(req.getDatabase(), arg.getCollection(), batch.iterator())));
}
use of com.eightkdata.mongowp.bson.BsonDocument in project torodb by torodb.
the class UpdateImplementation method apply.
@Override
public Status<UpdateResult> apply(Request req, Command<? super UpdateArgument, ? super UpdateResult> command, UpdateArgument arg, WriteMongodTransaction context) {
UpdateStatus updateStatus = new UpdateStatus();
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);
}
for (UpdateStatement updateStatement : arg.getStatements()) {
BsonDocument query = updateStatement.getQuery();
UpdateAction updateAction = UpdateActionTranslator.translate(updateStatement.getUpdate());
Cursor<ToroDocument> candidatesCursor;
switch(query.size()) {
case 0:
{
candidatesCursor = context.getTorodTransaction().findAll(req.getDatabase(), arg.getCollection()).asDocCursor();
break;
}
case 1:
{
try {
candidatesCursor = findByAttribute(context.getTorodTransaction(), req.getDatabase(), arg.getCollection(), query);
} catch (CommandFailed ex) {
return Status.from(ex);
}
break;
}
default:
{
return Status.from(ErrorCode.COMMAND_FAILED, "The given query is not supported right now");
}
}
if (candidatesCursor.hasNext()) {
try {
Stream<List<ToroDocument>> candidatesbatchStream;
if (updateStatement.isMulti()) {
candidatesbatchStream = StreamSupport.stream(Spliterators.spliteratorUnknownSize(candidatesCursor.batch(100), Spliterator.ORDERED), false);
} else {
candidatesbatchStream = Stream.of(ImmutableList.of(candidatesCursor.next()));
}
Stream<KvDocument> updatedCandidates = candidatesbatchStream.map(candidates -> {
updateStatus.increaseCandidates(candidates.size());
context.getTorodTransaction().delete(req.getDatabase(), arg.getCollection(), candidates);
return candidates;
}).flatMap(l -> l.stream()).map(candidate -> {
try {
updateStatus.increaseUpdated();
return update(updateAction, candidate);
} catch (UserException userException) {
throw new UserWrappedException(userException);
}
});
context.getTorodTransaction().insert(req.getDatabase(), arg.getCollection(), updatedCandidates);
} catch (UserWrappedException userWrappedException) {
throw userWrappedException.getCause();
}
} else if (updateStatement.isUpsert()) {
KvDocument toInsertCandidate;
if (updateAction instanceof SetDocumentUpdateAction) {
toInsertCandidate = ((SetDocumentUpdateAction) updateAction).getNewValue();
} else {
toInsertCandidate = update(updateAction, new ToroDocument(-1, (KvDocument) MongoWpConverter.translate(query)));
}
if (!toInsertCandidate.containsKey(Constants.ID)) {
KvDocument.Builder builder = new KvDocument.Builder();
for (DocEntry<?> entry : toInsertCandidate) {
builder.putValue(entry.getKey(), entry.getValue());
}
builder.putValue(Constants.ID, MongoWpConverter.translate(objectIdFactory.consumeObjectId()));
toInsertCandidate = builder.build();
}
updateStatus.increaseCandidates(1);
updateStatus.increaseCreated(toInsertCandidate.get(Constants.ID));
Stream<KvDocument> toInsertCandidates = Stream.of(toInsertCandidate);
context.getTorodTransaction().insert(req.getDatabase(), arg.getCollection(), toInsertCandidates);
}
}
} catch (UserException ex) {
//TODO: Improve error reporting
return Status.from(ErrorCode.COMMAND_FAILED, ex.getLocalizedMessage());
}
mongodMetrics.getUpdateModified().mark(updateStatus.updated);
mongodMetrics.getUpdateMatched().mark(updateStatus.candidates);
mongodMetrics.getUpdateUpserted().mark(updateStatus.upsertResults.size());
return Status.ok(new UpdateResult(updateStatus.updated, updateStatus.candidates, ImmutableList.copyOf(updateStatus.upsertResults)));
}
use of com.eightkdata.mongowp.bson.BsonDocument in project torodb by torodb.
the class CollectionOptions method unmarshal.
public static CollectionOptions unmarshal(BsonDocument doc) throws BadValueException {
boolean capped;
try {
capped = BsonReaderTool.getBooleanOrNumeric(doc, CAPPED_FIELD, false);
} catch (TypesMismatchException ex) {
capped = true;
}
long cappedSize;
try {
cappedSize = BsonReaderTool.getLong(doc, CAPPED_SIZE_FIELD, 0);
if (cappedSize < 0) {
throw new BadValueException("size has to be >= 0");
}
} catch (TypesMismatchException ex) {
//backward compatibility
cappedSize = 0;
}
long cappedMaxDocs;
try {
cappedMaxDocs = BsonReaderTool.getLong(doc, CAPPED_MAX_DOCS_FIELD, 0);
if (cappedMaxDocs < 0) {
throw new BadValueException("max has to be >= 0");
}
} catch (TypesMismatchException ex) {
//backward compatibility
cappedMaxDocs = 0;
}
final ImmutableList.Builder<Long> initialExtentSizes = ImmutableList.builder();
Long initialNumExtends;
BsonArray array;
try {
array = BsonReaderTool.getArray(doc, INITIAL_EXTENT_SIZES_FIELD, null);
if (array == null) {
initialNumExtends = null;
} else {
initialNumExtends = null;
for (int i = 0; i < array.size(); i++) {
BsonValue element = array.get(i);
if (!element.isNumber()) {
throw new BadValueException("'$nExtents'.'" + i + "' has " + "the value " + element.toString() + " which is " + "not a number");
}
initialExtentSizes.add(element.asNumber().longValue());
}
}
} catch (TypesMismatchException ex) {
try {
initialNumExtends = BsonReaderTool.getLong(doc, INITIAL_NUM_EXTENDS_FIELD);
} catch (NoSuchKeyException | TypesMismatchException ex1) {
initialNumExtends = null;
}
}
AutoIndexMode autoIndexMode;
try {
if (BsonReaderTool.getBoolean(doc, AUTO_INDEX_MODE_FIELD)) {
autoIndexMode = AutoIndexMode.YES;
} else {
autoIndexMode = AutoIndexMode.NO;
}
} catch (NoSuchKeyException | TypesMismatchException ex) {
autoIndexMode = AutoIndexMode.DEFAULT;
}
EnumSet<Flag> flags = EnumSet.noneOf(Flag.class);
try {
int flagInt = BsonReaderTool.getInteger(doc, FLAGS_FIELD, 0);
for (Flag value : Flag.values()) {
if ((flagInt & value.bit) != 0) {
flags.add(value);
}
}
} catch (TypesMismatchException ignore) {
//Nothing to do here
}
BsonDocument storageEngine;
try {
storageEngine = BsonReaderTool.getDocument(doc, STORAGE_ENGINE_FIELD, null);
} catch (TypesMismatchException ex) {
throw new BadValueException("'storageEngine' has to be a document.");
}
if (storageEngine != null) {
if (storageEngine.isEmpty()) {
throw new BadValueException("Empty 'storageEngine' options are invalid. " + "Please remove, or include valid options");
}
for (Entry<?> entry : storageEngine) {
if (!entry.getValue().isDocument()) {
throw new BadValueException("'storageEngie'.'" + entry.getKey() + "' has to be an embedded document");
}
}
}
boolean temp;
try {
temp = BsonReaderTool.getBoolean(doc, TEMP_FIELD, false);
} catch (TypesMismatchException ex) {
throw new BadValueException("Temp field must be a boolean");
}
return new DefaultCollectionOptions(capped, cappedSize, cappedMaxDocs, initialNumExtends, initialExtentSizes.build(), autoIndexMode, flags, storageEngine, temp);
}
use of com.eightkdata.mongowp.bson.BsonDocument in project torodb by torodb.
the class DeleteImplementation method apply.
@Override
public Status<Long> apply(Request req, Command<? super DeleteArgument, ? super Long> command, DeleteArgument arg, WriteMongodTransaction context) {
Long deleted = 0L;
for (DeleteStatement deleteStatement : arg.getStatements()) {
BsonDocument query = deleteStatement.getQuery();
switch(query.size()) {
case 0:
{
deleted += context.getTorodTransaction().deleteAll(req.getDatabase(), arg.getCollection());
break;
}
case 1:
{
try {
logDeleteCommand(arg);
deleted += deleteByAttribute(context.getTorodTransaction(), req.getDatabase(), arg.getCollection(), query);
} catch (CommandFailed ex) {
return Status.from(ex);
}
break;
}
default:
{
return Status.from(ErrorCode.COMMAND_FAILED, "The given query is not supported right now");
}
}
}
mongodMetrics.getDeletes().mark(deleted);
return Status.ok(deleted);
}
Aggregations