TsFileEpochManager.java 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

20
package org.apache.iotdb.db.pipe.extractor.realtime.epoch;
21

22 23
import org.apache.iotdb.db.pipe.event.common.tablet.PipeInsertNodeTabletInsertionEvent;
import org.apache.iotdb.db.pipe.event.common.tsfile.PipeTsFileInsertionEvent;
24
import org.apache.iotdb.db.pipe.event.realtime.PipeRealtimeEvent;
25 26
import org.apache.iotdb.db.queryengine.plan.planner.plan.node.write.InsertNode;
import org.apache.iotdb.db.storageengine.dataregion.tsfile.TsFileResource;
27 28 29 30 31

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
32 33
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
34 35 36 37 38 39 40 41
import java.util.stream.Collectors;

public class TsFileEpochManager {

  private static final Logger LOGGER = LoggerFactory.getLogger(TsFileEpochManager.class);

  private static final String[] EMPTY_MEASUREMENT_ARRAY = new String[0];

42
  private final ConcurrentMap<String, TsFileEpoch> filePath2Epoch = new ConcurrentHashMap<>();
43

44
  public PipeRealtimeEvent bindPipeTsFileInsertionEvent(
45 46 47 48
      PipeTsFileInsertionEvent event, TsFileResource resource) {
    final String filePath = resource.getTsFilePath();

    // this would not happen, but just in case
49 50 51 52 53 54
    filePath2Epoch.computeIfAbsent(
        filePath,
        path -> {
          LOGGER.info("TsFileEpoch not found for TsFile {}, creating a new one", path);
          return new TsFileEpoch(path);
        });
55

56
    return new PipeRealtimeEvent(
57 58 59
        event,
        filePath2Epoch.remove(filePath),
        resource.getDevices().stream()
60 61
            .collect(Collectors.toMap(device -> device, device -> EMPTY_MEASUREMENT_ARRAY)),
        event.getPattern());
62 63
  }

64
  public PipeRealtimeEvent bindPipeInsertNodeTabletInsertionEvent(
65
      PipeInsertNodeTabletInsertionEvent event, InsertNode node, TsFileResource resource) {
66
    return new PipeRealtimeEvent(
67 68
        event,
        filePath2Epoch.computeIfAbsent(resource.getTsFilePath(), TsFileEpoch::new),
69 70
        Collections.singletonMap(node.getDevicePath().getFullPath(), node.getMeasurements()),
        event.getPattern());
71 72
  }
}