提交 f58160c7 编写于 作者: Q qiaojialin

fix sonar

上级 ee66ca47
......@@ -73,7 +73,7 @@ error: encoding TS_2DIFF does not support BOOLEAN
### 标签点管理
我们可以在创建时间序列的时候,为它添加别名和额外的便签和属性信息。
我们可以在创建时间序列的时候,为它添加别名和额外的签和属性信息。
所用到的扩展的创建时间序列的SQL语句如下所示:
```
create timeseries root.turbine.d1.s1(temprature) with datatype=FLOAT, encoding=RLE, compression=SNAPPY tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2)
......@@ -84,7 +84,7 @@ create timeseries root.turbine.d1.s1(temprature) with datatype=FLOAT, encoding=R
> 注意:额外的标签和属性信息总的大小不能超过`tag_attribute_total_size`.
标签和属性的唯一差别在于,我们为便签信息在内存中维护了一个倒排索引,所以可以在`show timeseries`的条件语句中使用标签作为查询条件,你将会在下一节看到具体查询内容。
标签和属性的唯一差别在于,我们为签信息在内存中维护了一个倒排索引,所以可以在`show timeseries`的条件语句中使用标签作为查询条件,你将会在下一节看到具体查询内容。
## 查看时间序列
......
......@@ -23,7 +23,7 @@ import org.apache.iotdb.rpc.TSStatusCode;
/**
* If query metadata constructs schema but passes illegal parameters to EncodingConvertor or
* DataTypeConvertor,this exception will be threw.
* DataTypeConverter,this exception will be threw.
*/
public class MetadataException extends IoTDBException {
......
......@@ -37,6 +37,7 @@ import org.apache.iotdb.db.metadata.mnode.StorageGroupMNode;
import org.apache.iotdb.db.monitor.MonitorConstants;
import org.apache.iotdb.db.qp.constant.SQLConstant;
import org.apache.iotdb.db.qp.physical.sys.CreateTimeSeriesPlan;
import org.apache.iotdb.db.query.dataset.ShowTimeSeriesResult;
import org.apache.iotdb.db.utils.RandomDeleteCache;
import org.apache.iotdb.db.utils.TestOnly;
import org.apache.iotdb.tsfile.exception.cache.CacheException;
......@@ -48,7 +49,6 @@ import org.apache.iotdb.tsfile.utils.Pair;
import org.apache.iotdb.tsfile.write.schema.MeasurementSchema;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
......@@ -81,7 +81,7 @@ public class MManager {
// tag key -> tag value -> LeafMNode
private Map<String, Map<String, Set<LeafMNode>>> tagIndex = new HashMap<>();
private Map<String, Integer> seriesNumberInStorageGroups = new HashMap<>();
private Map<String, Integer> seriesNumberInStorageGroups;
private long maxSeriesNumberAmongStorageGroup;
private boolean initialized;
private IoTDBConfig config;
......@@ -304,27 +304,22 @@ public class MManager {
// check memory
IoTDBConfigDynamicAdapter.getInstance().addOrDeleteTimeSeries(1);
} catch (ConfigAdjusterException e) {
try {
removeFromTagInvertedIndex(mtree.deleteTimeseriesAndReturnEmptyStorageGroup(path).right);
} catch (IOException ex) {
throw new MetadataException(ex);
}
throw new MetadataException(e);
removeFromTagInvertedIndex(mtree.deleteTimeseriesAndReturnEmptyStorageGroup(path).right);
throw e;
}
try {
// write to log
if (writeToLog) {
// either tags or attributes is not empty
if ((plan.getTags() != null && !plan.getTags().isEmpty()) || (plan.getAttributes() != null && !plan.getAttributes().isEmpty())) {
offset = tagLogFile.write(plan.getTags(), plan.getAttributes());
}
logWriter.createTimeseries(plan, offset);
// write log
if (writeToLog) {
// either tags or attributes is not empty
if ((plan.getTags() != null && !plan.getTags().isEmpty()) || (plan.getAttributes() != null
&& !plan.getAttributes().isEmpty())) {
offset = tagLogFile.write(plan.getTags(), plan.getAttributes());
}
} catch (IOException e) {
throw new MetadataException(e.getMessage());
logWriter.createTimeseries(plan, offset);
}
leafMNode.setOffset(offset);
// update tag index
if (plan.getTags() != null) {
// tag key, tag value
for (Entry<String, String> entry : plan.getTags().entrySet()) {
......@@ -342,7 +337,8 @@ public class MManager {
maxSeriesNumberAmongStorageGroup = size + 1;
}
}
} catch (IOException | ConfigAdjusterException e) {
throw new MetadataException(e.getMessage());
} finally {
lock.writeLock().unlock();
}
......@@ -681,7 +677,8 @@ public class MManager {
}
}
public List<ShowTimeSeriesResult> getAllMeasurementSchema(String path, boolean isContains, String key, String value) throws MetadataException {
public List<ShowTimeSeriesResult> getAllMeasurementSchema(String prefixPath, boolean isContains,
String key, String value) throws MetadataException {
lock.readLock().lock();
try {
if (!tagIndex.containsKey(key)) {
......@@ -705,21 +702,21 @@ public class MManager {
}
}
List<ShowTimeSeriesResult> res = new LinkedList<>();
String[] nodes = MetaUtils.getNodeNames(path);
for (LeafMNode node : allMatchedNodes) {
if (match(node.getFullPath(), nodes)) {
String[] prefixNodes = MetaUtils.getNodeNames(prefixPath);
for (LeafMNode leaf : allMatchedNodes) {
if (match(leaf.getFullPath(), prefixNodes)) {
try {
Pair<Map<String, String>, Map<String, String>> pair =
tagLogFile.read(config.getTagAttributeTotalSize(), node.getOffset());
tagLogFile.read(config.getTagAttributeTotalSize(), leaf.getOffset());
pair.left.putAll(pair.right);
MeasurementSchema measurementSchema = node.getSchema();
res.add(new ShowTimeSeriesResult(node.getFullPath(), node.getAlias(),
getStorageGroupName(node.getFullPath()), measurementSchema.getType().toString(),
MeasurementSchema measurementSchema = leaf.getSchema();
res.add(new ShowTimeSeriesResult(leaf.getFullPath(), leaf.getAlias(),
getStorageGroupName(leaf.getFullPath()), measurementSchema.getType().toString(),
measurementSchema.getEncodingType().toString(),
measurementSchema.getCompressor().toString(), pair.left));
} catch (IOException e) {
logger.error(String.format("Something went wrong while deserialize tag info of %s", node.getFullPath()), e);
throw new MetadataException(e);
throw new MetadataException(
"Something went wrong while deserialize tag info of " + leaf.getFullPath(), e);
}
}
}
......@@ -729,13 +726,16 @@ public class MManager {
}
}
private boolean match(String fullPath, String[] target) {
/**
* whether the full path has the prefixNodes
*/
private boolean match(String fullPath, String[] prefixNodes) {
String[] nodes = MetaUtils.getNodeNames(fullPath);
if (nodes.length < target.length) {
if (nodes.length < prefixNodes.length) {
return false;
}
for (int i = 0; i < target.length; i++) {
if (!"*".equals(target[i]) && !target[i].equals(nodes[i])) {
for (int i = 0; i < prefixNodes.length; i++) {
if (!"*".equals(prefixNodes[i]) && !prefixNodes[i].equals(nodes[i])) {
return false;
}
}
......@@ -758,7 +758,7 @@ public class MManager {
try {
if (offset < 0) {
res.add(new ShowTimeSeriesResult(ansString[0], ansString[1], ansString[2],
ansString[3], ansString[4], ansString[5], Collections.EMPTY_MAP));
ansString[3], ansString[4], ansString[5], Collections.emptyMap()));
continue;
}
Pair<Map<String, String>, Map<String, String>> pair =
......@@ -767,8 +767,8 @@ public class MManager {
res.add(new ShowTimeSeriesResult(ansString[0], ansString[1], ansString[2],
ansString[3], ansString[4], ansString[5], pair.left));
} catch (IOException e) {
logger.error(String.format("Something went wrong while deserialize tag info of %s", ansString[0]), e);
throw new MetadataException(e);
throw new MetadataException(
"Something went wrong while deserialize tag info of " + ansString[0], e);
}
}
return res;
......@@ -777,26 +777,7 @@ public class MManager {
}
}
public static class ShowTimeSeriesResult {
public String name;
public String alias;
public String sgName;
public String dataType;
public String encoding;
public String compressor;
public Map<String, String> tagAndAttribute;
public ShowTimeSeriesResult(String name, String alias, String sgName, String dataType,
String encoding, String compressor, Map<String, String> tagAndAttribute) {
this.name = name;
this.alias = alias;
this.sgName = sgName;
this.dataType = dataType;
this.encoding = encoding;
this.compressor = compressor;
this.tagAndAttribute = tagAndAttribute;
}
}
public MeasurementSchema getSeriesSchema(String device, String measuremnet) throws MetadataException {
lock.readLock().lock();
......
......@@ -37,6 +37,7 @@ public class TagLogFile implements AutoCloseable {
private static final Logger logger = LoggerFactory.getLogger(TagLogFile.class);
private FileChannel fileChannel;
private static final String LENGTH_EXCEED_MSG = "Tag/Attribute exceeds the max length limit.";
private static final int MAX_LENGTH = IoTDBDescriptor.getInstance().getConfig().getTagAttributeTotalSize();
......@@ -60,6 +61,9 @@ public class TagLogFile implements AutoCloseable {
this.fileChannel.position(fileChannel.size());
}
/**
* @return tags map, attributes map
*/
public Pair<Map<String, String>, Map<String, String>> read(int size, long position) throws IOException {
ByteBuffer byteBuffer = ByteBuffer.allocate(size);
fileChannel.read(byteBuffer, position);
......@@ -96,14 +100,14 @@ public class TagLogFile implements AutoCloseable {
if (map == null) {
length += Integer.BYTES;
if (length > MAX_LENGTH) {
throw new MetadataException("Tag/Attribute exceeds the max length limit.");
throw new MetadataException(LENGTH_EXCEED_MSG);
}
ReadWriteIOUtils.write(0, byteBuffer);
return length;
}
length += Integer.BYTES;
if (length > MAX_LENGTH) {
throw new MetadataException("Tag/Attribute exceeds the max length limit.");
throw new MetadataException(LENGTH_EXCEED_MSG);
}
ReadWriteIOUtils.write(map.size(), byteBuffer);
byte[] bytes;
......@@ -112,7 +116,7 @@ public class TagLogFile implements AutoCloseable {
bytes = entry.getKey().getBytes();
length += (4 + bytes.length);
if (length > MAX_LENGTH) {
throw new MetadataException("Tag/Attribute exceeds the max length limit.");
throw new MetadataException(LENGTH_EXCEED_MSG);
}
ReadWriteIOUtils.write(bytes.length, byteBuffer);
byteBuffer.put(bytes);
......@@ -121,7 +125,7 @@ public class TagLogFile implements AutoCloseable {
bytes = entry.getValue().getBytes();
length += (4 + bytes.length);
if (length > MAX_LENGTH) {
throw new MetadataException("Tag/Attribute exceeds the max length limit.");
throw new MetadataException(LENGTH_EXCEED_MSG);
}
ReadWriteIOUtils.write(bytes.length, byteBuffer);
byteBuffer.put(bytes);
......
......@@ -36,6 +36,7 @@ public class LeafMNode extends MNode {
*/
private MeasurementSchema schema;
private String alias;
// tag/attribute's start offset in tag file
private long offset = -1;
private TimeValuePair cachedLastValuePair = null;
......
......@@ -50,6 +50,7 @@ import org.apache.iotdb.db.qp.physical.sys.*;
import org.apache.iotdb.db.query.context.QueryContext;
import org.apache.iotdb.db.query.dataset.AlignByDeviceDataSet;
import org.apache.iotdb.db.query.dataset.ListDataSet;
import org.apache.iotdb.db.query.dataset.ShowTimeSeriesResult;
import org.apache.iotdb.db.query.dataset.SingleDataSet;
import org.apache.iotdb.db.query.executor.IQueryRouter;
import org.apache.iotdb.db.query.executor.QueryRouter;
......@@ -77,11 +78,9 @@ import org.apache.iotdb.tsfile.write.schema.MeasurementSchema;
import org.apache.iotdb.tsfile.write.writer.RestorableTsFileIOWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.*;
import static org.apache.iotdb.db.conf.IoTDBConstant.*;
import static org.apache.iotdb.tsfile.common.constant.TsFileConstant.PATH_SEPARATOR;
import static org.apache.iotdb.tsfile.common.constant.TsFileConstant.TSFILE_SUFFIX;
......@@ -359,7 +358,7 @@ public class PlanExecutor implements IPlanExecutor {
}
private QueryDataSet processShowTimeseriesWithIndex(ShowTimeSeriesPlan timeSeriesPlan) throws MetadataException {
List<MManager.ShowTimeSeriesResult> timeseriesList = MManager.getInstance()
List<ShowTimeSeriesResult> timeseriesList = MManager.getInstance()
.getAllMeasurementSchema(timeSeriesPlan.getPath().toString(),
timeSeriesPlan.isContains(), timeSeriesPlan.getKey(), timeSeriesPlan.getValue());
return getQueryDataSet(timeseriesList);
......@@ -367,12 +366,12 @@ public class PlanExecutor implements IPlanExecutor {
private QueryDataSet processShowTimeseries(ShowTimeSeriesPlan timeSeriesPlan)
throws MetadataException {
List<MManager.ShowTimeSeriesResult> timeseriesList = MManager.getInstance()
List<ShowTimeSeriesResult> timeseriesList = MManager.getInstance()
.getAllMeasurementSchema(timeSeriesPlan.getPath().toString());
return getQueryDataSet(timeseriesList);
}
private QueryDataSet getQueryDataSet(List<MManager.ShowTimeSeriesResult> timeseriesList) {
private QueryDataSet getQueryDataSet(List<ShowTimeSeriesResult> timeseriesList) {
List<Path> paths = new ArrayList<>();
List<TSDataType> dataTypes = new ArrayList<>();
paths.add(new Path(COLUMN_TIMESERIES));
......@@ -389,7 +388,7 @@ public class PlanExecutor implements IPlanExecutor {
dataTypes.add(TSDataType.TEXT);
Set<String> tagAndAttributeName = new TreeSet<>();
for (MManager.ShowTimeSeriesResult result : timeseriesList) {
for (ShowTimeSeriesResult result : timeseriesList) {
tagAndAttributeName.addAll(result.tagAndAttribute.keySet());
}
for (String key : tagAndAttributeName) {
......@@ -398,15 +397,15 @@ public class PlanExecutor implements IPlanExecutor {
}
ListDataSet listDataSet = new ListDataSet(paths, dataTypes);
for (MManager.ShowTimeSeriesResult result : timeseriesList) {
for (ShowTimeSeriesResult result : timeseriesList) {
RowRecord record = new RowRecord(0);
updateRecord(record, result.name);
updateRecord(record, result.alias);
updateRecord(record, result.sgName);
updateRecord(record, result.dataType);
updateRecord(record, result.encoding);
updateRecord(record, result.compressor);
updateRecord(record, result.tagAndAttribute, paths);
updateRecord(record, result.getName());
updateRecord(record, result.getAlias());
updateRecord(record, result.getSgName());
updateRecord(record, result.getDataType());
updateRecord(record, result.getEncoding());
updateRecord(record, result.getCompressor());
updateRecord(record, result.getTagAndAttribute(), paths);
listDataSet.putRecord(record);
}
return listDataSet;
......
/*
* 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.query.dataset;
import java.util.Map;
public class ShowTimeSeriesResult {
private String name;
private String alias;
private String sgName;
private String dataType;
private String encoding;
private String compressor;
private Map<String, String> tagAndAttribute;
public ShowTimeSeriesResult(String name, String alias, String sgName, String dataType,
String encoding, String compressor, Map<String, String> tagAndAttribute) {
this.name = name;
this.alias = alias;
this.sgName = sgName;
this.dataType = dataType;
this.encoding = encoding;
this.compressor = compressor;
this.tagAndAttribute = tagAndAttribute;
}
public String getName() {
return name;
}
public String getAlias() {
return alias;
}
public String getSgName() {
return sgName;
}
public String getDataType() {
return dataType;
}
public String getEncoding() {
return encoding;
}
public String getCompressor() {
return compressor;
}
public Map<String, String> getTagAndAttribute() {
return tagAndAttribute;
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册