Skip to content

Commit

Permalink
Merging changes from steven in #624
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunjeet committed Aug 15, 2023
1 parent 614bbe9 commit 35a5adb
Show file tree
Hide file tree
Showing 8 changed files with 555 additions and 288 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public void indexTypeField(String type, String keyFieldPath) {
typeKeyIndexes.get(type).addFieldIndex(keyFieldPath, history.getLatestState());
}


public void indexTypeField(PrimaryKey primaryKey) {
indexTypeField(primaryKey, history.getLatestState());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.netflix.hollow.core.util.RemovedOrdinalIterator;
import java.util.Arrays;
import java.util.BitSet;
import java.util.HashMap;

public class HollowHistoryTypeKeyIndex {
private final PrimaryKey primaryKey;
Expand All @@ -45,7 +44,6 @@ public class HollowHistoryTypeKeyIndex {
private int maxIndexedOrdinal = 0;

private final HollowOrdinalMapper ordinalMapping;
private final HashMap<Integer, IntList> ordinalFieldHashMapping;


public HollowHistoryTypeKeyIndex(PrimaryKey primaryKey, HollowDataset dataModel) {
Expand All @@ -57,8 +55,7 @@ public HollowHistoryTypeKeyIndex(PrimaryKey primaryKey, HollowDataset dataModel)
this.keyFieldIsIndexed = new boolean[primaryKey.numFields()];
initializeKeyParts(dataModel);

this.ordinalMapping = new HollowOrdinalMapper(primaryKey, keyFieldIsIndexed, keyFieldIndices);
this.ordinalFieldHashMapping = new HashMap<>();
this.ordinalMapping = new HollowOrdinalMapper(primaryKey, keyFieldIsIndexed, keyFieldIndices, fieldTypes);
}

public boolean isInitialized() {
Expand All @@ -77,10 +74,6 @@ public String[] getKeyFields() {
return primaryKey.getFieldPaths();
}

public boolean[] getKeyFieldIsIndexed() {
return keyFieldIsIndexed;
}

public void addFieldIndex(String fieldName, HollowDataset dataModel) {
String[] fieldPathParts = PrimaryKey.getCompleteFieldPathParts(dataModel, primaryKey.getType(), fieldName);
for (int i = 0; i < primaryKey.numFields(); i++) {
Expand Down Expand Up @@ -112,6 +105,10 @@ private FieldType addSchemaField(HollowObjectSchema schema, String[] keyFieldNam
return schema.getFieldType(schemaPosition);
}

public boolean[] getKeyFieldIsIndexed() {
return keyFieldIsIndexed;
}

private void initializeKeyParts(HollowDataset dataModel) {
for (int i = 0; i < primaryKey.numFields(); i++) {
keyFieldNames[i] = PrimaryKey.getCompleteFieldPathParts(dataModel, primaryKey.getType(), primaryKey.getFieldPath(i));
Expand All @@ -128,6 +125,7 @@ public void update(HollowObjectTypeReadState latestTypeState, boolean isDeltaAnd
maxIndexedOrdinal = 0;
populateAllCurrentRecordKeysIntoIndex(latestTypeState);
}
ordinalMapping.prepareForRead();
}

private void populateNewCurrentRecordKeysIntoIndex(HollowObjectTypeReadState typeState) {
Expand All @@ -138,7 +136,7 @@ private void populateNewCurrentRecordKeysIntoIndex(HollowObjectTypeReadState typ
RemovedOrdinalIterator iter = new RemovedOrdinalIterator(populatedOrdinals, previousOrdinals);
int ordinal = iter.next();
while (ordinal != ORDINAL_NONE) {
writeKeyObject(typeState, ordinal, true);
writeKeyObject(typeState, ordinal);
ordinal = iter.next();
}
}
Expand All @@ -152,40 +150,24 @@ private void populateAllCurrentRecordKeysIntoIndex(HollowObjectTypeReadState typ

for (int i = 0; i < maxLength; i++) {
if (populatedOrdinals.get(i) || previousOrdinals.get(i))
writeKeyObject(typeState, i, false);
writeKeyObject(typeState, i);
}
}

private void writeKeyObject(HollowObjectTypeReadState typeState, int ordinal, boolean isDelta) {
private void writeKeyObject(HollowObjectTypeReadState typeState, int ordinal) {
int assignedOrdinal = maxIndexedOrdinal;
int assignedIndex = ordinalMapping.storeNewRecord(typeState, ordinal, assignedOrdinal);
boolean storedUniqueRecord = ordinalMapping.storeNewRecord(typeState, ordinal, assignedOrdinal);

// Identical record already in memory, no need to store fields
if(assignedIndex==ORDINAL_NONE)
if(!storedUniqueRecord)
return;
maxIndexedOrdinal+=1;

for (int i = 0; i < primaryKey.numFields(); i++)
writeKeyField(assignedOrdinal, i);
}

private void writeKeyField(int assignedOrdinal, int fieldIdx) {
if (!keyFieldIsIndexed[fieldIdx])
return;

Object fieldObject = ordinalMapping.getFieldObject(assignedOrdinal, fieldIdx);
int fieldHash = HashCodes.hashInt(HollowReadFieldUtils.hashObject(fieldObject));
if(!ordinalFieldHashMapping.containsKey(fieldHash))
ordinalFieldHashMapping.put(fieldHash, new IntList());

IntList matchingFieldList = ordinalFieldHashMapping.get(fieldHash);
matchingFieldList.add(assignedOrdinal);
}

public String getKeyDisplayString(int keyOrdinal) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < primaryKey.numFields(); i++) {
Object valueAtField = ordinalMapping.getFieldObject(keyOrdinal, i);
Object valueAtField = ordinalMapping.getFieldObject(keyOrdinal, i, fieldTypes[i]);
builder.append(valueAtField);
if (i < primaryKey.numFields() - 1)
builder.append(MULTI_FIELD_KEY_DELIMITER);
Expand Down Expand Up @@ -231,28 +213,13 @@ public IntList queryIndexedFields(final String query) {
break;
default:
}
addMatches(HashCodes.hashInt(hashCode), objectToFind, i, matchingKeys);
ordinalMapping.addMatches(HashCodes.hashInt(hashCode), objectToFind, i, fieldTypes[i], matchingKeys);
} catch(NumberFormatException ignore) {}
}
return matchingKeys;
}

public void addMatches(int hashCode, Object objectToMatch, int field, IntList results) {
if (!ordinalFieldHashMapping.containsKey(hashCode))
return;

IntList matchingOrdinals = ordinalFieldHashMapping.get(hashCode);
for(int i=0;i<matchingOrdinals.size();i++) {
int ordinal = matchingOrdinals.get(i);

Object matchingObject = ordinalMapping.getFieldObject(ordinal, field);
if(objectToMatch.equals(matchingObject)) {
results.add(ordinal);
}
}
}

public Object getKeyFieldValue(int keyFieldIdx, int keyOrdinal) {
return ordinalMapping.getFieldObject(keyOrdinal, keyFieldIdx);
return ordinalMapping.getFieldObject(keyOrdinal, keyFieldIdx, fieldTypes[keyFieldIdx]);
}
}
Loading

0 comments on commit 35a5adb

Please sign in to comment.