private void writeRecursively(DataOutput out, TSTNode node) throws IOException { if (node == null) { return; } out.writeString(new String(new char[] {node.splitchar}, 0, 1)); byte mask = 0; if (node.relatives[TSTNode.LOKID] != null) mask |= LO_KID; if (node.relatives[TSTNode.EQKID] != null) mask |= EQ_KID; if (node.relatives[TSTNode.HIKID] != null) mask |= HI_KID; if (node.data != null) mask |= HAS_VALUE; out.writeByte(mask); if (node.data != null) { out.writeLong(((Number)node.data).longValue()); } writeRecursively(out, node.relatives[TSTNode.LOKID]); writeRecursively(out, node.relatives[TSTNode.EQKID]); writeRecursively(out, node.relatives[TSTNode.HIKID]); }
private void writeRecursively(DataOutputStream out, TSTNode node) throws IOException { if (node == null) { return; } out.writeChar(node.splitchar); byte mask = 0; if (node.relatives[TSTNode.LOKID] != null) mask |= LO_KID; if (node.relatives[TSTNode.EQKID] != null) mask |= EQ_KID; if (node.relatives[TSTNode.HIKID] != null) mask |= HI_KID; if (node.data != null) mask |= HAS_VALUE; out.writeByte(mask); if (node.data != null) { out.writeLong(((Number)node.data).longValue()); } writeRecursively(out, node.relatives[TSTNode.LOKID]); writeRecursively(out, node.relatives[TSTNode.EQKID]); writeRecursively(out, node.relatives[TSTNode.HIKID]); }
@Override public boolean store(DataOutput output) throws IOException { output.writeVLong(count); TSTNode root = trie.getRoot(); if (root == null) { // empty tree return false; } writeRecursively(output, root); return true; }
@Override public boolean load(DataInput input) throws IOException { count = input.readVLong(); TSTNode root = trie.new TSTNode('\0', null); readRecursively(input, root); trie.setRoot(root); return true; }
@Override public boolean store(OutputStream output) throws IOException { TSTNode root = trie.getRoot(); if (root == null) { // empty tree return false; } DataOutputStream out = new DataOutputStream(output); try { writeRecursively(out, root); out.flush(); } finally { IOUtils.close(out); } return true; }
@Override public boolean load(InputStream input) throws IOException { DataInputStream in = new DataInputStream(input); TSTNode root = trie.new TSTNode('\0', null); try { readRecursively(in, root); trie.setRoot(root); } finally { IOUtils.close(in); } return true; }