GTSEncoder implements Cloneable { public synchronized void merge(GTSEncoder encoder) throws IOException { if (this.readonly) { throw new RuntimeException("Encoder is read-only."); } if (0 == this.size() && this.baseTimestamp == encoder.baseTimestamp && Arrays.equals(this.wrappingKey, encoder.wrappingKey)) { this.reset(encoder); return; } if (this.baseTimestamp != encoder.baseTimestamp || !Arrays.equals(this.wrappingKey, encoder.wrappingKey) || this.lastTimestamp != encoder.initialTimestamp || this.lastGeoXPPoint != encoder.initialGeoXPPoint || this.lastElevation != encoder.initialElevation || this.lastLongValue != encoder.initialLongValue || this.lastDoubleValue != encoder.initialDoubleValue || this.lastBDValue != encoder.initialBDValue || this.lastStringValue != encoder.initialStringValue) { GTSDecoder decoder = encoder.getDecoder(true); while (decoder.next()) { this.addValue(decoder.getTimestamp(), decoder.getLocation(), decoder.getElevation(), decoder.getBinaryValue()); } } else { this.stream.write(encoder.getBytes()); this.lastTimestamp = encoder.lastTimestamp; this.lastElevation = encoder.lastElevation; this.lastGeoXPPoint = encoder.lastGeoXPPoint; this.lastLongValue = encoder.lastLongValue; this.lastBDValue = encoder.lastBDValue; this.lastDoubleValue = encoder.lastDoubleValue; this.lastStringValue = encoder.lastStringValue; this.count += encoder.getCount(); } } GTSEncoder();  GTSEncoder(long baseTimestamp, byte[] key, byte[] content);  GTSEncoder(long baseTimestamp);  GTSEncoder(long baseTimestamp, byte[] key);  GTSEncoder(long baseTimestamp, byte[] key, int size); synchronized int addValue(long timestamp, long location, long elevation, Object value); void setWrappingKey(byte[] key); byte[] getBytes(); int size(); long getCount(); synchronized void encode(GeoTimeSerie gts); synchronized void encodeOptimized(GeoTimeSerie gts); static Object optimizeValue(Object value); synchronized GTSDecoder getDecoder(boolean safeMetadata); GTSDecoder getDecoder(); synchronized GTSDecoder getUnsafeDecoder(boolean blockWrites); synchronized void reset(GTSEncoder encoder); synchronized void reset(long baseTS); synchronized void resize(int target); synchronized void merge(GTSEncoder encoder); long getBaseTimestamp(); long getClassId(); void setClassId(long classId); long getLabelsId(); void setLabelsId(long labelsId); String getName(); void setName(String name); Map<String, String> getLabels(); void setLabels(Map<String, String> labels); void setLabel(String key, String value); void setMetadata(Metadata metadata); void safeSetMetadata(Metadata metadata); Metadata getMetadata(); Metadata getRawMetadata(); long getLastTimestamp(); void safeDelta(); synchronized void setCount(long count); synchronized void flush(); byte[] toBlock(boolean compress); void writeTo(OutputStream out); GTSEncoder cloneEmpty(); GTSEncoder clone(); static final byte FLAGS_ENCRYPTED; }
@Test public void testMerge() throws Exception { long now = System.currentTimeMillis() * 1000L; byte[] key = new byte[32]; GTSEncoder encoder1 = new GTSEncoder(now - 1000000L, key); encoder1.addValue(now, 111L, 11L, new BigDecimal("1.11")); encoder1.addValue(now + 1000000L, 222L, 22L, new BigDecimal("2.22")); GTSEncoder encoder2 = new GTSEncoder(now - 500000L); encoder2.addValue(now, GeoTimeSerie.NO_LOCATION, GeoTimeSerie.NO_ELEVATION, new BigDecimal("3.33")); encoder2.addValue(now + 500000L, GeoTimeSerie.NO_LOCATION, GeoTimeSerie.NO_ELEVATION, new BigDecimal("4.44")); encoder1.merge(encoder2); GTSDecoder decoder = encoder1.getDecoder(); Assert.assertTrue(decoder.next()); Assert.assertEquals(now, decoder.getTimestamp()); Assert.assertEquals(111L, decoder.getLocation()); Assert.assertEquals(11L, decoder.getElevation()); Assert.assertEquals(new BigDecimal("1.11"), decoder.getValue()); Assert.assertTrue(decoder.next()); Assert.assertEquals(now + 1000000L, decoder.getTimestamp()); Assert.assertEquals(222L, decoder.getLocation()); Assert.assertEquals(22L, decoder.getElevation()); Assert.assertEquals(new BigDecimal("2.22"), decoder.getValue()); Assert.assertTrue(decoder.next()); Assert.assertEquals(now, decoder.getTimestamp()); Assert.assertEquals(GeoTimeSerie.NO_LOCATION, decoder.getLocation()); Assert.assertEquals(GeoTimeSerie.NO_ELEVATION, decoder.getElevation()); Assert.assertEquals(new BigDecimal("3.33"), decoder.getValue()); Assert.assertTrue(decoder.next()); Assert.assertEquals(now + 500000L, decoder.getTimestamp()); Assert.assertEquals(GeoTimeSerie.NO_LOCATION, decoder.getLocation()); Assert.assertEquals(GeoTimeSerie.NO_ELEVATION, decoder.getElevation()); Assert.assertEquals(new BigDecimal("4.44"), decoder.getValue()); Assert.assertFalse(decoder.next()); encoder1 = new GTSEncoder(now - 1000000L, key); encoder1.addValue(now, 111L, 11L, new BigDecimal("1.11")); encoder1.addValue(now + 1000000L, 222L, 22L, new BigDecimal("2.22")); encoder2 = new GTSEncoder(now - 1000000L, key); encoder2.addValue(now, GeoTimeSerie.NO_LOCATION, GeoTimeSerie.NO_ELEVATION, new BigDecimal("3.33")); encoder2.addValue(now + 500000L, GeoTimeSerie.NO_LOCATION, GeoTimeSerie.NO_ELEVATION, new BigDecimal("4.44")); encoder1.merge(encoder2); decoder = encoder1.getDecoder(); Assert.assertTrue(decoder.next()); Assert.assertEquals(now, decoder.getTimestamp()); Assert.assertEquals(111L, decoder.getLocation()); Assert.assertEquals(11L, decoder.getElevation()); Assert.assertEquals(new BigDecimal("1.11"), decoder.getValue()); Assert.assertTrue(decoder.next()); Assert.assertEquals(now + 1000000L, decoder.getTimestamp()); Assert.assertEquals(222L, decoder.getLocation()); Assert.assertEquals(22L, decoder.getElevation()); Assert.assertEquals(new BigDecimal("2.22"), decoder.getValue()); Assert.assertTrue(decoder.next()); Assert.assertEquals(now, decoder.getTimestamp()); Assert.assertEquals(GeoTimeSerie.NO_LOCATION, decoder.getLocation()); Assert.assertEquals(GeoTimeSerie.NO_ELEVATION, decoder.getElevation()); Assert.assertEquals(new BigDecimal("3.33"), decoder.getValue()); Assert.assertTrue(decoder.next()); Assert.assertEquals(now + 500000L, decoder.getTimestamp()); Assert.assertEquals(GeoTimeSerie.NO_LOCATION, decoder.getLocation()); Assert.assertEquals(GeoTimeSerie.NO_ELEVATION, decoder.getElevation()); Assert.assertEquals(new BigDecimal("4.44"), decoder.getValue()); Assert.assertFalse(decoder.next()); }