private void insertIntoOneChunk( BTreeMap<Integer, ChunkMeta> chunkMetaMap, BTreeMap<Integer, Chunk> chunkMap, ArrayList<DataEntry> entryList) { // int timestamp_chunk = TimeConverter.roundLowerYear(entryList.get(0).timestamp); int timestamp_next_year = TimeUtil.roundNextYear(entryList.get(0).timestamp); if (timestamp_next_year <= entryList.get(entryList.size() - 1).timestamp) { throw new RuntimeException("data of more than one chunk"); } // ChunkMeta oldChunkMeta = chunkMetaMap.get(timestamp_chunk); ChunkMeta oldChunkMeta = getChunkMeta(chunkMetaMap, entryList.get(0).timestamp); if (oldChunkMeta == null) { insertChunk(chunkMetaMap, chunkMap, Chunk.of(entryList)); } else { Chunk oldChunk = chunkMap.get(oldChunkMeta.firstTimestamp); Iterator<DataEntry> oldIt = Arrays.stream(oldChunk.data).iterator(); Iterator<DataEntry> newIt = entryList.iterator(); ArrayList<DataEntry> resultList = new ArrayList<DataEntry>(); DataEntry old_curr = oldIt.hasNext() ? oldIt.next() : null; DataEntry new_curr = newIt.hasNext() ? newIt.next() : null; while (old_curr != null || new_curr != null) { if (old_curr != null) { if (new_curr != null) { if (old_curr.timestamp == new_curr.timestamp) { // overwrite old data with new data resultList.add(new_curr); old_curr = oldIt.hasNext() ? oldIt.next() : null; new_curr = newIt.hasNext() ? newIt.next() : null; } else if (old_curr.timestamp < new_curr.timestamp) { resultList.add(old_curr); old_curr = oldIt.hasNext() ? oldIt.next() : null; } else { resultList.add(new_curr); new_curr = newIt.hasNext() ? newIt.next() : null; } } else { resultList.add(old_curr); old_curr = oldIt.hasNext() ? oldIt.next() : null; } } else { resultList.add(new_curr); new_curr = newIt.hasNext() ? newIt.next() : null; } } removeChunk(chunkMetaMap, chunkMap, oldChunkMeta); insertChunk(chunkMetaMap, chunkMap, Chunk.of(resultList)); } }
/** * returns a new chunk without data in interval * * @param chunk * @param start * @param end * @return */ private static Chunk removeIntervalInChunk(Chunk chunk, int start, int end) { ArrayList<DataEntry> result = new ArrayList<DataEntry>(chunk.data.length); for (DataEntry value : chunk.data) { if (value.timestamp < start || end < value.timestamp) { result.add(value); } } return Chunk.of(result); }