MetaKeySet extends TreeSet<Key> { public List<Mutation> toMutations(long ts) { List<Mutation> results = new ArrayList<>(); String prevRow = null; Iterator<Key> keys = this.iterator(); Mutation m = null; while (keys.hasNext()) { Key next = keys.next(); if (null == prevRow || !(prevRow.equals(next.getRow().toString()))) { if (null != prevRow) { results.add(m); } m = new Mutation(next.getRow()); prevRow = next.getRow().toString(); } m.put(next.getColumnFamily(), next.getColumnQualifier(), ts, NULL_VALUE); } results.add(m); return results; } MetaKeySet(); List<Mutation> toMutations(long ts); static final Value NULL_VALUE; }
@Test public void testToMutations() { Meta one = new Meta("sys.cpu.user", "tag1", "value1"); Meta two = new Meta("sys.cpu.user", "tag2", "value2"); Meta three = new Meta("sys.cpu.user", "tag3", "value3"); MetaKeySet mks = new MetaKeySet(); mks.addAll(one.toKeys()); mks.addAll(two.toKeys()); mks.addAll(three.toKeys()); long ts = System.currentTimeMillis(); List<Mutation> muts = mks.toMutations(ts); Mutation e1 = new Mutation("m:sys.cpu.user"); e1.put("", "", ts, MetaKeySet.NULL_VALUE); Mutation e2 = new Mutation("t:sys.cpu.user"); e2.put("tag1", "", ts, MetaKeySet.NULL_VALUE); e2.put("tag2", "", ts, MetaKeySet.NULL_VALUE); e2.put("tag3", "", ts, MetaKeySet.NULL_VALUE); Mutation e3 = new Mutation("v:sys.cpu.user"); e3.put("tag1", "value1", ts, MetaKeySet.NULL_VALUE); e3.put("tag2", "value2", ts, MetaKeySet.NULL_VALUE); e3.put("tag3", "value3", ts, MetaKeySet.NULL_VALUE); Assert.assertEquals(3, muts.size()); Assert.assertTrue(muts.contains(e1)); Assert.assertTrue(muts.contains(e2)); Assert.assertTrue(muts.contains(e3)); }