private HashMap<String, ByteIterator> deserialize(byte[] bytes) { HashMap<String, ByteIterator> result = new HashMap<String, ByteIterator>(); ByteBuffer buf = ByteBuffer.wrap(bytes); int count = buf.getInt(); for (int i = 0; i < count; i++) { int keyLength = buf.getInt(); byte[] keyBytes = new byte[keyLength]; buf.get(keyBytes, buf.position(), keyLength); int valueLength = buf.getInt(); byte[] valueBytes = new byte[valueLength]; buf.get(valueBytes, buf.position(), valueLength); result.put(new String(keyBytes), new ByteArrayByteIterator(valueBytes)); } return result; }
private byte[] serialize(HashMap<String, ByteIterator> values) { ByteBuffer buf = ByteBuffer.allocate(BYTE_BUFFER_SIZE); // Number of elements in HashMap (int) buf.put((byte) values.size()); for (Map.Entry<String, ByteIterator> entry : values.entrySet()) { // Key string length (int) buf.put((byte) entry.getKey().length()); // Key bytes buf.put(entry.getKey().getBytes()); // Value bytes length (long) buf.put((byte) entry.getValue().bytesLeft()); // Value bytes buf.put((entry.getValue().toArray())); } byte[] result = new byte[buf.position()]; buf.get(result, 0, buf.position()); return result; }
@Override public int read( String table, String key, Set<String> fields, HashMap<String, ByteIterator> result) { try { byte[] value = db.get(key.getBytes()); HashMap<String, ByteIterator> deserialized = deserialize(value); result.putAll(deserialized); } catch (RocksDBException e) { System.out.format("[ERROR] caught the unexpceted exception -- %s\n", e); assert (false); } return 0; }