public static void serialize(DAG dag, DataOutput out) throws IOException { final ObjectId treeId = dag.originalTreeId; final STATE state = dag.getState(); final long childCount = dag.getTotalChildCount(); final Collection<NodeId> children = dag.children.values(); final Set<TreeId> buckets = dag.buckets; treeId.writeTo(out); out.writeByte(state.ordinal()); Varint.writeUnsignedVarLong(childCount, out); Varint.writeUnsignedVarInt(children.size(), out); Varint.writeUnsignedVarInt(buckets.size(), out); for (NodeId nodeid : children) { NodeId.write(nodeid, out); } for (TreeId tid : buckets) { byte[] bucketIndicesByDepth = tid.bucketIndicesByDepth; Varint.writeUnsignedVarInt(bucketIndicesByDepth.length, out); out.write(bucketIndicesByDepth); } }
@Test public void testEncodeCanonicalNodeIds() throws Exception { ByteArrayDataOutput out = ByteStreams.newDataOutput(); List<NodeId> nodes = new ArrayList<>(); int size = 10; for (int i = 0; i < size; i++) { NodeId n = new NodeId("node-" + i); nodes.add(n); NodeId.write(n, out); } ByteArrayDataInput in = ByteStreams.newDataInput(out.toByteArray()); List<NodeId> decoded = new ArrayList<>(); for (int i = 0; i < size; i++) { NodeId n = NodeId.read(in); decoded.add(n); } assertEquals(nodes, decoded); }
@Test public void testEncodeQuadTreeNodeIds() throws Exception { ByteArrayDataOutput out = ByteStreams.newDataOutput(); List<NodeId> nodes = new ArrayList<>(); int size = 10; for (int i = 0; i < size; i++) { Envelope env = new Envelope(i, i + 1, i, i + 1); NodeId n = new NodeId("node-" + i, env); nodes.add(n); NodeId.write(n, out); } ByteArrayDataInput in = ByteStreams.newDataInput(out.toByteArray()); List<NodeId> decoded = new ArrayList<>(); for (int i = 0; i < size; i++) { NodeId n = NodeId.read(in); decoded.add(n); } assertEquals(nodes, decoded); } }