
io.permazen.core.CompositeIndexStorageInfo Maven / Gradle / Ivy
/*
* Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
*/
package io.permazen.core;
import java.util.ArrayList;
import java.util.List;
/**
* Represents a composite index.
*/
class CompositeIndexStorageInfo extends IndexStorageInfo {
final List storageIds;
final List> fieldTypes;
CompositeIndexStorageInfo(CompositeIndex index) {
super(index.storageId);
// Gather field storage ID's
this.storageIds = new ArrayList<>(index.fields.size());
index.fields.stream()
.map(SimpleField::getStorageId)
.forEach(this.storageIds::add);
// Gather field types, genericized for indexing
this.fieldTypes = new ArrayList<>(index.fields.size());
index.fields.stream()
.map(SimpleField::getFieldType)
.map(FieldType::genericizeForIndex)
.forEach(this.fieldTypes::add);
}
Object getIndex(Transaction tx) {
switch (this.storageIds.size()) {
case 2:
return this.buildIndex(tx, this.fieldTypes.get(0), this.fieldTypes.get(1));
case 3:
return this.buildIndex(tx, this.fieldTypes.get(0), this.fieldTypes.get(1), this.fieldTypes.get(2));
case 4:
return this.buildIndex(tx, this.fieldTypes.get(0), this.fieldTypes.get(1), this.fieldTypes.get(2),
this.fieldTypes.get(3));
// COMPOSITE-INDEX
default:
throw new RuntimeException("internal error");
}
}
// This method exists solely to bind the generic type parameters
private CoreIndex2 buildIndex(Transaction tx,
FieldType value1Type,
FieldType value2Type) {
return new CoreIndex2<>(tx.kvt, new Index2View<>(this.storageId,
value1Type,
value2Type,
FieldTypeRegistry.OBJ_ID));
}
// This method exists solely to bind the generic type parameters
private CoreIndex3 buildIndex(Transaction tx,
FieldType value1Type,
FieldType value2Type,
FieldType value3Type) {
return new CoreIndex3<>(tx.kvt, new Index3View<>(this.storageId,
value1Type,
value2Type,
value3Type,
FieldTypeRegistry.OBJ_ID));
}
// This method exists solely to bind the generic type parameters
private CoreIndex4 buildIndex(Transaction tx,
FieldType value1Type,
FieldType value2Type,
FieldType value3Type,
FieldType value4Type) {
return new CoreIndex4<>(tx.kvt, new Index4View<>(this.storageId,
value1Type,
value2Type,
value3Type,
value4Type,
FieldTypeRegistry.OBJ_ID));
}
// Object
@Override
public String toString() {
return "composite index on fields " + this.fieldTypes;
}
@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (!super.equals(obj))
return false;
final CompositeIndexStorageInfo that = (CompositeIndexStorageInfo)obj;
return this.storageIds.equals(that.storageIds) && this.fieldTypes.equals(that.fieldTypes);
}
@Override
public int hashCode() {
return super.hashCode() ^ this.storageIds.hashCode() ^ this.fieldTypes.hashCode();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy