Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/main/java/com/redislabs/redisgraph/graph_entities/Edge.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,22 @@
public class Edge extends GraphEntity {

//members
private String relationshipType;
private String relationshipType;
private long source;
private long destination;

public Edge() {
super();
}

/**
* Use this constructor to reduce memory allocations
* when properties are added to the edge
* @param propertiesCapacity preallocate the capacity for the properties
*/
public Edge(int propertiesCapacity) {
super(propertiesCapacity);
}
//getters & setters

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,20 @@
public abstract class GraphEntity {
//members
protected long id;
protected final Map<String, Property<?>> propertyMap = new HashMap<>();
protected final Map<String, Property<?>> propertyMap;

public GraphEntity() {
propertyMap = new HashMap<>();
}

/**
* Use this constructor to reduce memory allocations
* when properties are added to the edge
* @param propertiesCapacity preallocate the capacity for the properties
*/
public GraphEntity(int propertiesCapacity) {
propertyMap = new HashMap<>(propertiesCapacity);
}

//setters & getters

Expand Down
19 changes: 18 additions & 1 deletion src/main/java/com/redislabs/redisgraph/graph_entities/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,24 @@
public class Node extends GraphEntity {

//members
private final List<String> labels = new ArrayList<>();
private final List<String> labels;

public Node() {
super();
labels = new ArrayList<>();
}

/**
* Use this constructor to reduce memory allocations
* when labels or properties are added to the node
* @param labelsCapacity preallocate the capacity for the node labels
* @param propertiesCapacity preallocate the capacity for the properties
*/
public Node(int labelsCapacity, int propertiesCapacity) {
super(propertiesCapacity);
this.labels = new ArrayList<>(labelsCapacity);
}


/**
* @param label - a label to be add
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,26 +140,28 @@ public Header getHeader() {
*/
@SuppressWarnings("unchecked")
private Node deserializeNode(List<Object> rawNodeData) {
Node node = new Node();
deserializeGraphEntityId(node, rawNodeData.get(0));

List<Long> labelsIndices = (List<Long>) rawNodeData.get(1);
List<List<Object>> rawProperties = (List<List<Object>>) rawNodeData.get(2);

Node node = new Node(labelsIndices.size(), rawProperties.size());
deserializeGraphEntityId(node, (Long) rawNodeData.get(0));

for (Long labelIndex : labelsIndices) {
String label = cache.getLabel(labelIndex.intValue(), redisGraph);
node.addLabel(label);
}
deserializeGraphEntityProperties(node, (List<List<Object>>) rawNodeData.get(2));

return node;
deserializeGraphEntityProperties(node, rawProperties);

return node;
}

/**
* @param graphEntity graph entity
* @param rawEntityId raw representation of entity id to be set to the graph
* entity
* @param id entity id to be set to the graph entity
*/
private void deserializeGraphEntityId(GraphEntity graphEntity, Object rawEntityId) {
long id = (Long) rawEntityId;
private void deserializeGraphEntityId(GraphEntity graphEntity, long id) {
graphEntity.setId(id);
}

Expand All @@ -172,16 +174,19 @@ private void deserializeGraphEntityId(GraphEntity graphEntity, Object rawEntityI
*/
@SuppressWarnings("unchecked")
private Edge deserializeEdge(List<Object> rawEdgeData) {
Edge edge = new Edge();
deserializeGraphEntityId(edge, rawEdgeData.get(0));

List<List<Object>> rawProperties = (List<List<Object>>) rawEdgeData.get(4);

Edge edge = new Edge(rawProperties.size());
deserializeGraphEntityId(edge, (Long) rawEdgeData.get(0));

String relationshipType = cache.getRelationshipType(((Long) rawEdgeData.get(1)).intValue(), redisGraph);
edge.setRelationshipType(relationshipType);

edge.setSource((long) rawEdgeData.get(2));
edge.setDestination((long) rawEdgeData.get(3));

deserializeGraphEntityProperties(edge, (List<List<Object>>) rawEdgeData.get(4));
deserializeGraphEntityProperties(edge, rawProperties);

return edge;
}
Expand Down Expand Up @@ -256,8 +261,11 @@ private Object deserializePoint(Object rawScalarData) {
@SuppressWarnings("unchecked")
private Map<String, Object> deserializeMap(Object rawScalarData) {
List<Object> keyTypeValueEntries = (List<Object>) rawScalarData;
Map<String, Object> map = new HashMap<>();
for (int i = 0; i < keyTypeValueEntries.size(); i += 2) {

int size = keyTypeValueEntries.size();
Map<String, Object> map = new HashMap<>(size >> 1); // set the capacity to half of the list

for (int i = 0; i < size; i += 2) {
String key = SafeEncoder.encode((byte[]) keyTypeValueEntries.get(i));
Object value = deserializeScalar((List<Object>) keyTypeValueEntries.get(i + 1));
map.put(key, value);
Expand Down