/** timestamps are in epoch milliseconds */ public static Dependencies create(long startTs, long endTs, List<DependencyLink> links) { return new Dependencies(startTs, endTs, links); }
void saveToCassandra(List<DependencyLink> links) { Dependencies thrift = Dependencies.create(day, day /** ignored */, links); ByteBuffer blob = thrift.toThrift(); log.info("Saving with day={}", dateStamp); CassandraConnector.apply(conf).withSessionDo(new AbstractFunction1<Session, Void>() { @Override public Void apply(Session session) { session.execute(QueryBuilder.insertInto(keyspace, "dependencies") .value("day", new Date(day)) .value("dependencies", blob) ); return null; } }); log.info("Done"); }
/** Writes the current instance in TBinaryProtocol */ public ByteBuffer toThrift() { Buffer buffer = new Buffer(sizeInBytes()); write(buffer); return ByteBuffer.wrap(buffer.toByteArray()); }
@Override public List<DependencyLink> map(ResultSet rs) { List<DependencyLink> unmerged = new ArrayList<>(); for (Row row : rs) { ByteBuffer encodedDayOfDependencies = row.getBytes("dependencies"); for (DependencyLink link : Dependencies.fromThrift(encodedDayOfDependencies).links()) { unmerged.add(link); } } return DependencyLinker.merge(unmerged); }
/** Reads from bytes serialized in TBinaryProtocol */ public static Dependencies fromThrift(ByteBuffer bytes) { long startTs = 0L; long endTs = 0L; List<DependencyLink> links = Collections.emptyList(); while (true) { ThriftField thriftField = ThriftField.read(bytes); if (thriftField.type == TYPE_STOP) break; if (thriftField.isEqualTo(START_TS)) { startTs = bytes.getLong(); } else if (thriftField.isEqualTo(END_TS)) { endTs = bytes.getLong(); } else if (thriftField.isEqualTo(LINKS)) { int length = ThriftCodec.readListLength(bytes); if (length == 0) continue; links = new ArrayList<>(length); for (int i = 0; i < length; i++) { links.add(DependencyLinkAdapter.read(bytes)); } } else { skip(bytes, thriftField.type); } } return Dependencies.create(startTs, endTs, links); }