/** * Converts a long to a 8-byte array so that lexicographic ordering of the * produced byte arrays sort the longs in descending order. * * @param l A long * @return A byte array */ public static byte[] writeReverseOrderedLong(long l) { byte[] b = new byte[8]; return writeReverseOrderedLong(l, b, 0); }
/** * Converts a long to a 8-byte array so that lexicographic ordering of the * produced byte arrays sort the longs in descending order. * * @param l A long * @return A byte array */ public static byte[] writeReverseOrderedLong(long l) { byte[] b = new byte[8]; return writeReverseOrderedLong(l, b, 0); }
/** * Converts a long to a 8-byte array so that lexicographic ordering of the * produced byte arrays sort the longs in descending order. * * @param l A long * @return A byte array */ public static byte[] writeReverseOrderedLong(long l) { byte[] b = new byte[8]; return writeReverseOrderedLong(l, b, 0); }
/** * Get the unique start time for a given entity as a byte array that sorts * the timestamps in reverse order (see {@link * GenericObjectMapper#writeReverseOrderedLong(long)}). * * @param entityId The id of the entity * @param entityType The type of the entity * @return A byte array, null if not found * @throws IOException */ private byte[] getStartTime(String entityId, String entityType) throws IOException { Long l = getStartTimeLong(entityId, entityType); return l == null ? null : writeReverseOrderedLong(l); }
/** * Get the unique start time for a given entity as a byte array that sorts * the timestamps in reverse order (see {@link * GenericObjectMapper#writeReverseOrderedLong(long)}). * * @param entityId The id of the entity * @param entityType The type of the entity * @return A byte array, null if not found * @throws IOException */ private byte[] getStartTime(String entityId, String entityType) throws IOException { Long l = getStartTimeLong(entityId, entityType); return l == null ? null : writeReverseOrderedLong(l); }
/** * Get the unique start time for a given entity as a byte array that sorts * the timestamps in reverse order (see {@link * GenericObjectMapper#writeReverseOrderedLong(long)}). * * @param entityId The id of the entity * @param entityType The type of the entity * @return A byte array, null if not found * @throws IOException */ private byte[] getStartTime(String entityId, String entityType) throws IOException { Long l = getStartTimeLong(entityId, entityType); return l == null ? null : writeReverseOrderedLong(l); }
/** * Get the unique start time for a given entity as a byte array that sorts the * timestamps in reverse order (see * {@link GenericObjectMapper#writeReverseOrderedLong(long)}). * * @param entityId * The id of the entity * @param entityType * The type of the entity * @return A byte array, null if not found * @throws IOException */ private byte[] getStartTime(String entityId, String entityType) throws IOException { Long l = getStartTimeLong(entityId, entityType); return l == null ? null : writeReverseOrderedLong(l); }
@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); }
void discardOldEntities(long timestamp) throws IOException, InterruptedException { byte[] reverseTimestamp = writeReverseOrderedLong(timestamp); long totalCount = 0; long t1 = System.currentTimeMillis();
void discardOldEntities(long timestamp) throws IOException, InterruptedException { byte[] reverseTimestamp = writeReverseOrderedLong(timestamp); long totalCount = 0; long t1 = System.currentTimeMillis();
starttimedb.put(b, writeReverseOrderedLong(suggestedStartTime)); } else {
writeReverseOrderedLong(suggestedStartTime, v, 0); writeReverseOrderedLong(startAndInsertTime.insertTime, v, 8); WriteOptions writeOptions = new WriteOptions(); writeOptions.sync(true);
@Override public TimelineEntity getEntity(String entityId, String entityType, EnumSet<Field> fields) throws IOException { Long revStartTime = getStartTimeLong(entityId, entityType); if (revStartTime == null) { if ( LOG.isDebugEnabled()) { LOG.debug("Could not find start time for {} {} ", entityType, entityId); } return null; } byte[] prefix = KeyBuilder.newInstance().add(entityType) .add(writeReverseOrderedLong(revStartTime)).add(entityId) .getBytesForLookup(); DB db = entitydb.getDBForStartTime(revStartTime); if (db == null) { if ( LOG.isDebugEnabled()) { LOG.debug("Could not find db for {} {} ", entityType, entityId); } return null; } try (DBIterator iterator = db.iterator()) { iterator.seek(prefix); return getEntity(entityId, entityType, revStartTime, fields, iterator, prefix, prefix.length); } }
writeReverseOrderedLong(suggestedStartTime, v, 0); writeReverseOrderedLong(startAndInsertTime.insertTime, v, 8); WriteOptions writeOptions = new WriteOptions(); writeOptions.sync(true);
writeReverseOrderedLong(suggestedStartTime, v, 0); writeReverseOrderedLong(startAndInsertTime.insertTime, v, 8); WriteOptions writeOptions = new WriteOptions(); writeOptions.sync(true);
private static void testEncoding(long l) { byte[] b = GenericObjectMapper.writeReverseOrderedLong(l); assertEquals("error decoding", l, GenericObjectMapper.readReverseOrderedLong(b, 0)); byte[] buf = new byte[16]; System.arraycopy(b, 0, buf, 5, 8); assertEquals("error decoding at offset", l, GenericObjectMapper.readReverseOrderedLong(buf, 5)); if (l > Long.MIN_VALUE) { byte[] a = GenericObjectMapper.writeReverseOrderedLong(l-1); assertEquals("error preserving ordering", 1, WritableComparator.compareBytes(a, 0, a.length, b, 0, b.length)); } if (l < Long.MAX_VALUE) { byte[] c = GenericObjectMapper.writeReverseOrderedLong(l+1); assertEquals("error preserving ordering", 1, WritableComparator.compareBytes(b, 0, b.length, c, 0, c.length)); } }
@Override public TimelineEntity getEntity(String entityId, String entityType, EnumSet<Field> fields) throws IOException { Long revStartTime = getStartTimeLong(entityId, entityType); if (revStartTime == null) { return null; } byte[] prefix = KeyBuilder.newInstance().add(ENTITY_ENTRY_PREFIX) .add(entityType).add(writeReverseOrderedLong(revStartTime)) .add(entityId).getBytesForLookup(); LeveldbIterator iterator = null; try { iterator = new LeveldbIterator(db); iterator.seek(prefix); if (fields == null) { fields = EnumSet.allOf(Field.class); } return getEntity(entityId, entityType, revStartTime, fields, iterator, prefix, prefix.length); } catch(DBException e) { throw new IOException(e); } finally { IOUtils.cleanupWithLogger(LOG, iterator); } }
@Override public TimelineEntity getEntity(String entityId, String entityType, EnumSet<Field> fields) throws IOException { Long revStartTime = getStartTimeLong(entityId, entityType); if (revStartTime == null) { return null; } byte[] prefix = KeyBuilder.newInstance().add(ENTITY_ENTRY_PREFIX) .add(entityType).add(writeReverseOrderedLong(revStartTime)) .add(entityId).getBytesForLookup(); LeveldbIterator iterator = null; try { iterator = new LeveldbIterator(db); iterator.seek(prefix); return getEntity(entityId, entityType, revStartTime, fields, iterator, prefix, prefix.length); } catch(DBException e) { throw new IOException(e); } finally { IOUtils.cleanup(LOG, iterator); } }
@Override public TimelineEntity getEntity(String entityId, String entityType, EnumSet<Field> fields) throws IOException { Long revStartTime = getStartTimeLong(entityId, entityType); if (revStartTime == null) { return null; } byte[] prefix = KeyBuilder.newInstance().add(ENTITY_ENTRY_PREFIX) .add(entityType).add(writeReverseOrderedLong(revStartTime)) .add(entityId).getBytesForLookup(); LeveldbIterator iterator = null; try { iterator = new LeveldbIterator(db); iterator.seek(prefix); return getEntity(entityId, entityType, revStartTime, fields, iterator, prefix, prefix.length); } catch(DBException e) { throw new IOException(e); } finally { IOUtils.cleanup(LOG, iterator); } }
writeReverseOrderedLong(60L))); assertEquals(3, getEntities("type_1").size()); assertEquals(1, getEntities("type_2").size()); writeReverseOrderedLong(123L))); List<TimelineEntity> entities = getEntities("type_2"); assertEquals(1, entities.size());