paths = new ArrayList<>();
- paths.add(path);
- return readTsFile.query(paths, null, null);
- }
-}
diff --git a/src/main/java/cn/edu/tsinghua/iotdb/index/kvmatch/KvMatchQueryRequest.java b/src/main/java/cn/edu/tsinghua/iotdb/index/kvmatch/KvMatchQueryRequest.java
deleted file mode 100644
index 79f7085a884..00000000000
--- a/src/main/java/cn/edu/tsinghua/iotdb/index/kvmatch/KvMatchQueryRequest.java
+++ /dev/null
@@ -1,211 +0,0 @@
-package cn.edu.tsinghua.iotdb.index.kvmatch;
-
-
-import cn.edu.tsinghua.iotdb.index.QueryRequest;
-import cn.edu.tsinghua.tsfile.timeseries.read.support.Path;
-
-/**
- * An instance of this class represents a query request with specific parameters.
- *
- * @author Jiaye Wu
- */
-public class KvMatchQueryRequest extends QueryRequest {
-
- private Path queryPath;
-
- private long queryStartTime;
-
- private long queryEndTime;
-
- private double epsilon;
-
- private double alpha;
-
- private double beta;
-
- /**
- * Private constructor used by the nested Builder class.
- *
- * @param builder builder used to create this query request
- */
- private KvMatchQueryRequest(final Builder builder) {
- super(builder.columnPath, builder.startTime, builder.endTime);
- this.epsilon = builder.epsilon;
- this.alpha = builder.alpha;
- this.beta = builder.beta;
- this.queryPath = builder.queryPath;
- this.queryStartTime = builder.queryStartTime;
- this.queryEndTime = builder.queryEndTime;
- }
-
- /**
- * Returns a {@link KvMatchQueryRequest.Builder} to create an {@link KvMatchQueryRequest} using descriptive methods.
- *
- * @return a new {@link KvMatchQueryRequest.Builder} instance
- */
- public static KvMatchQueryRequest.Builder builder(Path columnPath, Path queryPath, long queryStartTime, long queryEndTime, double epsilon) {
- return new Builder(columnPath, queryPath, queryStartTime, queryEndTime, epsilon);
- }
-
- public Path getQueryPath() {
- return queryPath;
- }
-
- public void setQueryPath(Path queryPath) {
- this.queryPath = queryPath;
- }
-
- public long getQueryStartTime() {
- return queryStartTime;
- }
-
- public void setQueryStartTime(long queryStartTime) {
- this.queryStartTime = queryStartTime;
- }
-
- public long getQueryEndTime() {
- return queryEndTime;
- }
-
- public void setQueryEndTime(long queryEndTime) {
- this.queryEndTime = queryEndTime;
- }
-
- public double getEpsilon() {
- return epsilon;
- }
-
- public void setEpsilon(double epsilon) {
- this.epsilon = epsilon;
- }
-
- public double getAlpha() {
- return alpha;
- }
-
- public void setAlpha(double alpha) {
- this.alpha = alpha;
- }
-
- public double getBeta() {
- return beta;
- }
-
- public void setBeta(double beta) {
- this.beta = beta;
- }
-
- /**
- * A nested builder class to create KvMatchQueryRequest
instances using descriptive methods.
- *
- * Example usage:
- *
- * KvMatchQueryRequest queryRequest = KvMatchQueryRequest.builder(columnPath, querySeries, epsilon)
- * .alpha(1.0)
- * .beta(0.0)
- * .startTime(1500350823)
- * .endTime(1500350823)
- * .build();
- *
- */
- public static final class Builder {
-
- private Path columnPath;
-
- private long startTime;
-
- private long endTime;
-
- private Path queryPath;
-
- private long queryStartTime;
-
- private long queryEndTime;
-
- private double epsilon;
-
- private double alpha;
-
- private double beta;
-
- /**
- * Constructs a new Builder
with the minimum
- * required parameters for an KvMatchQueryRequest
instance.
- *
- * @param columnPath the column path request to query
- * @param queryPath the column path used to extract pattern series
- * @param queryStartTime the start time of pattern series in query path
- * @param queryEndTime the end time of pattern series in query path
- * @param epsilon the distance threshold
- */
- private Builder(Path columnPath, Path queryPath, long queryStartTime, long queryEndTime, double epsilon) throws IllegalArgumentException {
- this.columnPath = columnPath;
- this.queryPath = queryPath;
- this.queryStartTime = queryStartTime;
- this.queryEndTime = queryEndTime;
- this.epsilon = epsilon;
- this.alpha = 1.0;
- this.beta = 0.0;
- this.startTime = 0;
- this.endTime = Long.MAX_VALUE;
- }
-
- /**
- * Sets the parameter alpha for the query request
- *
- * @param alpha the parameter alpha for the query request
- * @return this builder, to allow method chaining
- */
- public Builder alpha(final double alpha) {
- this.alpha = alpha;
- return this;
- }
-
- /**
- * Sets the parameter beta for the query request
- *
- * @param beta the parameter alpha for the query request
- * @return this builder, to allow method chaining
- */
- public Builder beta(final double beta) {
- this.beta = beta;
- return this;
- }
-
- /**
- * Sets the start time for the query request
- *
- * @param startTime the start time for the query request
- * @return this builder, to allow method chaining
- */
- public Builder startTime(final long startTime) {
- this.startTime = startTime;
- return this;
- }
-
- /**
- * Sets the end time for the query request
- *
- * @param endTime the end time for the query request
- * @return this builder, to allow method chaining
- */
- public Builder endTime(final long endTime) {
- this.endTime = endTime;
- return this;
- }
-
- /**
- * Constructs an {@link KvMatchQueryRequest} with the values declared by this {@link KvMatchQueryRequest.Builder}.
- *
- * @return the new {@link KvMatchQueryRequest}
- * @throws IllegalArgumentException if either required arguments is illegal or has been set
- */
- public KvMatchQueryRequest build() {
- if (columnPath == null || queryPath == null || epsilon < 0 ||
- alpha < 1.0 || beta < 0 || startTime > endTime || queryStartTime > queryEndTime) {
- throw new IllegalArgumentException("The given query request is not valid!");
- }
- return new KvMatchQueryRequest(this);
- }
- }
-}
diff --git a/src/main/java/cn/edu/tsinghua/iotdb/index/utils/IndexFileUtils.java b/src/main/java/cn/edu/tsinghua/iotdb/index/utils/IndexFileUtils.java
deleted file mode 100644
index dc03434bc3e..00000000000
--- a/src/main/java/cn/edu/tsinghua/iotdb/index/utils/IndexFileUtils.java
+++ /dev/null
@@ -1,44 +0,0 @@
-package cn.edu.tsinghua.iotdb.index.utils;
-
-import cn.edu.tsinghua.tsfile.timeseries.read.support.Path;
-import cn.edu.tsinghua.iotdb.conf.TsfileDBConfig;
-import cn.edu.tsinghua.iotdb.conf.TsfileDBDescriptor;
-
-import java.io.File;
-
-public class IndexFileUtils {
-
- private static final String DATA_FILE_PATH, INDEX_FILE_PATH;
-
- static {
- TsfileDBConfig config = TsfileDBDescriptor.getInstance().getConfig();
- DATA_FILE_PATH = File.separator + config.bufferWriteDir + File.separator;
- INDEX_FILE_PATH = File.separator + config.indexFileDir + File.separator;
- }
-
- public static String getIndexFilePath(Path path, String dataFilePath) {
- String nameSpacePath = new File(dataFilePath).getParentFile().getName();
- return dataFilePath.replace(DATA_FILE_PATH, INDEX_FILE_PATH) + "-" + path.getFullPath().replace(nameSpacePath + ".","");
- }
-
- public static String getIndexFilePathPrefix(String dataFilePath) {
- return dataFilePath.replace(DATA_FILE_PATH, INDEX_FILE_PATH);
- }
-
- public static String getIndexFilePathPrefix(File indexFile) {
- String str = indexFile.getAbsolutePath();
- int idx = str.lastIndexOf("-");
- return idx != -1 ? str.substring(0, idx) : str;
- }
-
- public static String getIndexFilePathSuffix(String str) {
- int idx = str.lastIndexOf("-");
- return idx != -1 ? str.substring(idx+1) : "";
- }
-
- public static String getIndexFilePathSuffix(File indexFile) {
- String str = indexFile.getAbsolutePath();
- int idx = str.lastIndexOf("-");
- return idx != -1 ? str.substring(idx+1) : "";
- }
-}
diff --git a/src/main/java/cn/edu/tsinghua/iotdb/index/utils/SyntheticDataGenerator.java b/src/main/java/cn/edu/tsinghua/iotdb/index/utils/SyntheticDataGenerator.java
deleted file mode 100644
index ce0e46c9fbe..00000000000
--- a/src/main/java/cn/edu/tsinghua/iotdb/index/utils/SyntheticDataGenerator.java
+++ /dev/null
@@ -1,126 +0,0 @@
-package cn.edu.tsinghua.iotdb.index.utils;
-
-import cn.edu.tsinghua.tsfile.file.metadata.enums.TSDataType;
-import cn.edu.tsinghua.tsfile.file.metadata.enums.TSEncoding;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.sql.Connection;
-import java.sql.DriverManager;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.concurrent.ThreadLocalRandom;
-
-/**
- * The class generates synthetic data series to TsFileDB for index building.
- *
- * @author Jiaye Wu
- */
-public class SyntheticDataGenerator {
-
- private static final Logger logger = LoggerFactory.getLogger(SyntheticDataGenerator.class);
-
- private static final String CREATE_TIME_SERIES_TEMPLATE = "create timeseries root.turbine.Beijing.%s.%s with datatype=%s,encoding=%s";
- private static final String INSERT_DATA_TEMPLATE = "insert into root.turbine.Beijing.%s(timestamp,%s) values (%s,%s)";
- private static final String INSERT_2DATA_TEMPLATE = "insert into root.turbine.Beijing.%s(timestamp,%s,%s) values (%s,%s,%s)";
- private static final String SET_STORAGE_GROUP_TEMPLATE = "set storage group to root.turbine.Beijing.%s";
- private static final String CREATE_INDEX_TEMPLATE = "create index on root.turbine.Beijing.%s.%s using kv-match";
- private static final String CLOSE_TEMPLATE = "close";
-
- private static final String JDBC_SERVER_URL = "jdbc:tsfile://127.0.0.1:6667/";
-// private static final String JDBC_SERVER_URL = "jdbc:tsfile://192.168.130.19:6667/";
-
- private Connection connection = null;
-
- private String deviceName;
- private int length;
- private long timeInterval;
-
- public SyntheticDataGenerator(String deviceName, int length, long timeInterval) {
- this.deviceName = deviceName;
- this.length = length;
- this.timeInterval = timeInterval;
- }
-
- public static void main(String[] args) throws ClassNotFoundException, SQLException, InterruptedException {
- long time = System.currentTimeMillis();
- SyntheticDataGenerator generator1 = new SyntheticDataGenerator("d3", 2000000, 10);
- generator1.start(time);
- }
-
- public void start(long t) throws ClassNotFoundException, SQLException {
- Class.forName("cn.edu.thu.tsfiledb.jdbc.TsfileDriver");
- connectServer();
-
- createTimeSeriesMetadata();
-
- Statement statement = connection.createStatement();
- double x1 = ThreadLocalRandom.current().nextDouble(-5, 5);
- double x2 = ThreadLocalRandom.current().nextDouble(-5, 5);
- for (int i = 1; i <= length; i++) {
- statement.execute(String.format(INSERT_2DATA_TEMPLATE, deviceName, "Speed", "Energy", t, (int) x1, x2));
-
- x2 += ThreadLocalRandom.current().nextDouble(-1, 1);
- x1 += ThreadLocalRandom.current().nextDouble(-1, 1);
- t += timeInterval;
-
- if (i % 10000 == 0) {
- logger.info("{}", i);
- }
- if (i % 1000000 == 0) {
- statement.execute(CLOSE_TEMPLATE);
- }
- }
-
- disconnectServer();
- }
-
- private void createTimeSeriesMetadata() throws SQLException {
- List sqls = new ArrayList<>();
- sqls.add(String.format(CREATE_TIME_SERIES_TEMPLATE, deviceName, "Speed", TSDataType.INT32, TSEncoding.RLE));
- sqls.add(String.format(CREATE_TIME_SERIES_TEMPLATE, deviceName, "Energy", TSDataType.FLOAT, TSEncoding.RLE));
- sqls.add(String.format(SET_STORAGE_GROUP_TEMPLATE, deviceName));
- sqls.add(String.format(CREATE_INDEX_TEMPLATE, deviceName, "Speed"));
- sqls.add(String.format(CREATE_INDEX_TEMPLATE, deviceName, "Energy"));
- executeSQL(sqls);
- }
-
- private void connectServer() {
- try {
- connection = DriverManager.getConnection(JDBC_SERVER_URL, "root", "root");
- } catch (SQLException e) {
- logger.error("Failed to connect the server {} because ", JDBC_SERVER_URL, e);
- System.exit(1);
- }
- }
-
- private void disconnectServer() {
- if (connection != null) {
- try {
- connection.close();
- } catch (SQLException e) {
- logger.error("Failed to disconnect the server {} because ", JDBC_SERVER_URL, e);
- }
- }
- }
-
- private void executeSQL(List sqls) throws SQLException {
- if (connection == null) {
- connectServer();
- }
- try {
- Statement statement = connection.createStatement();
- for (String sql : sqls) {
- try {
- statement.execute(sql);
- } catch (Exception e) {
- logger.error("Execute {} failed!", sql, e);
- }
- }
- } catch (SQLException e) {
- logger.error("Failed to execute {} because ", sqls, e);
- }
- }
-}
diff --git a/src/main/java/cn/edu/tsinghua/iotdb/metadata/ColumnSchema.java b/src/main/java/cn/edu/tsinghua/iotdb/metadata/ColumnSchema.java
index 0aa9644b279..a5947feff32 100644
--- a/src/main/java/cn/edu/tsinghua/iotdb/metadata/ColumnSchema.java
+++ b/src/main/java/cn/edu/tsinghua/iotdb/metadata/ColumnSchema.java
@@ -2,13 +2,11 @@ package cn.edu.tsinghua.iotdb.metadata;
import java.io.Serializable;
import java.util.HashMap;
-import java.util.HashSet;
import java.util.Map;
-import java.util.Set;
-import cn.edu.tsinghua.iotdb.index.IndexManager.IndexType;
import cn.edu.tsinghua.tsfile.file.metadata.enums.TSDataType;
import cn.edu.tsinghua.tsfile.file.metadata.enums.TSEncoding;
+import cn.edu.tsinghua.tsfile.format.Encoding;
public class ColumnSchema implements Serializable {
private static final long serialVersionUID = -8257474930341487207L;
@@ -17,35 +15,14 @@ public class ColumnSchema implements Serializable {
public TSDataType dataType;
public TSEncoding encoding;
private Map args;
- private Set indexNameSet;
public ColumnSchema(String name, TSDataType dataType, TSEncoding encoding) {
this.name = name;
this.dataType = dataType;
this.encoding = encoding;
this.args = new HashMap<>();
- this.indexNameSet = new HashSet<>();
}
- public boolean isHasIndex() {
- return !indexNameSet.isEmpty();
- }
-
- public boolean isHasIndex(IndexType indexType) {
- return indexNameSet.contains(indexType);
- }
-
- public Set getIndexSet() {
- return indexNameSet;
- }
-
-
- public void setHasIndex(IndexType indexType) {
- this.indexNameSet.add(indexType);
- }
-
- public void removeIndex(IndexType indexType) { this.indexNameSet.remove(indexType); }
-
public void putKeyValueToArgs(String key, String value) {
this.args.put(key, value);
}
@@ -73,5 +50,4 @@ public class ColumnSchema implements Serializable {
public void setArgsMap(Map argsMap) {
this.args = argsMap;
}
-
}
diff --git a/src/main/java/cn/edu/tsinghua/iotdb/metadata/MManager.java b/src/main/java/cn/edu/tsinghua/iotdb/metadata/MManager.java
index b47fef0eb89..f1587f450bc 100644
--- a/src/main/java/cn/edu/tsinghua/iotdb/metadata/MManager.java
+++ b/src/main/java/cn/edu/tsinghua/iotdb/metadata/MManager.java
@@ -14,15 +14,14 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import java.util.Set;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import cn.edu.tsinghua.iotdb.conf.TsfileDBDescriptor;
import cn.edu.tsinghua.iotdb.exception.MetadataArgsErrorException;
import cn.edu.tsinghua.iotdb.exception.PathErrorException;
-import cn.edu.tsinghua.iotdb.index.IndexManager;
-import cn.edu.tsinghua.iotdb.index.IndexManager.IndexType;
import cn.edu.tsinghua.tsfile.file.metadata.enums.TSDataType;
+import cn.edu.tsinghua.tsfile.format.DataType;
+import cn.edu.tsinghua.tsfile.format.Encoding;
import cn.edu.tsinghua.tsfile.timeseries.read.support.Path;
/**
@@ -30,712 +29,597 @@ import cn.edu.tsinghua.tsfile.timeseries.read.support.Path;
* and persistent it into files. This class contains all the interfaces to
* modify the metadata for delta system. All the operations will be write into
* the logs temporary in case the downtime of the delta system.
- *
+ *
* @author Jinrui Zhang
+ *
*/
public class MManager {
- // private static MManager manager = new MManager();
- private static final String ROOT_NAME = MetadataConstant.ROOT;
- // the lock for read/write
- private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
- // The file storing the serialize info for metadata
- private String datafilePath;
- // the log file path
- private String logFilePath;
- private MGraph mGraph;
- private BufferedWriter logWriter;
- private boolean writeToLog;
- private String metadataDirPath;
-
- private static class MManagerHolder {
- private static final MManager INSTANCE = new MManager();
- }
-
- public static MManager getInstance() {
-
- return MManagerHolder.INSTANCE;
- }
-
- private MManager() {
-
- metadataDirPath = TsfileDBDescriptor.getInstance().getConfig().metadataDir;
- if (metadataDirPath.length() > 0
- && metadataDirPath.charAt(metadataDirPath.length() - 1) != File.separatorChar) {
- metadataDirPath = metadataDirPath + File.separatorChar;
- }
- File metadataDir = new File(metadataDirPath);
- if (!metadataDir.exists()) {
- metadataDir.mkdirs();
- }
- datafilePath = metadataDirPath + MetadataConstant.METADATA_OBJ;
- logFilePath = metadataDirPath + MetadataConstant.METADATA_LOG;
- writeToLog = false;
- init();
- }
-
- private void init() {
-
- lock.writeLock().lock();
- File dataFile = new File(datafilePath);
- File logFile = new File(logFilePath);
- try {
- try {
- if (dataFile.exists()) {
- // init the metadata from the serialized file
- FileInputStream fis = new FileInputStream(dataFile);
- ObjectInputStream ois = new ObjectInputStream(fis);
- mGraph = (MGraph) ois.readObject();
- ois.close();
- fis.close();
- dataFile.delete();
- } else {
- // init the metadata from the operation log
- mGraph = new MGraph(ROOT_NAME);
- if (logFile.exists()) {
- FileReader fr;
- fr = new FileReader(logFile);
- BufferedReader br = new BufferedReader(fr);
- String cmd;
- while ((cmd = br.readLine()) != null) {
- operation(cmd);
- }
- br.close();
- }
- }
- FileWriter fw = new FileWriter(logFile, true);
- logWriter = new BufferedWriter(fw);
- writeToLog = true;
- } catch (Exception e) {
- e.printStackTrace();
- throw new RuntimeException(e);
- }
- } finally {
- lock.writeLock().unlock();
- }
- }
-
- public void clear() {
- lock.writeLock().lock();
- try {
- this.mGraph = new MGraph(ROOT_NAME);
- } finally {
- lock.writeLock().unlock();
- }
- }
-
- private void operation(String cmd) throws PathErrorException, IOException, MetadataArgsErrorException {
-
- String args[] = cmd.trim().split(",");
- if (args[0].equals(MetadataOperationType.ADD_PATH_TO_MTREE)) {
- String[] leftArgs;
- if (args.length > 4) {
- leftArgs = new String[args.length - 4];
- for (int k = 4; k < args.length; k++) {
- leftArgs[k - 4] = args[k];
- }
- } else {
- leftArgs = new String[0];
- }
- addPathToMTree(args[1], args[2], args[3], leftArgs);
- } else if (args[0].equals(MetadataOperationType.DELETE_PATH_FROM_MTREE)) {
- deletePathFromMTree(args[1]);
- } else if (args[0].equals(MetadataOperationType.SET_STORAGE_LEVEL_TO_MTREE)) {
- setStorageLevelToMTree(args[1]);
- } else if (args[0].equals(MetadataOperationType.ADD_A_PTREE)) {
- addAPTree(args[1]);
- } else if (args[0].equals(MetadataOperationType.ADD_A_PATH_TO_PTREE)) {
- addPathToPTree(args[1]);
- } else if (args[0].equals(MetadataOperationType.DELETE_PATH_FROM_PTREE)) {
- deletePathFromPTree(args[1]);
- } else if (args[0].equals(MetadataOperationType.LINK_MNODE_TO_PTREE)) {
- linkMNodeToPTree(args[1], args[2]);
- } else if (args[0].equals(MetadataOperationType.UNLINK_MNODE_FROM_PTREE)) {
- unlinkMNodeFromPTree(args[1], args[2]);
- } else if (args[0].equals(MetadataOperationType.ADD_INDEX_TO_PATH)) {
- addIndexForOneTimeseries(args[1], IndexType.valueOf(args[2]));
- } else if (args[0].equals(MetadataOperationType.DELETE_INDEX_FROM_PATH)) {
- deleteIndexForOneTimeseries(args[1], IndexType.valueOf(args[2]));
- }
- }
-
- private void initLogStream() {
- if (logWriter == null) {
- File logFile = new File(logFilePath);
- File metadataDir = new File(metadataDirPath);
- if (!metadataDir.exists()) {
- metadataDir.mkdirs();
- }
- FileWriter fileWriter;
- try {
-
- fileWriter = new FileWriter(logFile, true);
- logWriter = new BufferedWriter(fileWriter);
- } catch (IOException e) {
- e.printStackTrace();
- throw new RuntimeException(e);
- }
- }
- }
-
- /**
- *
- * Add one timeseries to metadata. Must invoke thepathExist
and
- * getFileNameByPath
method first to check timeseries.
- *
- *
- * @param path
- * the timeseries path
- * @param dataType
- * the datetype {@code DataType} for the timeseries
- * @param encoding
- * the encoding function {@code Encoding} for the timeseries
- * @param args
- * @throws PathErrorException
- * @throws IOException
- * @throws MetadataArgsErrorException
- */
- public void addPathToMTree(String path, String dataType, String encoding, String[] args)
- throws PathErrorException, IOException, MetadataArgsErrorException {
-
- lock.writeLock().lock();
- try {
- mGraph.addPathToMTree(path, dataType, encoding, args);
- if (writeToLog) {
- initLogStream();
- logWriter.write(MetadataOperationType.ADD_PATH_TO_MTREE + "," + path + "," + dataType + "," + encoding);
- for (int i = 0; i < args.length; i++) {
- logWriter.write("," + args[i]);
- }
- logWriter.newLine();
- logWriter.flush();
- }
- } finally {
- lock.writeLock().unlock();
- }
- }
-
- public String deletePathFromMTree(String path) throws PathErrorException, IOException {
-
- lock.writeLock().lock();
- try {
- String dataFileName = mGraph.deletePath(path);
- if (writeToLog) {
- initLogStream();
- logWriter.write(MetadataOperationType.DELETE_PATH_FROM_MTREE + "," + path);
- logWriter.newLine();
- logWriter.flush();
- }
- return dataFileName;
- } finally {
- lock.writeLock().unlock();
- }
- }
-
- public void setStorageLevelToMTree(String path) throws PathErrorException, IOException {
-
- lock.writeLock().lock();
- try {
- mGraph.setStorageLevel(path);
- if (writeToLog) {
- initLogStream();
- logWriter.write(MetadataOperationType.SET_STORAGE_LEVEL_TO_MTREE + "," + path);
- logWriter.newLine();
- logWriter.flush();
- }
- } finally {
- lock.writeLock().unlock();
- }
- }
-
- public void addAPTree(String pTreeRootName) throws IOException, MetadataArgsErrorException {
-
- lock.writeLock().lock();
- try {
- mGraph.addAPTree(pTreeRootName);
- if (writeToLog) {
- initLogStream();
- logWriter.write(MetadataOperationType.ADD_A_PTREE + "," + pTreeRootName);
- logWriter.newLine();
- logWriter.flush();
- }
- } finally {
- lock.writeLock().unlock();
- }
- }
-
- public void addPathToPTree(String path) throws PathErrorException, IOException, MetadataArgsErrorException {
-
- lock.writeLock().lock();
- try {
- mGraph.addPathToPTree(path);
- if (writeToLog) {
- initLogStream();
- logWriter.write(MetadataOperationType.ADD_A_PATH_TO_PTREE + "," + path);
- logWriter.newLine();
- logWriter.flush();
- }
- } finally {
- lock.writeLock().unlock();
- }
- }
-
- public void deletePathFromPTree(String path) throws PathErrorException, IOException {
-
- lock.writeLock().lock();
- try {
- mGraph.deletePath(path);
- if (writeToLog) {
- initLogStream();
- logWriter.write(MetadataOperationType.DELETE_PATH_FROM_PTREE + "," + path);
- logWriter.newLine();
- logWriter.flush();
- }
- } finally {
- lock.writeLock().unlock();
- }
- }
-
- public void linkMNodeToPTree(String path, String mPath) throws PathErrorException, IOException {
-
- lock.writeLock().lock();
- try {
- mGraph.linkMNodeToPTree(path, mPath);
- if (writeToLog) {
- initLogStream();
- logWriter.write(MetadataOperationType.LINK_MNODE_TO_PTREE + "," + path + "," + mPath);
- logWriter.newLine();
- logWriter.flush();
- }
- } finally {
- lock.writeLock().unlock();
- }
- }
-
- public void unlinkMNodeFromPTree(String path, String mPath) throws PathErrorException, IOException {
-
- lock.writeLock().lock();
- try {
- mGraph.unlinkMNodeFromPTree(path, mPath);
- if (writeToLog) {
- initLogStream();
- logWriter.write(MetadataOperationType.UNLINK_MNODE_FROM_PTREE + "," + path + "," + mPath);
- logWriter.newLine();
- logWriter.flush();
- }
- } finally {
- lock.writeLock().unlock();
- }
- }
-
- /**
- * Extract the DeltaObjectId from given path
- *
- * @param path
- * @return String represents the DeltaObjectId
- */
- public String getDeltaObjectTypeByPath(String path) throws PathErrorException {
-
- lock.readLock().lock();
- try {
- return mGraph.getDeltaObjectTypeByPath(path);
- } finally {
- lock.readLock().unlock();
- }
- }
-
- /**
- * Get series type for given path
- *
- * @param fullPath
- * @return TSDataType
- * @throws PathErrorException
- */
- public TSDataType getSeriesType(String fullPath) throws PathErrorException {
-
- lock.readLock().lock();
- try {
- return getSchemaForOnePath(fullPath).dataType;
- } finally {
- lock.readLock().unlock();
- }
- }
-
- /**
- * Get all DeltaObject type in current Metadata Tree
- *
- * @return a HashMap contains all distinct DeltaObject type separated by
- * DeltaObject Type
- * @throws PathErrorException
- */
- public Map> getSchemaForAllType() throws PathErrorException {
-
- lock.readLock().lock();
- try {
- return mGraph.getSchemaForAllType();
- } finally {
- lock.readLock().unlock();
- }
- }
-
- /**
- * Get the full Metadata info.
- *
- * @return A {@code Metadata} instance which stores all metadata info
- * @throws PathErrorException
- */
- public Metadata getMetadata() throws PathErrorException {
-
- lock.readLock().lock();
- try {
- return mGraph.getMetadata();
- } finally {
- lock.readLock().unlock();
- }
- }
-
- /**
- * Get all ColumnSchemas for given delta object type
- *
- * @param path
- * A path represented one Delta object
- * @return a list contains all column schema
- * @throws PathErrorException
- */
- @Deprecated
- public ArrayList getSchemaForOneType(String path) throws PathErrorException {
-
- lock.readLock().lock();
- try {
- return mGraph.getSchemaForOneType(path);
- } finally {
- lock.readLock().unlock();
- }
- }
-
- /**
- * Get all ColumnSchemas for the filenode path
- * @param path
- * @return ArrayList The list of the schema
- */
- public ArrayList getSchemaForFileName(String path){
-
- lock.readLock().lock();
- try{
- return mGraph.getSchemaForOneFileNode(path);
- }finally {
- lock.readLock().unlock();
- }
- }
- public Map getSchemaMapForOneFileNode(String path){
-
- lock.readLock().lock();
- try{
- return mGraph.getSchemaMapForOneFileNode(path);
- }finally {
- lock.readLock().unlock();
- }
- }
-
- public Map getNumSchemaMapForOneFileNode(String path){
-
- lock.readLock().lock();
- try{
- return mGraph.getNumSchemaMapForOneFileNode(path);
- }finally {
- lock.readLock().unlock();
- }
- }
-
-
-
- /**
- * Calculate the count of storage-level nodes included in given path
- *
- * @param path
- * @return The total count of storage-level nodes.
- * @throws PathErrorException
- */
- public int getFileCountForOneType(String path) throws PathErrorException {
-
- lock.readLock().lock();
- try {
- return mGraph.getFileCountForOneType(path);
- } finally {
- lock.readLock().unlock();
- }
- }
-
- /**
- * Get the file name for given path Notice: This method could be called if
- * and only if the path includes one node whose {@code isStorageLevel} is
- * true
- *
- * @param path
- * @return A String represented the file name
- * @throws PathErrorException
- */
- public String getFileNameByPath(String path) throws PathErrorException {
-
- lock.readLock().lock();
- try {
- return mGraph.getFileNameByPath(path);
- } catch (PathErrorException e) {
- throw new PathErrorException(String.format(e.getMessage()));
- } finally {
- lock.readLock().unlock();
- }
- }
-
- public boolean checkFileNameByPath(String path) {
-
- lock.readLock().lock();
- try {
- return mGraph.checkFileNameByPath(path);
- } finally {
- lock.readLock().unlock();
- }
- }
-
- public List getAllFileNames() throws PathErrorException {
-
- lock.readLock().lock();
- try {
- HashMap> res = getAllPathGroupByFileName(ROOT_NAME);
- List fileNameList = new ArrayList();
- for (String fileName : res.keySet()) {
- fileNameList.add(fileName);
- }
- return fileNameList;
- } finally {
- lock.readLock().unlock();
- }
- }
-
- /**
- * return a HashMap contains all the paths separated by File Name
- */
- public HashMap> getAllPathGroupByFileName(String path) throws PathErrorException {
-
- lock.readLock().lock();
- try {
- return mGraph.getAllPathGroupByFilename(path);
- } finally {
- lock.readLock().unlock();
- }
- }
-
- /**
- * return all paths for given path if the path is abstract.Or return the
- * path itself.
- */
- public ArrayList getPaths(String path) throws PathErrorException {
-
- lock.readLock().lock();
- try {
- ArrayList res = new ArrayList<>();
- HashMap> pathsGroupByFilename = getAllPathGroupByFileName(path);
- for (ArrayList ps : pathsGroupByFilename.values()) {
- res.addAll(ps);
- }
- return res;
- } finally {
- lock.readLock().unlock();
- }
- }
-
- /**
- * Check whether the path given exists
- */
- public boolean pathExist(String path) {
-
- lock.readLock().lock();
- try {
- return mGraph.pathExist(path);
- } finally {
- lock.readLock().unlock();
- }
- }
-
- /**
- * Get ColumnSchema for given path. Notice: Path must be a complete Path
- * from root to leaf node.
- */
- public ColumnSchema getSchemaForOnePath(String path) throws PathErrorException {
-
- lock.readLock().lock();
- try {
- return mGraph.getSchemaForOnePath(path);
- } finally {
- lock.readLock().unlock();
- }
- }
-
- /**
- * Check whether given path contains a MNode whose
- * {@code MNode.isStorageLevel} is true
- */
- public boolean checkFileLevel(List path) throws PathErrorException {
-
- lock.readLock().lock();
- try {
- for (Path p : path) {
- getFileNameByPath(p.getFullPath());
- }
- return true;
- } finally {
- lock.readLock().unlock();
- }
- }
-
- public void flushObjectToFile() throws IOException {
-
- lock.writeLock().lock();
- try {
- File dataFile = new File(datafilePath);
- // delete old metadata data file
- if (dataFile.exists()) {
- dataFile.delete();
- }
- File metadataDir = new File(metadataDirPath);
- if (!metadataDir.exists()) {
- metadataDir.mkdirs();
- }
- File tempFile = new File(datafilePath + MetadataConstant.METADATA_TEMP);
- FileOutputStream fos = new FileOutputStream(tempFile);
- ObjectOutputStream oos = new ObjectOutputStream(fos);
- oos.writeObject(mGraph);
- oos.close();
- // close the logFile stream
- if (logWriter != null) {
- logWriter.close();
- logWriter = null;
- }
- // rename temp file to data file
- tempFile.renameTo(dataFile);
- } finally {
- lock.writeLock().unlock();
- }
- }
-
- public String getMetadataInString() {
-
- lock.readLock().lock();
- try {
- return mGraph.toString();
- } finally {
- lock.readLock().unlock();
- }
- }
-
- /**
- * Get all timeseries path which have specified index
- *
- * @param path
- * @return
- * @throws PathErrorException
- */
- public List getAllIndexPaths(String path, IndexType indexType) throws PathErrorException {
- lock.readLock().lock();
- try {
- List ret = new ArrayList<>();
- ArrayList paths = getPaths(path);
- for (String timesereis : paths) {
- if (getSchemaForOnePath(timesereis).isHasIndex(indexType)) {
- ret.add(timesereis);
- }
- }
- return ret;
- } finally {
- lock.readLock().unlock();
- }
- }
-
- /**
- * Get all timeseries path which have any no-real-time index
- *
- * @param path
- * @return
- * @throws PathErrorException
- */
- public Map> getAllIndexPaths(String path) throws PathErrorException {
- lock.readLock().lock();
- try {
- Map> ret = new HashMap<>();
- ArrayList paths = getPaths(path);
- for (String timeseries : paths) {
- Set indexes = getSchemaForOnePath(timeseries).getIndexSet();
- if (!indexes.isEmpty()) {
- ret.put(timeseries, indexes);
- }
- }
- return ret;
- } finally {
- lock.readLock().unlock();
- }
- }
-
- /**
- * check the timeseries has index or not
- *
- * @param path
- * @param indexType
- * @return
- * @throws PathErrorException
- */
- public boolean checkPathIndex(String path, IndexType indexType) throws PathErrorException {
- lock.readLock().lock();
- try {
- if (getSchemaForOnePath(path).isHasIndex(indexType)) {
- return true;
- } else {
- return false;
- }
- } finally {
- lock.readLock().unlock();
- }
- }
-
- /**
- * add index for one timeseries
- *
- * @param path
- * @throws PathErrorException
- * @throws IOException
- */
- public void addIndexForOneTimeseries(String path, IndexType indexType) throws PathErrorException, IOException {
- lock.writeLock().lock();
- try {
- getSchemaForOnePath(path).setHasIndex(indexType);
- if (writeToLog) {
- initLogStream();
- logWriter.write(MetadataOperationType.ADD_INDEX_TO_PATH + "," + path + "," + indexType);
- logWriter.newLine();
- logWriter.flush();
- }
- } finally {
- lock.writeLock().unlock();
- }
- }
-
- /**
- * drop index for one timeseries
- *
- * @param path
- * @throws PathErrorException
- * @throws IOException
- */
- public void deleteIndexForOneTimeseries(String path, IndexType indexType) throws PathErrorException, IOException {
- lock.writeLock().lock();
- try {
- getSchemaForOnePath(path).removeIndex(indexType);
- if (writeToLog) {
- initLogStream();
- logWriter.write(MetadataOperationType.DELETE_INDEX_FROM_PATH + "," + path + "," + indexType);
- logWriter.newLine();
- logWriter.flush();
- }
- } finally {
- lock.writeLock().unlock();
- }
- }
+ // private static MManager manager = new MManager();
+ private static final String ROOT_NAME = MetadataConstant.ROOT;
+ // the lock for read/write
+ private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
+ // The file storing the serialize info for metadata
+ private String datafilePath;
+ // the log file path
+ private String logFilePath;
+ private MGraph mGraph;
+ private BufferedWriter logWriter;
+ private boolean writeToLog;
+ private String metadataDirPath;
+
+ private static class MManagerHolder {
+ private static final MManager INSTANCE = new MManager();
+ }
+
+ public static MManager getInstance() {
+
+ return MManagerHolder.INSTANCE;
+ }
+
+ private MManager() {
+
+ metadataDirPath = TsfileDBDescriptor.getInstance().getConfig().metadataDir;
+ if (metadataDirPath.length() > 0
+ && metadataDirPath.charAt(metadataDirPath.length() - 1) != File.separatorChar) {
+ metadataDirPath = metadataDirPath + File.separatorChar;
+ }
+ File metadataDir = new File(metadataDirPath);
+ if (!metadataDir.exists()) {
+ metadataDir.mkdirs();
+ }
+ datafilePath = metadataDirPath + MetadataConstant.METADATA_OBJ;
+ logFilePath = metadataDirPath + MetadataConstant.METADATA_LOG;
+ writeToLog = false;
+ init();
+ }
+
+ private void init() {
+
+ lock.writeLock().lock();
+ File dataFile = new File(datafilePath);
+ File logFile = new File(logFilePath);
+ try {
+ try {
+ if (dataFile.exists()) {
+ // init the metadata from the serialized file
+ FileInputStream fis = new FileInputStream(dataFile);
+ ObjectInputStream ois = new ObjectInputStream(fis);
+ mGraph = (MGraph) ois.readObject();
+ ois.close();
+ fis.close();
+ dataFile.delete();
+ } else {
+ // init the metadata from the operation log
+ mGraph = new MGraph(ROOT_NAME);
+ if (logFile.exists()) {
+ FileReader fr;
+ fr = new FileReader(logFile);
+ BufferedReader br = new BufferedReader(fr);
+ String cmd;
+ while ((cmd = br.readLine()) != null) {
+ operation(cmd);
+ }
+ br.close();
+ }
+ }
+ FileWriter fw = new FileWriter(logFile, true);
+ logWriter = new BufferedWriter(fw);
+ writeToLog = true;
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new RuntimeException(e);
+ }
+ } finally {
+ lock.writeLock().unlock();
+ }
+ }
+
+ public void clear() {
+ lock.writeLock().lock();
+ try {
+ this.mGraph = new MGraph(ROOT_NAME);
+ } finally {
+ lock.writeLock().unlock();
+ }
+ }
+
+ private void operation(String cmd) throws PathErrorException, IOException, MetadataArgsErrorException {
+
+ String args[] = cmd.trim().split(",");
+ if (args[0].equals(MetadataOperationType.ADD_PATH_TO_MTREE)) {
+ String[] leftArgs;
+ if (args.length > 4) {
+ leftArgs = new String[args.length - 4];
+ for (int k = 4; k < args.length; k++) {
+ leftArgs[k - 4] = args[k];
+ }
+ } else {
+ leftArgs = new String[0];
+ }
+ addPathToMTree(args[1], args[2], args[3], leftArgs);
+ } else if (args[0].equals(MetadataOperationType.DELETE_PATH_FROM_MTREE)) {
+ deletePathFromMTree(args[1]);
+ } else if (args[0].equals(MetadataOperationType.SET_STORAGE_LEVEL_TO_MTREE)) {
+ setStorageLevelToMTree(args[1]);
+ } else if (args[0].equals(MetadataOperationType.ADD_A_PTREE)) {
+ addAPTree(args[1]);
+ } else if (args[0].equals(MetadataOperationType.ADD_A_PATH_TO_PTREE)) {
+ addPathToPTree(args[1]);
+ } else if (args[0].equals(MetadataOperationType.DELETE_PATH_FROM_PTREE)) {
+ deletePathFromPTree(args[1]);
+ } else if (args[0].equals(MetadataOperationType.LINK_MNODE_TO_PTREE)) {
+ linkMNodeToPTree(args[1], args[2]);
+ } else if (args[0].equals(MetadataOperationType.UNLINK_MNODE_FROM_PTREE)) {
+ unlinkMNodeFromPTree(args[1], args[2]);
+ }
+ }
+
+ private void initLogStream() {
+ if (logWriter == null) {
+ File logFile = new File(logFilePath);
+ File metadataDir = new File(metadataDirPath);
+ if (!metadataDir.exists()) {
+ metadataDir.mkdirs();
+ }
+ FileWriter fileWriter;
+ try {
+
+ fileWriter = new FileWriter(logFile, true);
+ logWriter = new BufferedWriter(fileWriter);
+ } catch (IOException e) {
+ e.printStackTrace();
+ throw new RuntimeException(e);
+ }
+ }
+ }
+
+ /**
+ *
+ * Add one timeseries to metadata. Must invoke thepathExist
and
+ * getFileNameByPath
method first to check timeseries.
+ *
+ *
+ * @param path
+ * the timeseries path
+ * @param dataType
+ * the datetype {@code DataType} for the timeseries
+ * @param encoding
+ * the encoding function {@code Encoding} for the timeseries
+ * @param args
+ * @throws PathErrorException
+ * @throws IOException
+ * @throws MetadataArgsErrorException
+ */
+ public void addPathToMTree(String path, String dataType, String encoding, String[] args)
+ throws PathErrorException, IOException, MetadataArgsErrorException {
+
+ lock.writeLock().lock();
+ try {
+ mGraph.addPathToMTree(path, dataType, encoding, args);
+ if (writeToLog) {
+ initLogStream();
+ logWriter.write(MetadataOperationType.ADD_PATH_TO_MTREE + "," + path + "," + dataType + "," + encoding);
+ for (int i = 0; i < args.length; i++) {
+ logWriter.write("," + args[i]);
+ }
+ logWriter.newLine();
+ logWriter.flush();
+ }
+ } finally {
+ lock.writeLock().unlock();
+ }
+ }
+
+ public String deletePathFromMTree(String path) throws PathErrorException, IOException {
+
+ lock.writeLock().lock();
+ try {
+ String dataFileName = mGraph.deletePath(path);
+ if (writeToLog) {
+ initLogStream();
+ logWriter.write(MetadataOperationType.DELETE_PATH_FROM_MTREE + "," + path);
+ logWriter.newLine();
+ logWriter.flush();
+ }
+ return dataFileName;
+ } finally {
+ lock.writeLock().unlock();
+ }
+ }
+
+ public void setStorageLevelToMTree(String path) throws PathErrorException, IOException {
+
+ lock.writeLock().lock();
+ try {
+ mGraph.setStorageLevel(path);
+ if (writeToLog) {
+ initLogStream();
+ logWriter.write(MetadataOperationType.SET_STORAGE_LEVEL_TO_MTREE + "," + path);
+ logWriter.newLine();
+ logWriter.flush();
+ }
+ } finally {
+ lock.writeLock().unlock();
+ }
+ }
+
+ public void addAPTree(String pTreeRootName) throws IOException, MetadataArgsErrorException {
+
+ lock.writeLock().lock();
+ try {
+ mGraph.addAPTree(pTreeRootName);
+ if (writeToLog) {
+ initLogStream();
+ logWriter.write(MetadataOperationType.ADD_A_PTREE + "," + pTreeRootName);
+ logWriter.newLine();
+ logWriter.flush();
+ }
+ } finally {
+ lock.writeLock().unlock();
+ }
+ }
+
+ public void addPathToPTree(String path) throws PathErrorException, IOException, MetadataArgsErrorException {
+
+ lock.writeLock().lock();
+ try {
+ mGraph.addPathToPTree(path);
+ if (writeToLog) {
+ initLogStream();
+ logWriter.write(MetadataOperationType.ADD_A_PATH_TO_PTREE + "," + path);
+ logWriter.newLine();
+ logWriter.flush();
+ }
+ } finally {
+ lock.writeLock().unlock();
+ }
+ }
+
+ public void deletePathFromPTree(String path) throws PathErrorException, IOException {
+
+ lock.writeLock().lock();
+ try {
+ mGraph.deletePath(path);
+ if (writeToLog) {
+ initLogStream();
+ logWriter.write(MetadataOperationType.DELETE_PATH_FROM_PTREE + "," + path);
+ logWriter.newLine();
+ logWriter.flush();
+ }
+ } finally {
+ lock.writeLock().unlock();
+ }
+ }
+
+ public void linkMNodeToPTree(String path, String mPath) throws PathErrorException, IOException {
+
+ lock.writeLock().lock();
+ try {
+ mGraph.linkMNodeToPTree(path, mPath);
+ if (writeToLog) {
+ initLogStream();
+ logWriter.write(MetadataOperationType.LINK_MNODE_TO_PTREE + "," + path + "," + mPath);
+ logWriter.newLine();
+ logWriter.flush();
+ }
+ } finally {
+ lock.writeLock().unlock();
+ }
+ }
+
+ public void unlinkMNodeFromPTree(String path, String mPath) throws PathErrorException, IOException {
+
+ lock.writeLock().lock();
+ try {
+ mGraph.unlinkMNodeFromPTree(path, mPath);
+ if (writeToLog) {
+ initLogStream();
+ logWriter.write(MetadataOperationType.UNLINK_MNODE_FROM_PTREE + "," + path + "," + mPath);
+ logWriter.newLine();
+ logWriter.flush();
+ }
+ } finally {
+ lock.writeLock().unlock();
+ }
+ }
+
+ /**
+ * Extract the DeltaObjectId from given path
+ *
+ * @param path
+ * @return String represents the DeltaObjectId
+ */
+ public String getDeltaObjectTypeByPath(String path) throws PathErrorException {
+
+ lock.readLock().lock();
+ try {
+ return mGraph.getDeltaObjectTypeByPath(path);
+ } finally {
+ lock.readLock().unlock();
+ }
+ }
+
+ /**
+ * Get series type for given path
+ *
+ * @param fullPath
+ * @return TSDataType
+ * @throws PathErrorException
+ */
+ public TSDataType getSeriesType(String fullPath) throws PathErrorException {
+
+ lock.readLock().lock();
+ try {
+ return getSchemaForOnePath(fullPath).dataType;
+ } finally {
+ lock.readLock().unlock();
+ }
+ }
+
+ /**
+ * Get all DeltaObject type in current Metadata Tree
+ *
+ * @return a HashMap contains all distinct DeltaObject type separated by
+ * DeltaObject Type
+ * @throws PathErrorException
+ */
+ public Map> getSchemaForAllType() throws PathErrorException {
+
+ lock.readLock().lock();
+ try {
+ return mGraph.getSchemaForAllType();
+ } finally {
+ lock.readLock().unlock();
+ }
+ }
+
+ /**
+ * Get the full Metadata info.
+ *
+ * @return A {@code Metadata} instance which stores all metadata info
+ * @throws PathErrorException
+ */
+ public Metadata getMetadata() throws PathErrorException {
+
+ lock.readLock().lock();
+ try {
+ return mGraph.getMetadata();
+ } finally {
+ lock.readLock().unlock();
+ }
+ }
+
+ /**
+ * Get all ColumnSchemas for given delta object type
+ *
+ * @param path
+ * A path represented one Delta object
+ * @return a list contains all column schema
+ * @throws PathErrorException
+ */
+ @Deprecated
+ public ArrayList getSchemaForOneType(String path) throws PathErrorException {
+
+ lock.readLock().lock();
+ try {
+ return mGraph.getSchemaForOneType(path);
+ } finally {
+ lock.readLock().unlock();
+ }
+ }
+
+ /**
+ * Get all ColumnSchemas for the filenode path
+ * @param path
+ * @return ArrayList The list of the schema
+ */
+ public ArrayList getSchemaForFileName(String path){
+
+ lock.readLock().lock();
+ try{
+ return mGraph.getSchemaForOneFileNode(path);
+ }finally {
+ lock.readLock().unlock();
+ }
+ }
+ public Map getSchemaMapForOneFileNode(String path){
+
+ lock.readLock().lock();
+ try{
+ return mGraph.getSchemaMapForOneFileNode(path);
+ }finally {
+ lock.readLock().unlock();
+ }
+ }
+
+ public Map getNumSchemaMapForOneFileNode(String path){
+
+ lock.readLock().lock();
+ try{
+ return mGraph.getNumSchemaMapForOneFileNode(path);
+ }finally {
+ lock.readLock().unlock();
+ }
+ }
+
+
+
+ /**
+ * Calculate the count of storage-level nodes included in given path
+ *
+ * @param path
+ * @return The total count of storage-level nodes.
+ * @throws PathErrorException
+ */
+ public int getFileCountForOneType(String path) throws PathErrorException {
+
+ lock.readLock().lock();
+ try {
+ return mGraph.getFileCountForOneType(path);
+ } finally {
+ lock.readLock().unlock();
+ }
+ }
+
+ /**
+ * Get the file name for given path Notice: This method could be called if
+ * and only if the path includes one node whose {@code isStorageLevel} is
+ * true
+ *
+ * @param path
+ * @return A String represented the file name
+ * @throws PathErrorException
+ */
+ public String getFileNameByPath(String path) throws PathErrorException {
+
+ lock.readLock().lock();
+ try {
+ return mGraph.getFileNameByPath(path);
+ } catch (PathErrorException e) {
+ throw new PathErrorException(String.format(e.getMessage()));
+ } finally {
+ lock.readLock().unlock();
+ }
+ }
+
+ public boolean checkFileNameByPath(String path) {
+
+ lock.readLock().lock();
+ try {
+ return mGraph.checkFileNameByPath(path);
+ } finally {
+ lock.readLock().unlock();
+ }
+ }
+
+ public List getAllFileNames() throws PathErrorException {
+
+ lock.readLock().lock();
+ try {
+ HashMap> res = getAllPathGroupByFileName(ROOT_NAME);
+ List fileNameList = new ArrayList();
+ for (String fileName : res.keySet()) {
+ fileNameList.add(fileName);
+ }
+ return fileNameList;
+ } finally {
+ lock.readLock().unlock();
+ }
+ }
+
+ /**
+ * return a HashMap contains all the paths separated by File Name
+ */
+ public HashMap> getAllPathGroupByFileName(String path) throws PathErrorException {
+
+ lock.readLock().lock();
+ try {
+ return mGraph.getAllPathGroupByFilename(path);
+ } finally {
+ lock.readLock().unlock();
+ }
+ }
+
+ /**
+ * return all paths for given path if the path is abstract.Or return the
+ * path itself.
+ */
+ public ArrayList getPaths(String path) throws PathErrorException {
+
+ lock.readLock().lock();
+ try {
+ ArrayList res = new ArrayList<>();
+ HashMap> pathsGroupByFilename = getAllPathGroupByFileName(path);
+ for (ArrayList ps : pathsGroupByFilename.values()) {
+ res.addAll(ps);
+ }
+ return res;
+ } finally {
+ lock.readLock().unlock();
+ }
+ }
+
+ /**
+ * Check whether the path given exists
+ */
+ public boolean pathExist(String path) {
+
+ lock.readLock().lock();
+ try {
+ return mGraph.pathExist(path);
+ } finally {
+ lock.readLock().unlock();
+ }
+ }
+
+ /**
+ * Get ColumnSchema for given path. Notice: Path must be a complete Path
+ * from root to leaf node.
+ */
+ public ColumnSchema getSchemaForOnePath(String path) throws PathErrorException {
+
+ lock.readLock().lock();
+ try {
+ return mGraph.getSchemaForOnePath(path);
+ } finally {
+ lock.readLock().unlock();
+ }
+ }
+
+ /**
+ * Check whether given path contains a MNode whose
+ * {@code MNode.isStorageLevel} is true
+ */
+ public boolean checkFileLevel(List path) throws PathErrorException {
+
+ lock.readLock().lock();
+ try {
+ for (Path p : path) {
+ getFileNameByPath(p.getFullPath());
+ }
+ return true;
+ } finally {
+ lock.readLock().unlock();
+ }
+ }
+
+ public void flushObjectToFile() throws IOException {
+
+ lock.writeLock().lock();
+ try {
+ File dataFile = new File(datafilePath);
+ // delete old metadata data file
+ if (dataFile.exists()) {
+ dataFile.delete();
+ }
+ File metadataDir = new File(metadataDirPath);
+ if (!metadataDir.exists()) {
+ metadataDir.mkdirs();
+ }
+ File tempFile = new File(datafilePath + MetadataConstant.METADATA_TEMP);
+ FileOutputStream fos = new FileOutputStream(tempFile);
+ ObjectOutputStream oos = new ObjectOutputStream(fos);
+ oos.writeObject(mGraph);
+ oos.close();
+ // close the logFile stream
+ if (logWriter != null) {
+ logWriter.close();
+ logWriter = null;
+ }
+ // rename temp file to data file
+ tempFile.renameTo(dataFile);
+ } finally {
+ lock.writeLock().unlock();
+ }
+ }
+
+ public String getMetadataInString() {
+
+ lock.readLock().lock();
+ try {
+ return mGraph.toString();
+ } finally {
+ lock.readLock().unlock();
+ }
+ }
}
diff --git a/src/main/java/cn/edu/tsinghua/iotdb/metadata/MetadataOperationType.java b/src/main/java/cn/edu/tsinghua/iotdb/metadata/MetadataOperationType.java
index e219fc176fa..8bceee372cb 100644
--- a/src/main/java/cn/edu/tsinghua/iotdb/metadata/MetadataOperationType.java
+++ b/src/main/java/cn/edu/tsinghua/iotdb/metadata/MetadataOperationType.java
@@ -9,6 +9,4 @@ public class MetadataOperationType {
public final static String DELETE_PATH_FROM_PTREE = "5";
public final static String LINK_MNODE_TO_PTREE = "6";
public final static String UNLINK_MNODE_FROM_PTREE = "7";
- public final static String ADD_INDEX_TO_PATH = "8";
- public final static String DELETE_INDEX_FROM_PATH = "9";
}
diff --git a/src/main/java/cn/edu/tsinghua/iotdb/qp/QueryProcessor.java b/src/main/java/cn/edu/tsinghua/iotdb/qp/QueryProcessor.java
index c6d01f354de..ba8924cb1bb 100644
--- a/src/main/java/cn/edu/tsinghua/iotdb/qp/QueryProcessor.java
+++ b/src/main/java/cn/edu/tsinghua/iotdb/qp/QueryProcessor.java
@@ -106,7 +106,6 @@ public class QueryProcessor {
case LOADDATA:
case INSERT:
case INDEX:
- case INDEXQUERY:
return operator;
case QUERY:
case UPDATE:
@@ -114,7 +113,7 @@ public class QueryProcessor {
SFWOperator root = (SFWOperator) operator;
return optimizeSFWOperator(root, executor);
default:
- throw new LogicalOperatorException("unknown operator type:" + operator.getType());
+ throw new LogicalOperatorException("unknown operator type:{}" + operator.getType());
}
}
diff --git a/src/main/java/cn/edu/tsinghua/iotdb/qp/constant/SQLConstant.java b/src/main/java/cn/edu/tsinghua/iotdb/qp/constant/SQLConstant.java
index c2f40a3c0a9..8f911ed42d5 100644
--- a/src/main/java/cn/edu/tsinghua/iotdb/qp/constant/SQLConstant.java
+++ b/src/main/java/cn/edu/tsinghua/iotdb/qp/constant/SQLConstant.java
@@ -94,10 +94,9 @@ public class SQLConstant {
public static final int TOK_DELETE = 25;
public static final int TOK_UPDATE = 26;
public static final int TOK_QUERY = 27;
-
+
public static final int TOK_CREATE_INDEX = 31;
- public static final int TOK_DROP_INDEX = 32;
- public static final int TOK_QUERY_INDEX = 33;
+ public static final int TOK_SELECT_INDEX = 32;
public static final int TOK_AUTHOR_CREATE = 41;
public static final int TOK_AUTHOR_DROP = 42;
diff --git a/src/main/java/cn/edu/tsinghua/iotdb/qp/executor/OverflowQPExecutor.java b/src/main/java/cn/edu/tsinghua/iotdb/qp/executor/OverflowQPExecutor.java
index 4a573a230a1..513b3b554b2 100644
--- a/src/main/java/cn/edu/tsinghua/iotdb/qp/executor/OverflowQPExecutor.java
+++ b/src/main/java/cn/edu/tsinghua/iotdb/qp/executor/OverflowQPExecutor.java
@@ -16,9 +16,6 @@ import cn.edu.tsinghua.iotdb.engine.filenode.FileNodeManager;
import cn.edu.tsinghua.iotdb.exception.ArgsErrorException;
import cn.edu.tsinghua.iotdb.exception.FileNodeManagerException;
import cn.edu.tsinghua.iotdb.exception.PathErrorException;
-import cn.edu.tsinghua.iotdb.index.IndexManager;
-import cn.edu.tsinghua.iotdb.index.IoTIndex;
-import cn.edu.tsinghua.iotdb.index.common.IndexManagerException;
import cn.edu.tsinghua.iotdb.metadata.ColumnSchema;
import cn.edu.tsinghua.iotdb.metadata.MManager;
import cn.edu.tsinghua.iotdb.qp.constant.SQLConstant;
@@ -27,7 +24,6 @@ import cn.edu.tsinghua.iotdb.qp.logical.sys.MetadataOperator;
import cn.edu.tsinghua.iotdb.qp.logical.sys.PropertyOperator;
import cn.edu.tsinghua.iotdb.qp.physical.PhysicalPlan;
import cn.edu.tsinghua.iotdb.qp.physical.crud.DeletePlan;
-import cn.edu.tsinghua.iotdb.qp.physical.crud.IndexPlan;
import cn.edu.tsinghua.iotdb.qp.physical.crud.InsertPlan;
import cn.edu.tsinghua.iotdb.qp.physical.crud.UpdatePlan;
import cn.edu.tsinghua.iotdb.qp.physical.sys.AuthorPlan;
@@ -53,7 +49,6 @@ public class OverflowQPExecutor extends QueryProcessExecutor {
private OverflowQueryEngine queryEngine;
private FileNodeManager fileNodeManager;
private MManager mManager = MManager.getInstance();
-// private KvMatchIndex kvMatchIndex = KvMatchIndex.getInstance();
public OverflowQPExecutor() {
queryEngine = new OverflowQueryEngine();
@@ -92,72 +87,12 @@ public class OverflowQPExecutor extends QueryProcessExecutor {
case PROPERTY:
PropertyPlan property = (PropertyPlan) plan;
return operateProperty(property);
- case INDEX:
- IndexPlan indexPlan = (IndexPlan) plan;
- return operateIndex(indexPlan);
default:
throw new UnsupportedOperationException(
String.format("operation %s does not support", plan.getOperatorType()));
}
}
- private boolean operateIndex(IndexPlan indexPlan) throws ProcessorException {
- switch (indexPlan.getIndexOperatorType()) {
- case CREATE_INDEX:
- try {
- String path = indexPlan.getPaths().get(0).getFullPath();
- // check path
- if(!mManager.pathExist(path)){
- throw new ProcessorException(String.format("The timeseries %s does not exist.", path));
- }
- // check storage group
- mManager.getFileNameByPath(path);
- // check index
- if (mManager.checkPathIndex(path, indexPlan.getIndexType())) {
- throw new ProcessorException(String.format("The timeseries %s has already been indexed.", path));
- }
- // create index
- IoTIndex index = IndexManager.getIndexInstance(indexPlan.getIndexType());
- if(index == null)
- throw new IndexManagerException(indexPlan.getIndexType()+" doesn't support");
- Path indexPath = indexPlan.getPaths().get(0);
- if (index.build(indexPath, new ArrayList<>(), indexPlan.getParameters())) {
- mManager.addIndexForOneTimeseries(path,indexPlan.getIndexType());
- }
- } catch (IndexManagerException | PathErrorException | IOException e) {
- e.printStackTrace();
- throw new ProcessorException(e.getMessage());
- }
- break;
- case DROP_INDEX:
- try {
- String path = indexPlan.getPaths().get(0).getFullPath();
- // check path
- if(!mManager.pathExist(path)){
- throw new ProcessorException(String.format("The timeseries %s does not exist.", path));
- }
- // check index
- if (!mManager.checkPathIndex(path, indexPlan.getIndexType())) {
- throw new ProcessorException(String.format("The timeseries %s hasn't been indexed.", path));
- }
- IoTIndex index = IndexManager.getIndexInstance(indexPlan.getIndexType());
- if(index == null)
- throw new IndexManagerException(indexPlan.getIndexType()+" doesn't support");
- Path indexPath = indexPlan.getPaths().get(0);
- if (index.drop(indexPath)) {
- mManager.deleteIndexForOneTimeseries(path, indexPlan.getIndexType());
- }
- } catch (IndexManagerException | PathErrorException | IOException e) {
- e.printStackTrace();
- throw new ProcessorException(e.getMessage());
- }
- break;
- default:
- throw new ProcessorException(String.format("Not support the index operation %s", indexPlan.getIndexType()));
- }
- return true;
- }
-
@Override
public TSDataType getSeriesType(Path path) throws PathErrorException {
if (path.equals(SQLConstant.RESERVED_TIME))
@@ -194,7 +129,7 @@ public class OverflowQPExecutor extends QueryProcessExecutor {
throws ProcessorException {
try {
- return queryEngine.query(formNumber, paths, timeFilter, freqFilter, valueFilter, lastData, fetchSize, null);
+ return queryEngine.query(formNumber, paths, timeFilter, freqFilter, valueFilter, lastData, fetchSize);
} catch (Exception e) {
throw new ProcessorException(e.getMessage());
}
diff --git a/src/main/java/cn/edu/tsinghua/iotdb/qp/executor/QueryProcessExecutor.java b/src/main/java/cn/edu/tsinghua/iotdb/qp/executor/QueryProcessExecutor.java
index b7e0af16974..3b9fbadc0e1 100644
--- a/src/main/java/cn/edu/tsinghua/iotdb/qp/executor/QueryProcessExecutor.java
+++ b/src/main/java/cn/edu/tsinghua/iotdb/qp/executor/QueryProcessExecutor.java
@@ -1,14 +1,11 @@
package cn.edu.tsinghua.iotdb.qp.executor;
import cn.edu.tsinghua.iotdb.exception.PathErrorException;
-import cn.edu.tsinghua.iotdb.index.kvmatch.KvMatchQueryRequest;
import cn.edu.tsinghua.iotdb.metadata.MManager;
import cn.edu.tsinghua.iotdb.qp.exception.QueryProcessorException;
import cn.edu.tsinghua.iotdb.qp.executor.iterator.MergeQuerySetIterator;
-import cn.edu.tsinghua.iotdb.qp.executor.iterator.PatternQueryDataSetIterator;
import cn.edu.tsinghua.iotdb.qp.executor.iterator.QueryDataSetIterator;
import cn.edu.tsinghua.iotdb.qp.physical.PhysicalPlan;
-import cn.edu.tsinghua.iotdb.qp.physical.crud.IndexQueryPlan;
import cn.edu.tsinghua.iotdb.qp.physical.crud.MultiQueryPlan;
import cn.edu.tsinghua.iotdb.qp.physical.crud.SingleQueryPlan;
import cn.edu.tsinghua.iotdb.query.engine.FilterStructure;
@@ -39,9 +36,6 @@ public abstract class QueryProcessExecutor {
//process MultiQueryPlan
public Iterator processQuery(PhysicalPlan plan) throws QueryProcessorException {
- if(plan instanceof IndexQueryPlan){
- return ((IndexQueryPlan) plan).fetchQueryDateSet(getFetchSize());
- }
MultiQueryPlan mergeQuery = (MultiQueryPlan) plan;
List selectPlans = mergeQuery.getSingleQueryPlans();
switch (mergeQuery.getType()) {
diff --git a/src/main/java/cn/edu/tsinghua/iotdb/qp/executor/iterator/PatternQueryDataSetIterator.java b/src/main/java/cn/edu/tsinghua/iotdb/qp/executor/iterator/PatternQueryDataSetIterator.java
deleted file mode 100644
index 862328452d9..00000000000
--- a/src/main/java/cn/edu/tsinghua/iotdb/qp/executor/iterator/PatternQueryDataSetIterator.java
+++ /dev/null
@@ -1,78 +0,0 @@
-package cn.edu.tsinghua.iotdb.qp.executor.iterator;
-
-import cn.edu.tsinghua.iotdb.index.IndexManager;
-import cn.edu.tsinghua.iotdb.index.IoTIndex;
-import cn.edu.tsinghua.iotdb.index.common.IndexManagerException;
-import cn.edu.tsinghua.iotdb.index.kvmatch.KvMatchQueryRequest;
-import cn.edu.tsinghua.tsfile.timeseries.read.query.QueryDataSet;
-
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
-import static cn.edu.tsinghua.iotdb.index.IndexManager.IndexType.KvIndex;
-
-/**
- * Result wrap for KV-match index query, only return fetchSize number of results in one batch.
- *
- * @author Jiaye Wu
- */
-public class PatternQueryDataSetIterator implements Iterator {
-
- private static IoTIndex kvMatchIndexManager = IndexManager.getIndexInstance(KvIndex);
-
- private boolean noNext = false;
- private KvMatchQueryRequest queryRequest;
- private final int fetchSize;
- private QueryDataSet data = null;
- private QueryDataSet useddata = null;
-
- public PatternQueryDataSetIterator(KvMatchQueryRequest queryRequest, int fetchSize) {
- this.queryRequest = queryRequest;
- this.fetchSize = fetchSize;
- }
-
- @Override
- public boolean hasNext() {
- if (useddata != null) {
- useddata.clear();
- }
- if (noNext) {
- return false;
- }
- if (data == null || !data.hasNextRecord()) {
- try {
- List