This is an automated email from the ASF dual-hosted git repository.

jackietien pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new d8b334aae4 [IOTDB-4562] Optimization of ITimeIndex  (#7495)
d8b334aae4 is described below

commit d8b334aae47a88cc4b487fd5a62b48470a0adf30
Author: Chen YZ <[email protected]>
AuthorDate: Thu Oct 6 20:52:36 2022 +0800

    [IOTDB-4562] Optimization of ITimeIndex  (#7495)
---
 .../db/engine/storagegroup/TsFileResource.java     | 36 ++++-----
 .../storagegroup/timeindex/DeviceTimeIndex.java    | 25 ++++++
 .../storagegroup/timeindex/FileTimeIndex.java      | 44 ++++++-----
 .../engine/storagegroup/timeindex/ITimeIndex.java  | 20 +++++
 .../storagegroup/timeindex/V012FileTimeIndex.java  | 24 ++++--
 .../db/engine/storagegroup/TsFileResourceTest.java | 92 ++++++++++++++++++++++
 .../iotdb/db/rescon/ResourceManagerTest.java       | 10 +--
 7 files changed, 202 insertions(+), 49 deletions(-)

diff --git 
a/server/src/main/java/org/apache/iotdb/db/engine/storagegroup/TsFileResource.java
 
b/server/src/main/java/org/apache/iotdb/db/engine/storagegroup/TsFileResource.java
index e67203d242..289a88eccd 100644
--- 
a/server/src/main/java/org/apache/iotdb/db/engine/storagegroup/TsFileResource.java
+++ 
b/server/src/main/java/org/apache/iotdb/db/engine/storagegroup/TsFileResource.java
@@ -31,6 +31,7 @@ import 
org.apache.iotdb.db.engine.storagegroup.timeindex.DeviceTimeIndex;
 import org.apache.iotdb.db.engine.storagegroup.timeindex.FileTimeIndex;
 import org.apache.iotdb.db.engine.storagegroup.timeindex.ITimeIndex;
 import org.apache.iotdb.db.engine.storagegroup.timeindex.TimeIndexLevel;
+import org.apache.iotdb.db.engine.storagegroup.timeindex.V012FileTimeIndex;
 import org.apache.iotdb.db.engine.upgrade.UpgradeTask;
 import org.apache.iotdb.db.exception.PartitionViolationException;
 import org.apache.iotdb.db.metadata.utils.ResourceByPathUtils;
@@ -95,9 +96,6 @@ public class TsFileResource {
   /** time index */
   protected ITimeIndex timeIndex;
 
-  /** time index type, V012FileTimeIndex = 0, deviceTimeIndex = 1, 
fileTimeIndex = 2 */
-  private byte timeIndexType;
-
   private volatile ModificationFile modFile;
 
   private volatile ModificationFile compactionModFile;
@@ -162,7 +160,6 @@ public class TsFileResource {
     this.file = other.file;
     this.processor = other.processor;
     this.timeIndex = other.timeIndex;
-    this.timeIndexType = other.timeIndexType;
     this.modFile = other.modFile;
     this.status = other.status;
     this.pathToChunkMetadataListMap = other.pathToChunkMetadataListMap;
@@ -181,7 +178,6 @@ public class TsFileResource {
     this.file = file;
     this.version = FilePathUtils.splitAndGetTsFileVersion(this.file.getName());
     this.timeIndex = CONFIG.getTimeIndexLevel().getTimeIndex();
-    this.timeIndexType = (byte) CONFIG.getTimeIndexLevel().ordinal();
   }
 
   /** unsealed TsFile, for writter */
@@ -189,7 +185,6 @@ public class TsFileResource {
     this.file = file;
     this.version = FilePathUtils.splitAndGetTsFileVersion(this.file.getName());
     this.timeIndex = CONFIG.getTimeIndexLevel().getTimeIndex();
-    this.timeIndexType = (byte) CONFIG.getTimeIndexLevel().ordinal();
     this.processor = processor;
   }
 
@@ -202,7 +197,6 @@ public class TsFileResource {
       throws IOException {
     this.file = originTsFileResource.file;
     this.timeIndex = originTsFileResource.timeIndex;
-    this.timeIndexType = originTsFileResource.timeIndexType;
     this.pathToReadOnlyMemChunkMap.put(path, readOnlyMemChunk);
     this.pathToChunkMetadataListMap.put(path, chunkMetadataList);
     this.originTsFileResource = originTsFileResource;
@@ -217,7 +211,6 @@ public class TsFileResource {
       throws IOException {
     this.file = originTsFileResource.file;
     this.timeIndex = originTsFileResource.timeIndex;
-    this.timeIndexType = originTsFileResource.timeIndexType;
     this.pathToReadOnlyMemChunkMap = pathToReadOnlyMemChunkMap;
     this.pathToChunkMetadataListMap = pathToChunkMetadataListMap;
     generatePathToTimeSeriesMetadataMap();
@@ -230,14 +223,12 @@ public class TsFileResource {
       File file, Map<String, Integer> deviceToIndex, long[] startTimes, long[] 
endTimes) {
     this.file = file;
     this.timeIndex = new DeviceTimeIndex(deviceToIndex, startTimes, endTimes);
-    this.timeIndexType = 1;
   }
 
   public synchronized void serialize() throws IOException {
     try (OutputStream outputStream =
         fsFactory.getBufferedOutputStream(file + RESOURCE_SUFFIX + 
TEMP_SUFFIX)) {
       ReadWriteIOUtils.write(VERSION_NUMBER, outputStream);
-      ReadWriteIOUtils.write(timeIndexType, outputStream);
       timeIndex.serialize(outputStream);
 
       ReadWriteIOUtils.write(maxPlanIndex, outputStream);
@@ -258,8 +249,8 @@ public class TsFileResource {
   public void deserialize() throws IOException {
     try (InputStream inputStream = fsFactory.getBufferedInputStream(file + 
RESOURCE_SUFFIX)) {
       // The first byte is VERSION_NUMBER, second byte is timeIndexType.
-      timeIndexType = ReadWriteIOUtils.readBytes(inputStream, 2)[1];
-      timeIndex = 
TimeIndexLevel.valueOf(timeIndexType).getTimeIndex().deserialize(inputStream);
+      ReadWriteIOUtils.readByte(inputStream);
+      timeIndex = ITimeIndex.createTimeIndex(inputStream);
       maxPlanIndex = ReadWriteIOUtils.readLong(inputStream);
       minPlanIndex = ReadWriteIOUtils.readLong(inputStream);
       if (inputStream.available() > 0) {
@@ -273,8 +264,8 @@ public class TsFileResource {
 
     // upgrade from v0.12 to v0.13, we need to rewrite the TsFileResource if 
the previous time index
     // is file time index
-    if (timeIndexType == 0) {
-      timeIndexType = 2;
+    if (timeIndex.getTimeIndexType() == ITimeIndex.V012_FILE_TIME_INDEX_TYPE) {
+      timeIndex = ((V012FileTimeIndex) timeIndex).getFileTimeIndex();
       serialize();
     }
   }
@@ -299,7 +290,6 @@ public class TsFileResource {
         long time = ReadWriteIOUtils.readLong(inputStream);
         endTimesArray[i] = time;
       }
-      timeIndexType = (byte) 1;
       timeIndex = new DeviceTimeIndex(deviceMap, startTimesArray, 
endTimesArray);
       if (inputStream.available() > 0) {
         int versionSize = ReadWriteIOUtils.readInt(inputStream);
@@ -989,12 +979,21 @@ public class TsFileResource {
   }
 
   public byte getTimeIndexType() {
-    return timeIndexType;
+    return timeIndex.getTimeIndexType();
   }
 
   @TestOnly
   public void setTimeIndexType(byte type) {
-    this.timeIndexType = type;
+    switch (type) {
+      case ITimeIndex.DEVICE_TIME_INDEX_TYPE:
+        this.timeIndex = new DeviceTimeIndex();
+        break;
+      case ITimeIndex.FILE_TIME_INDEX_TYPE:
+        this.timeIndex = new FileTimeIndex();
+        break;
+      default:
+        throw new UnsupportedOperationException();
+    }
   }
 
   public long getRamSize() {
@@ -1003,7 +1002,7 @@ public class TsFileResource {
 
   /** the DeviceTimeIndex degrade to FileTimeIndex and release memory */
   public long degradeTimeIndex() {
-    TimeIndexLevel timeIndexLevel = TimeIndexLevel.valueOf(timeIndexType);
+    TimeIndexLevel timeIndexLevel = TimeIndexLevel.valueOf(getTimeIndexType());
     // if current timeIndex is FileTimeIndex, no need to degrade
     if (timeIndexLevel == TimeIndexLevel.FILE_TIME_INDEX) {
       return 0;
@@ -1014,7 +1013,6 @@ public class TsFileResource {
     long endTime = timeIndex.getMaxEndTime();
     // replace the DeviceTimeIndex with FileTimeIndex
     timeIndex = new FileTimeIndex(startTime, endTime);
-    timeIndexType = 2;
     return ramSize - timeIndex.calculateRamSize();
   }
 
diff --git 
a/server/src/main/java/org/apache/iotdb/db/engine/storagegroup/timeindex/DeviceTimeIndex.java
 
b/server/src/main/java/org/apache/iotdb/db/engine/storagegroup/timeindex/DeviceTimeIndex.java
index 9e7daf659c..8b40d5fb33 100644
--- 
a/server/src/main/java/org/apache/iotdb/db/engine/storagegroup/timeindex/DeviceTimeIndex.java
+++ 
b/server/src/main/java/org/apache/iotdb/db/engine/storagegroup/timeindex/DeviceTimeIndex.java
@@ -38,6 +38,7 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.nio.ByteBuffer;
 import java.util.Arrays;
+import java.util.HashSet;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
@@ -83,6 +84,7 @@ public class DeviceTimeIndex implements ITimeIndex {
 
   @Override
   public void serialize(OutputStream outputStream) throws IOException {
+    ReadWriteIOUtils.write(getTimeIndexType(), outputStream);
     int deviceNum = deviceToIndex.size();
 
     ReadWriteIOUtils.write(deviceNum, outputStream);
@@ -153,6 +155,24 @@ public class DeviceTimeIndex implements ITimeIndex {
     return deviceToIndex.keySet();
   }
 
+  /**
+   * Deserialize TimeIndex and get devices only.
+   *
+   * @param inputStream inputStream
+   * @return device name
+   */
+  public static Set<String> getDevices(InputStream inputStream) throws 
IOException {
+    int deviceNum = ReadWriteIOUtils.readInt(inputStream);
+    inputStream.skip(2L * deviceNum * ReadWriteIOUtils.LONG_LEN);
+    Set<String> devices = new HashSet<>();
+    for (int i = 0; i < deviceNum; i++) {
+      String path = ReadWriteIOUtils.readString(inputStream).intern();
+      inputStream.skip(ReadWriteIOUtils.INT_LEN);
+      devices.add(path);
+    }
+    return devices;
+  }
+
   @Override
   public boolean endTimeEmpty() {
     for (long endTime : endTimes) {
@@ -364,4 +384,9 @@ public class DeviceTimeIndex implements ITimeIndex {
 
     return hasMatchedDevice ? new Pair<>(startTime, endTime) : null;
   }
+
+  @Override
+  public byte getTimeIndexType() {
+    return ITimeIndex.DEVICE_TIME_INDEX_TYPE;
+  }
 }
diff --git 
a/server/src/main/java/org/apache/iotdb/db/engine/storagegroup/timeindex/FileTimeIndex.java
 
b/server/src/main/java/org/apache/iotdb/db/engine/storagegroup/timeindex/FileTimeIndex.java
index 9129ce240d..03c5a1d94b 100644
--- 
a/server/src/main/java/org/apache/iotdb/db/engine/storagegroup/timeindex/FileTimeIndex.java
+++ 
b/server/src/main/java/org/apache/iotdb/db/engine/storagegroup/timeindex/FileTimeIndex.java
@@ -23,8 +23,7 @@ import org.apache.iotdb.commons.path.PartialPath;
 import org.apache.iotdb.db.engine.StorageEngine;
 import org.apache.iotdb.db.engine.storagegroup.TsFileResource;
 import org.apache.iotdb.db.exception.PartitionViolationException;
-import org.apache.iotdb.db.query.control.FileReaderManager;
-import org.apache.iotdb.tsfile.read.TsFileSequenceReader;
+import org.apache.iotdb.tsfile.fileSystem.FSFactoryProducer;
 import org.apache.iotdb.tsfile.utils.FilePathUtils;
 import org.apache.iotdb.tsfile.utils.Pair;
 import org.apache.iotdb.tsfile.utils.RamUsageEstimator;
@@ -39,15 +38,12 @@ import java.io.OutputStream;
 import java.nio.ByteBuffer;
 import java.nio.file.NoSuchFileException;
 import java.util.Collections;
-import java.util.HashSet;
 import java.util.Set;
 
 public class FileTimeIndex implements ITimeIndex {
 
   private static final Logger logger = 
LoggerFactory.getLogger(FileTimeIndex.class);
 
-  private static final FileReaderManager FILE_READER_MANAGER = 
FileReaderManager.getInstance();
-
   /** start time */
   protected long startTime;
 
@@ -66,19 +62,17 @@ public class FileTimeIndex implements ITimeIndex {
 
   @Override
   public void serialize(OutputStream outputStream) throws IOException {
-    ReadWriteIOUtils.write(startTime, outputStream);
-    ReadWriteIOUtils.write(endTime, outputStream);
+    throw new UnsupportedOperationException();
   }
 
   @Override
   public FileTimeIndex deserialize(InputStream inputStream) throws IOException 
{
-    return new FileTimeIndex(
-        ReadWriteIOUtils.readLong(inputStream), 
ReadWriteIOUtils.readLong(inputStream));
+    throw new UnsupportedOperationException();
   }
 
   @Override
   public FileTimeIndex deserialize(ByteBuffer buffer) {
-    return new FileTimeIndex(buffer.getLong(), buffer.getLong());
+    throw new UnsupportedOperationException();
   }
 
   @Override
@@ -88,23 +82,30 @@ public class FileTimeIndex implements ITimeIndex {
 
   @Override
   public Set<String> getDevices(String tsFilePath, TsFileResource 
tsFileResource) {
-    FILE_READER_MANAGER.increaseFileReaderReference(tsFileResource, 
tsFileResource.isClosed());
-    try {
-      TsFileSequenceReader fileReader = 
FileReaderManager.getInstance().get(tsFilePath, true);
-      return new HashSet<>(fileReader.getAllDevices());
+    tsFileResource.readLock();
+    try (InputStream inputStream =
+        FSFactoryProducer.getFSFactory()
+            .getBufferedInputStream(tsFilePath + 
TsFileResource.RESOURCE_SUFFIX)) {
+      // The first byte is VERSION_NUMBER, second byte is timeIndexType.
+      ReadWriteIOUtils.readBytes(inputStream, 2);
+      return DeviceTimeIndex.getDevices(inputStream);
     } catch (NoSuchFileException e) {
       // deleted by ttl
       if (tsFileResource.isDeleted()) {
         return Collections.emptySet();
       } else {
-        logger.error("Can't read file {} from disk ", tsFilePath, e);
-        throw new RuntimeException("Can't read file " + tsFilePath + " from 
disk");
+        logger.error(
+            "Can't read file {} from disk ", tsFilePath + 
TsFileResource.RESOURCE_SUFFIX, e);
+        throw new RuntimeException(
+            "Can't read file " + tsFilePath + TsFileResource.RESOURCE_SUFFIX + 
" from disk");
       }
     } catch (Exception e) {
-      logger.error("Failed to get devices from tsfile: {}", tsFilePath, e);
-      throw new RuntimeException("Failed to get devices from tsfile:: " + 
tsFilePath);
+      logger.error(
+          "Failed to get devices from tsfile: {}", tsFilePath + 
TsFileResource.RESOURCE_SUFFIX, e);
+      throw new RuntimeException(
+          "Failed to get devices from tsfile: " + tsFilePath + 
TsFileResource.RESOURCE_SUFFIX);
     } finally {
-      FILE_READER_MANAGER.decreaseFileReaderReference(tsFileResource, 
tsFileResource.isClosed());
+      tsFileResource.readUnlock();
     }
   }
 
@@ -231,4 +232,9 @@ public class FileTimeIndex implements ITimeIndex {
   public Pair<Long, Long> getPossibleStartTimeAndEndTime(PartialPath 
devicePattern) {
     return new Pair<>(startTime, endTime);
   }
+
+  @Override
+  public byte getTimeIndexType() {
+    return ITimeIndex.FILE_TIME_INDEX_TYPE;
+  }
 }
diff --git 
a/server/src/main/java/org/apache/iotdb/db/engine/storagegroup/timeindex/ITimeIndex.java
 
b/server/src/main/java/org/apache/iotdb/db/engine/storagegroup/timeindex/ITimeIndex.java
index e3dd93685b..fd91d214ea 100644
--- 
a/server/src/main/java/org/apache/iotdb/db/engine/storagegroup/timeindex/ITimeIndex.java
+++ 
b/server/src/main/java/org/apache/iotdb/db/engine/storagegroup/timeindex/ITimeIndex.java
@@ -23,6 +23,7 @@ import org.apache.iotdb.commons.path.PartialPath;
 import org.apache.iotdb.db.engine.storagegroup.TsFileResource;
 import org.apache.iotdb.db.exception.PartitionViolationException;
 import org.apache.iotdb.tsfile.utils.Pair;
+import org.apache.iotdb.tsfile.utils.ReadWriteIOUtils;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -32,6 +33,10 @@ import java.util.Set;
 
 public interface ITimeIndex {
 
+  byte V012_FILE_TIME_INDEX_TYPE = 0;
+  byte DEVICE_TIME_INDEX_TYPE = 1;
+  byte FILE_TIME_INDEX_TYPE = 2;
+
   int SPANS_MULTI_TIME_PARTITIONS_FLAG_ID = -1;
 
   /**
@@ -190,4 +195,19 @@ public interface ITimeIndex {
   boolean mayContainsDevice(String device);
 
   Pair<Long, Long> getPossibleStartTimeAndEndTime(PartialPath devicePattern);
+
+  /**
+   * Get TimeIndex Type
+   *
+   * @return V012FileTimeIndex = 0, deviceTimeIndex = 1, fileTimeIndex = 2
+   */
+  byte getTimeIndexType();
+
+  static ITimeIndex createTimeIndex(InputStream inputStream) throws 
IOException {
+    byte timeIndexType = ReadWriteIOUtils.readByte(inputStream);
+    if (timeIndexType == -1) {
+      throw new IOException("The end of stream has been reached");
+    }
+    return 
TimeIndexLevel.valueOf(timeIndexType).getTimeIndex().deserialize(inputStream);
+  }
 }
diff --git 
a/server/src/main/java/org/apache/iotdb/db/engine/storagegroup/timeindex/V012FileTimeIndex.java
 
b/server/src/main/java/org/apache/iotdb/db/engine/storagegroup/timeindex/V012FileTimeIndex.java
index 9443f8c977..4f959583ca 100644
--- 
a/server/src/main/java/org/apache/iotdb/db/engine/storagegroup/timeindex/V012FileTimeIndex.java
+++ 
b/server/src/main/java/org/apache/iotdb/db/engine/storagegroup/timeindex/V012FileTimeIndex.java
@@ -34,9 +34,15 @@ import java.util.Set;
 
 public class V012FileTimeIndex implements ITimeIndex {
 
+  FileTimeIndex fileTimeIndex;
+
   /** devices */
   public V012FileTimeIndex() {}
 
+  public FileTimeIndex getFileTimeIndex() {
+    return fileTimeIndex;
+  }
+
   @Override
   public void serialize(OutputStream outputStream) throws IOException {
     throw new UnsupportedOperationException(
@@ -44,22 +50,25 @@ public class V012FileTimeIndex implements ITimeIndex {
   }
 
   @Override
-  public FileTimeIndex deserialize(InputStream inputStream) throws IOException 
{
+  public ITimeIndex deserialize(InputStream inputStream) throws IOException {
     int size = ReadWriteIOUtils.readInt(inputStream);
     for (int i = 0; i < size; i++) {
       ReadWriteIOUtils.readString(inputStream);
     }
-    return new FileTimeIndex(
-        ReadWriteIOUtils.readLong(inputStream), 
ReadWriteIOUtils.readLong(inputStream));
+    fileTimeIndex =
+        new FileTimeIndex(
+            ReadWriteIOUtils.readLong(inputStream), 
ReadWriteIOUtils.readLong(inputStream));
+    return this;
   }
 
   @Override
-  public FileTimeIndex deserialize(ByteBuffer buffer) {
+  public ITimeIndex deserialize(ByteBuffer buffer) {
     int size = buffer.getInt();
     for (int i = 0; i < size; i++) {
       SerializeUtils.deserializeString(buffer);
     }
-    return new FileTimeIndex(buffer.getLong(), buffer.getLong());
+    fileTimeIndex = new FileTimeIndex(buffer.getLong(), buffer.getLong());
+    return this;
   }
 
   @Override
@@ -181,4 +190,9 @@ public class V012FileTimeIndex implements ITimeIndex {
     throw new UnsupportedOperationException(
         "V012FileTimeIndex should be rewritten while upgrading and 
getPossibleStartTimeAndEndTime() method should not be called any more.");
   }
+
+  @Override
+  public byte getTimeIndexType() {
+    return ITimeIndex.V012_FILE_TIME_INDEX_TYPE;
+  }
 }
diff --git 
a/server/src/test/java/org/apache/iotdb/db/engine/storagegroup/TsFileResourceTest.java
 
b/server/src/test/java/org/apache/iotdb/db/engine/storagegroup/TsFileResourceTest.java
new file mode 100644
index 0000000000..06f3af6fdd
--- /dev/null
+++ 
b/server/src/test/java/org/apache/iotdb/db/engine/storagegroup/TsFileResourceTest.java
@@ -0,0 +1,92 @@
+/*
+ * 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.
+ */
+package org.apache.iotdb.db.engine.storagegroup;
+
+import org.apache.iotdb.db.constant.TestConstant;
+import org.apache.iotdb.db.engine.storagegroup.timeindex.DeviceTimeIndex;
+import org.apache.iotdb.db.engine.storagegroup.timeindex.ITimeIndex;
+
+import org.apache.commons.io.FileUtils;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.stream.IntStream;
+
+public class TsFileResourceTest {
+  private final File file =
+      new File(
+          
TsFileNameGenerator.generateNewTsFilePath(TestConstant.BASE_OUTPUT_PATH, 1, 1, 
1, 1));
+  private final TsFileResource tsFileResource = new TsFileResource(file);
+  private final Map<String, Integer> deviceToIndex = new HashMap<>();
+  private final long[] startTimes = new long[DEVICE_NUM];
+  private final long[] endTimes = new long[DEVICE_NUM];
+  private static final int DEVICE_NUM = 100;
+
+  @Before
+  public void setUp() {
+    IntStream.range(0, DEVICE_NUM).forEach(i -> deviceToIndex.put("root.sg.d" 
+ i, i));
+    DeviceTimeIndex deviceTimeIndex = new DeviceTimeIndex(deviceToIndex, 
startTimes, endTimes);
+    IntStream.range(0, DEVICE_NUM)
+        .forEach(
+            i -> {
+              deviceTimeIndex.updateStartTime("root.sg.d" + i, i);
+              deviceTimeIndex.updateEndTime("root.sg.d" + i, i + 1);
+            });
+    tsFileResource.setTimeIndex(deviceTimeIndex);
+    tsFileResource.setStatus(TsFileResourceStatus.CLOSED);
+  }
+
+  @After
+  public void tearDown() throws IOException {
+    // clean fake file
+    if (file.exists()) {
+      FileUtils.delete(file);
+    }
+    File resourceFile = new File(file.getName() + 
TsFileResource.RESOURCE_SUFFIX);
+    if (resourceFile.exists()) {
+      FileUtils.delete(resourceFile);
+    }
+  }
+
+  @Test
+  public void testSerializeAndDeserialize() throws IOException {
+    tsFileResource.serialize();
+    TsFileResource derTsFileResource = new TsFileResource(file);
+    derTsFileResource.deserialize();
+    Assert.assertEquals(tsFileResource, derTsFileResource);
+  }
+
+  @Test
+  public void testDegradeAndFileTimeIndex() {
+    Assert.assertEquals(ITimeIndex.DEVICE_TIME_INDEX_TYPE, 
tsFileResource.getTimeIndexType());
+    tsFileResource.degradeTimeIndex();
+    Assert.assertEquals(ITimeIndex.FILE_TIME_INDEX_TYPE, 
tsFileResource.getTimeIndexType());
+    Assert.assertEquals(deviceToIndex.keySet(), tsFileResource.getDevices());
+    for (int i = 0; i < DEVICE_NUM; i++) {
+      Assert.assertEquals(tsFileResource.getStartTime("root.sg1.d" + i), 0);
+      Assert.assertEquals(tsFileResource.getEndTime("root.sg1.d" + i), 
DEVICE_NUM);
+    }
+  }
+}
diff --git 
a/server/src/test/java/org/apache/iotdb/db/rescon/ResourceManagerTest.java 
b/server/src/test/java/org/apache/iotdb/db/rescon/ResourceManagerTest.java
index 4eb277d663..4f1513a353 100644
--- a/server/src/test/java/org/apache/iotdb/db/rescon/ResourceManagerTest.java
+++ b/server/src/test/java/org/apache/iotdb/db/rescon/ResourceManagerTest.java
@@ -356,7 +356,6 @@ public class ResourceManagerTest {
   @Test(expected = RuntimeException.class)
   public void testAllFileTimeIndexDegrade() throws IOException, 
WriteProcessException {
     long reducedMemory = 0;
-    CONFIG.setTimeIndexLevel(String.valueOf(TimeIndexLevel.FILE_TIME_INDEX));
     double curTimeIndexMemoryThreshold = 322;
     
tsFileResourceManager.setTimeIndexMemoryThreshold(curTimeIndexMemoryThreshold);
     try {
@@ -373,13 +372,12 @@ public class ResourceManagerTest {
                         + 0
                         + ".tsfile"));
         TsFileResource tsFileResource = new TsFileResource(file);
-        tsFileResource.setStatus(TsFileResourceStatus.CLOSED);
-        tsFileResource.updatePlanIndexes((long) i);
-        seqResources.add(tsFileResource);
         assertEquals(
-            TimeIndexLevel.FILE_TIME_INDEX,
+            TimeIndexLevel.DEVICE_TIME_INDEX,
             TimeIndexLevel.valueOf(tsFileResource.getTimeIndexType()));
+        seqResources.add(tsFileResource);
         long previousRamSize = tsFileResource.calculateRamSize();
+        System.out.println(previousRamSize);
         prepareFile(tsFileResource, i * ptNum, ptNum, 0);
         tsFileResourceManager.registerSealedTsFileResource(tsFileResource);
         assertEquals(
@@ -388,7 +386,7 @@ public class ResourceManagerTest {
         reducedMemory = previousRamSize - tsFileResource.calculateRamSize();
       }
     } catch (RuntimeException e) {
-      assertEquals(0, reducedMemory);
+      assertEquals(1072, reducedMemory);
       assertEquals(7, seqResources.size());
       throw e;
     }

Reply via email to