use of com.torodb.core.language.AttributeReference in project torodb by torodb.
the class CreateIndexesImplementation method apply.
@Override
public Status<CreateIndexesResult> apply(Request req, Command<? super CreateIndexesArgument, ? super CreateIndexesResult> command, CreateIndexesArgument arg, WriteMongodTransaction context) {
int indexesBefore = (int) context.getTorodTransaction().getIndexesInfo(req.getDatabase(), arg.getCollection()).count();
int indexesAfter = indexesBefore;
try {
boolean existsCollection = context.getTorodTransaction().existsCollection(req.getDatabase(), arg.getCollection());
if (!existsCollection) {
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);
}
boolean createdCollectionAutomatically = !existsCollection;
for (IndexOptions indexOptions : arg.getIndexesToCreate()) {
if (indexOptions.getKeys().size() < 1) {
return Status.from(ErrorCode.CANNOT_CREATE_INDEX, "Index keys cannot be empty.");
}
if (indexOptions.isBackground()) {
throw new CommandFailed("createIndexes", "Building index in background is not supported right now");
}
if (indexOptions.isSparse()) {
throw new CommandFailed("createIndexes", "Sparse index are not supported right now");
}
List<IndexFieldInfo> fields = new ArrayList<>(indexOptions.getKeys().size());
for (IndexOptions.Key indexKey : indexOptions.getKeys()) {
AttributeReference.Builder attRefBuilder = new AttributeReference.Builder();
for (String key : indexKey.getKeys()) {
attRefBuilder.addObjectKey(key);
}
IndexType indexType = indexKey.getType();
if (!KnownType.contains(indexType)) {
return Status.from(ErrorCode.CANNOT_CREATE_INDEX, "bad index key pattern: Unknown index plugin '" + indexKey.getType().getName() + "'");
}
Optional<FieldIndexOrdering> ordering = indexType.accept(filedIndexOrderingConverterVisitor, null);
if (!ordering.isPresent()) {
throw new CommandFailed("createIndexes", "Index of type " + indexType.getName() + " is not supported right now");
}
fields.add(new IndexFieldInfo(attRefBuilder.build(), ordering.get().isAscending()));
}
if (context.getTorodTransaction().createIndex(req.getDatabase(), arg.getCollection(), indexOptions.getName(), fields, indexOptions.isUnique())) {
indexesAfter++;
}
}
String note = null;
if (indexesAfter == indexesBefore) {
note = "all indexes already exist";
}
return Status.ok(new CreateIndexesResult(indexesBefore, indexesAfter, note, createdCollectionAutomatically));
} catch (UserException ex) {
return Status.from(ErrorCode.COMMAND_FAILED, ex.getLocalizedMessage());
} catch (CommandFailed ex) {
return Status.from(ex);
}
}
use of com.torodb.core.language.AttributeReference in project torodb by torodb.
the class IncrementUpdateActionTest method testNullAttribute1.
@Test(expected = UserException.class)
public void testNullAttribute1() throws UpdateException {
UpdatedToroDocumentBuilder builder = UpdatedToroDocumentBuilder.create();
builder.newObject("f1").newArray("f2").setValue(3, KvInteger.of(3));
new IncrementUpdateAction(Lists.<AttributeReference>newArrayList(new AttributeReference(Lists.<AttributeReference.Key<?>>newArrayList(new AttributeReference.ObjectKey("f1"), new AttributeReference.ObjectKey("f2"), new AttributeReference.ArrayKey(2)))), KvInteger.of(1)).apply(builder);
;
}
use of com.torodb.core.language.AttributeReference in project torodb by torodb.
the class UpdateActionTranslator method translateSetField.
private static void translateSetField(CompositeUpdateAction.Builder builder, BsonDocument argument) {
for (Entry<?> entry : argument) {
Collection<AttributeReference> attRefs = parseAttributeReference(entry.getKey());
KvValue<?> translatedValue = MongoWpConverter.translate(entry.getValue());
builder.add(new SetFieldUpdateAction(attRefs, translatedValue), false);
}
}
use of com.torodb.core.language.AttributeReference in project torodb by torodb.
the class UpdateActionTranslator method translateUnsetField.
private static void translateUnsetField(CompositeUpdateAction.Builder builder, BsonDocument argument) {
for (Entry<?> entry : argument) {
Collection<AttributeReference> attRefs = parseAttributeReference(entry.getKey());
builder.add(new UnsetFieldUpdateAction(attRefs), false);
}
}
use of com.torodb.core.language.AttributeReference in project torodb by torodb.
the class ToroIndexConverter method to.
@Override
public String to(NamedToroIndex userObject) {
JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
objectBuilder.add(NAME_KEY, userObject.getName());
if (userObject.isUnique()) {
objectBuilder.add(UNIQUE_KEY, true);
}
JsonArrayBuilder attsBuilder = Json.createArrayBuilder();
JsonArrayBuilder descBuilder = Json.createArrayBuilder();
int attPosition = 0;
boolean hasDescending = false;
for (Map.Entry<AttributeReference, IndexType> entry : userObject.getAttributes().entrySet()) {
attsBuilder.add(entry.getKey().toString());
if (IndexType.desc.equals(entry.getValue())) {
descBuilder.add(attPosition);
hasDescending = true;
}
attPosition++;
}
objectBuilder.add(ATTS_KEY, attsBuilder);
if (hasDescending) {
objectBuilder.add(DESCENDING, descBuilder);
}
StringWriter stringWriter = new StringWriter(200);
JsonWriter jsonWriter = Json.createWriter(stringWriter);
jsonWriter.writeObject(objectBuilder.build());
return stringWriter.toString();
}
Aggregations