use of com.torodb.kvdocument.values.KvDocument in project torodb by torodb.
the class R2DTranslatorTest method readDocumentInArray.
/*
* Document: { "name" : "jero", "numbers" : [ 666, { "address": "myhome" } ] }
*/
@Test
public void readDocumentInArray() {
int did = 1;
/*
* Root Level
*/
MetaDocPartBuilder builder = new MetaDocPartBuilder(rootRef);
builder.addMetaField("name", "name_s", FieldType.STRING);
builder.addMetaField("numbers", "numbers_e", FieldType.CHILD);
builder.addRow(did, null, did, null, "jero", IsArray);
MockedDocPartResult root = builder.getResultSet();
/*
* Second Level
*/
int pid = did;
TableRef secondRef = fact.createChild(rootRef, "numbers");
MetaDocPartBuilder secondBuilder = new MetaDocPartBuilder(secondRef);
secondBuilder.addMetaScalar("v_i", FieldType.INTEGER);
secondBuilder.addMetaField("address", "address_s", FieldType.STRING);
secondBuilder.addRow(did, pid, 20, 0, 666, null);
secondBuilder.addRow(did, pid, 21, 1, null, "myhome");
MockedDocPartResult secondLevel = secondBuilder.getResultSet();
List<DocPartResult> lst = Lists.newArrayList(secondLevel, root);
R2DTranslator r2dTranslator = new R2DTranslatorImpl();
Collection<ToroDocument> readedDocuments = r2dTranslator.translate(lst.iterator());
assertEquals(1, readedDocuments.size());
KvDocument doc = readedDocuments.iterator().next().getRoot();
assertEquals("jero", doc.get("name").getValue());
KvValue<?> kvValue = doc.get("numbers");
assertTrue(kvValue instanceof KvArray);
KvArray array = (KvArray) kvValue;
assertEquals(2, array.size());
assertEquals(666, array.get(0).getValue());
KvValue<?> kvValueSecond = array.get(1);
assertNotNull(kvValueSecond);
assertTrue(kvValueSecond instanceof KvDocument);
KvDocument doc2 = (KvDocument) kvValueSecond;
assertEquals("myhome", doc2.get("address").getValue());
}
use of com.torodb.kvdocument.values.KvDocument in project torodb by torodb.
the class R2DTranslatorTest method readDocumentWithMultipleFields.
/*
* Document: { "name" : "jero", "address" : "my home", "age" : 25 }
*/
@Test
public void readDocumentWithMultipleFields() {
MetaDocPartBuilder builder = new MetaDocPartBuilder(rootRef);
builder.addMetaField("name", "name_s", FieldType.STRING);
builder.addMetaField("address", "address_s", FieldType.STRING);
builder.addMetaField("age", "age_i", FieldType.INTEGER);
builder.addRow(1, null, 1, null, "jero", "my home", 25);
MockedDocPartResult root = builder.getResultSet();
List<DocPartResult> lst = Collections.singletonList(root);
R2DTranslator r2dTranslator = new R2DTranslatorImpl();
Collection<ToroDocument> readedDocuments = r2dTranslator.translate(lst.iterator());
assertEquals(1, readedDocuments.size());
KvDocument doc = readedDocuments.iterator().next().getRoot();
assertEquals("jero", doc.get("name").getValue());
assertEquals("my home", doc.get("address").getValue());
assertEquals(25, doc.get("age").getValue());
}
use of com.torodb.kvdocument.values.KvDocument in project torodb by torodb.
the class Document2RelStackTest method parseDocument.
private CollectionData parseDocument(String... docNames) {
MockRidGenerator ridGenerator = new MockRidGenerator();
IdentifierFactory identifierFactory = new DefaultIdentifierFactory(new MockIdentifierInterface());
MutableMetaDatabase db = mutableSnapshot.getMetaDatabaseByName(DB1);
D2RTranslator translator = new D2RTranslatorStack(tableRefFactory, identifierFactory, ridGenerator, db, db.getMetaCollectionByName(COLLA));
for (String doc : docNames) {
KvDocument document = parser.createFromResource("docs/" + doc);
translator.translate(document);
}
return translator.getCollectionDataAccumulator();
}
use of com.torodb.kvdocument.values.KvDocument 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.kvdocument.values.KvDocument 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)));
}
Aggregations