use of com.torodb.core.TableRef in project torodb by torodb.
the class BatchMetaCollectionTest method testAddMetaDocPart.
@Test
public void testAddMetaDocPart() {
TableRef tableRef = tableRefFactory.createChild(tableRefFactory.createRoot(), "aPath");
String tableId = "aTableId";
BatchMetaDocPart newDocPart = collection.addMetaDocPart(tableRef, tableId);
assertEquals(newDocPart, collection.getMetaDocPartByTableRef(tableRef));
assertNotNull(newDocPart);
verify(delegate).addMetaDocPart(tableRef, tableId);
}
use of com.torodb.core.TableRef in project torodb by torodb.
the class SqlTorodTransaction method findByAttRef.
@Override
public TorodCursor findByAttRef(String dbName, String colName, AttributeReference attRef, KvValue<?> value) {
MetaDatabase db = getInternalTransaction().getMetaSnapshot().getMetaDatabaseByName(dbName);
if (db == null) {
LOGGER.trace("Db with name " + dbName + " does not exist. An empty cursor is returned");
return new EmptyTorodCursor();
}
MetaCollection col = db.getMetaCollectionByName(colName);
if (col == null) {
LOGGER.trace("Collection " + dbName + '.' + colName + " does not exist. An empty cursor is returned");
return new EmptyTorodCursor();
}
TableRef ref = extractTableRef(attRef);
String lastKey = extractKeyName(attRef.getKeys().get(attRef.getKeys().size() - 1));
MetaDocPart docPart = col.getMetaDocPartByTableRef(ref);
if (docPart == null) {
LOGGER.trace("DocPart " + dbName + '.' + colName + '.' + ref + " does not exist. An empty cursor is returned");
return new EmptyTorodCursor();
}
MetaField field = docPart.getMetaFieldByNameAndType(lastKey, FieldType.from(value.getType()));
if (field == null) {
LOGGER.trace("Field " + dbName + '.' + colName + '.' + ref + '.' + lastKey + " does not exist. An empty cursor is returned");
return new EmptyTorodCursor();
}
return toToroCursor(getInternalTransaction().getBackendTransaction().findByField(db, col, docPart, field, value));
}
use of com.torodb.core.TableRef 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.TableRef in project torodb by torodb.
the class SharedWriteBackendTransactionImpl method dropIndex.
@Override
public void dropIndex(MetaDatabase db, MutableMetaCollection col, MetaIndex index) {
Preconditions.checkState(!isClosed(), "This transaction is closed");
getSqlInterface().getMetaDataWriteInterface().deleteMetaIndex(getDsl(), db, col, index);
Iterator<TableRef> tableRefIterator = index.streamTableRefs().iterator();
while (tableRefIterator.hasNext()) {
TableRef tableRef = tableRefIterator.next();
MutableMetaDocPart docPart = col.getMetaDocPartByTableRef(tableRef);
if (docPart != null) {
Iterator<? extends MetaIdentifiedDocPartIndex> docPartIndexesIterator = docPart.streamIndexes().iterator();
while (docPartIndexesIterator.hasNext()) {
MetaIdentifiedDocPartIndex docPartIndex = docPartIndexesIterator.next();
if (index.isCompatible(docPart, docPartIndex)) {
boolean existsAnyOtherCompatibleIndex = col.streamContainedMetaIndexes().anyMatch(otherIndex -> otherIndex != index && otherIndex.isCompatible(docPart, docPartIndex));
if (!existsAnyOtherCompatibleIndex) {
dropIndex(db, col, docPart, docPartIndex);
LOGGER.info("Dropped index {} for table {}", docPartIndex.getIdentifier(), docPart.getIdentifier());
}
}
}
}
}
}
use of com.torodb.core.TableRef in project torodb by torodb.
the class SharedWriteBackendTransactionImpl method addDocPart.
@Override
public void addDocPart(MetaDatabase db, MetaCollection col, MetaDocPart newDocPart) {
Preconditions.checkState(!isClosed(), "This transaction is closed");
getSqlInterface().getMetaDataWriteInterface().addMetaDocPart(getDsl(), db, col, newDocPart);
TableRef tableRef = newDocPart.getTableRef();
if (tableRef.isRoot()) {
getSqlInterface().getStructureInterface().createRootDocPartTable(getDsl(), db.getIdentifier(), newDocPart.getIdentifier(), tableRef);
getSqlInterface().getStructureInterface().streamRootDocPartTableIndexesCreation(db.getIdentifier(), newDocPart.getIdentifier(), tableRef).forEach(consumer -> {
String index = consumer.apply(getDsl());
LOGGER.info("Created internal index {} for table {}", index, newDocPart.getIdentifier());
});
} else {
getSqlInterface().getStructureInterface().createDocPartTable(getDsl(), db.getIdentifier(), newDocPart.getIdentifier(), tableRef, col.getMetaDocPartByTableRef(tableRef.getParent().get()).getIdentifier());
getSqlInterface().getStructureInterface().streamDocPartTableIndexesCreation(db.getIdentifier(), newDocPart.getIdentifier(), tableRef, col.getMetaDocPartByTableRef(tableRef.getParent().get()).getIdentifier()).forEach(consumer -> {
String index = consumer.apply(getDsl());
LOGGER.info("Created internal index {} for table {}", index, newDocPart.getIdentifier());
});
}
}
Aggregations