Skip to content

Commit

Permalink
Creating index on id-column by default (#282)
Browse files Browse the repository at this point in the history
Former-commit-id: 16ce106
  • Loading branch information
silvanheller authored Mar 20, 2022
1 parent 4ef6cb5 commit 4dcacce
Showing 1 changed file with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import static org.vitrivr.cineast.core.db.setup.AttributeDefinition.AttributeType.TEXT;
import static org.vitrivr.cineast.core.db.setup.AttributeDefinition.AttributeType.VECTOR;
import static org.vitrivr.cineast.core.util.CineastConstants.GENERIC_ID_COLUMN_QUALIFIER;
import static org.vitrivr.cottontail.grpc.CottontailGrpc.IndexType.HASH;
import static org.vitrivr.cottontail.grpc.CottontailGrpc.IndexType.HASH_UQ;

import io.grpc.StatusRuntimeException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import org.vitrivr.cineast.core.data.entities.MediaObjectDescriptor;
Expand Down Expand Up @@ -85,7 +88,7 @@ public boolean createTagEntity() {
this.cottontail.client.create(create);

/* tag ids should be unique */
this.createIndex(entityName, TagReader.TAG_ID_COLUMNNAME, IndexType.HASH_UQ, txId);
this.createIndex(entityName, TagReader.TAG_ID_COLUMNNAME, HASH_UQ, txId);
/* tag names do not necessarily have to be unique */
this.createIndex(entityName, TagReader.TAG_NAME_COLUMNNAME, IndexType.HASH, txId);
/* could be used for autocomplete */
Expand Down Expand Up @@ -114,7 +117,7 @@ public boolean createMultiMediaObjectsEntity() {
this.cottontail.client.create(entity);

/* Create index. */
this.createIndex(entityName, MediaObjectDescriptor.FIELDNAMES[0], IndexType.HASH_UQ, txId);
this.createIndex(entityName, MediaObjectDescriptor.FIELDNAMES[0], HASH_UQ, txId);
this.cottontail.client.commit(txId);
return true;
} catch (StatusRuntimeException e) {
Expand All @@ -141,7 +144,7 @@ public boolean createSegmentEntity() {
this.cottontail.client.create(entity);

/* Create indexes. */
this.createIndex(entityName, MediaSegmentDescriptor.FIELDNAMES[0], IndexType.HASH_UQ, txId);
this.createIndex(entityName, MediaSegmentDescriptor.FIELDNAMES[0], HASH_UQ, txId);
this.createIndex(entityName, MediaSegmentDescriptor.FIELDNAMES[1], IndexType.HASH, txId);
this.cottontail.client.commit(txId);
return true;
Expand Down Expand Up @@ -205,7 +208,9 @@ public boolean createFeatureEntity(String featureEntityName, boolean unique, int
final AttributeDefinition[] attributes = Arrays.stream(featureNames)
.map(s -> new AttributeDefinition(s, VECTOR, length))
.toArray(AttributeDefinition[]::new);
return this.createFeatureEntity(featureEntityName, unique, attributes);
var feature = this.createFeatureEntity(featureEntityName, unique, attributes);
this.createIndex(featureEntityName, GENERIC_ID_COLUMN_QUALIFIER, unique ? IndexType.HASH_UQ : IndexType.HASH, null);
return feature;
}

@Override
Expand All @@ -214,15 +219,19 @@ public boolean createFeatureEntity(String featureEntityName, boolean unique, Att
final HashMap<String, String> hints = new HashMap<>(1);
extended[0] = new AttributeDefinition(GENERIC_ID_COLUMN_QUALIFIER, AttributeDefinition.AttributeType.STRING, hints);
System.arraycopy(attributes, 0, extended, 1, attributes.length);
return this.createEntity(featureEntityName, extended);
var feature = this.createEntity(featureEntityName, extended);
this.createIndex(featureEntityName, GENERIC_ID_COLUMN_QUALIFIER, unique ? IndexType.HASH_UQ : IndexType.HASH, null);
return feature;
}

@Override
public boolean createIdEntity(String entityName, AttributeDefinition... attributes) {
final AttributeDefinition[] extended = new AttributeDefinition[attributes.length + 1];
extended[0] = new AttributeDefinition(GENERIC_ID_COLUMN_QUALIFIER, AttributeDefinition.AttributeType.STRING);
System.arraycopy(attributes, 0, extended, 1, attributes.length);
return this.createEntity(entityName, extended);
var ent = this.createEntity(entityName, extended);
this.createIndex(entityName, GENERIC_ID_COLUMN_QUALIFIER, IndexType.HASH, null);
return ent;
}

@Override
Expand Down Expand Up @@ -341,9 +350,13 @@ public static Type mapAttributeType(AttributeDefinition.AttributeType type) {
}


private void createIndex(String entityName, String attribute, IndexType type, long txId) {
private void createIndex(String entityName, String attribute, IndexType type, Long txId) {
final String indexName = entityName + ".idx_" + attribute + "_" + type.name().toLowerCase();
final CreateIndex index = new CreateIndex(indexName, type).column(entityName + "." + attribute).txId(txId);
final CreateIndex index = new CreateIndex(indexName, type).column(entityName + "." + attribute);
if (txId != null) {
index.txId(txId);
}
this.cottontail.client.create(index);
}

}

0 comments on commit 4dcacce

Please sign in to comment.