entities = new LevelDBMapAdapter<>(entityDb);
@Override public V get(K entityId) { V result = null; // Read the start time from the index byte[] startTimeBytes = entityDb.get(getStartTimeKey(entityId)); if (startTimeBytes == null) { return null; } // Build the key for the entity storage and read it try { result = getEntityForKey(getEntityKey(entityId, startTimeBytes)); } catch (IOException e) { LOG.error("GenericObjectMapper cannot read key from key " + entityId.toString() + " into an object. Read aborted! "); LOG.error(e.getMessage()); } return result; }
@Override public void put(K entityId, V entity) { Long startTime = entity.getStartTime(); if (startTime == null) { startTime = System.currentTimeMillis(); } // Build the key for the entity storage and read it byte[] startTimeBytes = GenericObjectMapper.writeReverseOrderedLong( startTime); try { byte[] valueBytes = GenericObjectMapper.write(entity); entityDb.put(getEntityKey(entityId, startTimeBytes), valueBytes); } catch (IOException e) { LOG.error("GenericObjectMapper cannot write " + entity.getClass().getName() + " into a byte array. Write aborted! "); LOG.error(e.getMessage()); } // Build the key for the start time index entityDb.put(getStartTimeKey(entityId), startTimeBytes); }
@Override public void remove(K entityId) { // Read the start time from the index (key starts with an "i") then delete // the record LeveldbUtils.KeyBuilder startTimeKeyBuilder = LeveldbUtils.KeyBuilder.newInstance(); startTimeKeyBuilder.add(TIME_INDEX_PREFIX).add(entityId.getId()) .add(entityId.getType()); byte[] startTimeBytes = entityDb.get(startTimeKeyBuilder.getBytes()); if (startTimeBytes == null) { return; } entityDb.delete(startTimeKeyBuilder.getBytes()); // Build the key for the entity storage and delete it entityDb.delete(getEntityKey(entityId, startTimeBytes)); }
@Override public Iterator<V> valueSetIterator() { return getIterator(null, Long.MAX_VALUE); }
@Override public Iterator<V> valueSetIterator(V minV) { return getIterator( new EntityIdentifier(minV.getEntityId(), minV.getEntityType()), minV.getStartTime()); }