use of com.torodb.core.transaction.metainf.MutableMetaCollection in project torodb by torodb.
the class SqlWriteTorodTransaction method dropCollection.
@Override
public void dropCollection(String db, String collection) throws RollbackException, UserException {
MutableMetaDatabase metaDb = getMetaDatabaseOrThrowException(db);
MutableMetaCollection metaColl = getMetaCollectionOrThrowException(metaDb, collection);
getInternalTransaction().getBackendTransaction().dropCollection(metaDb, metaColl);
metaDb.removeMetaCollectionByName(collection);
}
use of com.torodb.core.transaction.metainf.MutableMetaCollection in project torodb by torodb.
the class SqlWriteTorodTransaction method dropIndex.
@SuppressFBWarnings(value = "RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT", justification = "Findbugs thinks MutableMetaCollection#removeMetaIndexByName" + "has no side effect")
@Override
public boolean dropIndex(String dbName, String colName, String indexName) {
MutableMetaDatabase db = getInternalTransaction().getMetaSnapshot().getMetaDatabaseByName(dbName);
if (db == null) {
return false;
}
MutableMetaCollection col = db.getMetaCollectionByName(colName);
if (col == null) {
return false;
}
MetaIndex index = col.getMetaIndexByName(indexName);
if (index == null) {
return false;
}
col.removeMetaIndexByName(indexName);
getInternalTransaction().getBackendTransaction().dropIndex(db, col, index);
return true;
}
use of com.torodb.core.transaction.metainf.MutableMetaCollection in project torodb by torodb.
the class SqlWriteTorodTransaction method createMetaCollection.
protected MutableMetaCollection createMetaCollection(MutableMetaDatabase metaDb, String colName) {
MutableMetaCollection metaCol;
Preconditions.checkState(!isClosed());
metaCol = metaDb.addMetaCollection(colName, getConnection().getServer().getIdentifierFactory().toCollectionIdentifier(getInternalTransaction().getMetaSnapshot(), metaDb.getName(), colName));
getInternalTransaction().getBackendTransaction().addCollection(metaDb, metaCol);
return metaCol;
}
use of com.torodb.core.transaction.metainf.MutableMetaCollection 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.MutableMetaCollection in project torodb by torodb.
the class SameThreadInsertPipeline method insert.
@Override
public void insert(Stream<KvDocument> docs) throws RollbackException, UserException {
D2RTranslationBatchFunction d2rFun = new D2RTranslationBatchFunction(translatorFactory, metaDb, mutableMetaCollection);
DefaultToBackendFunction r2BackendFun = new DefaultToBackendFunction(jobFactory, metaDb, mutableMetaCollection);
try {
Iterators.partition(docs.iterator(), docBatchSize).forEachRemaining(list -> {
CollectionData collData = d2rFun.apply(list);
Iterable<BackendTransactionJob> jobs = r2BackendFun.apply(collData);
jobs.forEach(Unchecked.consumer(job -> job.execute(backendConnection)));
});
} catch (UncheckedException ex) {
Throwable cause = ex.getCause();
if (cause != null && cause instanceof UserException) {
throw (UserException) cause;
}
throw ex;
}
}
Aggregations