提交 20d55139 编写于 作者: L lta

add property plan codec

上级 6e2dcead
......@@ -56,6 +56,51 @@ public class PropertyOperator extends RootOperator {
}
public enum PropertyType {
ADD_TREE, ADD_PROPERTY_LABEL, DELETE_PROPERTY_LABEL, ADD_PROPERTY_TO_METADATA, DEL_PROPERTY_FROM_METADATA
ADD_TREE, ADD_PROPERTY_LABEL, DELETE_PROPERTY_LABEL, ADD_PROPERTY_TO_METADATA, DEL_PROPERTY_FROM_METADATA;
/**
* deserialize short number.
*
* @param i short number
* @return NamespaceType
*/
public static PropertyType deserialize(short i) {
switch (i) {
case 0:
return ADD_TREE;
case 1:
return ADD_PROPERTY_LABEL;
case 2:
return DELETE_PROPERTY_LABEL;
case 3:
return ADD_PROPERTY_TO_METADATA;
case 4:
return DEL_PROPERTY_FROM_METADATA;
default:
return null;
}
}
/**
* serialize.
*
* @return short number
*/
public short serialize() {
switch (this) {
case ADD_TREE:
return 0;
case ADD_PROPERTY_LABEL:
return 1;
case DELETE_PROPERTY_LABEL:
return 2;
case ADD_PROPERTY_TO_METADATA:
return 3;
case DEL_PROPERTY_FROM_METADATA:
return 4;
default:
return 0;
}
}
}
}
......@@ -20,6 +20,7 @@ package org.apache.iotdb.db.qp.physical.sys;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.apache.iotdb.db.qp.logical.Operator;
import org.apache.iotdb.db.qp.logical.sys.PropertyOperator;
import org.apache.iotdb.db.qp.physical.PhysicalPlan;
......@@ -72,4 +73,23 @@ public class PropertyPlan extends PhysicalPlan {
}
return ret;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
PropertyPlan that = (PropertyPlan) o;
return propertyType == that.propertyType &&
Objects.equals(propertyPath, that.propertyPath) &&
Objects.equals(metadataPath, that.metadataPath);
}
@Override
public int hashCode() {
return Objects.hash(propertyType, propertyPath, metadataPath);
}
}
......@@ -33,12 +33,14 @@ import org.apache.iotdb.db.conf.IoTDBConfig;
import org.apache.iotdb.db.conf.IoTDBDescriptor;
import org.apache.iotdb.db.qp.logical.sys.AuthorOperator;
import org.apache.iotdb.db.qp.logical.sys.MetadataOperator;
import org.apache.iotdb.db.qp.logical.sys.PropertyOperator;
import org.apache.iotdb.db.qp.physical.crud.DeletePlan;
import org.apache.iotdb.db.qp.physical.crud.InsertPlan;
import org.apache.iotdb.db.qp.physical.crud.UpdatePlan;
import org.apache.iotdb.db.qp.physical.sys.AuthorPlan;
import org.apache.iotdb.db.qp.physical.sys.LoadDataPlan;
import org.apache.iotdb.db.qp.physical.sys.MetadataPlan;
import org.apache.iotdb.db.qp.physical.sys.PropertyPlan;
import org.apache.iotdb.db.utils.ByteBufferUtils;
import org.apache.iotdb.tsfile.file.metadata.enums.CompressionType;
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
......@@ -276,7 +278,7 @@ public class CodecInstances {
buffer.put((byte) type);
int authorType = plan.getAuthorType().serialize();
buffer.put((byte)authorType);
buffer.put((byte) authorType);
ByteBufferUtils.putString(buffer, plan.getUserName());
ByteBufferUtils.putString(buffer, plan.getRoleName());
......@@ -312,7 +314,7 @@ public class CodecInstances {
int permissionListLen = buffer.getInt();
if (permissionListLen != -1) {
permissions = new HashSet<>(permissionListLen);
for(int i =0 ; i < permissionListLen; i ++) {
for (int i = 0; i < permissionListLen; i++) {
permissions.add(buffer.getInt());
}
}
......@@ -359,4 +361,42 @@ public class CodecInstances {
}
};
static final Codec<PropertyPlan> propertyPlanCodec = new Codec<PropertyPlan>() {
ThreadLocal<ByteBuffer> localBuffer = new ThreadLocal<>();
@Override
public byte[] encode(PropertyPlan plan) {
int type = SystemLogOperator.PROPERTY;
if (localBuffer.get() == null) {
localBuffer.set(ByteBuffer.allocate(config.getMaxLogEntrySize()));
}
ByteBuffer buffer = localBuffer.get();
buffer.clear();
buffer.put((byte) type);
int propertyType = plan.getPropertyType().serialize();
buffer.put((byte) propertyType);
Path metadataPath = plan.getMetadataPath();
Path propertyPath = plan.getPropertyPath();
ByteBufferUtils.putString(buffer, metadataPath == null ? null : metadataPath.toString());
ByteBufferUtils.putString(buffer, propertyPath == null ? null : propertyPath.toString());
return Arrays.copyOfRange(buffer.array(), 0, buffer.position());
}
@Override
public PropertyPlan decode(byte[] bytes) throws IOException {
ByteBuffer buffer = ByteBuffer.wrap(bytes);
buffer.get(); // read and skip an int representing "type"
PropertyOperator.PropertyType propertyType = PropertyOperator.PropertyType
.deserialize(buffer.get());
String metadataPath = ByteBufferUtils.readString(buffer);
String propertyPath = ByteBufferUtils.readString(buffer);
return new PropertyPlan(propertyType, propertyPath == null ? null : new Path(propertyPath),
metadataPath == null ? null : new Path(metadataPath));
}
};
}
......@@ -40,7 +40,8 @@ public enum PhysicalPlanCodec {
DELETEPLAN(SystemLogOperator.DELETE, CodecInstances.deletePlanCodec),
METADATAPLAN(SystemLogOperator.METADATA, CodecInstances.metadataPlanCodec),
AUTHORPLAN(SystemLogOperator.AUTHOR, CodecInstances.authorPlanCodec),
LOADDATAPLAN(SystemLogOperator.LOADDATA, CodecInstances.loadDataPlanCodec);
LOADDATAPLAN(SystemLogOperator.LOADDATA, CodecInstances.loadDataPlanCodec),
PROPERTYPLAN(SystemLogOperator.PROPERTY, CodecInstances.propertyPlanCodec);
private static final HashMap<Integer, PhysicalPlanCodec> codecMap = new HashMap<>();
......
......@@ -29,6 +29,7 @@ import org.apache.iotdb.db.qp.physical.crud.UpdatePlan;
import org.apache.iotdb.db.qp.physical.sys.AuthorPlan;
import org.apache.iotdb.db.qp.physical.sys.LoadDataPlan;
import org.apache.iotdb.db.qp.physical.sys.MetadataPlan;
import org.apache.iotdb.db.qp.physical.sys.PropertyPlan;
public class PhysicalPlanLogTransfer {
......@@ -49,6 +50,8 @@ public class PhysicalPlanLogTransfer {
codec = (Codec<PhysicalPlan>) PhysicalPlanCodec.fromOpcode(SystemLogOperator.AUTHOR).codec;
} else if (plan instanceof LoadDataPlan) {
codec = (Codec<PhysicalPlan>) PhysicalPlanCodec.fromOpcode(SystemLogOperator.LOADDATA).codec;
} else if (plan instanceof PropertyPlan) {
codec = (Codec<PhysicalPlan>) PhysicalPlanCodec.fromOpcode(SystemLogOperator.PROPERTY).codec;
} else{
throw new UnsupportedOperationException(
"SystemLogOperator given is not supported. " + plan.getOperatorType());
......
......@@ -31,4 +31,5 @@ public class SystemLogOperator {
public static final int METADATA = 3;
public static final int AUTHOR = 4;
public static final int LOADDATA = 5;
public static final int PROPERTY = 6;
}
......@@ -33,6 +33,7 @@ import org.apache.iotdb.db.qp.physical.crud.UpdatePlan;
import org.apache.iotdb.db.qp.physical.sys.AuthorPlan;
import org.apache.iotdb.db.qp.physical.sys.LoadDataPlan;
import org.apache.iotdb.db.qp.physical.sys.MetadataPlan;
import org.apache.iotdb.db.qp.physical.sys.PropertyPlan;
import org.apache.iotdb.db.qp.utils.MemIntQpExecutor;
import org.apache.iotdb.tsfile.read.common.Path;
import org.junit.Test;
......@@ -90,51 +91,20 @@ public class PhysicalPlanLogTransferTest {
byte[] loadDataPlanProperty = loadDataPlanCodec.encode(loadDataPlan);
assertEquals(true, Arrays.equals(loadDataPlanProperty, loadDataPlanBytesTest));
/** Property Plan test **/
sql = "add label label1021 to property propropro";
PropertyPlan propertyPlan = (PropertyPlan) processor.parseSQLToPhysicalPlan(sql);
byte[] propertyPlanBytesTest = PhysicalPlanLogTransfer.operatorToLog(propertyPlan);
Codec<PropertyPlan> propertyPlanCodec = CodecInstances.propertyPlanCodec;
byte[] propertyPlanProperty = propertyPlanCodec.encode(propertyPlan);
assertEquals(true, Arrays.equals(propertyPlanProperty, propertyPlanBytesTest));
}
@Test
public void logToOperator()
throws IOException, ArgsErrorException, ProcessorException, QueryProcessorException, AuthException {
/** Insert Plan test **/
byte[] insertPlanBytesTest = PhysicalPlanLogTransfer.operatorToLog(insertPlan);
InsertPlan insertPlanTest = (InsertPlan) PhysicalPlanLogTransfer
.logToOperator(insertPlanBytesTest);
assertEquals(true, insertPlanTest.equals(insertPlan));
/** Delete Plan test **/
byte[] deletePlanBytesTest = PhysicalPlanLogTransfer.operatorToLog(deletePlan);
DeletePlan deletePlanTest = (DeletePlan) PhysicalPlanLogTransfer
.logToOperator(deletePlanBytesTest);
assertEquals(true, deletePlanTest.equals(deletePlan));
/** Update Plan test **/
byte[] updatePlanBytesTest = PhysicalPlanLogTransfer.operatorToLog(updatePlan);
UpdatePlan updatePlanTest = (UpdatePlan) PhysicalPlanLogTransfer
.logToOperator(updatePlanBytesTest);
assertEquals(true, updatePlanTest.equals(updatePlan));
/** Metadata Plan test **/
String metadataStatement = "create timeseries root.vehicle.d1.s1 with datatype=INT32,encoding=RLE";
MetadataPlan metadataPlan = (MetadataPlan) processor.parseSQLToPhysicalPlan(metadataStatement);
byte[] metadataPlanBytesTest = PhysicalPlanLogTransfer.operatorToLog(metadataPlan);
MetadataPlan metadataPlanTest = (MetadataPlan) PhysicalPlanLogTransfer
.logToOperator(metadataPlanBytesTest);
assertEquals(true, metadataPlanTest.equals(metadataPlan));
/** Author Plan test **/
String sql = "grant role xm privileges 'SET_STORAGE_GROUP','DELETE_TIMESERIES' on root.vehicle.device.sensor";
AuthorPlan authorPlan = (AuthorPlan) processor.parseSQLToPhysicalPlan(sql);
byte[] authorPlanBytesTest = PhysicalPlanLogTransfer.operatorToLog(authorPlan);
AuthorPlan authorPlanTest = (AuthorPlan) PhysicalPlanLogTransfer
.logToOperator(authorPlanBytesTest);
assertEquals(true, authorPlanTest.equals(authorPlan));
/** LoadData Plan test **/
byte[] loadDataPlanBytesTest = PhysicalPlanLogTransfer.operatorToLog(loadDataPlan);
LoadDataPlan loadDataPlanTest = (LoadDataPlan) PhysicalPlanLogTransfer
.logToOperator(loadDataPlanBytesTest);
assertEquals(true, loadDataPlan.equals(loadDataPlanTest));
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册