|
43 | 43 | import org.apache.tsfile.file.metadata.IDeviceID; |
44 | 44 | import org.apache.tsfile.read.TimeValuePair; |
45 | 45 | import org.apache.tsfile.utils.Pair; |
| 46 | +import org.apache.tsfile.write.chunk.AlignedChunkWriterImpl; |
46 | 47 | import org.apache.tsfile.write.schema.IMeasurementSchema; |
47 | 48 | import org.apache.tsfile.write.schema.MeasurementSchema; |
| 49 | +import org.apache.tsfile.write.writer.TsFileIOWriter; |
48 | 50 | import org.junit.After; |
49 | 51 | import org.junit.Assert; |
50 | 52 | import org.junit.Before; |
51 | 53 | import org.junit.Test; |
52 | 54 |
|
53 | 55 | import java.io.File; |
| 56 | +import java.io.IOException; |
54 | 57 | import java.util.ArrayList; |
| 58 | +import java.util.Arrays; |
55 | 59 | import java.util.Collections; |
56 | 60 | import java.util.HashMap; |
57 | 61 | import java.util.List; |
@@ -295,6 +299,94 @@ public void testAlignedTsFileWithNullValueCompaction() throws Exception { |
295 | 299 | CompactionCheckerUtils.validDataByValueList(originData, compactedData); |
296 | 300 | } |
297 | 301 |
|
| 302 | + @Test |
| 303 | + public void testAlignedCompactionWithLazilyLoadedIntermediatePriorityPage() throws Exception { |
| 304 | + String device = storageGroup + ".d0"; |
| 305 | + List<IMeasurementSchema> schemas = |
| 306 | + Arrays.asList( |
| 307 | + new MeasurementSchema("s0", TSDataType.INT64), |
| 308 | + new MeasurementSchema("s1", TSDataType.INT64)); |
| 309 | + IDeviceID deviceId = IDeviceID.Factory.DEFAULT_FACTORY.create(device); |
| 310 | + |
| 311 | + TsFileResource highPriorityResource = |
| 312 | + writeAlignedTsFile( |
| 313 | + "1-3-0-0.tsfile", |
| 314 | + deviceId, |
| 315 | + schemas, |
| 316 | + new long[] {1, 2}, |
| 317 | + new Long[][] {{10L, null}, {12L, null}}); |
| 318 | + TsFileResource lowPriorityResource = |
| 319 | + writeAlignedTsFile( |
| 320 | + "1-1-0-0.tsfile", |
| 321 | + deviceId, |
| 322 | + schemas, |
| 323 | + new long[] {1, 2}, |
| 324 | + new Long[][] {{null, 11L}, {null, 1L}}); |
| 325 | + TsFileResource intermediatePriorityResource = |
| 326 | + writeAlignedTsFile( |
| 327 | + "2-2-0-0.tsfile", deviceId, schemas, new long[] {2}, new Long[][] {{null, 2L}}); |
| 328 | + |
| 329 | + List<TsFileResource> sourceResources = |
| 330 | + Arrays.asList(highPriorityResource, lowPriorityResource, intermediatePriorityResource); |
| 331 | + TsFileResource targetResource = |
| 332 | + TsFileNameGenerator.getInnerCompactionTargetFileResource(sourceResources, true); |
| 333 | + performer.setSourceFiles(sourceResources); |
| 334 | + performer.setTargetFiles(Collections.singletonList(targetResource)); |
| 335 | + performer.setSummary(new FastCompactionTaskSummary()); |
| 336 | + performer.perform(); |
| 337 | + CompactionUtils.moveTargetFile( |
| 338 | + Collections.singletonList(targetResource), CompactionTaskType.INNER_SEQ, storageGroup); |
| 339 | + |
| 340 | + IFullPath alignedPath = new AlignedFullPath(deviceId, Arrays.asList("s0", "s1"), schemas); |
| 341 | + List<TimeValuePair> compactedData = |
| 342 | + CompactionCheckerUtils.getDataByQuery( |
| 343 | + Collections.singletonList(alignedPath), |
| 344 | + Collections.singletonList(targetResource), |
| 345 | + Collections.emptyList()) |
| 346 | + .get(alignedPath); |
| 347 | + TimeValuePair compactedPoint = |
| 348 | + compactedData.stream().filter(point -> point.getTimestamp() == 2).findFirst().orElse(null); |
| 349 | + |
| 350 | + Assert.assertNotNull(compactedPoint); |
| 351 | + Assert.assertEquals(12L, compactedPoint.getValue().getVector()[0].getLong()); |
| 352 | + Assert.assertEquals(2L, compactedPoint.getValue().getVector()[1].getLong()); |
| 353 | + } |
| 354 | + |
| 355 | + private TsFileResource writeAlignedTsFile( |
| 356 | + String fileName, |
| 357 | + IDeviceID deviceId, |
| 358 | + List<IMeasurementSchema> schemas, |
| 359 | + long[] timestamps, |
| 360 | + Long[][] values) |
| 361 | + throws IOException { |
| 362 | + TsFileResource resource = new TsFileResource(new File(dataDirectory, fileName)); |
| 363 | + resource.setSeq(true); |
| 364 | + try (TsFileIOWriter writer = new TsFileIOWriter(resource.getTsFile())) { |
| 365 | + AlignedChunkWriterImpl chunkWriter = new AlignedChunkWriterImpl(schemas); |
| 366 | + for (int row = 0; row < timestamps.length; row++) { |
| 367 | + long timestamp = timestamps[row]; |
| 368 | + chunkWriter.getTimeChunkWriter().write(timestamp); |
| 369 | + for (int column = 0; column < schemas.size(); column++) { |
| 370 | + Long value = values[row][column]; |
| 371 | + chunkWriter |
| 372 | + .getValueChunkWriterByIndex(column) |
| 373 | + .write(timestamp, value == null ? 0 : value, value == null); |
| 374 | + } |
| 375 | + } |
| 376 | + writer.startChunkGroup(deviceId); |
| 377 | + chunkWriter.writeToFileWriter(writer); |
| 378 | + writer.endChunkGroup(); |
| 379 | + writer.endFile(); |
| 380 | + } |
| 381 | + for (long timestamp : timestamps) { |
| 382 | + resource.updateStartTime(deviceId, timestamp); |
| 383 | + resource.updateEndTime(deviceId, timestamp); |
| 384 | + } |
| 385 | + resource.close(); |
| 386 | + resource.serialize(); |
| 387 | + return resource; |
| 388 | + } |
| 389 | + |
298 | 390 | @Test |
299 | 391 | public void testAlignedTsFileWithDifferentSchemaInDifferentTsFileCompaction() throws Exception { |
300 | 392 | List<String> devices = new ArrayList<>(); |
|
0 commit comments