use of com.torodb.core.language.AttributeReference in project torodb by torodb.
the class IncrementUpdateActionTest method testIllegalPath.
@Test(expected = UserException.class)
public void testIllegalPath() 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.ArrayKey(2), new AttributeReference.ArrayKey(2)))), KvInteger.of(1)).apply(builder);
;
}
use of com.torodb.core.language.AttributeReference in project torodb by torodb.
the class CreateCollectionReplImpl method apply.
@Override
public Status<Empty> apply(Request req, Command<? super CreateCollectionArgument, ? super Empty> command, CreateCollectionArgument arg, SharedWriteTorodTransaction trans) {
try {
LOGGER.info("Creating collection {}.{}", req.getDatabase(), arg.getCollection());
if (arg.getOptions().isCapped()) {
LOGGER.info("Ignoring capped flag for collection {}.{}", req.getDatabase(), arg.getCollection());
}
if (!trans.existsCollection(req.getDatabase(), arg.getCollection())) {
trans.createIndex(req.getDatabase(), arg.getCollection(), Constants.ID_INDEX, ImmutableList.of(new IndexFieldInfo(new AttributeReference(Arrays.asList(new Key[] { new ObjectKey(Constants.ID) })), FieldIndexOrdering.ASC.isAscending())), true);
}
trans.createCollection(req.getDatabase(), arg.getCollection());
} catch (UserException ex) {
reportErrorIgnored(LOGGER, command, ex);
}
return Status.ok();
}
use of com.torodb.core.language.AttributeReference in project torodb by torodb.
the class SqlWriteTorodTransaction method createIndex.
@Override
public boolean createIndex(String dbName, String colName, String indexName, List<IndexFieldInfo> fields, boolean unique) throws UserException {
if (fields.size() > 1) {
throw new UnsupportedCompoundIndexException(dbName, colName, indexName);
}
MutableMetaDatabase metaDb = getOrCreateMetaDatabase(dbName);
MutableMetaCollection metaColl = getOrCreateMetaCollection(metaDb, colName);
List<Tuple3<TableRef, String, FieldIndexOrdering>> indexFieldDefs = new ArrayList<>(fields.size());
for (IndexFieldInfo field : fields) {
AttributeReference attRef = field.getAttributeReference();
FieldIndexOrdering ordering = field.isAscending() ? FieldIndexOrdering.ASC : FieldIndexOrdering.DESC;
TableRef tableRef = extractTableRef(attRef);
String lastKey = extractKeyName(attRef.getKeys().get(attRef.getKeys().size() - 1));
indexFieldDefs.add(new Tuple3<>(tableRef, lastKey, ordering));
}
if (unique) {
TableRef anyIndexTableRef = indexFieldDefs.stream().findAny().get().v1();
boolean isUniqueIndexWithMutlipleTableRefs = indexFieldDefs.stream().anyMatch(t -> !t.v1().equals(anyIndexTableRef));
if (isUniqueIndexWithMutlipleTableRefs) {
throw new UnsupportedUniqueIndexException(dbName, colName, indexName);
}
}
boolean indexExists = metaColl.streamContainedMetaIndexes().anyMatch(index -> index.getName().equals(indexName) || (index.isUnique() == unique && index.size() == indexFieldDefs.size() && Seq.seq(index.iteratorFields()).allMatch(indexField -> {
Tuple3<TableRef, String, FieldIndexOrdering> indexFieldDef = indexFieldDefs.get(indexField.getPosition());
return indexFieldDef != null && indexFieldDef.v1().equals(indexField.getTableRef()) && indexFieldDef.v2().equals(indexField.getName()) && indexFieldDef.v3() == indexField.getOrdering();
})));
if (!indexExists) {
MutableMetaIndex metaIndex = metaColl.addMetaIndex(indexName, unique);
for (Tuple3<TableRef, String, FieldIndexOrdering> indexFieldDef : indexFieldDefs) {
metaIndex.addMetaIndexField(indexFieldDef.v1(), indexFieldDef.v2(), indexFieldDef.v3());
}
getInternalTransaction().getBackendTransaction().createIndex(metaDb, metaColl, metaIndex);
}
return !indexExists;
}
use of com.torodb.core.language.AttributeReference in project torodb by torodb.
the class IndexedAttributes method toString.
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (AttributeReference attribute : attributes) {
sb.append(attribute).append(' ');
sb.append("(");
sb.append(orderingInfo.get(attribute).name());
sb.append(")");
sb.append(", ");
}
sb.delete(sb.length() - 2, sb.length());
return sb.toString();
}
use of com.torodb.core.language.AttributeReference in project torodb by torodb.
the class ToroIndexConverter method from.
@Override
public NamedToroIndex from(String databaseObject) {
JsonReader reader = Json.createReader(new StringReader(databaseObject));
JsonObject object = reader.readObject();
IndexedAttributes.Builder builder = new IndexedAttributes.Builder();
JsonArray attsArray = object.getJsonArray(ATTS_KEY);
Set<Integer> descendingAttPos;
if (object.containsKey(DESCENDING)) {
JsonArray descArray = object.getJsonArray(DESCENDING);
descendingAttPos = Sets.newHashSetWithExpectedSize(descArray.size());
for (int i = 0; i < descArray.size(); i++) {
descendingAttPos.add(descArray.getInt(i));
}
} else {
descendingAttPos = Collections.emptySet();
}
for (int i = 0; i < attsArray.size(); i++) {
String att = attsArray.getString(i);
AttributeReference attRef = parseAttRef(att);
if (descendingAttPos.contains(i)) {
builder.addAttribute(attRef, IndexType.desc);
} else {
builder.addAttribute(attRef, IndexType.asc);
}
}
return new DefaultNamedToroIndex(object.getString(NAME_KEY), builder.build(), databaseName, collectionName, object.getBoolean(UNIQUE_KEY, false));
}
Aggregations