@SuppressWarnings("deprecation") public static Granularity getGranularityFromJson(final JsonNode node) { final JsonNode granularityNode = node.get(SMILE_EVENT_GRANULARITY_TOKEN_NAME); if (granularityNode == null) { return Granularity.HOURLY; } try { return Granularity.valueOf(granularityNode.asText()); } catch (IllegalArgumentException e) { // hmmmh. Returning null seems dangerous; but that's what we had... return null; } }
public List<ReadableDateTime> reverseSteps(final ReadableInterval interval) { final List<ReadableDateTime> result = forwardSteps(interval); Collections.reverse(result); return result; }
public String getPathForDateTime(final ReadableDateTime dateTime) { return String.format("%s/%s", prefix, granularity.getRelativePathFor(dateTime)); } }
public Collection<String> getPathsForInterval(final ReadableInterval interval) { final Collection<String> paths = new ArrayList<String>(); granularity.stepThroughInterval(interval, new Granularity.Callback<RuntimeException>() { public void step(final ReadableInterval stepInterval) throws RuntimeException { paths.add(getPathForDateTime(stepInterval.getStart())); } }); return paths; }
private void toBytes() throws IOException { if (serializedBytes == null) { final ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream(); outputBuffer.write(ByteBuffer.allocate(8).putLong(eventDateTime.getMillis()).array()); final byte[] granularityBytes = granularity.name().getBytes(Charset.forName("UTF-8")); outputBuffer.write(ByteBuffer.allocate(4).putInt(granularityBytes.length).array()); outputBuffer.write(ByteBuffer.allocate(granularityBytes.length).put(granularityBytes).array()); serializer.open(outputBuffer); serializer.serialize(thriftEnvelope); serializer.close(); serializedBytes = outputBuffer.toByteArray(); } }
public SmileEnvelopeEvent(final String eventName, final Granularity granularity, final DateTime eventDateTime, final Map<String, Object> map) throws IOException { this.eventName = eventName; this.eventDateTime = eventDateTime; this.granularity = granularity; ObjectNode root = getObjectMapper().createObjectNode(); root.put(SMILE_EVENT_DATETIME_TOKEN_NAME, eventDateTime.getMillis()); root.put(SMILE_EVENT_GRANULARITY_TOKEN_NAME, granularity.toString()); for (Map.Entry<String, Object> entry : map.entrySet()) { addToTree(root, entry.getKey(), entry.getValue()); } this.root = root; }
@Override public boolean equals(final Object obj) { if (!(obj instanceof SmileEnvelopeEvent)) { return false; } final Event other = (Event) obj; return other.getName().equals(eventName) && other.getEventDateTime().equals(eventDateTime) && other.getGranularity().equals(granularity) && other.getData().equals(root); } }
public List<ReadableDateTime> forwardSteps(final ReadableInterval interval) { final List<ReadableDateTime> result = new ArrayList<ReadableDateTime>(); stepThroughInterval(interval, new Callback<RuntimeException>() { @Override public void step(final ReadableInterval stepInterval) { result.add(stepInterval.getStart()); } }); return result; }
/** * Given an InputStream, extract the eventDateTime, granularity and thriftEnvelope to build * the ThriftEnvelopeEvent. * This method expects the stream to be open and won't close it for you. * * @param in InputStream to read * @throws IOException generic I/O Exception */ private void deserializeFromStream(final InputStream in) throws IOException { final byte[] dateTimeBytes = new byte[8]; in.read(dateTimeBytes, 0, 8); eventDateTime = new DateTime(ByteBuffer.wrap(dateTimeBytes).getLong(0)); final byte[] sizeGranularityInBytes = new byte[4]; in.read(sizeGranularityInBytes, 0, 4); final byte[] granularityBytes = new byte[ByteBuffer.wrap(sizeGranularityInBytes).getInt(0)]; in.read(granularityBytes, 0, granularityBytes.length); granularity = Granularity.valueOf(new String(granularityBytes, Charset.forName("UTF-8"))); thriftEnvelope = deserializer.deserialize(null); }