ColinLeeo commented on code in PR #19198:
URL: https://github.com/apache/iotdb/pull/19198#discussion_r4559034529
##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/buffer/TimeSeriesMetadataCache.java:
##########
@@ -316,6 +317,216 @@ public TimeseriesMetadata get(
}
}
+ /**
+ * Load the TimeseriesMetadata of {@code allSensors} of a device, reading
the device in a single
+ * batch when the per-measurement cache does not already hold all of them.
This is used by
+ * wide-table scans (a device with many columns) to avoid walking the device
metadata index once
+ * per column / leaf node.
+ *
+ * <p>The returned map always holds copies of the cached (or freshly read)
metadata, so the caller
+ * may apply {@code setModified} and {@code setChunkMetadataLoader} without
corrupting cached
+ * entries — matching {@link #get}.
+ *
+ * @param filePath path of the tsfile
+ * @param tsFileID id of the tsfile
+ * @param device target device
+ * @param allSensors measurements the caller needs, including the time
column keyed by ""
+ * @param queryContext query context used for metrics and to detect external
tsfiles
+ * @param ignoreNotExists whether a device missing from a file that claims a
device-level time
+ * index should be skipped instead of reported as an inconsistency
+ * @return map from measurementId (including the time column, keyed by "")
to TimeseriesMetadata;
+ * entries whose series is empty (count == 0) are mapped to null
+ * @throws IOException if the metadata cannot be read from disk or the
device is unexpectedly
+ * absent
+ */
+ public Map<String, TimeseriesMetadata> getDeviceTimeSeriesMetadata(
+ String filePath,
+ TsFileID tsFileID,
+ IDeviceID device,
+ Set<String> allSensors,
+ QueryContext queryContext,
+ boolean ignoreNotExists)
+ throws IOException {
+ long startTime = System.nanoTime();
+ long loadBloomFilterTime = 0;
+ boolean externalTsFile = queryContext.isExternalTsFileScan();
+ LongConsumer timeSeriesMetadataIoSizeRecorder =
+
queryContext.getQueryStatistics().getLoadTimeSeriesMetadataActualIOSize()::addAndGet;
+ LongConsumer bloomFilterIoSizeRecorder =
+
queryContext.getQueryStatistics().getLoadBloomFilterActualIOSize()::addAndGet;
+ boolean cacheHit = true;
+
+ try {
+ if (CACHE_ENABLE && !externalTsFile) {
+ Map<String, TimeseriesMetadata> cached =
+ getCachedDeviceTimeSeriesMetadata(tsFileID, device, allSensors);
+ if (cached != null) {
+ return cached;
+ }
+ // serialize concurrent reads of the same device+file (same lock key
as get) so threads
+ // don't each deserialize the whole device and race on the shared
reader
+ synchronized (
+ devices.computeIfAbsent(device.toString() + SEPARATOR + filePath,
WeakReference::new)) {
+ cached = getCachedDeviceTimeSeriesMetadata(tsFileID, device,
allSensors);
+ if (cached != null) {
+ return cached;
+ }
+ cacheHit = false;
+ long bloomFilterStartTime = System.nanoTime();
+ boolean devicePossiblyPresent =
+ isDevicePossiblyPresent(
+ filePath, tsFileID, device, queryContext,
bloomFilterIoSizeRecorder);
+ loadBloomFilterTime = System.nanoTime() - bloomFilterStartTime;
+ if (!devicePossiblyPresent) {
+ return Collections.emptyMap();
+ }
+ TsFileSequenceReader reader =
+ FileReaderManager.getInstance()
+ .get(filePath, tsFileID, true,
timeSeriesMetadataIoSizeRecorder, false);
+ return readDeviceTimeSeriesMetadata(
+ reader, tsFileID, device, allSensors, true, ignoreNotExists);
+ }
+ }
+ cacheHit = false;
+ long bloomFilterStartTime = System.nanoTime();
+ boolean devicePossiblyPresent =
+ isDevicePossiblyPresent(
+ filePath, tsFileID, device, queryContext,
bloomFilterIoSizeRecorder);
+ loadBloomFilterTime = System.nanoTime() - bloomFilterStartTime;
+ if (!devicePossiblyPresent) {
+ return Collections.emptyMap();
+ }
+ TsFileSequenceReader reader =
+ FileReaderManager.getInstance()
+ .get(filePath, tsFileID, true, timeSeriesMetadataIoSizeRecorder,
externalTsFile);
+ return readDeviceTimeSeriesMetadata(
+ reader, tsFileID, device, allSensors, false, ignoreNotExists);
+ } finally {
+
queryContext.getQueryStatistics().getLoadBloomFilterTime().getAndAdd(loadBloomFilterTime);
+ if (cacheHit) {
+ queryContext
+ .getQueryStatistics()
+ .getLoadTimeSeriesMetadataFromCacheCount()
+ .incrementAndGet();
+ queryContext
+ .getQueryStatistics()
+ .getLoadTimeSeriesMetadataFromCacheTime()
+ .getAndAdd(System.nanoTime() - startTime - loadBloomFilterTime);
+ } else {
+ queryContext
+ .getQueryStatistics()
+ .getLoadTimeSeriesMetadataFromDiskCount()
+ .incrementAndGet();
+ queryContext
+ .getQueryStatistics()
+ .getLoadTimeSeriesMetadataFromDiskTime()
+ .getAndAdd(System.nanoTime() - startTime - loadBloomFilterTime);
+ }
+ }
+ }
+
+ /**
+ * The time column (keyed by "") is written for every aligned device, so its
bloom filter entry is
+ * a cheap per-file presence probe. A null filter cannot rule the device out.
+ */
+ private boolean isDevicePossiblyPresent(
+ String filePath,
+ TsFileID tsFileID,
+ IDeviceID device,
+ QueryContext queryContext,
+ LongConsumer bloomFilterIoSizeRecorder)
+ throws IOException {
+ BloomFilter bloomFilter =
+ BloomFilterCache.getInstance()
+ .get(
+ new BloomFilterCache.BloomFilterCacheKey(filePath, tsFileID),
+ false,
+ bloomFilterIoSizeRecorder,
+
queryContext.getQueryStatistics().getLoadBloomFilterFromCacheCount()::addAndGet,
+
queryContext.getQueryStatistics().getLoadBloomFilterFromDiskCount()::addAndGet,
+ false);
+ return bloomFilter == null
+ || bloomFilter.contains(device.toString() +
IoTDBConstant.PATH_SEPARATOR);
+ }
+
+ /**
+ * @return copies of the cached metadata for every measurement in {@code
allSensors}, or null as
+ * soon as one of them is not cached and the device therefore has to be
read from disk
+ */
+ private Map<String, TimeseriesMetadata> getCachedDeviceTimeSeriesMetadata(
+ TsFileID tsFileID, IDeviceID device, Set<String> allSensors) {
+ Map<String, TimeseriesMetadata> result = new HashMap<>(allSensors.size());
+ for (String measurement : allSensors) {
+ TimeseriesMetadata metadata =
+ lruCache.getIfPresent(new TimeSeriesMetadataCacheKey(tsFileID,
device, measurement));
+ if (metadata == null) {
+ return null;
Review Comment:
Implemented the cache-hit part: cached measurements are retained and only
the missing ones go to the reader, with a second check under the device lock.
The reader still loads the whole device because there is no selective
group-level API yet; that remains the follow-up.
_🤖 Addressed by [Claude Code](https://claude.com/claude-code)_
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]