Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev tags #71

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ private static void processCommand(String command) {
try
{
List<String> paths = getTimeseries();
Set<Long> timestamps = new HashSet();
Set<Long> timestamps = new HashSet<>();
for (int i = 0; i < paths.size(); i += MAX_GETDATA_NUM)
{
List<String> ins = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@

import cn.edu.tsinghua.iginx.exceptions.ExecutionException;
import cn.edu.tsinghua.iginx.exceptions.StatusCode;
import cn.edu.tsinghua.iginx.metadata.TagTools;
import cn.edu.tsinghua.iginx.query.result.QueryDataPlanExecuteResult;
import cn.edu.tsinghua.iginx.thrift.DataType;
import cn.edu.tsinghua.iginx.thrift.QueryDataResp;
import cn.edu.tsinghua.iginx.thrift.QueryDataSet;
import cn.edu.tsinghua.iginx.utils.Bitmap;
import cn.edu.tsinghua.iginx.utils.ByteUtils;
import cn.edu.tsinghua.iginx.utils.CheckedFunction;
import cn.edu.tsinghua.iginx.utils.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -164,7 +166,9 @@ public void combineResult(QueryDataResp resp, List<QueryDataPlanExecuteResult> p
}
}
}
resp.setPaths(columnNameList);
List<Pair<String, Map<String, String>>> pathAndTagsList = TagTools.splitTags(columnNameList);
resp.setPaths(pathAndTagsList.stream().map(e -> e.k).collect(Collectors.toList()));
resp.setTagsList(pathAndTagsList.stream().map(e -> e.v).collect(Collectors.toList()));
resp.setDataTypeList(columnTypeList);
resp.setQueryDataSet(new QueryDataSet(ByteUtils.getColumnByteBuffer(timestamps.toArray(), DataType.LONG),
valuesList, bitmapList));
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/cn/edu/tsinghua/iginx/conf/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,11 @@ public class Constants {

public static final String SCHEMA_MAPPING_PREFIX = "/schema";

public static final String TAG_PREFIX = "@";

public static final String VALUE_PREFIX = "$";

public static final String DOT = ".";

}

158 changes: 158 additions & 0 deletions core/src/main/java/cn/edu/tsinghua/iginx/metadata/TagTools.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* 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 cn.edu.tsinghua.iginx.metadata;

import cn.edu.tsinghua.iginx.conf.Constants;
import cn.edu.tsinghua.iginx.utils.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class TagTools {

private static final Logger logger = LoggerFactory.getLogger(TagTools.class);

public boolean hasTags(String path) {
return path.contains(Constants.TAG_PREFIX);
}

public static Pair<String, Map<String, String>> splitTags(String pathWithTags) {
int splitIndex = pathWithTags.indexOf(Constants.TAG_PREFIX);
if (splitIndex == -1) { // 没有 tag
return new Pair<>(pathWithTags, Collections.emptyMap());
}
String path = pathWithTags.substring(0, splitIndex - 1);
List<String> tagKList = new ArrayList<>();
List<String> tagVList = new ArrayList<>();

String[] tagKVs = pathWithTags.substring(splitIndex).split("\\.");
for (String tagKOrV: tagKVs) {
if (tagKOrV.startsWith(Constants.TAG_PREFIX)) {
tagKList.add(tagKOrV.substring(Constants.TAG_PREFIX.length()));
} else if (tagKOrV.startsWith(Constants.VALUE_PREFIX)) {
tagVList.add(tagKOrV.substring(Constants.VALUE_PREFIX.length()));
} else {
throw new IllegalArgumentException("unexpected path with tags: " + pathWithTags);
}
}
if (tagKList.size() != tagVList.size()) {
throw new IllegalArgumentException("unexpected path with tags: " + pathWithTags);
}
Map<String, String> tags = new HashMap<>();
for (int i = 0; i < tagKList.size(); i++) {
tags.put(tagKList.get(i), tagVList.get(i));
}
return new Pair<>(path, tags);
}

public static List<Pair<String, Map<String, String>>> splitTags(List<String> pathsWithTags) {
if (pathsWithTags == null) {
return Collections.emptyList();
}
return pathsWithTags.stream().map(TagTools::splitTags).collect(Collectors.toList());
}

public static List<String> concatTags(List<String> paths, List<Map<String, String>> tagsList) {
if (tagsList == null || tagsList.size() == 0) {
return paths;
}
List<String> pathsWithTags = new ArrayList<>();
for (int i = 0; i < paths.size(); i++) {
String path = paths.get(i);
Map<String, String> tags = tagsList.get(i);
if (tags == null || tags.size() == 0) {
pathsWithTags.add(path);
continue;
}

List<Map.Entry<String, String>> tagList = tags.entrySet().stream().sorted(Map.Entry.comparingByKey()).collect(Collectors.toList());
StringBuilder tagKBuilder = new StringBuilder();
StringBuilder tagVBuilder = new StringBuilder();

for (Map.Entry<String, String> tag : tagList) {
tagKBuilder.append(Constants.DOT);
tagKBuilder.append(Constants.TAG_PREFIX);
tagKBuilder.append(tag.getKey());

tagVBuilder.append(Constants.DOT);
tagVBuilder.append(Constants.VALUE_PREFIX);
tagVBuilder.append(tag.getValue());
}
pathsWithTags.add(path + tagKBuilder.toString() + tagVBuilder.toString());
}
return pathsWithTags;
}

public static String toString(String path, Map<String, String> tags) {
if (tags == null || tags.isEmpty()) {
return path;
}
StringBuilder builder = new StringBuilder(path);
List<Map.Entry<String, String>> tagList = tags.entrySet().stream().sorted(Map.Entry.comparingByKey()).collect(Collectors.toList());
for (int i = 0; i < tagList.size(); i++) {
Map.Entry<String, String> tag = tagList.get(i);
if (i == 0) {
builder.append("{");
} else {
builder.append(", ");
}
builder.append(tag.getKey());
builder.append("=");
builder.append(tag.getValue());
}
builder.append('}');
return builder.toString();
}

public static void main(String[] args) {
List<String> paths = Arrays.asList("cpu.usage", "memory.usage", "java");
List<Map<String, String>> tagsList = new ArrayList<>();

Map<String, String> tags1 = new HashMap<>();
tags1.put("host", "1");
tagsList.add(tags1);

Map<String, String> tags2 = new HashMap<>();
tags2.put("host", "1");
tags2.put("type", "ddr4");
tags2.put("producer", "Samsung");
tagsList.add(tags2);

Map<String, String> tags3 = new HashMap<>();
tagsList.add(tags3);

List<String> pathsWithTags = concatTags(paths, tagsList);
for (String pathWithTags: pathsWithTags) {
System.out.println(pathWithTags);
Pair<String, Map<String, String>> pathAndTags = splitTags(pathWithTags);
System.out.println(pathAndTags.k);
System.out.println(pathAndTags.v);
System.out.println(toString(pathAndTags.k, pathAndTags.v));
}
System.out.println();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ public class InsertColumnRecordsPlan extends InsertRecordsPlan {
private static final Logger logger = LoggerFactory.getLogger(InsertColumnRecordsPlan.class);

public InsertColumnRecordsPlan(List<String> paths, long[] timestamps, Object[] valuesList, List<Bitmap> bitmapList,
List<DataType> dataTypeList, List<Map<String, String>> attributesList, StorageUnitMeta storageUnit) {
super(paths, timestamps, valuesList, bitmapList, dataTypeList, attributesList, storageUnit);
List<DataType> dataTypeList, List<Map<String, String>> tagsList, StorageUnitMeta storageUnit) {
super(paths, timestamps, valuesList, bitmapList, dataTypeList, tagsList, storageUnit);
this.setIginxPlanType(INSERT_COLUMN_RECORDS);
}

public InsertColumnRecordsPlan(List<String> paths, long[] timestamps, Object[] valuesList, List<Bitmap> bitmapList,
List<DataType> dataTypeList, List<Map<String, String>> attributesList) {
this(paths, timestamps, valuesList, bitmapList, dataTypeList, attributesList, null);
List<DataType> dataTypeList, List<Map<String, String>> tagsList) {
this(paths, timestamps, valuesList, bitmapList, dataTypeList, tagsList, null);
}

public Pair<Object[], List<Bitmap>> getValuesAndBitmapsByIndexes(Pair<Integer, Integer> rowIndexes, TimeSeriesInterval interval) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package cn.edu.tsinghua.iginx.plan;

import cn.edu.tsinghua.iginx.metadata.TagTools;
import cn.edu.tsinghua.iginx.metadata.entity.StorageUnitMeta;
import cn.edu.tsinghua.iginx.metadata.entity.TimeInterval;
import cn.edu.tsinghua.iginx.metadata.entity.TimeSeriesInterval;
Expand Down Expand Up @@ -49,17 +50,17 @@ public abstract class InsertRecordsPlan extends DataPlan {

private List<DataType> dataTypeList;

private List<Map<String, String>> attributesList;
private List<Map<String, String>> tagsList;

protected InsertRecordsPlan(List<String> paths, long[] timestamps, Object[] valuesList, List<Bitmap> bitmapList,
List<DataType> dataTypeList, List<Map<String, String>> attributesList, StorageUnitMeta storageUnit) {
super(false, paths, timestamps[0], timestamps[timestamps.length - 1], storageUnit);
List<DataType> dataTypeList, List<Map<String, String>> tagsList, StorageUnitMeta storageUnit) {
super(false, TagTools.concatTags(paths, tagsList), timestamps[0], timestamps[timestamps.length - 1], storageUnit);
this.setIginxPlanType(INSERT_RECORDS);
this.timestamps = timestamps;
this.valuesList = valuesList;
this.bitmapList = bitmapList;
this.dataTypeList = dataTypeList;
this.attributesList = attributesList;
this.tagsList = tagsList;
}

public long getTimestamp(int index) {
Expand Down Expand Up @@ -151,19 +152,19 @@ public List<DataType> getDataTypeListByInterval(TimeSeriesInterval interval) {
}

public Map<String, String> getAttributes(int index) {
if (attributesList == null || attributesList.isEmpty()) {
if (tagsList == null || tagsList.isEmpty()) {
logger.info("There are no attributes in the InsertRecordsPlan.");
return null;
}
if (index < 0 || index >= attributesList.size()) {
if (index < 0 || index >= tagsList.size()) {
logger.error("The given index {} is out of bounds.", index);
return null;
}
return attributesList.get(index);
return tagsList.get(index);
}

public List<Map<String, String>> getAttributesByInterval(TimeSeriesInterval interval) {
if (attributesList == null || attributesList.isEmpty()) {
public List<Map<String, String>> getTagsByInterval(TimeSeriesInterval interval) {
if (tagsList == null || tagsList.isEmpty()) {
logger.info("There are no attributes in the InsertRecordsPlan.");
return null;
}
Expand All @@ -177,7 +178,7 @@ public List<Map<String, String>> getAttributesByInterval(TimeSeriesInterval inte
endIndex = i;
}
}
return attributesList.subList(startIndex, endIndex + 1);
return tagsList.subList(startIndex, endIndex + 1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ public class InsertRowRecordsPlan extends InsertRecordsPlan {
private static final Logger logger = LoggerFactory.getLogger(InsertRowRecordsPlan.class);

public InsertRowRecordsPlan(List<String> paths, long[] timestamps, Object[] valuesList, List<Bitmap> bitmapList,
List<DataType> dataTypeList, List<Map<String, String>> attributesList, StorageUnitMeta storageUnit) {
super(paths, timestamps, valuesList, bitmapList, dataTypeList, attributesList, storageUnit);
List<DataType> dataTypeList, List<Map<String, String>> tagsList, StorageUnitMeta storageUnit) {
super(paths, timestamps, valuesList, bitmapList, dataTypeList, tagsList, storageUnit);
this.setIginxPlanType(INSERT_ROW_RECORDS);
}

public InsertRowRecordsPlan(List<String> paths, long[] timestamps, Object[] valuesList, List<Bitmap> bitmapList,
List<DataType> dataTypeList, List<Map<String, String>> attributesList) {
this(paths, timestamps, valuesList, bitmapList, dataTypeList, attributesList, null);
List<DataType> dataTypeList, List<Map<String, String>> tagsList) {
this(paths, timestamps, valuesList, bitmapList, dataTypeList, tagsList, null);
}

public Pair<Object[], List<Bitmap>> getValuesAndBitmapsByIndexes(Pair<Integer, Integer> rowIndexes, TimeSeriesInterval interval) {
Expand Down
11 changes: 7 additions & 4 deletions core/src/main/java/cn/edu/tsinghua/iginx/plan/QueryDataPlan.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,26 @@
*/
package cn.edu.tsinghua.iginx.plan;

import cn.edu.tsinghua.iginx.metadata.TagTools;
import cn.edu.tsinghua.iginx.metadata.entity.StorageUnitMeta;
import cn.edu.tsinghua.iginx.metadata.entity.TimeSeriesInterval;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static cn.edu.tsinghua.iginx.plan.IginxPlan.IginxPlanType.QUERY_DATA;

public class QueryDataPlan extends DataPlan {

private static final Logger logger = LoggerFactory.getLogger(QueryDataPlan.class);

public QueryDataPlan(List<String> paths, long startTime, long endTime) {
super(true, paths, startTime, endTime, null);
public QueryDataPlan(List<String> paths, List<Map<String, String>> tagsList, long startTime, long endTime) {
super(true, TagTools.concatTags(paths, tagsList), startTime, endTime, null);
this.setIginxPlanType(QUERY_DATA);
paths = this.getPaths();
boolean isStartPrefix = paths.get(0).contains("*");
String startTimeSeries = trimPath(paths.get(0));
boolean isEndPrefix = paths.get(getPathsNum() - 1).contains("*");
Expand All @@ -60,8 +63,8 @@ public QueryDataPlan(List<String> paths, long startTime, long endTime) {
this.setTsInterval(new TimeSeriesInterval(startTimeSeries, endTimeSeries));
}

public QueryDataPlan(List<String> paths, long startTime, long endTime, StorageUnitMeta storageUnit) {
this(paths, startTime, endTime);
public QueryDataPlan(List<String> paths, List<Map<String, String>> tagsList, long startTime, long endTime, StorageUnitMeta storageUnit) {
this(paths, tagsList, startTime, endTime);
this.setStorageUnit(storageUnit);
this.setSync(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public void insertColumnRecords(List<String> paths, long[] timestamps, Object[]
req.setValuesList(valueBufferList);
req.setBitmapList(bitmapBufferList);
req.setDataTypeList(dataTypeList);
req.setAttributesList(attributesList);
req.setTagsList(attributesList);

Status status;
do {
Expand Down Expand Up @@ -333,7 +333,7 @@ public void insertRowRecords(List<String> paths, long[] timestamps, Object[] val
req.setValuesList(valueBufferList);
req.setBitmapList(bitmapBufferList);
req.setDataTypeList(dataTypeList);
req.setAttributesList(attributesList);
req.setTagsList(attributesList);

Status status;
do {
Expand Down
Loading