/** * Read and decode a {@link Node} from the {@link ObjectFile}. The {@code id} must * have originally been generated by {@link #encodeStore}. */ public static Node fetchDecode(long id, ObjectFile file) { ByteBuffer bb = file.read(id); if ( bb == null ) return null; return decode(bb); }
public static Hash hash(Node n) { Hash h = new Hash(LenNodeHash); setHash(h, n); return h; }
/** * Encode and write a {@link Node} to the {@link ObjectFile}. * Uses the given {@link ByteBuffer} for encoding space if possible. * Returns the location, suitable for use with {@link #fetchDecode}. */ public static long encodeStore(Node node, ObjectFile file, ByteBuffer bb) { int maxSize = nodec.maxSize(node); if ( bb == null ) return allocEncodeWrite(node, file, maxSize); if ( bb.capacity() < maxSize ) // Buffer may not be big enough. return allocEncodeWrite(node, file, maxSize); // Use buffer provided. bb.clear(); return encodeWrite(node, file, bb); }
private static void hash(Hash h, String lex, String lang, String datatype, NodeType nodeType) { if ( datatype == null ) datatype = ""; if ( lang == null ) lang = ""; String toHash = lex + "|" + lang + "|" + datatype + "|" + nodeType.getName(); MessageDigest digest; try { digest = allocDigest(); digest.update(Bytes.string2bytes(toHash)); if ( h.getLen() == 16 ) // MD5 is 16 bytes. digest.digest(h.getBytes(), 0, 16); else { byte b[] = digest.digest(); // 16 bytes. // Avoid the copy if length is 16? // digest.digest(bytes, 0, length) needs 16 bytes System.arraycopy(b, 0, h.getBytes(), 0, h.getLen()); } deallocDigest(digest); return; } catch (DigestException ex) { Log.error(NodeLib.class, "DigestException", ex); } }
/** * Encode and write a {@link Node} to the {@link ObjectFile}. Returns the location, * suitable for use with {@link #fetchDecode}. */ public static long encodeStore(Node node, ObjectFile file) { return encodeStore(node, file, null); }
/** Encode and write, allocating space as needed */ private static long allocEncodeWrite(Node node, ObjectFile file, int maxSize) { ByteBuffer bb = ByteBuffer.allocate(maxSize); return encodeWrite(node, file, bb); }
private final NodeId writeNodeToTable(Node node) { syncNeeded = true ; long x = NodeLib.encodeStore(node, getObjects(), writeBuffer) ; return NodeId.create(x); }
private Iterator<Pair<NodeId, Node>> all2() { Iterator<Pair<Long, ByteBuffer>> objs = objects.all() ; Function<Pair<Long, ByteBuffer>, Pair<NodeId, Node>> transform = item -> { NodeId id = NodeId.create(item.car().longValue()); ByteBuffer bb = item.cdr(); Node n = NodeLib.decode(bb); return new Pair<>(id, n); }; return Iter.map(objs, transform) ; }
NodeLib.setHash(h, n) ; String str = Bytes.asHex(h.getBytes()) ; System.out.printf("%s %08d 0x%s # %s\n", id, x, str, n) ;
private static Iterator<Pair<NodeId, Node>> all(ObjectFile objFile) { Iterator<Pair<Long, ByteBuffer>> objs = objFile.all() ; Function<Pair<Long, ByteBuffer>, Pair<NodeId, Node>> transform = item -> { NodeId id = NodeId.create(item.car().longValue()); ByteBuffer bb = item.cdr(); Node n = NodeLib.decode(bb); return new Pair<>(id, n); }; return Iter.map(objs, transform) ; }
NodeLib.setHash(h, n) ; String str = Bytes.asHex(h.getBytes()) ; System.out.printf("%s %08d 0x%s # %s\n", id, x, str, n) ;
private static Iterator<Pair<NodeId, Node>> all(ObjectFile objFile) { Iterator<Pair<Long, ByteBuffer>> objs = objFile.all() ; Function<Pair<Long, ByteBuffer>, Pair<NodeId, Node>> transform = item -> { NodeId id = NodeId.create(item.car().longValue()); ByteBuffer bb = item.cdr(); Node n = NodeLib.decode(bb); return new Pair<>(id, n); }; return Iter.map(objs, transform) ; }
private final NodeId accessIndex(Node node, boolean create) setHash(hash, node) ; byte k[] = hash.getBytes() ;