use of com.torodb.core.transaction.metainf.MutableMetaIndex 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.transaction.metainf.MutableMetaIndex in project torodb by torodb.
the class ExclusiveWriteBackendTransactionImpl method copyMetaCollection.
private void copyMetaCollection(MetaDatabase fromDb, MetaCollection fromColl, MutableMetaDatabase toDb, MutableMetaCollection toColl) {
IdentifierFactory identifierFactory = getIdentifierFactory();
Iterator<? extends MetaIndex> fromMetaIndexIterator = fromColl.streamContainedMetaIndexes().iterator();
while (fromMetaIndexIterator.hasNext()) {
MetaIndex fromMetaIndex = fromMetaIndexIterator.next();
MutableMetaIndex toMetaIndex = toColl.addMetaIndex(fromMetaIndex.getName(), fromMetaIndex.isUnique());
getSqlInterface().getMetaDataWriteInterface().addMetaIndex(getDsl(), toDb, toColl, toMetaIndex);
copyIndexFields(fromMetaIndex, toDb, toColl, toMetaIndex);
}
Iterator<? extends MetaDocPart> fromMetaDocPartIterator = fromColl.streamContainedMetaDocParts().iterator();
while (fromMetaDocPartIterator.hasNext()) {
MetaDocPart fromMetaDocPart = fromMetaDocPartIterator.next();
MutableMetaDocPart toMetaDocPart = toColl.addMetaDocPart(fromMetaDocPart.getTableRef(), identifierFactory.toDocPartIdentifier(toDb, toColl.getName(), fromMetaDocPart.getTableRef()));
getSqlInterface().getMetaDataWriteInterface().addMetaDocPart(getDsl(), toDb, toColl, toMetaDocPart);
copyScalar(identifierFactory, fromMetaDocPart, toDb, toColl, toMetaDocPart);
copyFields(identifierFactory, fromMetaDocPart, toDb, toColl, toMetaDocPart);
copyIndexes(identifierFactory, fromMetaDocPart, toDb, toColl, toMetaDocPart);
int nextRid = ridGenerator.getDocPartRidGenerator(fromDb.getName(), fromColl.getName()).nextRid(fromMetaDocPart.getTableRef());
ridGenerator.getDocPartRidGenerator(toDb.getName(), toColl.getName()).setNextRid(toMetaDocPart.getTableRef(), nextRid - 1);
}
}
Aggregations