}
@Override
public HKey ancestorHKey(Table table)
{
HKey ancestorHKey;
IndexToHKey indexToHKey;
if (index.isGroupIndex()) {
ancestorHKey = keyCreator.newHKey(table.hKey());
indexToHKey = ((GroupIndex)index).indexToHKey(table.getDepth());
} else {
ancestorHKey = keyCreator.newHKey(index.leafMostTable().hKey());
indexToHKey = ((TableIndex)index).indexToHKey();
}
Key hKey = keyCreator.createKey();
hKey.clear();
for (int i = 0; i < indexToHKey.getLength(); i++) {
if (indexToHKey.isOrdinal(i)) {
hKey.append(indexToHKey.getOrdinal(i));
} else {
int indexField = indexToHKey.getIndexRowPosition(i);
if (index.isSpatial()) {
// A spatial index has a single key column (the z-value), representing the declared spatial key columns.
if (indexField > index.firstSpatialArgument())
indexField -= index.dimensions() - 1;
}
Key keySource = iKey;
if (indexField < 0 || indexField > keySource.getDepth()) {
throw new IllegalStateException(String.format("keySource: %s, indexField: %s",
keySource, indexField));
}
PersistitKey.appendFieldFromKey(hKey, keySource, indexField, index.getIndexName());
}
}
ancestorHKey.copyFrom(hKey);
if (index.isTableIndex() && index.leafMostTable() != table) {
this.leafTableHKey = ancestorHKey;
ancestorHKey = keyCreator.newHKey(table.hKey());
this.leafTableHKey.copyTo(ancestorHKey);
ancestorHKey.useSegments(table.getDepth() + 1);
}
return ancestorHKey;
}