Skip to content

Commit

Permalink
chore: make FlatRecordTraversalNode concurrent safe (#699)
Browse files Browse the repository at this point in the history
* chore: make FlatRecordTraversalNode concurrent safe

* keep only one path
  • Loading branch information
eduardoramirez authored Oct 25, 2024
1 parent 60e0fdd commit a4568a7
Show file tree
Hide file tree
Showing 13 changed files with 532 additions and 707 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@
import com.netflix.hollow.api.objects.HollowRecord;
import com.netflix.hollow.api.objects.generic.GenericHollowList;
import com.netflix.hollow.core.schema.HollowListSchema;
import com.netflix.hollow.core.schema.HollowSchema;
import com.netflix.hollow.core.util.IntList;
import com.netflix.hollow.core.write.HollowListTypeWriteState;
import com.netflix.hollow.core.write.HollowListWriteRecord;
import com.netflix.hollow.core.write.HollowTypeWriteState;
import com.netflix.hollow.core.write.HollowWriteRecord;
import com.netflix.hollow.core.write.objectmapper.flatrecords.FlatRecordReader;
import com.netflix.hollow.core.write.objectmapper.flatrecords.FlatRecordWriter;
import com.netflix.hollow.core.write.objectmapper.flatrecords.traversal.FlatRecordTraversalListNode;
import com.netflix.hollow.core.write.objectmapper.flatrecords.traversal.FlatRecordTraversalNode;
Expand All @@ -34,7 +32,6 @@
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class HollowListTypeMapper extends HollowTypeMapper {
Expand Down Expand Up @@ -129,25 +126,11 @@ protected Object parseHollowRecord(HollowRecord record) {
}

@Override
protected Object parseFlatRecord(HollowSchema recordSchema, FlatRecordReader reader, Map<Integer, Object> parsedObjects) {
List<Object> collection = new ArrayList<>();

int size = reader.readCollectionSize();
for (int i = 0; i < size; i++) {
int ordinal = reader.readOrdinal();
Object element = parsedObjects.get(ordinal);
collection.add(element);
}

return collection;
}

@Override
protected Object parseFlatRecordTraversalNode(FlatRecordTraversalNode node) {
protected Object parseFlatRecord(FlatRecordTraversalNode node) {
List<Object> collection = new ArrayList<>();

for (FlatRecordTraversalNode elementNode : (FlatRecordTraversalListNode) node) {
Object element = elementMapper.parseFlatRecordTraversalNode(elementNode);
Object element = elementMapper.parseFlatRecord(elementNode);
collection.add(element);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@
import com.netflix.hollow.api.objects.HollowRecord;
import com.netflix.hollow.api.objects.generic.GenericHollowMap;
import com.netflix.hollow.core.schema.HollowMapSchema;
import com.netflix.hollow.core.schema.HollowSchema;
import com.netflix.hollow.core.util.HollowObjectHashCodeFinder;
import com.netflix.hollow.core.write.HollowMapTypeWriteState;
import com.netflix.hollow.core.write.HollowMapWriteRecord;
import com.netflix.hollow.core.write.HollowTypeWriteState;
import com.netflix.hollow.core.write.HollowWriteRecord;
import com.netflix.hollow.core.write.HollowWriteStateEngine;
import com.netflix.hollow.core.write.objectmapper.flatrecords.FlatRecordReader;
import com.netflix.hollow.core.write.objectmapper.flatrecords.FlatRecordWriter;
import com.netflix.hollow.core.write.objectmapper.flatrecords.traversal.FlatRecordTraversalMapNode;
import com.netflix.hollow.core.write.objectmapper.flatrecords.traversal.FlatRecordTraversalNode;
Expand Down Expand Up @@ -142,31 +140,13 @@ protected Object parseHollowRecord(HollowRecord record) {
}

@Override
protected Object parseFlatRecord(HollowSchema recordSchema, FlatRecordReader reader, Map<Integer, Object> parsedObjects) {
Map<Object, Object> collection = new HashMap<>();

int size = reader.readCollectionSize();
int keyOrdinal = 0;
for (int i = 0; i < size; i++) {
int keyOrdinalDelta = reader.readOrdinal();
int valueOrdinal = reader.readOrdinal();
keyOrdinal += keyOrdinalDelta;
Object key = parsedObjects.get(keyOrdinal);
Object value = parsedObjects.get(valueOrdinal);
collection.put(key, value);
}

return collection;
}

@Override
protected Object parseFlatRecordTraversalNode(FlatRecordTraversalNode node) {
protected Object parseFlatRecord(FlatRecordTraversalNode node) {
FlatRecordTraversalMapNode mapNode = (FlatRecordTraversalMapNode) node;
Map<Object, Object> collection = new HashMap<>();

for (Map.Entry<FlatRecordTraversalNode, FlatRecordTraversalNode> entry : mapNode.entrySet()) {
Object key = keyMapper.parseFlatRecordTraversalNode(entry.getKey());
Object value = valueMapper.parseFlatRecordTraversalNode(entry.getValue());
Object key = keyMapper.parseFlatRecord(entry.getKey());
Object value = valueMapper.parseFlatRecord(entry.getValue());
collection.put(key, value);
}
return collection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,28 +100,13 @@ public <T> T readFlat(FlatRecordTraversalNode node) {
if (typeMapper == null) {
throw new IllegalArgumentException("No type mapper found for schema " + schemaName);
}
Object obj = typeMapper.parseFlatRecordTraversalNode(node);
Object obj = typeMapper.parseFlatRecord(node);
return (T) obj;
}

public <T> T readFlat(FlatRecord record) {
FlatRecordReader recordReader = new FlatRecordReader(record);

int ordinal = 0;
Map<Integer, Object> parsedObjects = new HashMap<>();
while(recordReader.hasMore()) {
HollowSchema schema = recordReader.readSchema();
HollowTypeMapper mapper = typeMappers.get(schema.getName());
if (mapper == null) {
recordReader.skipSchema(schema);
} else {
Object obj = mapper.parseFlatRecord(schema, recordReader, parsedObjects);
parsedObjects.put(ordinal, obj);
}
ordinal++;
}

return (T) parsedObjects.get(ordinal - 1);
FlatRecordTraversalNode node = new FlatRecordTraversalObjectNode(record);
return readFlat(node);
}

/**
Expand Down
Loading

0 comments on commit a4568a7

Please sign in to comment.