use of com.torodb.torod.IndexFieldInfo in project torodb by torodb.
the class DropIndexesImplementation method indexFieldsMatchKeys.
private boolean indexFieldsMatchKeys(IndexInfo index, List<IndexOptions.Key> keys) {
if (index.getFields().size() != keys.size()) {
return false;
}
Iterator<IndexFieldInfo> fieldsIterator = index.getFields().iterator();
Iterator<IndexOptions.Key> keysIterator = keys.iterator();
while (fieldsIterator.hasNext()) {
IndexFieldInfo field = fieldsIterator.next();
IndexOptions.Key key = keysIterator.next();
if ((field.isAscending() && key.getType() != KnownType.asc.getIndexType()) || (!field.isAscending() && key.getType() != KnownType.desc.getIndexType()) || (field.getAttributeReference().getKeys().size() != key.getKeys().size())) {
return false;
}
Iterator<AttributeReference.Key<?>> fieldKeysIterator = field.getAttributeReference().getKeys().iterator();
Iterator<String> keyKeysIterator = key.getKeys().iterator();
while (fieldKeysIterator.hasNext()) {
AttributeReference.Key<?> fieldKey = fieldKeysIterator.next();
String keyKey = keyKeysIterator.next();
if (!fieldKey.toString().equals(keyKey)) {
return false;
}
}
}
return true;
}
use of com.torodb.torod.IndexFieldInfo 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.torod.IndexFieldInfo in project torodb by torodb.
the class DropIndexesReplImpl method indexFieldsMatchKeys.
private boolean indexFieldsMatchKeys(IndexInfo index, List<IndexOptions.Key> keys) {
if (index.getFields().size() != keys.size()) {
return false;
}
Iterator<IndexFieldInfo> fieldsIterator = index.getFields().iterator();
Iterator<IndexOptions.Key> keysIterator = keys.iterator();
while (fieldsIterator.hasNext()) {
IndexFieldInfo field = fieldsIterator.next();
IndexOptions.Key key = keysIterator.next();
if ((field.isAscending() && key.getType() != KnownType.asc.getIndexType()) || (!field.isAscending() && key.getType() != KnownType.desc.getIndexType()) || (field.getAttributeReference().getKeys().size() != key.getKeys().size())) {
return false;
}
Iterator<AttributeReference.Key<?>> fieldKeysIterator = field.getAttributeReference().getKeys().iterator();
Iterator<String> keyKeysIterator = key.getKeys().iterator();
while (fieldKeysIterator.hasNext()) {
AttributeReference.Key<?> fieldKey = fieldKeysIterator.next();
String keyKey = keyKeysIterator.next();
if (!fieldKey.toString().equals(keyKey)) {
return false;
}
}
}
return true;
}
Aggregations