SegmentParser { public NodeInfo parseNode(RecordId nodeId) { int size = 0; int nodeCount = 0; int propertyCount = 0; Segment segment = nodeId.getSegment(); String stableId = reader.readNode(nodeId).getStableId(); RecordId templateId = segment.readRecordId(nodeId.getRecordNumber(), 0, 1); onTemplate(nodeId, templateId); Template template = reader.readTemplate(templateId); if (template.getChildName() == MANY_CHILD_NODES) { RecordId childMapId = segment.readRecordId(nodeId.getRecordNumber(), 0, 2); MapRecord childMap = reader.readMap(childMapId); onMap(nodeId, childMapId, childMap); for (ChildNodeEntry childNodeEntry : childMap.getEntries()) { NodeState child = childNodeEntry.getNodeState(); if (child instanceof SegmentNodeState) { RecordId childId = ((Record) child).getRecordId(); onNode(nodeId, childId); nodeCount++; } } } else if (template.getChildName() != ZERO_CHILD_NODES) { RecordId childId = segment.readRecordId(nodeId.getRecordNumber(), 0, 2); onNode(nodeId, childId); nodeCount++; } int ids = template.getChildName() == ZERO_CHILD_NODES ? 1 : 2; size += ids * RECORD_ID_BYTES; PropertyTemplate[] propertyTemplates = template.getPropertyTemplates(); if (propertyTemplates.length > 0) { size += RECORD_ID_BYTES; RecordId id = segment.readRecordId(nodeId.getRecordNumber(), 0, ids + 1); ListRecord pIds = new ListRecord(id, propertyTemplates.length); for (int i = 0; i < propertyTemplates.length; i++) { RecordId propertyId = pIds.getEntry(i); onProperty(nodeId, propertyId, propertyTemplates[i]); propertyCount++; } onList(nodeId, id, propertyTemplates.length); } return new NodeInfo(nodeId, stableId, nodeCount, propertyCount, size); } SegmentParser(@NotNull SegmentReader reader); NodeInfo parseNode(RecordId nodeId); TemplateInfo parseTemplate(RecordId templateId); MapInfo parseMap(RecordId parentId, RecordId mapId, MapRecord map); MapInfo parseMapDiff(RecordId mapId, MapRecord map); MapInfo parseMapLeaf(RecordId mapId, MapRecord map); MapInfo parseMapBranch(RecordId mapId, MapRecord map); PropertyInfo parseProperty(RecordId parentId, RecordId propertyId, PropertyTemplate template); ValueInfo parseValue(RecordId parentId, RecordId valueId, Type<?> type); BlobInfo parseBlob(RecordId blobId); BlobInfo parseString(RecordId stringId); ListInfo parseList(RecordId parentId, RecordId listId, int count); ListBucketInfo parseListBucket(RecordId listId, int index, int count, int capacity);  }
@Test public void emptyNode() throws IOException { SegmentNodeState node = new SegmentNodeState(store.getReader(), writer, store.getBlobStore(), writer.writeNode(EMPTY_NODE)); NodeInfo info = new TestParser(store.getReader(), "emptyNode") { @Override protected void onTemplate(RecordId parentId, RecordId templateId) { } }.parseNode(node.getRecordId()); assertEquals(node.getRecordId(), info.nodeId); assertEquals(0, info.nodeCount); assertEquals(0, info.propertyCount); assertEquals(info.nodeId.toString10(), info.stableId); }
@Test public void singleChildNode() throws IOException { NodeBuilder builder = EMPTY_NODE.builder(); builder.setChildNode("child"); SegmentNodeState node = new SegmentNodeState(store.getReader(), writer, store.getBlobStore(), writer.writeNode(builder.getNodeState())); NodeInfo info = new TestParser(store.getReader(), "singleChildNode") { @Override protected void onNode(RecordId parentId, RecordId nodeId) { } @Override protected void onTemplate(RecordId parentId, RecordId templateId) { } }.parseNode(node.getRecordId()); assertEquals(node.getRecordId(), info.nodeId); assertEquals(1, info.nodeCount); assertEquals(0, info.propertyCount); assertEquals(info.nodeId.toString10(), info.stableId); }
@Test public void node() throws IOException { final NodeBuilder builder = EMPTY_NODE.builder(); builder.setChildNode("one"); builder.setChildNode("two"); builder.setProperty("three", 42); SegmentNodeState node = new SegmentNodeState(store.getReader(), writer, store.getBlobStore(), writer.writeNode(builder.getNodeState())); NodeInfo info = new TestParser(store.getReader(), "node") { @Override protected void onNode(RecordId parentId, RecordId nodeId) { } @Override protected void onTemplate(RecordId parentId, RecordId templateId) { } @Override protected void onMap(RecordId parentId, RecordId mapId, MapRecord map) { } @Override protected void onProperty(RecordId parentId, RecordId propertyId, PropertyTemplate template) { } @Override protected void onList(RecordId parentId, RecordId listId, int count) { } }.parseNode(node.getRecordId()); assertEquals(node.getRecordId(), info.nodeId); assertEquals(2, info.nodeCount); assertEquals(1, info.propertyCount); assertEquals(info.nodeId.toString10(), info.stableId); }