diff --git a/confignode/src/main/java/org/apache/iotdb/confignode/consensus/request/write/database/DatabaseSchemaPlan.java b/confignode/src/main/java/org/apache/iotdb/confignode/consensus/request/write/database/DatabaseSchemaPlan.java index 2430b77369e32..96910a8e92502 100644 --- a/confignode/src/main/java/org/apache/iotdb/confignode/consensus/request/write/database/DatabaseSchemaPlan.java +++ b/confignode/src/main/java/org/apache/iotdb/confignode/consensus/request/write/database/DatabaseSchemaPlan.java @@ -50,12 +50,12 @@ public TDatabaseSchema getSchema() { @Override protected void serializeImpl(DataOutputStream stream) throws IOException { stream.writeShort(getType().getPlanType()); - ThriftConfigNodeSerDeUtils.serializeTStorageGroupSchema(schema, stream); + ThriftConfigNodeSerDeUtils.serializeTDatabaseSchema(schema, stream); } @Override protected void deserializeImpl(ByteBuffer buffer) throws IOException { - schema = ThriftConfigNodeSerDeUtils.deserializeTStorageGroupSchema(buffer); + schema = ThriftConfigNodeSerDeUtils.deserializeTDatabaseSchema(buffer); } @Override diff --git a/confignode/src/main/java/org/apache/iotdb/confignode/persistence/executor/ConfigPlanExecutor.java b/confignode/src/main/java/org/apache/iotdb/confignode/persistence/executor/ConfigPlanExecutor.java index d88cdd2dfb541..86910b222a2ac 100644 --- a/confignode/src/main/java/org/apache/iotdb/confignode/persistence/executor/ConfigPlanExecutor.java +++ b/confignode/src/main/java/org/apache/iotdb/confignode/persistence/executor/ConfigPlanExecutor.java @@ -22,6 +22,7 @@ import org.apache.iotdb.common.rpc.thrift.TSchemaNode; import org.apache.iotdb.commons.auth.AuthException; import org.apache.iotdb.commons.path.PartialPath; +import org.apache.iotdb.commons.schema.node.MNodeType; import org.apache.iotdb.commons.snapshot.SnapshotProcessor; import org.apache.iotdb.confignode.consensus.request.ConfigPhysicalPlan; import org.apache.iotdb.confignode.consensus.request.auth.AuthorPlan; @@ -115,7 +116,6 @@ import org.apache.iotdb.confignode.rpc.thrift.TDatabaseSchema; import org.apache.iotdb.confignode.rpc.thrift.TShowRegionReq; import org.apache.iotdb.consensus.common.DataSet; -import org.apache.iotdb.db.metadata.mnode.MNodeType; import org.apache.iotdb.rpc.TSStatusCode; import org.apache.iotdb.tsfile.utils.Pair; diff --git a/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/ClusterSchemaInfo.java b/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/ClusterSchemaInfo.java index 8d9200256edb6..6f05cff326202 100644 --- a/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/ClusterSchemaInfo.java +++ b/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/ClusterSchemaInfo.java @@ -133,7 +133,10 @@ public TSStatus createDatabase(DatabaseSchemaPlan plan) { mTree.setStorageGroup(partialPathName); // Set DatabaseSchema - mTree.getDatabaseNodeByDatabasePath(partialPathName).setStorageGroupSchema(databaseSchema); + mTree + .getDatabaseNodeByDatabasePath(partialPathName) + .getAsMNode() + .setDatabaseSchema(databaseSchema); result.setCode(TSStatusCode.SUCCESS_STATUS.getStatusCode()); } catch (MetadataException e) { @@ -159,7 +162,7 @@ public TSStatus alterDatabase(DatabaseSchemaPlan plan) { PartialPath partialPathName = new PartialPath(alterSchema.getName()); TDatabaseSchema currentSchema = - mTree.getDatabaseNodeByDatabasePath(partialPathName).getStorageGroupSchema(); + mTree.getDatabaseNodeByDatabasePath(partialPathName).getAsMNode().getDatabaseSchema(); // TODO: Support alter other fields if (alterSchema.isSetMinSchemaRegionGroupNum()) { currentSchema.setMinSchemaRegionGroupNum(alterSchema.getMinSchemaRegionGroupNum()); @@ -192,7 +195,10 @@ public TSStatus alterDatabase(DatabaseSchemaPlan plan) { currentSchema.getMaxDataRegionGroupNum()); } - mTree.getDatabaseNodeByDatabasePath(partialPathName).setStorageGroupSchema(currentSchema); + mTree + .getDatabaseNodeByDatabasePath(partialPathName) + .getAsMNode() + .setDatabaseSchema(currentSchema); result.setCode(TSStatusCode.SUCCESS_STATUS.getStatusCode()); } catch (MetadataException e) { LOGGER.error(ERROR_NAME, e); @@ -259,7 +265,8 @@ public DatabaseSchemaResp getMatchedDatabaseSchemas(GetDatabasePlan plan) { List matchedPaths = mTree.getMatchedDatabases(patternPath, false); for (PartialPath path : matchedPaths) { schemaMap.put( - path.getFullPath(), mTree.getDatabaseNodeByDatabasePath(path).getStorageGroupSchema()); + path.getFullPath(), + mTree.getDatabaseNodeByDatabasePath(path).getAsMNode().getDatabaseSchema()); } result.setSchemaMap(schemaMap); result.setStatus(new TSStatus(TSStatusCode.SUCCESS_STATUS.getStatusCode())); @@ -282,7 +289,7 @@ public TSStatus setTTL(SetTTLPlan plan) { List matchedPaths = mTree.getBelongedDatabases(patternPath); if (!matchedPaths.isEmpty()) { for (PartialPath path : matchedPaths) { - mTree.getDatabaseNodeByDatabasePath(path).getStorageGroupSchema().setTTL(plan.getTTL()); + mTree.getDatabaseNodeByDatabasePath(path).setDataTTL(plan.getTTL()); } result.setCode(TSStatusCode.SUCCESS_STATUS.getStatusCode()); } else { @@ -306,7 +313,8 @@ public TSStatus setSchemaReplicationFactor(SetSchemaReplicationFactorPlan plan) if (mTree.isDatabaseAlreadySet(path)) { mTree .getDatabaseNodeByDatabasePath(path) - .getStorageGroupSchema() + .getAsMNode() + .getDatabaseSchema() .setSchemaReplicationFactor(plan.getSchemaReplicationFactor()); result.setCode(TSStatusCode.SUCCESS_STATUS.getStatusCode()); } else { @@ -329,7 +337,8 @@ public TSStatus setDataReplicationFactor(SetDataReplicationFactorPlan plan) { if (mTree.isDatabaseAlreadySet(path)) { mTree .getDatabaseNodeByDatabasePath(path) - .getStorageGroupSchema() + .getAsMNode() + .getDatabaseSchema() .setDataReplicationFactor(plan.getDataReplicationFactor()); result.setCode(TSStatusCode.SUCCESS_STATUS.getStatusCode()); } else { @@ -352,7 +361,8 @@ public TSStatus setTimePartitionInterval(SetTimePartitionIntervalPlan plan) { if (mTree.isDatabaseAlreadySet(path)) { mTree .getDatabaseNodeByDatabasePath(path) - .getStorageGroupSchema() + .getAsMNode() + .getDatabaseSchema() .setTimePartitionInterval(plan.getTimePartitionInterval()); result.setCode(TSStatusCode.SUCCESS_STATUS.getStatusCode()); } else { @@ -381,7 +391,7 @@ public TSStatus adjustMaxRegionGroupCount(AdjustMaxRegionGroupNumPlan plan) { plan.getMaxRegionGroupNumMap().entrySet()) { PartialPath path = new PartialPath(entry.getKey()); TDatabaseSchema databaseSchema = - mTree.getDatabaseNodeByDatabasePath(path).getStorageGroupSchema(); + mTree.getDatabaseNodeByDatabasePath(path).getAsMNode().getDatabaseSchema(); databaseSchema.setMaxSchemaRegionGroupNum(entry.getValue().getLeft()); databaseSchema.setMaxDataRegionGroupNum(entry.getValue().getRight()); } @@ -445,7 +455,10 @@ public TDatabaseSchema getMatchedDatabaseSchemaByName(String database) throws DatabaseNotExistsException { databaseReadWriteLock.readLock().lock(); try { - return mTree.getDatabaseNodeByDatabasePath(new PartialPath(database)).getStorageGroupSchema(); + return mTree + .getDatabaseNodeByDatabasePath(new PartialPath(database)) + .getAsMNode() + .getDatabaseSchema(); } catch (MetadataException e) { throw new DatabaseNotExistsException(database); } finally { @@ -468,7 +481,8 @@ public Map getMatchedDatabaseSchemasByName(List List matchedPaths = mTree.getMatchedDatabases(patternPath, false); for (PartialPath path : matchedPaths) { schemaMap.put( - path.getFullPath(), mTree.getDatabaseNodeByPath(path).getStorageGroupSchema()); + path.getFullPath(), + mTree.getDatabaseNodeByPath(path).getAsMNode().getDatabaseSchema()); } } } catch (MetadataException e) { @@ -491,7 +505,7 @@ public int getMinRegionGroupNum(String database, TConsensusGroupType consensusGr try { PartialPath path = new PartialPath(database); TDatabaseSchema storageGroupSchema = - mTree.getDatabaseNodeByDatabasePath(path).getStorageGroupSchema(); + mTree.getDatabaseNodeByDatabasePath(path).getAsMNode().getDatabaseSchema(); switch (consensusGroupType) { case SchemaRegion: return storageGroupSchema.getMinSchemaRegionGroupNum(); @@ -519,7 +533,7 @@ public int getMaxRegionGroupNum(String database, TConsensusGroupType consensusGr try { PartialPath path = new PartialPath(database); TDatabaseSchema storageGroupSchema = - mTree.getDatabaseNodeByDatabasePath(path).getStorageGroupSchema(); + mTree.getDatabaseNodeByDatabasePath(path).getAsMNode().getDatabaseSchema(); switch (consensusGroupType) { case SchemaRegion: return storageGroupSchema.getMaxSchemaRegionGroupNum(); @@ -853,7 +867,7 @@ public Map getMatchedDatabaseSchemasByOneName( List matchedPaths = mTree.getBelongedDatabases(patternPath); for (PartialPath path : matchedPaths) { schemaMap.put( - path.getFullPath(), mTree.getDatabaseNodeByPath(path).getStorageGroupSchema()); + path.getFullPath(), mTree.getDatabaseNodeByPath(path).getAsMNode().getDatabaseSchema()); } } catch (MetadataException e) { LOGGER.warn(ERROR_NAME, e); diff --git a/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/DeleteDatabaseProcedure.java b/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/DeleteDatabaseProcedure.java index b2beaf2f35c1f..f1271c25b1e18 100644 --- a/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/DeleteDatabaseProcedure.java +++ b/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/DeleteDatabaseProcedure.java @@ -274,14 +274,14 @@ protected DeleteStorageGroupState getInitialState() { public void serialize(DataOutputStream stream) throws IOException { stream.writeShort(ProcedureType.DELETE_STORAGE_GROUP_PROCEDURE.getTypeCode()); super.serialize(stream); - ThriftConfigNodeSerDeUtils.serializeTStorageGroupSchema(deleteDatabaseSchema, stream); + ThriftConfigNodeSerDeUtils.serializeTDatabaseSchema(deleteDatabaseSchema, stream); } @Override public void deserialize(ByteBuffer byteBuffer) { super.deserialize(byteBuffer); try { - deleteDatabaseSchema = ThriftConfigNodeSerDeUtils.deserializeTStorageGroupSchema(byteBuffer); + deleteDatabaseSchema = ThriftConfigNodeSerDeUtils.deserializeTDatabaseSchema(byteBuffer); } catch (ThriftSerDeException e) { LOG.error("Error in deserialize DeleteStorageGroupProcedure", e); } diff --git a/integration-test/src/test/java/org/apache/iotdb/db/it/schema/AbstractSchemaIT.java b/integration-test/src/test/java/org/apache/iotdb/db/it/schema/AbstractSchemaIT.java index 380be264870f9..0c74d462253db 100644 --- a/integration-test/src/test/java/org/apache/iotdb/db/it/schema/AbstractSchemaIT.java +++ b/integration-test/src/test/java/org/apache/iotdb/db/it/schema/AbstractSchemaIT.java @@ -60,7 +60,7 @@ public void setUp() throws Exception { break; case SchemaFile: EnvFactory.getEnv().getConfig().getCommonConfig().setSchemaEngineMode("Schema_File"); - allocateMemoryForSchemaRegion(3600); + allocateMemoryForSchemaRegion(4000); break; } } diff --git a/integration-test/src/test/java/org/apache/iotdb/db/it/schema/IoTDBSortedShowTimeseriesIT.java b/integration-test/src/test/java/org/apache/iotdb/db/it/schema/IoTDBSortedShowTimeseriesIT.java index 3c70fc0511588..c3fcbfe250415 100644 --- a/integration-test/src/test/java/org/apache/iotdb/db/it/schema/IoTDBSortedShowTimeseriesIT.java +++ b/integration-test/src/test/java/org/apache/iotdb/db/it/schema/IoTDBSortedShowTimeseriesIT.java @@ -100,7 +100,7 @@ public IoTDBSortedShowTimeseriesIT(SchemaTestMode schemaTestMode) { public void setUp() throws Exception { super.setUp(); if (schemaTestMode.equals(SchemaTestMode.SchemaFile)) { - allocateMemoryForSchemaRegion(5500); + allocateMemoryForSchemaRegion(10000); } EnvFactory.getEnv().initClusterEnvironment(); createSchema(); diff --git a/integration-test/src/test/java/org/apache/iotdb/db/it/schema/IoTDBTagIT.java b/integration-test/src/test/java/org/apache/iotdb/db/it/schema/IoTDBTagIT.java index 98e0e408631ea..cc94dec78595e 100644 --- a/integration-test/src/test/java/org/apache/iotdb/db/it/schema/IoTDBTagIT.java +++ b/integration-test/src/test/java/org/apache/iotdb/db/it/schema/IoTDBTagIT.java @@ -53,7 +53,7 @@ public IoTDBTagIT(SchemaTestMode schemaTestMode) { public void setUp() throws Exception { super.setUp(); if (schemaTestMode.equals(SchemaTestMode.SchemaFile)) { - allocateMemoryForSchemaRegion(5500); + allocateMemoryForSchemaRegion(10000); } EnvFactory.getEnv().initClusterEnvironment(); } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/IMNode.java b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/IMNode.java similarity index 51% rename from server/src/main/java/org/apache/iotdb/db/metadata/mnode/IMNode.java rename to node-commons/src/main/java/org/apache/iotdb/commons/schema/node/IMNode.java index b7b2e452e823b..f3f8f48b80489 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/IMNode.java +++ b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/IMNode.java @@ -16,24 +16,26 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.iotdb.db.metadata.mnode; +package org.apache.iotdb.commons.schema.node; import org.apache.iotdb.commons.path.PartialPath; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeContainer; +import org.apache.iotdb.commons.schema.node.visitor.MNodeVisitor; import org.apache.iotdb.commons.schema.tree.ITreeNode; -import org.apache.iotdb.db.metadata.mnode.container.IMNodeContainer; -import org.apache.iotdb.db.metadata.mnode.visitor.MNodeVisitor; -import org.apache.iotdb.db.metadata.mtree.store.disk.cache.CacheEntry; /** This interface defines a MNode's operation interfaces. */ -public interface IMNode extends ITreeNode { +public interface IMNode> extends ITreeNode { String getName(); void setName(String name); - IMNode getParent(); + N getParent(); - void setParent(IMNode parent); + void setParent(N parent); String getFullPath(); @@ -43,64 +45,44 @@ public interface IMNode extends ITreeNode { boolean hasChild(String name); - IMNode getChild(String name); + N getChild(String name); - IMNode addChild(String name, IMNode child); + N addChild(String name, N child); - IMNode addChild(IMNode child); + N addChild(N child); - IMNode deleteChild(String name); + N deleteChild(String name); // this method will replace the oldChild with the newChild, the data of oldChild will be moved to // newChild - void replaceChild(String oldChildName, IMNode newChildNode); + void replaceChild(String oldChildName, N newChildNode); // this method will move all the reference or value of current node's attributes to newMNode - void moveDataToNewMNode(IMNode newMNode); + void moveDataToNewMNode(N newMNode); - IMNodeContainer getChildren(); + IMNodeContainer getChildren(); - void setChildren(IMNodeContainer children); - - boolean isUseTemplate(); - - void setUseTemplate(boolean useTemplate); - - /** @return the logic id of template set or activated on this node, id>=-1 */ - int getSchemaTemplateId(); - - /** @return the template id with current state, may be negative since unset or deactivation */ - int getSchemaTemplateIdWithState(); - - void setSchemaTemplateId(int schemaTemplateId); - - void preUnsetSchemaTemplate(); - - void rollbackUnsetSchemaTemplate(); - - boolean isSchemaTemplatePreUnset(); - - void unsetSchemaTemplate(); + void setChildren(IMNodeContainer children); boolean isAboveDatabase(); - boolean isStorageGroup(); + boolean isDatabase(); - boolean isEntity(); + boolean isDevice(); boolean isMeasurement(); MNodeType getMNodeType(Boolean isConfig); - IStorageGroupMNode getAsStorageGroupMNode(); + IDatabaseMNode getAsDatabaseMNode(); - IEntityMNode getAsEntityMNode(); + IDeviceMNode getAsDeviceMNode(); - IMeasurementMNode getAsMeasurementMNode(); + IMeasurementMNode getAsMeasurementMNode(); - CacheEntry getCacheEntry(); + R accept(MNodeVisitor visitor, C context); - void setCacheEntry(CacheEntry cacheEntry); + int estimateSize(); - R accept(MNodeVisitor visitor, C context); + N getAsMNode(); } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/MNodeType.java b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/MNodeType.java similarity index 97% rename from server/src/main/java/org/apache/iotdb/db/metadata/mnode/MNodeType.java rename to node-commons/src/main/java/org/apache/iotdb/commons/schema/node/MNodeType.java index 6b498eb2b73a0..133bc37a7c305 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/MNodeType.java +++ b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/MNodeType.java @@ -17,7 +17,7 @@ * under the License. */ -package org.apache.iotdb.db.metadata.mnode; +package org.apache.iotdb.commons.schema.node; import org.apache.iotdb.tsfile.utils.ReadWriteIOUtils; diff --git a/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/common/AbstractAboveDatabaseMNode.java b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/common/AbstractAboveDatabaseMNode.java new file mode 100644 index 0000000000000..454a0e2038861 --- /dev/null +++ b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/common/AbstractAboveDatabaseMNode.java @@ -0,0 +1,191 @@ +/* + * 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.commons.schema.node.common; + +import org.apache.iotdb.commons.path.PartialPath; +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.MNodeType; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeContainer; +import org.apache.iotdb.commons.schema.node.visitor.MNodeVisitor; + +public abstract class AbstractAboveDatabaseMNode, BasicNode extends IMNode> + implements IMNode { + + protected final BasicNode basicMNode; + + public AbstractAboveDatabaseMNode(BasicNode basicMNode) { + this.basicMNode = basicMNode; + } + + @Override + public String getName() { + return basicMNode.getName(); + } + + @Override + public void setName(String name) { + basicMNode.setName(name); + } + + @Override + public N getParent() { + if (basicMNode.getParent() == null) { + return null; + } + return basicMNode.getParent(); + } + + @Override + public void setParent(N parent) { + basicMNode.setParent(parent); + } + + @Override + public R accept(MNodeVisitor visitor, C context) { + throw new UnsupportedOperationException("Wrong MNode Type"); + } + + @Override + public String getFullPath() { + return basicMNode.getFullPath(); + } + + @Override + public void setFullPath(String fullPath) { + basicMNode.setFullPath(fullPath); + } + + @Override + public PartialPath getPartialPath() { + return basicMNode.getPartialPath(); + } + + @Override + public boolean hasChild(String name) { + return basicMNode.hasChild(name); + } + + @Override + public N getChild(String name) { + return basicMNode.getChild(name); + } + + @Override + public N addChild(String name, N child) { + N res = basicMNode.addChild(name, child); + if (res == child) { + child.setParent(this.getAsMNode()); + } + return res; + } + + @Override + public N addChild(N child) { + N res = basicMNode.addChild(child); + if (res == child) { + child.setParent(this.getAsMNode()); + } + return res; + } + + @Override + public N deleteChild(String name) { + return basicMNode.deleteChild(name); + } + + @Override + public void replaceChild(String oldChildName, N newChildNode) { + basicMNode.replaceChild(oldChildName, newChildNode); + } + + @Override + public void moveDataToNewMNode(N newMNode) { + basicMNode.moveDataToNewMNode(newMNode); + } + + @Override + public IMNodeContainer getChildren() { + return basicMNode.getChildren(); + } + + @Override + public void setChildren(IMNodeContainer children) { + basicMNode.setChildren(children); + } + + @Override + public boolean isAboveDatabase() { + return true; + } + + @Override + public boolean isDatabase() { + return false; + } + + @Override + public boolean isDevice() { + return false; + } + + @Override + public boolean isMeasurement() { + return false; + } + + @Override + public MNodeType getMNodeType(Boolean isConfig) { + return MNodeType.INTERNAL; + } + + @Override + public IDatabaseMNode getAsDatabaseMNode() { + throw new UnsupportedOperationException("Wrong MNode Type"); + } + + @Override + public IDeviceMNode getAsDeviceMNode() { + throw new UnsupportedOperationException("Wrong MNode Type"); + } + + @Override + public IMeasurementMNode getAsMeasurementMNode() { + throw new UnsupportedOperationException("Wrong MNode Type"); + } + + /** + * The basic memory occupied by any AbstractDatabaseDeviceMNode object + * + *
    + *
  1. object header, 8B + *
  2. node attributes + *
      + *
    1. basicMNode reference, 8B + *
    + *
  3. MapEntry in parent + *
+ */ + @Override + public int estimateSize() { + return 8 + basicMNode.estimateSize(); + } +} diff --git a/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/common/AbstractDatabaseDeviceMNode.java b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/common/AbstractDatabaseDeviceMNode.java new file mode 100644 index 0000000000000..2577a426a466b --- /dev/null +++ b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/common/AbstractDatabaseDeviceMNode.java @@ -0,0 +1,290 @@ +/* + * 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.commons.schema.node.common; + +import org.apache.iotdb.commons.path.PartialPath; +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.MNodeType; +import org.apache.iotdb.commons.schema.node.info.IDatabaseDeviceInfo; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeContainer; +import org.apache.iotdb.commons.schema.node.visitor.MNodeVisitor; + +import java.util.Map; + +public abstract class AbstractDatabaseDeviceMNode, BasicNode extends IMNode> + implements IDatabaseMNode, IDeviceMNode { + + private final IDatabaseDeviceInfo databaseDeviceInfo; + protected final BasicNode basicMNode; + + public AbstractDatabaseDeviceMNode( + BasicNode basicMNode, IDatabaseDeviceInfo databaseDeviceInfo) { + this.basicMNode = basicMNode; + this.databaseDeviceInfo = databaseDeviceInfo; + } + + public BasicNode getBasicMNode() { + return basicMNode; + } + + @Override + public String getName() { + return basicMNode.getName(); + } + + @Override + public void setName(String name) { + basicMNode.setName(name); + } + + @Override + public N getParent() { + return basicMNode.getParent(); + } + + @Override + public void setParent(N parent) { + basicMNode.setParent(parent); + } + + @Override + public String getFullPath() { + return basicMNode.getFullPath(); + } + + @Override + public void setFullPath(String fullPath) { + basicMNode.setFullPath(fullPath); + } + + @Override + public PartialPath getPartialPath() { + return basicMNode.getPartialPath(); + } + + @Override + public boolean hasChild(String name) { + return basicMNode.hasChild(name); + } + + @Override + public N getChild(String name) { + return basicMNode.getChild(name); + } + + @Override + public N addChild(String name, N child) { + N res = basicMNode.addChild(name, child); + if (res == child) { + child.setParent(this.getAsMNode()); + } + return res; + } + + @Override + public N addChild(N child) { + N res = basicMNode.addChild(child); + if (res == child) { + child.setParent(this.getAsMNode()); + } + return res; + } + + @Override + public N deleteChild(String name) { + return basicMNode.deleteChild(name); + } + + @Override + public void replaceChild(String oldChildName, N newChildNode) { + basicMNode.replaceChild(oldChildName, newChildNode); + } + + @Override + public void moveDataToNewMNode(N newMNode) { + basicMNode.moveDataToNewMNode(newMNode); + if (newMNode.isDevice()) { + databaseDeviceInfo.moveDataToNewMNode(newMNode.getAsDeviceMNode()); + } + if (newMNode.isDatabase()) { + databaseDeviceInfo.moveDataToNewMNode(newMNode.getAsDatabaseMNode()); + } + } + + @Override + public IMNodeContainer getChildren() { + return basicMNode.getChildren(); + } + + @Override + public void setChildren(IMNodeContainer children) { + basicMNode.setChildren(children); + } + + @Override + public boolean isAboveDatabase() { + return false; + } + + @Override + public boolean isDatabase() { + return true; + } + + @Override + public boolean isDevice() { + return true; + } + + @Override + public boolean isMeasurement() { + return false; + } + + @Override + public MNodeType getMNodeType(Boolean isConfig) { + return MNodeType.STORAGE_GROUP; + } + + @Override + public IDatabaseMNode getAsDatabaseMNode() { + return this; + } + + @Override + public IDeviceMNode getAsDeviceMNode() { + return this; + } + + @Override + public IMeasurementMNode getAsMeasurementMNode() { + throw new UnsupportedOperationException("Wrong MNode Type"); + } + + @Override + public R accept(MNodeVisitor visitor, C context) { + return visitor.visitDatabaseDeviceMNode(this, context); + } + + @Override + public long getDataTTL() { + return databaseDeviceInfo.getDataTTL(); + } + + @Override + public void setDataTTL(long dataTTL) { + databaseDeviceInfo.setDataTTL(dataTTL); + } + + @Override + public boolean addAlias(String alias, IMeasurementMNode child) { + return databaseDeviceInfo.addAlias(alias, child); + } + + @Override + public void deleteAliasChild(String alias) { + databaseDeviceInfo.deleteAliasChild(alias); + } + + @Override + public Map> getAliasChildren() { + return databaseDeviceInfo.getAliasChildren(); + } + + @Override + public void setAliasChildren(Map> aliasChildren) { + databaseDeviceInfo.setAliasChildren(aliasChildren); + } + + @Override + public boolean isUseTemplate() { + return databaseDeviceInfo.isUseTemplate(); + } + + @Override + public void setUseTemplate(boolean useTemplate) { + databaseDeviceInfo.setUseTemplate(useTemplate); + } + + @Override + public void setSchemaTemplateId(int schemaTemplateId) { + databaseDeviceInfo.setSchemaTemplateId(schemaTemplateId); + } + + @Override + public int getSchemaTemplateId() { + return databaseDeviceInfo.getSchemaTemplateId(); + } + + @Override + public int getSchemaTemplateIdWithState() { + return databaseDeviceInfo.getSchemaTemplateIdWithState(); + } + + @Override + public boolean isPreDeactivateTemplate() { + return databaseDeviceInfo.isPreDeactivateTemplate(); + } + + @Override + public void preDeactivateTemplate() { + databaseDeviceInfo.preDeactivateTemplate(); + } + + @Override + public void rollbackPreDeactivateTemplate() { + databaseDeviceInfo.rollbackPreDeactivateTemplate(); + } + + @Override + public void deactivateTemplate() { + databaseDeviceInfo.deactivateTemplate(); + } + + @Override + public boolean isAligned() { + return databaseDeviceInfo.isAligned(); + } + + @Override + public void setAligned(boolean isAligned) { + databaseDeviceInfo.setAligned(isAligned); + } + + /** + * The basic memory occupied by any AbstractDatabaseDeviceMNode object + * + *
    + *
  1. object header, 8B + *
  2. node attributes + *
      + *
    1. databaseDeviceInfo reference, 8B + *
    2. basicMNode reference, 8B + *
    + *
  3. MapEntry in parent + *
+ */ + @Override + public int estimateSize() { + return 8 + 8 + databaseDeviceInfo.estimateSize() + basicMNode.estimateSize(); + } +} diff --git a/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/common/AbstractDatabaseMNode.java b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/common/AbstractDatabaseMNode.java new file mode 100644 index 0000000000000..6781bad26a159 --- /dev/null +++ b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/common/AbstractDatabaseMNode.java @@ -0,0 +1,215 @@ +/* + * 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.commons.schema.node.common; + +import org.apache.iotdb.commons.path.PartialPath; +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.MNodeType; +import org.apache.iotdb.commons.schema.node.info.IDatabaseInfo; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeContainer; +import org.apache.iotdb.commons.schema.node.visitor.MNodeVisitor; + +public abstract class AbstractDatabaseMNode, BasicNode extends IMNode> + implements IDatabaseMNode { + + private static final long serialVersionUID = 7999036474525817732L; + + private final IDatabaseInfo databaseInfo; + protected final BasicNode basicMNode; + + public AbstractDatabaseMNode(BasicNode basicMNode, IDatabaseInfo databaseInfo) { + this.basicMNode = basicMNode; + this.databaseInfo = databaseInfo; + } + + public BasicNode getBasicMNode() { + return basicMNode; + } + + @Override + public String getName() { + return basicMNode.getName(); + } + + @Override + public void setName(String name) { + basicMNode.setName(name); + } + + @Override + public N getParent() { + return basicMNode.getParent(); + } + + @Override + public void setParent(N parent) { + basicMNode.setParent(parent); + } + + @Override + public String getFullPath() { + return basicMNode.getFullPath(); + } + + @Override + public void setFullPath(String fullPath) { + basicMNode.setFullPath(fullPath); + } + + @Override + public PartialPath getPartialPath() { + return basicMNode.getPartialPath(); + } + + @Override + public boolean hasChild(String name) { + return basicMNode.hasChild(name); + } + + @Override + public N getChild(String name) { + return basicMNode.getChild(name); + } + + @Override + public N addChild(String name, N child) { + N res = basicMNode.addChild(name, child); + if (res == child) { + child.setParent(this.getAsMNode()); + } + return res; + } + + @Override + public N addChild(N child) { + N res = basicMNode.addChild(child); + if (res == child) { + child.setParent(this.getAsMNode()); + } + return res; + } + + @Override + public N deleteChild(String name) { + return basicMNode.deleteChild(name); + } + + @Override + public void replaceChild(String oldChildName, N newChildNode) { + basicMNode.replaceChild(oldChildName, newChildNode); + } + + @Override + public void moveDataToNewMNode(N newMNode) { + basicMNode.moveDataToNewMNode(newMNode); + if (newMNode.isDatabase()) { + databaseInfo.moveDataToNewMNode(newMNode.getAsDatabaseMNode()); + } + } + + @Override + public IMNodeContainer getChildren() { + return basicMNode.getChildren(); + } + + @Override + public void setChildren(IMNodeContainer children) { + basicMNode.setChildren(children); + } + + @Override + public boolean isAboveDatabase() { + return false; + } + + @Override + public boolean isDatabase() { + return true; + } + + @Override + public boolean isDevice() { + return false; + } + + @Override + public boolean isMeasurement() { + return false; + } + + @Override + public MNodeType getMNodeType(Boolean isConfig) { + return MNodeType.STORAGE_GROUP; + } + + @Override + public IDatabaseMNode getAsDatabaseMNode() { + return this; + } + + @Override + public IDeviceMNode getAsDeviceMNode() { + throw new UnsupportedOperationException("Wrong MNode Type"); + } + + @Override + public IMeasurementMNode getAsMeasurementMNode() { + throw new UnsupportedOperationException("Wrong MNode Type"); + } + + @Override + public R accept(MNodeVisitor visitor, C context) { + return visitor.visitDatabaseMNode(this, context); + } + + @Override + public long getDataTTL() { + return databaseInfo.getDataTTL(); + } + + @Override + public void setDataTTL(long dataTTL) { + databaseInfo.setDataTTL(dataTTL); + } + + /** + * The basic memory occupied by any AbstractDatabaseMNode object + * + *
    + *
  1. object header, 8B + *
  2. node attributes + *
      + *
    1. databaseInfo reference, 8B + *
    2. basicMNode reference, 8B + *
    + *
  3. MapEntry in parent + *
+ */ + @Override + public int estimateSize() { + return 8 + 8 + databaseInfo.estimateSize() + basicMNode.estimateSize(); + } + + protected IDatabaseInfo getDatabaseInfo() { + return databaseInfo; + } +} diff --git a/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/common/AbstractDeviceMNode.java b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/common/AbstractDeviceMNode.java new file mode 100644 index 0000000000000..093eaaf4e6304 --- /dev/null +++ b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/common/AbstractDeviceMNode.java @@ -0,0 +1,280 @@ +/* + * 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.commons.schema.node.common; + +import org.apache.iotdb.commons.path.PartialPath; +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.MNodeType; +import org.apache.iotdb.commons.schema.node.info.IDeviceInfo; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeContainer; +import org.apache.iotdb.commons.schema.node.visitor.MNodeVisitor; + +import java.util.Map; + +public abstract class AbstractDeviceMNode, BasicNode extends IMNode> + implements IDeviceMNode { + + private final IDeviceInfo deviceInfo; + protected final BasicNode basicMNode; + + public AbstractDeviceMNode(BasicNode basicMNode, IDeviceInfo deviceInfo) { + this.basicMNode = basicMNode; + this.deviceInfo = deviceInfo; + } + + public BasicNode getBasicMNode() { + return basicMNode; + } + + @Override + public String getName() { + return basicMNode.getName(); + } + + @Override + public void setName(String name) { + basicMNode.setName(name); + } + + @Override + public N getParent() { + return basicMNode.getParent(); + } + + @Override + public void setParent(N parent) { + basicMNode.setParent(parent); + } + + @Override + public String getFullPath() { + return basicMNode.getFullPath(); + } + + @Override + public void setFullPath(String fullPath) { + basicMNode.setFullPath(fullPath); + } + + @Override + public PartialPath getPartialPath() { + return basicMNode.getPartialPath(); + } + + @Override + public boolean hasChild(String name) { + return basicMNode.hasChild(name) || deviceInfo.hasAliasChild(name); + } + + @Override + public N getChild(String name) { + N res = basicMNode.getChild(name); + if (res == null) { + res = deviceInfo.getAliasChild(name); + } + return res; + } + + @Override + public N addChild(String name, N child) { + N res = basicMNode.addChild(name, child); + if (res == child) { + child.setParent(this.getAsMNode()); + } + return res; + } + + @Override + public N addChild(N child) { + N res = basicMNode.addChild(child); + if (res == child) { + child.setParent(this.getAsMNode()); + } + return res; + } + + @Override + public N deleteChild(String name) { + return basicMNode.deleteChild(name); + } + + @Override + public void replaceChild(String oldChildName, N newChildNode) { + basicMNode.replaceChild(oldChildName, newChildNode); + } + + @Override + public void moveDataToNewMNode(N newMNode) { + basicMNode.moveDataToNewMNode(newMNode); + if (newMNode.isDevice()) { + deviceInfo.moveDataToNewMNode(newMNode.getAsDeviceMNode()); + } + } + + @Override + public IMNodeContainer getChildren() { + return basicMNode.getChildren(); + } + + @Override + public void setChildren(IMNodeContainer children) { + basicMNode.setChildren(children); + } + + @Override + public boolean isAboveDatabase() { + return false; + } + + @Override + public boolean isDatabase() { + return false; + } + + @Override + public boolean isDevice() { + return true; + } + + @Override + public boolean isMeasurement() { + return false; + } + + @Override + public MNodeType getMNodeType(Boolean isConfig) { + return MNodeType.DEVICE; + } + + @Override + public IDatabaseMNode getAsDatabaseMNode() { + throw new UnsupportedOperationException("Wrong MNode Type"); + } + + @Override + public IDeviceMNode getAsDeviceMNode() { + return this; + } + + @Override + public IMeasurementMNode getAsMeasurementMNode() { + throw new UnsupportedOperationException("Wrong MNode Type"); + } + + @Override + public R accept(MNodeVisitor visitor, C context) { + return visitor.visitDeviceMNode(this, context); + } + + @Override + public boolean addAlias(String alias, IMeasurementMNode child) { + return deviceInfo.addAlias(alias, child); + } + + @Override + public void deleteAliasChild(String alias) { + deviceInfo.deleteAliasChild(alias); + } + + @Override + public Map> getAliasChildren() { + return deviceInfo.getAliasChildren(); + } + + @Override + public void setAliasChildren(Map> aliasChildren) { + deviceInfo.setAliasChildren(aliasChildren); + } + + @Override + public boolean isUseTemplate() { + return deviceInfo.isUseTemplate(); + } + + @Override + public void setUseTemplate(boolean useTemplate) { + deviceInfo.setUseTemplate(useTemplate); + } + + @Override + public void setSchemaTemplateId(int schemaTemplateId) { + deviceInfo.setSchemaTemplateId(schemaTemplateId); + } + + @Override + public int getSchemaTemplateId() { + return deviceInfo.getSchemaTemplateId(); + } + + @Override + public int getSchemaTemplateIdWithState() { + return deviceInfo.getSchemaTemplateIdWithState(); + } + + @Override + public boolean isPreDeactivateTemplate() { + return deviceInfo.isPreDeactivateTemplate(); + } + + @Override + public void preDeactivateTemplate() { + deviceInfo.preDeactivateTemplate(); + } + + @Override + public void rollbackPreDeactivateTemplate() { + deviceInfo.rollbackPreDeactivateTemplate(); + } + + @Override + public void deactivateTemplate() { + deviceInfo.deactivateTemplate(); + } + + @Override + public boolean isAligned() { + return deviceInfo.isAligned(); + } + + @Override + public void setAligned(boolean isAligned) { + deviceInfo.setAligned(isAligned); + } + + /** + * The basic memory occupied by any AbstractDeviceMNode object + * + *
    + *
  1. object header, 8B + *
  2. node attributes + *
      + *
    1. deviceInfo reference, 8B + *
    2. basicMNode reference, 8B + *
    + *
  3. MapEntry in parent + *
+ */ + @Override + public int estimateSize() { + return 8 + 8 + deviceInfo.estimateSize() + basicMNode.estimateSize(); + } +} diff --git a/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/common/AbstractMeasurementMNode.java b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/common/AbstractMeasurementMNode.java new file mode 100644 index 0000000000000..602c5d8d3c82f --- /dev/null +++ b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/common/AbstractMeasurementMNode.java @@ -0,0 +1,256 @@ +/* + * 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.commons.schema.node.common; + +import org.apache.iotdb.commons.path.MeasurementPath; +import org.apache.iotdb.commons.path.PartialPath; +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.MNodeType; +import org.apache.iotdb.commons.schema.node.info.IMeasurementInfo; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeContainer; +import org.apache.iotdb.commons.schema.node.visitor.MNodeVisitor; +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; +import org.apache.iotdb.tsfile.write.schema.IMeasurementSchema; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public abstract class AbstractMeasurementMNode, BasicNode extends IMNode> + implements IMeasurementMNode { + + private static final Logger logger = LoggerFactory.getLogger(AbstractMeasurementMNode.class); + + private final IMeasurementInfo measurementInfo; + protected final BasicNode basicMNode; + + public AbstractMeasurementMNode(BasicNode basicMNode, IMeasurementInfo measurementInfo) { + this.basicMNode = basicMNode; + this.measurementInfo = measurementInfo; + } + + public BasicNode getBasicMNode() { + return basicMNode; + } + + @Override + public String getName() { + return basicMNode.getName(); + } + + @Override + public void setName(String name) { + basicMNode.setName(name); + } + + @Override + public N getParent() { + if (basicMNode.getParent() == null) { + return null; + } + return basicMNode.getParent(); + } + + @Override + public void setParent(N parent) { + basicMNode.setParent(parent); + } + + /** + * get MeasurementPath of this node + * + * @return MeasurementPath + */ + @Override + public MeasurementPath getMeasurementPath() { + MeasurementPath result = new MeasurementPath(getPartialPath(), getSchema()); + result.setUnderAlignedEntity(getParent().getAsDeviceMNode().isAligned()); + return result; + } + + @Override + public IMeasurementSchema getSchema() { + return measurementInfo.getSchema(); + } + + @Override + public void setSchema(IMeasurementSchema schema) { + measurementInfo.setSchema(schema); + } + + @Override + public TSDataType getDataType() { + return measurementInfo.getDataType(); + } + + @Override + public long getOffset() { + return measurementInfo.getOffset(); + } + + @Override + public void setOffset(long offset) { + measurementInfo.setOffset(offset); + } + + @Override + public String getAlias() { + return measurementInfo.getAlias(); + } + + @Override + public void setAlias(String alias) { + measurementInfo.setAlias(alias); + } + + @Override + public boolean isPreDeleted() { + return measurementInfo.isPreDeleted(); + } + + @Override + public void setPreDeleted(boolean preDeleted) { + measurementInfo.setPreDeleted(preDeleted); + } + + @Override + public R accept(MNodeVisitor visitor, C context) { + return visitor.visitMeasurementMNode(this, context); + } + + @Override + public String getFullPath() { + return basicMNode.getFullPath(); + } + + @Override + public void setFullPath(String fullPath) { + basicMNode.setFullPath(fullPath); + } + + @Override + public PartialPath getPartialPath() { + return basicMNode.getPartialPath(); + } + + @Override + public boolean hasChild(String name) { + return false; + } + + @Override + public N getChild(String name) { + logger.warn("current node {} is a MeasurementMNode, can not get child {}", getName(), name); + throw new RuntimeException( + String.format( + "current node %s is a MeasurementMNode, can not get child %s", getName(), name)); + } + + @Override + public N addChild(String name, N child) { + // Do nothing + return null; + } + + @Override + public N addChild(N child) { + return null; + } + + @Override + public N deleteChild(String name) { + return null; + } + + @Override + public void replaceChild(String oldChildName, N newChildNode) {} + + @Override + public void moveDataToNewMNode(N newMNode) { + basicMNode.moveDataToNewMNode(newMNode); + if (newMNode.isMeasurement()) { + measurementInfo.moveDataToNewMNode(newMNode.getAsMeasurementMNode()); + } + } + + @Override + public void setChildren(IMNodeContainer children) { + // Do nothing + } + + @Override + public boolean isAboveDatabase() { + return false; + } + + @Override + public boolean isDatabase() { + return false; + } + + @Override + public boolean isDevice() { + return false; + } + + @Override + public boolean isMeasurement() { + return true; + } + + @Override + public MNodeType getMNodeType(Boolean isConfig) { + return MNodeType.MEASUREMENT; + } + + @Override + public IDatabaseMNode getAsDatabaseMNode() { + throw new UnsupportedOperationException("Wrong MNode Type"); + } + + @Override + public IDeviceMNode getAsDeviceMNode() { + throw new UnsupportedOperationException("Wrong MNode Type"); + } + + @Override + public IMeasurementMNode getAsMeasurementMNode() { + return this; + } + + /** + * The basic memory occupied by any AbstractMeasurementMNode object + * + *
    + *
  1. object header, 8B + *
  2. node attributes + *
      + *
    1. measurementInfo reference, 8B + *
    2. basicMNode reference, 8B + *
    + *
  3. MapEntry in parent + *
+ */ + @Override + public int estimateSize() { + return 8 + 8 + measurementInfo.estimateSize() + basicMNode.estimateSize(); + } +} diff --git a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/multimode/MTreeNonMemoryTest.java b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/info/IDatabaseDeviceInfo.java similarity index 73% rename from server/src/test/java/org/apache/iotdb/db/metadata/mtree/multimode/MTreeNonMemoryTest.java rename to node-commons/src/main/java/org/apache/iotdb/commons/schema/node/info/IDatabaseDeviceInfo.java index 2de16968af571..35034a7464e39 100644 --- a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/multimode/MTreeNonMemoryTest.java +++ b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/info/IDatabaseDeviceInfo.java @@ -16,13 +16,9 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.iotdb.db.metadata.mtree.multimode; +package org.apache.iotdb.commons.schema.node.info; -import org.apache.iotdb.db.conf.IoTDBDescriptor; +import org.apache.iotdb.commons.schema.node.IMNode; -public class MTreeNonMemoryTest extends MTreeDiskModeTest { - @Override - protected void setMemSize() { - IoTDBDescriptor.getInstance().getConfig().setCachedMNodeSizeInSchemaFileMode(0); - } -} +public interface IDatabaseDeviceInfo> + extends IDatabaseInfo, IDeviceInfo {} diff --git a/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/info/IDatabaseInfo.java b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/info/IDatabaseInfo.java new file mode 100644 index 0000000000000..8fd6c3d2a8673 --- /dev/null +++ b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/info/IDatabaseInfo.java @@ -0,0 +1,33 @@ +/* + * 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.commons.schema.node.info; + +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; + +public interface IDatabaseInfo> { + + void moveDataToNewMNode(IDatabaseMNode newMNode); + + long getDataTTL(); + + void setDataTTL(long dataTTL); + + int estimateSize(); +} diff --git a/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/info/IDeviceInfo.java b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/info/IDeviceInfo.java new file mode 100644 index 0000000000000..bc625833d5b5a --- /dev/null +++ b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/info/IDeviceInfo.java @@ -0,0 +1,68 @@ +/* + * 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.commons.schema.node.info; + +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; + +import java.util.Map; + +public interface IDeviceInfo> { + + void moveDataToNewMNode(IDeviceMNode newMNode); + + boolean addAlias(String alias, IMeasurementMNode child); + + void deleteAliasChild(String alias); + + Map> getAliasChildren(); + + void setAliasChildren(Map> aliasChildren); + + boolean hasAliasChild(String name); + + N getAliasChild(String name); + + boolean isUseTemplate(); + + void setUseTemplate(boolean useTemplate); + + void setSchemaTemplateId(int schemaTemplateId); + + /** @return the logic id of template set or activated on this node, id>=-1 */ + int getSchemaTemplateId(); + + /** @return the template id with current state, may be negative since unset or deactivation */ + int getSchemaTemplateIdWithState(); + + boolean isPreDeactivateTemplate(); + + void preDeactivateTemplate(); + + void rollbackPreDeactivateTemplate(); + + void deactivateTemplate(); + + boolean isAligned(); + + void setAligned(boolean isAligned); + + int estimateSize(); +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/estimator/IMNodeSizeEstimator.java b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/info/IMeasurementInfo.java similarity index 55% rename from server/src/main/java/org/apache/iotdb/db/metadata/mnode/estimator/IMNodeSizeEstimator.java rename to node-commons/src/main/java/org/apache/iotdb/commons/schema/node/info/IMeasurementInfo.java index e9ecfd4cd849b..ccb9254bd581e 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/estimator/IMNodeSizeEstimator.java +++ b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/info/IMeasurementInfo.java @@ -16,23 +16,33 @@ * specific language governing permissions and limitations * under the License. */ +package org.apache.iotdb.commons.schema.node.info; -package org.apache.iotdb.db.metadata.mnode.estimator; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; +import org.apache.iotdb.tsfile.write.schema.IMeasurementSchema; -import org.apache.iotdb.db.metadata.mnode.IMNode; +public interface IMeasurementInfo { -import static org.apache.iotdb.db.metadata.mnode.estimator.BasicMNodSizeEstimator.ALIAS_BASE_SIZE; -import static org.apache.iotdb.db.metadata.mnode.estimator.BasicMNodSizeEstimator.ENTITY_NODE_BASE_SIZE; + IMeasurementSchema getSchema(); -public interface IMNodeSizeEstimator { + void setSchema(IMeasurementSchema schema); - int estimateSize(IMNode node); + TSDataType getDataType(); - static int getEntityNodeBaseSize() { - return ENTITY_NODE_BASE_SIZE; - } + String getAlias(); - static int getAliasBaseSize() { - return ALIAS_BASE_SIZE; - } + void setAlias(String alias); + + long getOffset(); + + void setOffset(long offset); + + boolean isPreDeleted(); + + void setPreDeleted(boolean preDeleted); + + int estimateSize(); + + void moveDataToNewMNode(IMeasurementMNode newMNode); } diff --git a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/multimode/MTreeFullMemoryTest.java b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/role/IDatabaseMNode.java similarity index 73% rename from server/src/test/java/org/apache/iotdb/db/metadata/mtree/multimode/MTreeFullMemoryTest.java rename to node-commons/src/main/java/org/apache/iotdb/commons/schema/node/role/IDatabaseMNode.java index e7f58836bc3cc..570b7a31e090b 100644 --- a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/multimode/MTreeFullMemoryTest.java +++ b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/role/IDatabaseMNode.java @@ -16,13 +16,13 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.iotdb.db.metadata.mtree.multimode; +package org.apache.iotdb.commons.schema.node.role; -import org.apache.iotdb.db.conf.IoTDBDescriptor; +import org.apache.iotdb.commons.schema.node.IMNode; -public class MTreeFullMemoryTest extends MTreeDiskModeTest { - @Override - protected void setMemSize() { - IoTDBDescriptor.getInstance().getConfig().setCachedMNodeSizeInSchemaFileMode(10000); - } +/** This interface defines a DatabaseMNode's operation interfaces. */ +public interface IDatabaseMNode> extends IMNode { + long getDataTTL(); + + void setDataTTL(long dataTTL); } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/IEntityMNode.java b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/role/IDeviceMNode.java similarity index 63% rename from server/src/main/java/org/apache/iotdb/db/metadata/mnode/IEntityMNode.java rename to node-commons/src/main/java/org/apache/iotdb/commons/schema/node/role/IDeviceMNode.java index 6bbf5717c1bec..8713933d5eb0b 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/IEntityMNode.java +++ b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/role/IDeviceMNode.java @@ -16,25 +16,33 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.iotdb.db.metadata.mnode; +package org.apache.iotdb.commons.schema.node.role; -import java.util.Map; +import org.apache.iotdb.commons.schema.node.IMNode; -public interface IEntityMNode extends IMNode { +import java.util.Map; - boolean addAlias(String alias, IMeasurementMNode child); +public interface IDeviceMNode> extends IMNode { + boolean addAlias(String alias, IMeasurementMNode child); void deleteAliasChild(String alias); - Map getAliasChildren(); + Map> getAliasChildren(); - void setAliasChildren(Map aliasChildren); + void setAliasChildren(Map> aliasChildren); - @Override boolean isUseTemplate(); void setUseTemplate(boolean useTemplate); + void setSchemaTemplateId(int schemaTemplateId); + + /** @return the logic id of template set or activated on this node, id>=-1 */ + int getSchemaTemplateId(); + + /** @return the template id with current state, may be negative since unset or deactivation */ + int getSchemaTemplateIdWithState(); + boolean isPreDeactivateTemplate(); void preDeactivateTemplate(); diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/IMeasurementMNode.java b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/role/IMeasurementMNode.java similarity index 84% rename from server/src/main/java/org/apache/iotdb/db/metadata/mnode/IMeasurementMNode.java rename to node-commons/src/main/java/org/apache/iotdb/commons/schema/node/role/IMeasurementMNode.java index 5f4cccdf93795..57ad4dd94845a 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/IMeasurementMNode.java +++ b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/role/IMeasurementMNode.java @@ -16,23 +16,21 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.iotdb.db.metadata.mnode; +package org.apache.iotdb.commons.schema.node.role; import org.apache.iotdb.commons.path.MeasurementPath; +import org.apache.iotdb.commons.schema.node.IMNode; import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; import org.apache.iotdb.tsfile.write.schema.IMeasurementSchema; /** This interface defines a MeasurementMNode's operation interfaces. */ -public interface IMeasurementMNode extends IMNode { - - @Override - IEntityMNode getParent(); - - MeasurementPath getMeasurementPath(); +public interface IMeasurementMNode> extends IMNode { IMeasurementSchema getSchema(); - TSDataType getDataType(String measurementId); + void setSchema(IMeasurementSchema schema); + + TSDataType getDataType(); String getAlias(); @@ -45,4 +43,6 @@ public interface IMeasurementMNode extends IMNode { boolean isPreDeleted(); void setPreDeleted(boolean preDeleted); + + MeasurementPath getMeasurementPath(); } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/container/IMNodeContainer.java b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/utils/IMNodeContainer.java similarity index 81% rename from server/src/main/java/org/apache/iotdb/db/metadata/mnode/container/IMNodeContainer.java rename to node-commons/src/main/java/org/apache/iotdb/commons/schema/node/utils/IMNodeContainer.java index e8e0cb6820c0e..f3522686f781d 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/container/IMNodeContainer.java +++ b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/utils/IMNodeContainer.java @@ -16,10 +16,10 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.iotdb.db.metadata.mnode.container; +package org.apache.iotdb.commons.schema.node.utils; -import org.apache.iotdb.db.metadata.mnode.IMNode; +import org.apache.iotdb.commons.schema.node.IMNode; import java.util.Map; -public interface IMNodeContainer extends Map {} +public interface IMNodeContainer> extends Map {} diff --git a/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/utils/IMNodeFactory.java b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/utils/IMNodeFactory.java new file mode 100644 index 0000000000000..f8aaa669bcbc7 --- /dev/null +++ b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/utils/IMNodeFactory.java @@ -0,0 +1,42 @@ +/* + * 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.commons.schema.node.utils; + +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.tsfile.write.schema.IMeasurementSchema; + +public interface IMNodeFactory> { + IMeasurementMNode createMeasurementMNode( + IDeviceMNode parent, String name, IMeasurementSchema schema, String alias); + + IDeviceMNode createDeviceMNode(N parent, String name); + + IDatabaseMNode createDatabaseMNode(N parent, String name); + + IDatabaseMNode createDatabaseMNode(N parent, String name, long dataTTL); + + N createDatabaseDeviceMNode(N parent, String name, long dataTTL); + + N createAboveDatabaseMNode(N parent, String name); + + N createInternalMNode(N parent, String name); +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/iterator/IMNodeIterator.java b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/utils/IMNodeIterator.java similarity index 82% rename from server/src/main/java/org/apache/iotdb/db/metadata/mnode/iterator/IMNodeIterator.java rename to node-commons/src/main/java/org/apache/iotdb/commons/schema/node/utils/IMNodeIterator.java index 254219a280630..de1c710b22890 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/iterator/IMNodeIterator.java +++ b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/utils/IMNodeIterator.java @@ -16,13 +16,13 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.iotdb.db.metadata.mnode.iterator; +package org.apache.iotdb.commons.schema.node.utils; -import org.apache.iotdb.db.metadata.mnode.IMNode; +import org.apache.iotdb.commons.schema.node.IMNode; import java.util.Iterator; -public interface IMNodeIterator extends Iterator { +public interface IMNodeIterator> extends Iterator { void close(); } diff --git a/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/visitor/MNodeVisitor.java b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/visitor/MNodeVisitor.java new file mode 100644 index 0000000000000..d156295eb0db8 --- /dev/null +++ b/node-commons/src/main/java/org/apache/iotdb/commons/schema/node/visitor/MNodeVisitor.java @@ -0,0 +1,42 @@ +/* + * 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.commons.schema.node.visitor; + +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.common.AbstractDatabaseDeviceMNode; +import org.apache.iotdb.commons.schema.node.common.AbstractDatabaseMNode; +import org.apache.iotdb.commons.schema.node.common.AbstractDeviceMNode; +import org.apache.iotdb.commons.schema.node.common.AbstractMeasurementMNode; + +public abstract class MNodeVisitor { + + public abstract R visitBasicMNode(IMNode node, C context); + + public abstract R visitDatabaseMNode( + AbstractDatabaseMNode> node, C context); + + public abstract R visitDatabaseDeviceMNode( + AbstractDatabaseDeviceMNode> node, C context); + + public abstract R visitDeviceMNode(AbstractDeviceMNode> node, C context); + + public abstract R visitMeasurementMNode( + AbstractMeasurementMNode> node, C context); +} diff --git a/node-commons/src/main/java/org/apache/iotdb/commons/utils/ThriftConfigNodeSerDeUtils.java b/node-commons/src/main/java/org/apache/iotdb/commons/utils/ThriftConfigNodeSerDeUtils.java index 3a2be10483ad9..8b2f3a3c25ef1 100644 --- a/node-commons/src/main/java/org/apache/iotdb/commons/utils/ThriftConfigNodeSerDeUtils.java +++ b/node-commons/src/main/java/org/apache/iotdb/commons/utils/ThriftConfigNodeSerDeUtils.java @@ -66,7 +66,7 @@ private static TBinaryProtocol generateReadProtocol(InputStream inputStream) return new TBinaryProtocol(tioStreamTransport); } - public static void serializeTStorageGroupSchema( + public static void serializeTDatabaseSchema( TDatabaseSchema storageGroupSchema, ByteBuffer buffer) { try { storageGroupSchema.write(generateWriteProtocol(buffer)); @@ -75,7 +75,7 @@ public static void serializeTStorageGroupSchema( } } - public static TDatabaseSchema deserializeTStorageGroupSchema(ByteBuffer buffer) { + public static TDatabaseSchema deserializeTDatabaseSchema(ByteBuffer buffer) { TDatabaseSchema storageGroupSchema = new TDatabaseSchema(); try { storageGroupSchema.read(generateReadProtocol(buffer)); @@ -85,7 +85,7 @@ public static TDatabaseSchema deserializeTStorageGroupSchema(ByteBuffer buffer) return storageGroupSchema; } - public static void serializeTStorageGroupSchema( + public static void serializeTDatabaseSchema( TDatabaseSchema storageGroupSchema, OutputStream outputStream) { try { storageGroupSchema.write(generateWriteProtocol(outputStream)); @@ -94,7 +94,7 @@ public static void serializeTStorageGroupSchema( } } - public static TDatabaseSchema deserializeTStorageGroupSchema(InputStream inputStream) { + public static TDatabaseSchema deserializeTDatabaseSchema(InputStream inputStream) { TDatabaseSchema storageGroupSchema = new TDatabaseSchema(); try { storageGroupSchema.read(generateReadProtocol(inputStream)); diff --git a/node-commons/src/test/java/org/apache/iotdb/commons/utils/ThriftConfigNodeSerDeUtilsTest.java b/node-commons/src/test/java/org/apache/iotdb/commons/utils/ThriftConfigNodeSerDeUtilsTest.java index 5d949079aceac..ac5b742722bbf 100644 --- a/node-commons/src/test/java/org/apache/iotdb/commons/utils/ThriftConfigNodeSerDeUtilsTest.java +++ b/node-commons/src/test/java/org/apache/iotdb/commons/utils/ThriftConfigNodeSerDeUtilsTest.java @@ -46,10 +46,10 @@ public void readWriteTStorageGroupSchemaTest() { storageGroupSchema0.setDataReplicationFactor(3); storageGroupSchema0.setTimePartitionInterval(604800); - ThriftConfigNodeSerDeUtils.serializeTStorageGroupSchema(storageGroupSchema0, buffer); + ThriftConfigNodeSerDeUtils.serializeTDatabaseSchema(storageGroupSchema0, buffer); buffer.flip(); TDatabaseSchema storageGroupSchema1 = - ThriftConfigNodeSerDeUtils.deserializeTStorageGroupSchema(buffer); + ThriftConfigNodeSerDeUtils.deserializeTDatabaseSchema(buffer); Assert.assertEquals(storageGroupSchema0, storageGroupSchema1); } diff --git a/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/RSchemaReadWriteHandler.java b/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/RSchemaReadWriteHandler.java index ea127b46df4df..621897dd2a38c 100644 --- a/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/RSchemaReadWriteHandler.java +++ b/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/RSchemaReadWriteHandler.java @@ -21,11 +21,11 @@ import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.path.PartialPath; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; import org.apache.iotdb.commons.utils.TestOnly; import org.apache.iotdb.db.conf.IoTDBConfig; import org.apache.iotdb.db.conf.IoTDBDescriptor; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.MeasurementMNode; +import org.apache.iotdb.db.metadata.mnode.mem.impl.MeasurementMNode; import org.apache.iotdb.db.metadata.schemaregion.rocksdb.mnode.RMNodeType; import org.apache.iotdb.tsfile.utils.ReadWriteIOUtils; import org.apache.iotdb.tsfile.write.schema.MeasurementSchema; diff --git a/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/RSchemaRegion.java b/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/RSchemaRegion.java index 7a30ea527e3cb..69aec0c9561d5 100644 --- a/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/RSchemaRegion.java +++ b/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/RSchemaRegion.java @@ -27,6 +27,8 @@ import org.apache.iotdb.commons.path.MeasurementPath; import org.apache.iotdb.commons.path.PartialPath; import org.apache.iotdb.commons.path.PathPatternTree; +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; import org.apache.iotdb.commons.utils.PathUtils; import org.apache.iotdb.commons.utils.TestOnly; import org.apache.iotdb.db.conf.IoTDBConfig; @@ -42,8 +44,6 @@ import org.apache.iotdb.db.metadata.idtable.IDTable; import org.apache.iotdb.db.metadata.idtable.IDTableManager; import org.apache.iotdb.db.metadata.metric.ISchemaRegionMetric; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; import org.apache.iotdb.db.metadata.plan.schemaregion.read.IShowDevicesPlan; import org.apache.iotdb.db.metadata.plan.schemaregion.read.IShowNodesPlan; import org.apache.iotdb.db.metadata.plan.schemaregion.read.IShowTimeSeriesPlan; @@ -622,7 +622,7 @@ public Pair> deleteTimeseries(PartialPath pathPattern, bool // TODO: tags invert index update } readWriteHandler.executeBatch(batch); - if (!deletedNode.getParent().isStorageGroup()) { + if (!deletedNode.getParent().isDatabase()) { parentNeedsToCheck.add(deletedNode.getParent()); } } @@ -645,7 +645,7 @@ public Pair> deleteTimeseries(PartialPath pathPattern, bool Stream parentStream = parentNeedsToCheck.parallelStream(); parentStream.forEach( currentNode -> { - if (!currentNode.isStorageGroup()) { + if (!currentNode.isDatabase()) { PartialPath parentPath = currentNode.getPartialPath(); int level = parentPath.getNodeLength(); int end = parentPath.getNodeLength() - 1; @@ -655,7 +655,7 @@ public Pair> deleteTimeseries(PartialPath pathPattern, bool readWriteHandler.deleteNode( parentPath.getNodes(), RSchemaUtils.typeOfMNode(currentNode)); IMNode parentNode = currentNode.getParent(); - if (!parentNode.isStorageGroup()) { + if (!parentNode.isDatabase()) { tempSet.add(currentNode.getParent()); } } catch (Exception e) { diff --git a/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/RSchemaUtils.java b/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/RSchemaUtils.java index f38d3290775d2..f5f31136e12f7 100644 --- a/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/RSchemaUtils.java +++ b/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/RSchemaUtils.java @@ -22,13 +22,13 @@ import org.apache.iotdb.commons.conf.IoTDBConstant; import org.apache.iotdb.commons.exception.IllegalPathException; import org.apache.iotdb.commons.path.PartialPath; +import org.apache.iotdb.commons.schema.node.IMNode; import org.apache.iotdb.commons.utils.PathUtils; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.schemaregion.rocksdb.mnode.REntityMNode; +import org.apache.iotdb.db.metadata.schemaregion.rocksdb.mnode.RDatabaseMNode; +import org.apache.iotdb.db.metadata.schemaregion.rocksdb.mnode.RDeviceMNode; import org.apache.iotdb.db.metadata.schemaregion.rocksdb.mnode.RMNodeType; import org.apache.iotdb.db.metadata.schemaregion.rocksdb.mnode.RMNodeValueType; import org.apache.iotdb.db.metadata.schemaregion.rocksdb.mnode.RMeasurementMNode; -import org.apache.iotdb.db.metadata.schemaregion.rocksdb.mnode.RStorageGroupMNode; import org.apache.iotdb.tsfile.utils.BytesUtils; import org.apache.iotdb.tsfile.utils.ReadWriteIOUtils; import org.apache.iotdb.tsfile.write.schema.IMeasurementSchema; @@ -170,11 +170,11 @@ public static PartialPath getPartialPathFromInnerPath(String path, char level) { public static RMNodeType typeOfMNode(IMNode mNode) { // order sensitive - if (mNode instanceof REntityMNode) { + if (mNode instanceof RDeviceMNode) { return RMNodeType.ENTITY; } - if (mNode instanceof RStorageGroupMNode) { + if (mNode instanceof RDatabaseMNode) { return RMNodeType.STORAGE_GROUP; } diff --git a/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/mnode/RStorageGroupMNode.java b/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/mnode/RDatabaseMNode.java similarity index 74% rename from schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/mnode/RStorageGroupMNode.java rename to schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/mnode/RDatabaseMNode.java index 8efabdfb29fab..3fb4dbaf77c49 100644 --- a/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/mnode/RStorageGroupMNode.java +++ b/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/mnode/RDatabaseMNode.java @@ -21,16 +21,16 @@ import org.apache.iotdb.commons.conf.CommonDescriptor; import org.apache.iotdb.commons.exception.MetadataException; -import org.apache.iotdb.confignode.rpc.thrift.TDatabaseSchema; -import org.apache.iotdb.db.metadata.mnode.IStorageGroupMNode; -import org.apache.iotdb.db.metadata.mnode.MNodeType; +import org.apache.iotdb.commons.schema.node.MNodeType; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; +import org.apache.iotdb.db.metadata.mnode.mem.IMemMNode; import org.apache.iotdb.db.metadata.schemaregion.rocksdb.RSchemaConstants; import org.apache.iotdb.db.metadata.schemaregion.rocksdb.RSchemaReadWriteHandler; import org.apache.iotdb.db.metadata.schemaregion.rocksdb.RSchemaUtils; import org.rocksdb.RocksDBException; -public class RStorageGroupMNode extends RInternalMNode implements IStorageGroupMNode { +public class RDatabaseMNode extends RInternalMNode implements IDatabaseMNode { private long dataTTL; @@ -39,14 +39,12 @@ public class RStorageGroupMNode extends RInternalMNode implements IStorageGroupM * * @param fullPath */ - public RStorageGroupMNode( - String fullPath, long dataTTL, RSchemaReadWriteHandler readWriteHandler) { + public RDatabaseMNode(String fullPath, long dataTTL, RSchemaReadWriteHandler readWriteHandler) { super(fullPath, readWriteHandler); this.dataTTL = dataTTL; } - public RStorageGroupMNode( - String fullPath, byte[] value, RSchemaReadWriteHandler readWriteHandler) { + public RDatabaseMNode(String fullPath, byte[] value, RSchemaReadWriteHandler readWriteHandler) { super(fullPath, readWriteHandler); Object ttl = RSchemaUtils.parseNodeValue(value, RMNodeValueType.TTL); if (ttl == null) { @@ -70,12 +68,25 @@ void updateChildNode(String childName, int childNameMaxLevel) throws MetadataExc } @Override - public boolean isStorageGroup() { + public IMemMNode addChild(String name, IMemMNode child) { + return null; + } + + @Override + public IMemMNode addChild(IMemMNode child) { + return null; + } + + @Override + public void replaceChild(String oldChildName, IMemMNode newChildNode) {} + + @Override + public boolean isDatabase() { return true; } @Override - public boolean isEntity() { + public boolean isDevice() { return false; } @@ -100,19 +111,7 @@ public void setDataTTL(long dataTTL) { } @Override - public void setSchemaReplicationFactor(int schemaReplicationFactor) {} - - @Override - public void setDataReplicationFactor(int dataReplicationFactor) {} - - @Override - public void setTimePartitionInterval(long timePartitionInterval) {} - - @Override - public void setStorageGroupSchema(TDatabaseSchema schema) {} - - @Override - public TDatabaseSchema getStorageGroupSchema() { + public IMemMNode getAsMNode() { return null; } } diff --git a/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/mnode/REntityMNode.java b/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/mnode/RDeviceMNode.java similarity index 70% rename from schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/mnode/REntityMNode.java rename to schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/mnode/RDeviceMNode.java index 4fedc5423988e..0ce285a7194a1 100644 --- a/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/mnode/REntityMNode.java +++ b/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/mnode/RDeviceMNode.java @@ -20,9 +20,11 @@ package org.apache.iotdb.db.metadata.schemaregion.rocksdb.mnode; import org.apache.iotdb.commons.exception.MetadataException; -import org.apache.iotdb.db.metadata.mnode.IEntityMNode; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.MNodeType; +import org.apache.iotdb.commons.schema.node.MNodeType; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.visitor.MNodeVisitor; +import org.apache.iotdb.db.metadata.mnode.mem.IMemMNode; import org.apache.iotdb.db.metadata.schemaregion.rocksdb.RSchemaConstants; import org.apache.iotdb.db.metadata.schemaregion.rocksdb.RSchemaReadWriteHandler; import org.apache.iotdb.db.metadata.schemaregion.rocksdb.RSchemaUtils; @@ -33,7 +35,7 @@ import java.nio.ByteBuffer; import java.util.Map; -public class REntityMNode extends RInternalMNode implements IEntityMNode { +public class RDeviceMNode extends RInternalMNode implements IDeviceMNode { private volatile boolean isAligned = false; @@ -42,7 +44,7 @@ public class REntityMNode extends RInternalMNode implements IEntityMNode { * * @param fullPath */ - public REntityMNode(String fullPath, RSchemaReadWriteHandler readWriteHandler) { + public RDeviceMNode(String fullPath, RSchemaReadWriteHandler readWriteHandler) { super(fullPath, readWriteHandler); } @@ -58,7 +60,7 @@ void updateChildNode(String childName, int childNameMaxLevel) throws MetadataExc } } - public REntityMNode(String fullPath, byte[] value, RSchemaReadWriteHandler readWriteHandler) { + public RDeviceMNode(String fullPath, byte[] value, RSchemaReadWriteHandler readWriteHandler) { super(fullPath, readWriteHandler); deserialize(value); } @@ -76,12 +78,37 @@ public void deleteAliasChild(String alias) { } @Override - public Map getAliasChildren() { + public Map> getAliasChildren() { throw new UnsupportedOperationException(); } @Override - public void setAliasChildren(Map aliasChildren) { + public void setAliasChildren(Map> aliasChildren) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isUseTemplate() { + throw new UnsupportedOperationException(); + } + + @Override + public void setUseTemplate(boolean useTemplate) { + throw new UnsupportedOperationException(); + } + + @Override + public void setSchemaTemplateId(int schemaTemplateId) { + throw new UnsupportedOperationException(); + } + + @Override + public int getSchemaTemplateId() { + throw new UnsupportedOperationException(); + } + + @Override + public int getSchemaTemplateIdWithState() { throw new UnsupportedOperationException(); } @@ -123,7 +150,7 @@ private void deserialize(byte[] value) { } @Override - public boolean isEntity() { + public boolean isDevice() { return true; } @@ -131,4 +158,14 @@ public boolean isEntity() { public MNodeType getMNodeType(Boolean isConfig) { return MNodeType.DEVICE; } + + @Override + public Object accept(MNodeVisitor visitor, Object context) { + throw new UnsupportedOperationException(); + } + + @Override + public IMemMNode getAsMNode() { + return this; + } } diff --git a/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/mnode/RInternalMNode.java b/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/mnode/RInternalMNode.java index cff6733d21044..338dfbc79064e 100644 --- a/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/mnode/RInternalMNode.java +++ b/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/mnode/RInternalMNode.java @@ -20,16 +20,16 @@ package org.apache.iotdb.db.metadata.schemaregion.rocksdb.mnode; import org.apache.iotdb.commons.exception.MetadataException; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.MNodeType; -import org.apache.iotdb.db.metadata.mnode.container.IMNodeContainer; +import org.apache.iotdb.commons.schema.node.MNodeType; +import org.apache.iotdb.commons.schema.node.utils.IMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.mem.IMemMNode; import org.apache.iotdb.db.metadata.schemaregion.rocksdb.RSchemaConstants; import org.apache.iotdb.db.metadata.schemaregion.rocksdb.RSchemaReadWriteHandler; import org.apache.iotdb.db.metadata.schemaregion.rocksdb.RSchemaUtils; import org.rocksdb.RocksDBException; -public class RInternalMNode extends RMNode { +public class RInternalMNode extends RMNode implements IMemMNode { private volatile boolean useTemplate = false; @@ -58,13 +58,13 @@ void updateChildNode(String childName, int childNameMaxLevel) throws MetadataExc @Override public boolean hasChild(String name) { String childPathName = fullPath.concat(RSchemaConstants.PATH_SEPARATOR).concat(name); - IMNode node = getNodeBySpecifiedPath(childPathName); + IMemMNode node = getNodeBySpecifiedPath(childPathName); return node != null; } /** get the child with the name */ @Override - public IMNode getChild(String name) { + public IMemMNode getChild(String name) { String childPathName = fullPath.concat(RSchemaConstants.PATH_SEPARATOR).concat(name); return getNodeBySpecifiedPath(childPathName); } @@ -77,7 +77,7 @@ public IMNode getChild(String name) { * @return */ @Override - public IMNode addChild(String name, IMNode child) { + public IMemMNode addChild(String name, IMemMNode child) { child.setParent(this); String childName = fullPath.concat(RSchemaConstants.PATH_SEPARATOR).concat(name); int childNameMaxLevel = RSchemaUtils.getLevelByPartialPath(childName); @@ -105,14 +105,14 @@ public IMNode addChild(String name, IMNode child) { * @return return the MNode already added */ @Override - public IMNode addChild(IMNode child) { + public IMemMNode addChild(IMemMNode child) { addChild(child.getName(), child); return child; } /** delete a child */ @Override - public IMNode deleteChild(String name) { + public IMemMNode deleteChild(String name) { String childPathName = fullPath.concat(RSchemaConstants.PATH_SEPARATOR).concat(name); int nodeNameMaxLevel = RSchemaUtils.getLevelByPartialPath(childPathName); for (RMNodeType type : RMNodeType.values()) { @@ -141,7 +141,7 @@ public IMNode deleteChild(String name) { * @param newChildNode new child node */ @Override - public void replaceChild(String oldChildName, IMNode newChildNode) { + public void replaceChild(String oldChildName, IMemMNode newChildNode) { if (!oldChildName.equals(newChildNode.getName())) { throw new RuntimeException("New child's name must be the same as old child's name!"); } @@ -150,7 +150,7 @@ public void replaceChild(String oldChildName, IMNode newChildNode) { } @Override - public IMNodeContainer getChildren() { + public IMNodeContainer getChildren() { throw new UnsupportedOperationException(); } @@ -160,12 +160,12 @@ public MNodeType getMNodeType(Boolean isConfig) { } @Override - public boolean isUseTemplate() { - return useTemplate; + public int estimateSize() { + throw new UnsupportedOperationException(); } @Override - public void setUseTemplate(boolean useTemplate) { - this.useTemplate = useTemplate; + public IMemMNode getAsMNode() { + throw new UnsupportedOperationException(); } } diff --git a/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/mnode/RMNode.java b/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/mnode/RMNode.java index 2dbad227c9c30..05cd849158e8c 100644 --- a/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/mnode/RMNode.java +++ b/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/mnode/RMNode.java @@ -22,13 +22,13 @@ import org.apache.iotdb.commons.exception.IllegalPathException; import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.path.PartialPath; -import org.apache.iotdb.db.metadata.mnode.IEntityMNode; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.IStorageGroupMNode; -import org.apache.iotdb.db.metadata.mnode.container.IMNodeContainer; -import org.apache.iotdb.db.metadata.mnode.visitor.MNodeVisitor; -import org.apache.iotdb.db.metadata.mtree.store.disk.cache.CacheEntry; +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeContainer; +import org.apache.iotdb.commons.schema.node.visitor.MNodeVisitor; +import org.apache.iotdb.db.metadata.mnode.mem.IMemMNode; import org.apache.iotdb.db.metadata.schemaregion.rocksdb.RSchemaConstants; import org.apache.iotdb.db.metadata.schemaregion.rocksdb.RSchemaReadWriteHandler; import org.apache.iotdb.db.metadata.schemaregion.rocksdb.RSchemaUtils; @@ -39,13 +39,13 @@ import java.util.Objects; -public abstract class RMNode implements IMNode { +public abstract class RMNode implements IMNode { /** from root to this node, only be set when used once for InternalMNode */ protected String fullPath; protected RSchemaReadWriteHandler readWriteHandler; - protected IMNode parent; + protected IMemMNode parent; protected String name; @@ -71,7 +71,7 @@ public void setName(String name) { } @Override - public IMNode getParent() { + public IMemMNode getParent() { if (parent != null) { return parent; } @@ -81,9 +81,9 @@ public IMNode getParent() { return parent; } - protected IMNode getNodeBySpecifiedPath(String keyName) { + protected IMemMNode getNodeBySpecifiedPath(String keyName) { byte[] value = null; - IMNode node; + IMemMNode node; int nodeNameMaxLevel = RSchemaUtils.getLevelByPartialPath(keyName); for (RMNodeType type : RMNodeType.values()) { String parentInnerName = @@ -96,13 +96,13 @@ protected IMNode getNodeBySpecifiedPath(String keyName) { if (value != null) { switch (type.getValue()) { case RSchemaConstants.NODE_TYPE_SG: - node = new RStorageGroupMNode(keyName, value, readWriteHandler); + node = new RDatabaseMNode(keyName, value, readWriteHandler); return node; case RSchemaConstants.NODE_TYPE_INTERNAL: node = new RInternalMNode(keyName, readWriteHandler); return node; case RSchemaConstants.NODE_TYPE_ENTITY: - node = new REntityMNode(keyName, value, readWriteHandler); + node = new RDeviceMNode(keyName, value, readWriteHandler); return node; case RSchemaConstants.NODE_TYPE_MEASUREMENT: node = new RMeasurementMNode(keyName, value, readWriteHandler); @@ -114,7 +114,7 @@ protected IMNode getNodeBySpecifiedPath(String keyName) { } @Override - public void setParent(IMNode parent) { + public void setParent(IMemMNode parent) { this.parent = parent; } @@ -143,58 +143,18 @@ public void setFullPath(String fullPath) { this.fullPath = fullPath; } - @Override - public boolean isUseTemplate() { - return false; - } - - @Override - public int getSchemaTemplateId() { - throw new UnsupportedOperationException(); - } - - @Override - public int getSchemaTemplateIdWithState() { - throw new UnsupportedOperationException(); - } - - @Override - public void setSchemaTemplateId(int schemaTemplateId) { - throw new UnsupportedOperationException(); - } - - @Override - public void preUnsetSchemaTemplate() { - throw new UnsupportedOperationException(); - } - - @Override - public void rollbackUnsetSchemaTemplate() { - throw new UnsupportedOperationException(); - } - - @Override - public boolean isSchemaTemplatePreUnset() { - throw new UnsupportedOperationException(); - } - - @Override - public void unsetSchemaTemplate() { - throw new UnsupportedOperationException(); - } - @Override public boolean isAboveDatabase() { return false; } @Override - public boolean isStorageGroup() { + public boolean isDatabase() { return false; } @Override - public boolean isEntity() { + public boolean isDevice() { return false; } @@ -204,18 +164,18 @@ public boolean isMeasurement() { } @Override - public IStorageGroupMNode getAsStorageGroupMNode() { - if (isStorageGroup()) { - return (IStorageGroupMNode) this; + public IDatabaseMNode getAsDatabaseMNode() { + if (isDatabase()) { + return (IDatabaseMNode) this; } else { throw new UnsupportedOperationException("Wrong MNode Type"); } } @Override - public IEntityMNode getAsEntityMNode() { - if (isEntity()) { - return (IEntityMNode) this; + public IDeviceMNode getAsDeviceMNode() { + if (isDevice()) { + return (IDeviceMNode) this; } else { throw new UnsupportedOperationException("Wrong MNode Type"); } @@ -253,17 +213,7 @@ public String toString() { } @Override - public void moveDataToNewMNode(IMNode newMNode) { - throw new UnsupportedOperationException("Temporarily unsupported"); - } - - @Override - public CacheEntry getCacheEntry() { - throw new UnsupportedOperationException("Temporarily unsupported"); - } - - @Override - public void setCacheEntry(CacheEntry cacheEntry) { + public void moveDataToNewMNode(IMemMNode newMNode) { throw new UnsupportedOperationException("Temporarily unsupported"); } @@ -274,8 +224,7 @@ public void setChildren(IMNodeContainer children) { @Override public R accept(MNodeVisitor visitor, C context) { - throw new UnsupportedOperationException("RMNode doesn't support this method"); + throw new UnsupportedOperationException("Wrong MNode Type"); } - // end } diff --git a/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/mnode/RMeasurementMNode.java b/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/mnode/RMeasurementMNode.java index c13879c5529bd..58a9708d449b3 100644 --- a/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/mnode/RMeasurementMNode.java +++ b/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/mnode/RMeasurementMNode.java @@ -21,11 +21,10 @@ import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.path.MeasurementPath; -import org.apache.iotdb.db.metadata.mnode.IEntityMNode; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.MNodeType; -import org.apache.iotdb.db.metadata.mnode.container.IMNodeContainer; +import org.apache.iotdb.commons.schema.node.MNodeType; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.mem.IMemMNode; import org.apache.iotdb.db.metadata.schemaregion.rocksdb.RSchemaConstants; import org.apache.iotdb.db.metadata.schemaregion.rocksdb.RSchemaReadWriteHandler; import org.apache.iotdb.db.metadata.schemaregion.rocksdb.RSchemaUtils; @@ -40,7 +39,7 @@ import java.nio.ByteBuffer; import java.util.Map; -public class RMeasurementMNode extends RMNode implements IMeasurementMNode { +public class RMeasurementMNode extends RMNode implements IMeasurementMNode, IMemMNode { protected String alias; @@ -79,17 +78,22 @@ public RMeasurementMNode( } @Override - public IEntityMNode getParent() { + public IMemMNode getParent() { if (super.getParent() == null) { return null; } - return parent.getAsEntityMNode(); + return parent; + } + + @Override + public IMemMNode getAsMNode() { + return null; } @Override public MeasurementPath getMeasurementPath() { MeasurementPath result = new MeasurementPath(super.getPartialPath(), schema); - result.setUnderAlignedEntity(getParent().isAligned()); + result.setUnderAlignedEntity(getParent().getAsDeviceMNode().isAligned()); if (alias != null && !alias.isEmpty()) { result.setMeasurementAlias(alias); } @@ -102,7 +106,12 @@ public IMeasurementSchema getSchema() { } @Override - public TSDataType getDataType(String measurementId) { + public void setSchema(IMeasurementSchema schema) { + this.schema = schema; + } + + @Override + public TSDataType getDataType() { return schema.getType(); } @@ -167,40 +176,37 @@ public boolean hasChild(String name) { } @Override - public IMNode getChild(String name) { + public IMemMNode getChild(String name) { throw new RuntimeException( String.format( "current node %s is a MeasurementMNode, can not get child %s", super.name, name)); } @Override - public IMNode addChild(String name, IMNode child) { + public IMemMNode addChild(String name, IMemMNode child) { // Do nothing return child; } @Override - public IMNode addChild(IMNode child) { + public IMemMNode addChild(IMemMNode child) { return null; } @Override - public IMNode deleteChild(String name) { + public IMemMNode deleteChild(String name) { // Do nothing return null; } @Override - public void replaceChild(String oldChildName, IMNode newChildNode) {} + public void replaceChild(String oldChildName, IMemMNode newChildNode) {} @Override public IMNodeContainer getChildren() { throw new UnsupportedOperationException(); } - @Override - public void setUseTemplate(boolean useTemplate) {} - @Override public boolean isMeasurement() { return true; @@ -211,6 +217,11 @@ public MNodeType getMNodeType(Boolean isConfig) { return MNodeType.MEASUREMENT; } + @Override + public int estimateSize() { + throw new UnsupportedOperationException(); + } + public Map getTags() { return tags; } diff --git a/schema-engine-tag/src/main/java/org/apache/iotdb/db/metadata/tagSchemaRegion/TagSchemaRegion.java b/schema-engine-tag/src/main/java/org/apache/iotdb/db/metadata/tagSchemaRegion/TagSchemaRegion.java index 7aacc567402ae..75b73a4d9e6e2 100644 --- a/schema-engine-tag/src/main/java/org/apache/iotdb/db/metadata/tagSchemaRegion/TagSchemaRegion.java +++ b/schema-engine-tag/src/main/java/org/apache/iotdb/db/metadata/tagSchemaRegion/TagSchemaRegion.java @@ -25,6 +25,7 @@ import org.apache.iotdb.commons.path.MeasurementPath; import org.apache.iotdb.commons.path.PartialPath; import org.apache.iotdb.commons.path.PathPatternTree; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; import org.apache.iotdb.commons.utils.TestOnly; import org.apache.iotdb.db.conf.IoTDBConfig; import org.apache.iotdb.db.conf.IoTDBDescriptor; @@ -37,7 +38,6 @@ import org.apache.iotdb.db.metadata.idtable.entry.SHA256DeviceID; import org.apache.iotdb.db.metadata.idtable.entry.SchemaEntry; import org.apache.iotdb.db.metadata.metric.ISchemaRegionMetric; -import org.apache.iotdb.db.metadata.mnode.IStorageGroupMNode; import org.apache.iotdb.db.metadata.plan.schemaregion.impl.write.SchemaRegionWritePlanFactory; import org.apache.iotdb.db.metadata.plan.schemaregion.read.IShowDevicesPlan; import org.apache.iotdb.db.metadata.plan.schemaregion.read.IShowNodesPlan; @@ -87,7 +87,7 @@ public class TagSchemaRegion implements ISchemaRegion { // when a path ends with ".**", it represents batch processing private final String TAIL = ".**"; - private final IStorageGroupMNode storageGroupMNode; + private final IDatabaseMNode storageGroupMNode; private final String storageGroupFullPath; private final SchemaRegionId schemaRegionId; private final String schemaRegionDirPath; @@ -103,7 +103,7 @@ public class TagSchemaRegion implements ISchemaRegion { public TagSchemaRegion( PartialPath storageGroup, SchemaRegionId schemaRegionId, - IStorageGroupMNode storageGroupMNode, + IDatabaseMNode storageGroupMNode, ISeriesNumerMonitor seriesNumerMonitor) throws MetadataException { storageGroupFullPath = storageGroup.getFullPath(); diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/idtable/entry/InsertMeasurementMNode.java b/server/src/main/java/org/apache/iotdb/db/metadata/idtable/entry/InsertMeasurementMNode.java deleted file mode 100644 index 1b376d4c518fd..0000000000000 --- a/server/src/main/java/org/apache/iotdb/db/metadata/idtable/entry/InsertMeasurementMNode.java +++ /dev/null @@ -1,287 +0,0 @@ -/* - * 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.metadata.idtable.entry; - -import org.apache.iotdb.commons.path.MeasurementPath; -import org.apache.iotdb.commons.path.PartialPath; -import org.apache.iotdb.db.metadata.mnode.IEntityMNode; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.IStorageGroupMNode; -import org.apache.iotdb.db.metadata.mnode.MNodeType; -import org.apache.iotdb.db.metadata.mnode.container.IMNodeContainer; -import org.apache.iotdb.db.metadata.mnode.visitor.MNodeVisitor; -import org.apache.iotdb.db.metadata.mtree.store.disk.cache.CacheEntry; -import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; -import org.apache.iotdb.tsfile.write.schema.IMeasurementSchema; -import org.apache.iotdb.tsfile.write.schema.MeasurementSchema; - -/** - * Generated entity implements IMeasurementMNode interface to unify insert logic through id table - * and SchemaProcessor - */ -public class InsertMeasurementMNode implements IMeasurementMNode { - SchemaEntry schemaEntry; - - IMeasurementSchema schema; - - public InsertMeasurementMNode(String measurementId, SchemaEntry schemaEntry) { - this.schemaEntry = schemaEntry; - schema = - new MeasurementSchema( - measurementId, - schemaEntry.getTSDataType(), - schemaEntry.getTSEncoding(), - schemaEntry.getCompressionType()); - } - - // region support methods - - @Override - public boolean isPreDeleted() { - return false; - } - - @Override - public void setPreDeleted(boolean preDeleted) {} - - @Override - public IMeasurementSchema getSchema() { - return schema; - } - - @Override - public TSDataType getDataType(String measurementId) { - return schemaEntry.getTSDataType(); - } - - @Override - public IEntityMNode getParent() { - return null; - } - - @Override - public String toString() { - return schema.getMeasurementId(); - } - - // endregion - - // region unsupported methods - @Override - public String getName() { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public void setName(String name) { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public void setParent(IMNode parent) { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public String getFullPath() { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public void setFullPath(String fullPath) { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public PartialPath getPartialPath() { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public boolean hasChild(String name) { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public IMNode getChild(String name) { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public IMNode addChild(String name, IMNode child) { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public IMNode addChild(IMNode child) { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public IMNode deleteChild(String name) { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public void replaceChild(String oldChildName, IMNode newChildNode) { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public void moveDataToNewMNode(IMNode newMNode) { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public IMNodeContainer getChildren() { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public void setChildren(IMNodeContainer children) { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public boolean isUseTemplate() { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public void setUseTemplate(boolean useTemplate) { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public int getSchemaTemplateId() { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public int getSchemaTemplateIdWithState() { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public void setSchemaTemplateId(int schemaTemplateId) { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public void preUnsetSchemaTemplate() { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public void rollbackUnsetSchemaTemplate() { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public boolean isSchemaTemplatePreUnset() { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public void unsetSchemaTemplate() { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public boolean isAboveDatabase() { - return false; - } - - @Override - public boolean isStorageGroup() { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public boolean isEntity() { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public boolean isMeasurement() { - return true; - } - - @Override - public MNodeType getMNodeType(Boolean isConfig) { - return MNodeType.MEASUREMENT; - } - - @Override - public IStorageGroupMNode getAsStorageGroupMNode() { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public IEntityMNode getAsEntityMNode() { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public IMeasurementMNode getAsMeasurementMNode() { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public CacheEntry getCacheEntry() { - return null; - } - - @Override - public void setCacheEntry(CacheEntry cacheEntry) {} - - @Override - public R accept(MNodeVisitor visitor, C context) { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public MeasurementPath getMeasurementPath() { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public String getAlias() { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public void setAlias(String alias) { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public long getOffset() { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - @Override - public void setOffset(long offset) { - throw new UnsupportedOperationException("insert measurement mnode doesn't support this method"); - } - - // endregion -} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/MNode.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/MNode.java deleted file mode 100644 index cfd7f6ee4d2da..0000000000000 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/MNode.java +++ /dev/null @@ -1,200 +0,0 @@ -/* - * 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.metadata.mnode; - -import org.apache.iotdb.commons.conf.IoTDBConstant; -import org.apache.iotdb.commons.path.PartialPath; -import org.apache.iotdb.db.metadata.mtree.store.disk.cache.CacheEntry; - -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; - -public abstract class MNode implements IMNode { - - /** Name of the MNode */ - protected String name; - - protected IMNode parent; - - /** from root to this node, only be set when used once for InternalMNode */ - protected String fullPath; - - protected CacheEntry cacheEntry; - - /** Constructor of MNode. */ - protected MNode(IMNode parent, String name) { - this.parent = parent; - this.name = name; - } - - @Override - public String getName() { - return name; - } - - @Override - public void setName(String name) { - this.name = name; - } - - @Override - public IMNode getParent() { - return parent; - } - - @Override - public void setParent(IMNode parent) { - this.parent = parent; - } - - /** - * get partial path of this node - * - * @return partial path - */ - @Override - public PartialPath getPartialPath() { - List detachedPath = new ArrayList<>(); - IMNode temp = this; - detachedPath.add(temp.getName()); - while (temp.getParent() != null) { - temp = temp.getParent(); - detachedPath.add(0, temp.getName()); - } - return new PartialPath(detachedPath.toArray(new String[0])); - } - - /** get full path */ - @Override - public String getFullPath() { - if (fullPath == null) { - fullPath = concatFullPath(); - } - return fullPath; - } - - String concatFullPath() { - StringBuilder builder = new StringBuilder(name); - IMNode curr = this; - while (curr.getParent() != null) { - curr = curr.getParent(); - builder.insert(0, IoTDBConstant.PATH_SEPARATOR).insert(0, curr.getName()); - } - return builder.toString(); - } - - @Override - public void setFullPath(String fullPath) { - this.fullPath = fullPath; - } - - @Override - public void moveDataToNewMNode(IMNode newMNode) { - newMNode.setParent(parent); - newMNode.setCacheEntry(cacheEntry); - } - - @Override - public boolean isUseTemplate() { - return false; - } - - @Override - public boolean isStorageGroup() { - return false; - } - - @Override - public boolean isEntity() { - return false; - } - - @Override - public boolean isMeasurement() { - return false; - } - - @Override - public IStorageGroupMNode getAsStorageGroupMNode() { - if (isStorageGroup()) { - return (IStorageGroupMNode) this; - } else { - throw new UnsupportedOperationException("Wrong MNode Type"); - } - } - - @Override - public IEntityMNode getAsEntityMNode() { - if (isEntity()) { - return (IEntityMNode) this; - } else { - throw new UnsupportedOperationException("Wrong MNode Type"); - } - } - - @Override - public IMeasurementMNode getAsMeasurementMNode() { - if (isMeasurement()) { - return (IMeasurementMNode) this; - } else { - throw new UnsupportedOperationException("Wrong MNode Type"); - } - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - MNode mNode = (MNode) o; - if (fullPath == null) { - return Objects.equals(getFullPath(), mNode.getFullPath()); - } else { - return Objects.equals(fullPath, mNode.getFullPath()); - } - } - - @Override - public int hashCode() { - if (fullPath == null) { - return Objects.hash(getFullPath()); - } else { - return Objects.hash(fullPath); - } - } - - @Override - public String toString() { - return this.getName(); - } - - @Override - public CacheEntry getCacheEntry() { - return cacheEntry; - } - - @Override - public void setCacheEntry(CacheEntry cacheEntry) { - this.cacheEntry = cacheEntry; - } -} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/MNodeUtils.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/MNodeUtils.java deleted file mode 100644 index c5fed50dfd205..0000000000000 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/MNodeUtils.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * 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.metadata.mnode; - -public class MNodeUtils { - - /** - * When a measurement, represented by template or MeasurementMNode, is going to be added to a - * node, the node should be set to entity. Before invoking this method, lock the related MTree - * part first. - * - * @param node node to be transformed - * @return generated entityMNode - */ - public static IEntityMNode setToEntity(IMNode node) { - IEntityMNode entityMNode; - if (node.isEntity()) { - entityMNode = node.getAsEntityMNode(); - } else { - if (node.isStorageGroup()) { - entityMNode = - new StorageGroupEntityMNode( - node.getParent(), node.getName(), node.getAsStorageGroupMNode().getDataTTL()); - node.moveDataToNewMNode(entityMNode); - } else { - entityMNode = new EntityMNode(node.getParent(), node.getName()); - if (node.getParent() != null) { - node.getParent().replaceChild(node.getName(), entityMNode); - } else { - node.moveDataToNewMNode(entityMNode); - } - } - } - return entityMNode; - } - - /** - * When there's no measurement, represented by template or MeasurementMNode, is under this - * entityMNode, it should not act as entity anymore. Before invoking this method, lock related - * MTree structure first. - * - * @param entityMNode node to be transformed - * @return generated NoEntity node - */ - public static IMNode setToInternal(IEntityMNode entityMNode) { - IMNode node; - IMNode parent = entityMNode.getParent(); - if (entityMNode.isStorageGroup()) { - node = - new StorageGroupMNode( - parent, entityMNode.getName(), entityMNode.getAsStorageGroupMNode().getDataTTL()); - } else { - node = new InternalMNode(parent, entityMNode.getName()); - } - - if (parent != null) { - parent.replaceChild(entityMNode.getName(), node); - } - return node; - } -} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/MeasurementMNode.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/MeasurementMNode.java deleted file mode 100644 index 2151a170c023e..0000000000000 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/MeasurementMNode.java +++ /dev/null @@ -1,233 +0,0 @@ -/* - * 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.metadata.mnode; - -import org.apache.iotdb.commons.path.MeasurementPath; -import org.apache.iotdb.db.metadata.mnode.container.IMNodeContainer; -import org.apache.iotdb.db.metadata.mnode.container.MNodeContainers; -import org.apache.iotdb.db.metadata.mnode.visitor.MNodeVisitor; -import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; -import org.apache.iotdb.tsfile.write.schema.IMeasurementSchema; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class MeasurementMNode extends MNode implements IMeasurementMNode { - - private static final Logger logger = LoggerFactory.getLogger(MeasurementMNode.class); - - /** alias name of this measurement */ - protected String alias; - /** tag/attribute's start offset in tag file */ - private long offset = -1; - /** measurement's Schema for one timeseries represented by current leaf node */ - private IMeasurementSchema schema; - /** whether this measurement is pre deleted and considered in black list */ - private boolean preDeleted = false; - - /** - * MeasurementMNode factory method. The type of returned MeasurementMNode is according to the - * schema type. The default type is UnaryMeasurementMNode, which means if schema == null, an - * UnaryMeasurementMNode will return. - */ - public static IMeasurementMNode getMeasurementMNode( - IEntityMNode parent, String measurementName, IMeasurementSchema schema, String alias) { - return new MeasurementMNode(parent, measurementName, schema, alias); - } - - /** @param alias alias of measurementName */ - public MeasurementMNode(IMNode parent, String name, IMeasurementSchema schema, String alias) { - super(parent, name); - this.schema = schema; - this.alias = alias; - } - - @Override - public IEntityMNode getParent() { - if (parent == null) { - return null; - } - return parent.getAsEntityMNode(); - } - - /** - * get MeasurementPath of this node - * - * @return MeasurementPath - */ - @Override - public MeasurementPath getMeasurementPath() { - MeasurementPath result = new MeasurementPath(super.getPartialPath(), schema); - result.setUnderAlignedEntity(getParent().isAligned()); - return result; - } - - @Override - public IMeasurementSchema getSchema() { - return schema; - } - - /** - * get data type - * - * @param measurementId if it's a vector schema, we need sensor name of it - * @return measurement data type - */ - @Override - public TSDataType getDataType(String measurementId) { - return schema.getType(); - } - - @Override - public long getOffset() { - return offset; - } - - @Override - public void setOffset(long offset) { - this.offset = offset; - } - - @Override - public String getAlias() { - return alias; - } - - @Override - public void setAlias(String alias) { - this.alias = alias; - } - - @Override - public boolean isPreDeleted() { - return preDeleted; - } - - @Override - public void setPreDeleted(boolean preDeleted) { - this.preDeleted = preDeleted; - } - - @Override - public R accept(MNodeVisitor visitor, C context) { - return visitor.visitMeasurementMNode(this, context); - } - - @Override - public String getFullPath() { - if (fullPath != null) { - return fullPath; - } - return concatFullPath(); - } - - @Override - public boolean hasChild(String name) { - return false; - } - - @Override - public IMNode getChild(String name) { - MeasurementMNode.logger.warn( - "current node {} is a MeasurementMNode, can not get child {}", this.name, name); - throw new RuntimeException( - String.format( - "current node %s is a MeasurementMNode, can not get child %s", super.name, name)); - } - - @Override - public IMNode addChild(String name, IMNode child) { - // Do nothing - return null; - } - - @Override - public IMNode addChild(IMNode child) { - return null; - } - - @Override - public IMNode deleteChild(String name) { - return null; - } - - @Override - public void replaceChild(String oldChildName, IMNode newChildNode) {} - - @Override - public IMNodeContainer getChildren() { - return MNodeContainers.emptyMNodeContainer(); - } - - @Override - public void setChildren(IMNodeContainer children) { - // Do nothing - } - - @Override - public int getSchemaTemplateId() { - MeasurementMNode.logger.warn( - "current node {} is a MeasurementMNode, can not get Schema Template", name); - throw new UnsupportedOperationException( - String.format("current node %s is a MeasurementMNode, can not get Schema Template", name)); - } - - @Override - public int getSchemaTemplateIdWithState() { - MeasurementMNode.logger.warn( - "current node {} is a MeasurementMNode, can not get Schema Template", name); - throw new UnsupportedOperationException( - String.format("current node %s is a MeasurementMNode, can not get Schema Template", name)); - } - - @Override - public void setSchemaTemplateId(int schemaTemplateId) {} - - @Override - public void preUnsetSchemaTemplate() {} - - @Override - public void rollbackUnsetSchemaTemplate() {} - - @Override - public boolean isSchemaTemplatePreUnset() { - return false; - } - - @Override - public void unsetSchemaTemplate() {} - - @Override - public boolean isAboveDatabase() { - return false; - } - - @Override - public void setUseTemplate(boolean useTemplate) {} - - @Override - public boolean isMeasurement() { - return true; - } - - @Override - public MNodeType getMNodeType(Boolean isConfig) { - return MNodeType.MEASUREMENT; - } -} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/StorageGroupEntityMNode.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/StorageGroupEntityMNode.java deleted file mode 100644 index f68a5ddad3f02..0000000000000 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/StorageGroupEntityMNode.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * 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.metadata.mnode; - -import org.apache.iotdb.confignode.rpc.thrift.TDatabaseSchema; -import org.apache.iotdb.db.metadata.mnode.visitor.MNodeVisitor; - -public class StorageGroupEntityMNode extends EntityMNode implements IStorageGroupMNode { - /** - * when the data file in a database is older than dataTTL, it is considered invalid and will be - * eventually deleted. - */ - private long dataTTL; - - public StorageGroupEntityMNode(IMNode parent, String name, long dataTTL) { - super(parent, name); - this.dataTTL = dataTTL; - } - - @Override - public String getFullPath() { - if (fullPath == null) { - fullPath = concatFullPath().intern(); - } - return fullPath; - } - - @Override - public long getDataTTL() { - return dataTTL; - } - - @Override - public void setDataTTL(long dataTTL) { - this.dataTTL = dataTTL; - } - - @Override - public void setSchemaReplicationFactor(int schemaReplicationFactor) {} - - @Override - public void setDataReplicationFactor(int dataReplicationFactor) {} - - @Override - public void setTimePartitionInterval(long timePartitionInterval) {} - - @Override - public void setStorageGroupSchema(TDatabaseSchema schema) {} - - @Override - public TDatabaseSchema getStorageGroupSchema() { - return null; - } - - @Override - public void moveDataToNewMNode(IMNode newMNode) { - super.moveDataToNewMNode(newMNode); - } - - @Override - public boolean isStorageGroup() { - return true; - } - - @Override - public MNodeType getMNodeType(Boolean isConfig) { - return MNodeType.STORAGE_GROUP; - } - - @Override - public R accept(MNodeVisitor visitor, C context) { - return visitor.visitStorageGroupEntityMNode(this, context); - } -} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/StorageGroupMNode.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/StorageGroupMNode.java deleted file mode 100644 index 2cf4d75c75f90..0000000000000 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/StorageGroupMNode.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * 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.metadata.mnode; - -import org.apache.iotdb.confignode.rpc.thrift.TDatabaseSchema; -import org.apache.iotdb.db.metadata.mnode.visitor.MNodeVisitor; - -public class StorageGroupMNode extends InternalMNode implements IStorageGroupMNode { - - private static final long serialVersionUID = 7999036474525817732L; - - private TDatabaseSchema schema; - - public StorageGroupMNode(IMNode parent, String name) { - super(parent, name); - } - - // TODO: @yukun, remove this constructor - public StorageGroupMNode(IMNode parent, String name, long dataTTL) { - super(parent, name); - this.schema = new TDatabaseSchema(name).setTTL(dataTTL); - } - - @Override - public String getFullPath() { - if (fullPath == null) { - fullPath = concatFullPath().intern(); - } - return fullPath; - } - - @Override - public long getDataTTL() { - return schema.getTTL(); - } - - @Override - public void setDataTTL(long dataTTL) { - schema.setTTL(dataTTL); - } - - @Override - public void setSchemaReplicationFactor(int schemaReplicationFactor) { - schema.setSchemaReplicationFactor(schemaReplicationFactor); - } - - @Override - public void setDataReplicationFactor(int dataReplicationFactor) { - schema.setDataReplicationFactor(dataReplicationFactor); - } - - @Override - public void setTimePartitionInterval(long timePartitionInterval) { - schema.setTimePartitionInterval(timePartitionInterval); - } - - @Override - public void setStorageGroupSchema(TDatabaseSchema schema) { - this.schema = schema; - } - - @Override - public TDatabaseSchema getStorageGroupSchema() { - return schema; - } - - @Override - public void moveDataToNewMNode(IMNode newMNode) { - super.moveDataToNewMNode(newMNode); - } - - @Override - public boolean isStorageGroup() { - return true; - } - - @Override - public MNodeType getMNodeType(Boolean isConfig) { - return MNodeType.STORAGE_GROUP; - } - - @Override - public R accept(MNodeVisitor visitor, C context) { - return visitor.visitStorageGroupMNode(this, context); - } -} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/IStorageGroupMNode.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/config/IConfigMNode.java similarity index 60% rename from server/src/main/java/org/apache/iotdb/db/metadata/mnode/IStorageGroupMNode.java rename to server/src/main/java/org/apache/iotdb/db/metadata/mnode/config/IConfigMNode.java index 25d9e2f7b0e90..43fcd5bf318fc 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/IStorageGroupMNode.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/config/IConfigMNode.java @@ -16,24 +16,30 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.iotdb.db.metadata.mnode; +package org.apache.iotdb.db.metadata.mnode.config; +import org.apache.iotdb.commons.schema.node.IMNode; import org.apache.iotdb.confignode.rpc.thrift.TDatabaseSchema; -/** This interface defines a StorageGroupMNode's operation interfaces. */ -public interface IStorageGroupMNode extends IMNode { +public interface IConfigMNode extends IMNode { - long getDataTTL(); + void setSchemaTemplateId(int id); - void setDataTTL(long dataTTL); + int getSchemaTemplateId(); - void setSchemaReplicationFactor(int schemaReplicationFactor); + void preUnsetSchemaTemplate(); - void setDataReplicationFactor(int dataReplicationFactor); + void rollbackUnsetSchemaTemplate(); - void setTimePartitionInterval(long timePartitionInterval); + boolean isSchemaTemplatePreUnset(); - void setStorageGroupSchema(TDatabaseSchema schema); + void unsetSchemaTemplate(); - TDatabaseSchema getStorageGroupSchema(); + default void setDatabaseSchema(TDatabaseSchema schema) { + throw new UnsupportedOperationException(); + } + + default TDatabaseSchema getDatabaseSchema() { + throw new UnsupportedOperationException(); + } } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/config/basic/ConfigBasicMNode.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/config/basic/ConfigBasicMNode.java new file mode 100644 index 0000000000000..b1866e1c2120e --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/config/basic/ConfigBasicMNode.java @@ -0,0 +1,279 @@ +/* + * 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.metadata.mnode.config.basic; + +import org.apache.iotdb.commons.conf.IoTDBConstant; +import org.apache.iotdb.commons.path.PartialPath; +import org.apache.iotdb.commons.schema.node.MNodeType; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeContainer; +import org.apache.iotdb.commons.schema.node.visitor.MNodeVisitor; +import org.apache.iotdb.db.metadata.mnode.config.IConfigMNode; +import org.apache.iotdb.db.metadata.mnode.config.container.ConfigMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.config.info.ConfigMNodeInfo; + +import java.util.ArrayList; +import java.util.List; + +public class ConfigBasicMNode implements IConfigMNode { + + private static final long serialVersionUID = -770028375899514063L; + + private IConfigMNode parent; + private final ConfigMNodeInfo configMNodeInfo; + + /** from root to this node, only be set when used once for InternalMNode */ + private String fullPath; + + /** Constructor of MNode. */ + public ConfigBasicMNode(IConfigMNode parent, String name) { + this.parent = parent; + this.configMNodeInfo = new ConfigMNodeInfo(name); + } + + @Override + public String getName() { + return configMNodeInfo.getName(); + } + + @Override + public void setName(String name) { + configMNodeInfo.setName(name); + } + + @Override + public IConfigMNode getParent() { + return parent; + } + + @Override + public void setParent(IConfigMNode parent) { + this.parent = parent; + } + + @Override + public String getFullPath() { + if (fullPath == null) { + fullPath = concatFullPath(); + } + return fullPath; + } + + String concatFullPath() { + StringBuilder builder = new StringBuilder(getName()); + IConfigMNode curr = this; + while (curr.getParent() != null) { + curr = curr.getParent(); + builder.insert(0, IoTDBConstant.PATH_SEPARATOR).insert(0, curr.getName()); + } + return builder.toString(); + } + + @Override + public void setFullPath(String fullPath) { + this.fullPath = fullPath; + } + + @Override + public PartialPath getPartialPath() { + List detachedPath = new ArrayList<>(); + IConfigMNode temp = this; + detachedPath.add(temp.getName()); + while (temp.getParent() != null) { + temp = temp.getParent(); + detachedPath.add(0, temp.getName()); + } + return new PartialPath(detachedPath.toArray(new String[0])); + } + + /** check whether the MNode has a child with the name */ + @Override + public boolean hasChild(String name) { + return false; + } + + /** get the child with the name */ + @Override + public IConfigMNode getChild(String name) { + return null; + } + + /** + * add a child to current mnode + * + * @param name child's name + * @param child child's node + * @return the child of this node after addChild + */ + @Override + public IConfigMNode addChild(String name, IConfigMNode child) { + return null; + } + + /** + * Add a child to the current mnode. + * + *

This method will not take the child's name as one of the inputs and will also make this + * Mnode be child node's parent. All is to reduce the probability of mistaken by users and be more + * convenient for users to use. And the return of this method is used to conveniently construct a + * chain of time series for users. + * + * @param child child's node + * @return return the MNode already added + */ + @Override + public IConfigMNode addChild(IConfigMNode child) { + return null; + } + + /** delete a child */ + @Override + public IConfigMNode deleteChild(String name) { + return null; + } + + /** + * Replace a child of this mnode. New child's name must be the same as old child's name. + * + * @param oldChildName measurement name + * @param newChildNode new child node + */ + @Override + public synchronized void replaceChild(String oldChildName, IConfigMNode newChildNode) {} + + @Override + public void moveDataToNewMNode(IConfigMNode newMNode) { + newMNode.setParent(parent); + newMNode.setSchemaTemplateId(configMNodeInfo.getSchemaTemplateIdWithState()); + } + + @Override + public IMNodeContainer getChildren() { + return ConfigMNodeContainer.emptyMNodeContainer(); + } + + @Override + public void setChildren(IMNodeContainer children) {} + + @Override + public boolean isAboveDatabase() { + return false; + } + + @Override + public boolean isDatabase() { + return false; + } + + @Override + public boolean isDevice() { + return false; + } + + @Override + public boolean isMeasurement() { + return false; + } + + @Override + public MNodeType getMNodeType(Boolean isConfig) { + return isConfig ? MNodeType.SG_INTERNAL : MNodeType.INTERNAL; + } + + @Override + public IDatabaseMNode getAsDatabaseMNode() { + throw new UnsupportedOperationException("Wrong MNode Type"); + } + + @Override + public IDeviceMNode getAsDeviceMNode() { + throw new UnsupportedOperationException("Wrong MNode Type"); + } + + @Override + public IMeasurementMNode getAsMeasurementMNode() { + throw new UnsupportedOperationException("Wrong MNode Type"); + } + + @Override + public R accept(MNodeVisitor visitor, C context) { + return visitor.visitBasicMNode(this, context); + } + + @Override + public void setSchemaTemplateId(int id) { + configMNodeInfo.setSchemaTemplateId(id); + } + + @Override + public int getSchemaTemplateId() { + return configMNodeInfo.getSchemaTemplateId(); + } + + @Override + public void preUnsetSchemaTemplate() { + configMNodeInfo.preUnsetSchemaTemplate(); + } + + @Override + public void rollbackUnsetSchemaTemplate() { + configMNodeInfo.rollbackUnsetSchemaTemplate(); + } + + @Override + public boolean isSchemaTemplatePreUnset() { + return configMNodeInfo.isSchemaTemplatePreUnset(); + } + + @Override + public void unsetSchemaTemplate() { + configMNodeInfo.unsetSchemaTemplate(); + } + + /** + * The basic memory occupied by any ConfigBasicMNode object + * + *

    + *
  1. object header, 8B + *
  2. node attributes + *
      + *
    1. basicMNodeInfo reference, 8B + *
    2. parent reference, 8B + *
    3. fullPath reference, 8B + *
    + *
  3. MapEntry in parent + *
      + *
    1. key reference, 8B + *
    2. value reference, 8B + *
    3. entry size, see ConcurrentHashMap.Node, 28 + *
    + *
+ */ + @Override + public int estimateSize() { + return 8 + 8 + 8 + 8 + 8 + 8 + 28 + configMNodeInfo.estimateSize(); + } + + @Override + public IConfigMNode getAsMNode() { + return this; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/config/container/ConfigMNodeContainer.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/config/container/ConfigMNodeContainer.java new file mode 100644 index 0000000000000..52e190f907365 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/config/container/ConfigMNodeContainer.java @@ -0,0 +1,93 @@ +/* + * 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.metadata.mnode.config.container; + +import org.apache.iotdb.commons.schema.node.utils.IMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.config.IConfigMNode; + +import org.jetbrains.annotations.NotNull; + +import java.util.AbstractMap; +import java.util.Collection; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import static java.util.Collections.emptySet; + +public class ConfigMNodeContainer extends ConcurrentHashMap + implements IMNodeContainer { + + private static final IMNodeContainer EMPTY_CONTAINER = + new ConfigMNodeContainer.EmptyContainer(); + + public static IMNodeContainer emptyMNodeContainer() { + return EMPTY_CONTAINER; + } + + private static class EmptyContainer extends AbstractMap + implements IMNodeContainer { + + @Override + public int size() { + return 0; + } + + @Override + public boolean isEmpty() { + return true; + } + + @Override + public boolean containsKey(Object key) { + return false; + } + + @Override + public boolean containsValue(Object value) { + return false; + } + + @Override + public IConfigMNode get(Object key) { + return null; + } + + @Override + @NotNull + public Set keySet() { + return emptySet(); + } + + @Override + @NotNull + public Collection values() { + return emptySet(); + } + + @NotNull + public Set> entrySet() { + return emptySet(); + } + + @Override + public boolean equals(Object o) { + return o == this; + } + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/config/factory/ConfigMNodeFactory.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/config/factory/ConfigMNodeFactory.java new file mode 100644 index 0000000000000..dc1a03e36b033 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/config/factory/ConfigMNodeFactory.java @@ -0,0 +1,81 @@ +/* + * 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.metadata.mnode.config.factory; + +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; +import org.apache.iotdb.db.metadata.mnode.config.IConfigMNode; +import org.apache.iotdb.db.metadata.mnode.config.impl.ConfigBasicInternalMNode; +import org.apache.iotdb.db.metadata.mnode.config.impl.ConfigDatabaseMNode; +import org.apache.iotdb.tsfile.write.schema.IMeasurementSchema; + +public class ConfigMNodeFactory implements IMNodeFactory { + private ConfigMNodeFactory() {} + + private static class ConfigMNodeFactoryHolder { + private static final ConfigMNodeFactory INSTANCE = new ConfigMNodeFactory(); + + private ConfigMNodeFactoryHolder() {} + } + + public static ConfigMNodeFactory getInstance() { + return ConfigMNodeFactory.ConfigMNodeFactoryHolder.INSTANCE; + } + + @Override + public IMeasurementMNode createMeasurementMNode( + IDeviceMNode parent, String name, IMeasurementSchema schema, String alias) { + throw new UnsupportedOperationException(); + } + + @Override + public IDeviceMNode createDeviceMNode(IConfigMNode parent, String name) { + throw new UnsupportedOperationException(); + } + + @Override + public IDatabaseMNode createDatabaseMNode(IConfigMNode parent, String name) { + return new ConfigDatabaseMNode(parent, name); + } + + @Override + public IDatabaseMNode createDatabaseMNode( + IConfigMNode parent, String name, long dataTTL) { + IDatabaseMNode res = new ConfigDatabaseMNode(parent, name); + res.setDataTTL(dataTTL); + return res; + } + + @Override + public IConfigMNode createDatabaseDeviceMNode(IConfigMNode parent, String name, long dataTTL) { + throw new UnsupportedOperationException(); + } + + @Override + public IConfigMNode createAboveDatabaseMNode(IConfigMNode parent, String name) { + throw new UnsupportedOperationException(); + } + + @Override + public IConfigMNode createInternalMNode(IConfigMNode parent, String name) { + return new ConfigBasicInternalMNode(parent, name); + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/config/impl/ConfigBasicInternalMNode.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/config/impl/ConfigBasicInternalMNode.java new file mode 100644 index 0000000000000..daef341a6ee06 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/config/impl/ConfigBasicInternalMNode.java @@ -0,0 +1,181 @@ +/* + * 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.metadata.mnode.config.impl; + +import org.apache.iotdb.commons.schema.node.utils.IMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.config.IConfigMNode; +import org.apache.iotdb.db.metadata.mnode.config.basic.ConfigBasicMNode; +import org.apache.iotdb.db.metadata.mnode.config.container.ConfigMNodeContainer; + +/** + * This class is the implementation of Metadata Node. One MNode instance represents one node in the + * Metadata Tree + */ +public class ConfigBasicInternalMNode extends ConfigBasicMNode { + + /** + * suppress warnings reason: volatile for double synchronized check + * + *

This will be a ConcurrentHashMap instance + */ + @SuppressWarnings("squid:S3077") + private transient volatile IMNodeContainer children = null; + + /** Constructor of MNode. */ + public ConfigBasicInternalMNode(IConfigMNode parent, String name) { + super(parent, name); + } + + /** check whether the MNode has a child with the name */ + @Override + public boolean hasChild(String name) { + return (children != null && children.containsKey(name)); + } + + /** get the child with the name */ + @Override + public IConfigMNode getChild(String name) { + IConfigMNode child = null; + if (children != null) { + child = children.get(name); + } + return child; + } + + /** + * add a child to current mnode + * + * @param name child's name + * @param child child's node + * @return the child of this node after addChild + */ + @Override + public IConfigMNode addChild(String name, IConfigMNode child) { + /* use cpu time to exchange memory + * measurementNode's children should be null to save memory + * add child method will only be called when writing MTree, which is not a frequent operation + */ + if (children == null) { + // double check, children is volatile + synchronized (this) { + if (children == null) { + children = new ConfigMNodeContainer(); + } + } + } + child.setParent(this); + IConfigMNode existingChild = children.putIfAbsent(name, child); + return existingChild == null ? child : existingChild; + } + + /** + * Add a child to the current mnode. + * + *

This method will not take the child's name as one of the inputs and will also make this + * Mnode be child node's parent. All is to reduce the probability of mistaken by users and be more + * convenient for users to use. And the return of this method is used to conveniently construct a + * chain of time series for users. + * + * @param child child's node + * @return return the MNode already added + */ + @Override + public IConfigMNode addChild(IConfigMNode child) { + /* use cpu time to exchange memory + * measurementNode's children should be null to save memory + * add child method will only be called when writing MTree, which is not a frequent operation + */ + if (children == null) { + // double check, children is volatile + synchronized (this) { + if (children == null) { + children = new ConfigMNodeContainer(); + } + } + } + + child.setParent(this); + children.putIfAbsent(child.getName(), child); + return child; + } + + /** delete a child */ + @Override + public IConfigMNode deleteChild(String name) { + if (children != null) { + return children.remove(name); + } + return null; + } + + /** + * Replace a child of this mnode. New child's name must be the same as old child's name. + * + * @param oldChildName measurement name + * @param newChildNode new child node + */ + @Override + public synchronized void replaceChild(String oldChildName, IConfigMNode newChildNode) { + if (!oldChildName.equals(newChildNode.getName())) { + throw new RuntimeException("New child's name must be the same as old child's name!"); + } + IConfigMNode oldChildNode = this.getChild(oldChildName); + if (oldChildNode == null) { + return; + } + + oldChildNode.moveDataToNewMNode(newChildNode); + + children.replace(newChildNode.getName(), newChildNode); + } + + @Override + public void moveDataToNewMNode(IConfigMNode newMNode) { + super.moveDataToNewMNode(newMNode); + + if (children != null) { + newMNode.setChildren(children); + children.forEach((childName, childNode) -> childNode.setParent(newMNode)); + } + } + + @Override + public IMNodeContainer getChildren() { + if (children == null) { + return ConfigMNodeContainer.emptyMNodeContainer(); + } + return children; + } + + @Override + public void setChildren(IMNodeContainer children) { + this.children = children; + } + + /** MNodeContainer reference and basic occupation, 8 + 80B */ + @Override + public int estimateSize() { + return 8 + 80 + super.estimateSize(); + } + + @Override + public IConfigMNode getAsMNode() { + return this; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/config/impl/ConfigDatabaseMNode.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/config/impl/ConfigDatabaseMNode.java new file mode 100644 index 0000000000000..e2f425f2d39a0 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/config/impl/ConfigDatabaseMNode.java @@ -0,0 +1,80 @@ +/* + * 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.metadata.mnode.config.impl; + +import org.apache.iotdb.commons.schema.node.common.AbstractDatabaseMNode; +import org.apache.iotdb.confignode.rpc.thrift.TDatabaseSchema; +import org.apache.iotdb.db.metadata.mnode.config.IConfigMNode; +import org.apache.iotdb.db.metadata.mnode.config.basic.ConfigBasicMNode; +import org.apache.iotdb.db.metadata.mnode.config.info.ConfigDatabaseInfo; + +public class ConfigDatabaseMNode extends AbstractDatabaseMNode + implements IConfigMNode { + private final ConfigDatabaseInfo configDatabaseInfo; + + public ConfigDatabaseMNode(IConfigMNode parent, String name) { + super(new ConfigBasicInternalMNode(parent, name), new ConfigDatabaseInfo(name)); + this.configDatabaseInfo = (ConfigDatabaseInfo) getDatabaseInfo(); + } + + @Override + public void setSchemaTemplateId(int id) { + basicMNode.setSchemaTemplateId(id); + } + + @Override + public int getSchemaTemplateId() { + return basicMNode.getSchemaTemplateId(); + } + + @Override + public void preUnsetSchemaTemplate() { + basicMNode.preUnsetSchemaTemplate(); + } + + @Override + public void rollbackUnsetSchemaTemplate() { + basicMNode.rollbackUnsetSchemaTemplate(); + } + + @Override + public boolean isSchemaTemplatePreUnset() { + return basicMNode.isSchemaTemplatePreUnset(); + } + + @Override + public void unsetSchemaTemplate() { + basicMNode.unsetSchemaTemplate(); + } + + @Override + public void setDatabaseSchema(TDatabaseSchema schema) { + configDatabaseInfo.setSchema(schema); + } + + @Override + public TDatabaseSchema getDatabaseSchema() { + return configDatabaseInfo.getSchema(); + } + + @Override + public IConfigMNode getAsMNode() { + return this; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/config/info/ConfigDatabaseInfo.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/config/info/ConfigDatabaseInfo.java new file mode 100644 index 0000000000000..c07e40c69d1d4 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/config/info/ConfigDatabaseInfo.java @@ -0,0 +1,70 @@ +/* + * 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.metadata.mnode.config.info; + +import org.apache.iotdb.commons.schema.node.info.IDatabaseInfo; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; +import org.apache.iotdb.confignode.rpc.thrift.TDatabaseSchema; +import org.apache.iotdb.db.metadata.mnode.config.IConfigMNode; + +public class ConfigDatabaseInfo implements IDatabaseInfo { + + private TDatabaseSchema schema; + + public ConfigDatabaseInfo(String name) { + this.schema = new TDatabaseSchema(name); + } + + @Override + public void moveDataToNewMNode(IDatabaseMNode newMNode) { + newMNode.getAsMNode().setDatabaseSchema(schema); + } + + @Override + public long getDataTTL() { + return schema.getTTL(); + } + + @Override + public void setDataTTL(long dataTTL) { + schema.setTTL(dataTTL); + } + + public TDatabaseSchema getSchema() { + return schema; + } + + public void setSchema(TDatabaseSchema schema) { + this.schema = schema; + } + + /** + * The memory occupied by an DatabaseDeviceInfo based occupation + * + *

    + *
  1. object header, 8B + *
  2. reference schema, 8B + *
  3. object TDatabaseSchema, 112B (calculated by RamUsageEstimator) + *
+ */ + @Override + public int estimateSize() { + return 8 + 8 + 112; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/config/info/ConfigMNodeInfo.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/config/info/ConfigMNodeInfo.java new file mode 100644 index 0000000000000..26ac24eaf7608 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/config/info/ConfigMNodeInfo.java @@ -0,0 +1,82 @@ +/* + * 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.metadata.mnode.config.info; + +import org.apache.iotdb.db.metadata.mnode.mem.info.BasicMNodeInfo; + +import static org.apache.iotdb.db.metadata.MetadataConstant.NON_TEMPLATE; + +public class ConfigMNodeInfo extends BasicMNodeInfo { + /** + * This field is mainly used in cluster schema template features. In InternalMNode of ConfigMTree, + * this field represents the template set on this node. The normal usage value range is [0, + * Int.MaxValue], since this is implemented as auto inc id. The default value -1 means + * NON_TEMPLATE. This value will be set negative to implement some pre-delete features. + */ + protected int schemaTemplateId = NON_TEMPLATE; + + public ConfigMNodeInfo(String name) { + super(name); + } + + public void setSchemaTemplateId(int schemaTemplateId) { + this.schemaTemplateId = schemaTemplateId; + } + + /** + * In InternalMNode, schemaTemplateId represents the template set on this node. The pre unset + * mechanism is implemented by making this value negative. Since value 0 and -1 are all occupied, + * the available negative value range is [Int.MIN_VALUE, -2]. The value of a pre unset case equals + * the negative normal value minus 2. For example, if the id of set template is 0, then - 0 - 2 = + * -2 represents the pre unset operation of this template on this node. + */ + public int getSchemaTemplateId() { + return schemaTemplateId >= -1 ? schemaTemplateId : -schemaTemplateId - 2; + } + + public int getSchemaTemplateIdWithState() { + return schemaTemplateId; + } + + public void preUnsetSchemaTemplate() { + if (this.schemaTemplateId > -1) { + this.schemaTemplateId = -schemaTemplateId - 2; + } + } + + public void rollbackUnsetSchemaTemplate() { + if (schemaTemplateId < -1) { + schemaTemplateId = -schemaTemplateId - 2; + } + } + + public boolean isSchemaTemplatePreUnset() { + return schemaTemplateId < -1; + } + + public void unsetSchemaTemplate() { + this.schemaTemplateId = -1; + } + + @Override + public int estimateSize() { + // int schemaTemplateId, 4B + return super.estimateSize() + 4; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/estimator/BasicMNodSizeEstimator.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/estimator/BasicMNodSizeEstimator.java deleted file mode 100644 index ff695e753bdee..0000000000000 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/estimator/BasicMNodSizeEstimator.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * 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.metadata.mnode.estimator; - -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; - -public class BasicMNodSizeEstimator implements IMNodeSizeEstimator { - /** - * The basic memory occupied by any MNode object - * - *
    - *
  1. object header, 8B - *
  2. node attributes - *
      - *
    1. name reference, name length and name hash code, 8 + 4 + 4 = 16B - *
    2. parent reference, 8B - *
    3. fullPath reference, 8B - *
    4. cacheEntry reference, 8B - *
    - *
  3. MapEntry in parent - *
      - *
    1. key reference, 8B - *
    2. value reference, 8B - *
    3. entry size, see ConcurrentHashMap.Node, 28 - *
    - *
- */ - protected static final int NODE_BASE_SIZE = 92; - - /** - * The basic extra memory occupied by an InternalMNode based on MNode occupation - * - *
    - *
  1. template reference, 8B - *
  2. boolean useTemplate, 1B - *
  3. MNodeContainer reference and basic occupation, 8 + 80B - *
- */ - protected static final int INTERNAL_NODE_BASE_SIZE = 97; - - /** - * The basic extra memory occupied by an StorageGroupMNode based on InternalMNode occupation - * - *
    - *
  1. dataTTL, 8B - *
  2. fullPath length and hashCode, 4 + 4 = 8B - *
- */ - protected static final int STORAGE_GROUP_NODE_BASE_SIZE = 16; - - /** - * The basic extra memory occupied by an EntityMNode based on InternalMNode occupation - * - *
    - *
  1. isAligned, 1B - *
  2. aliasChildren reference, 8B - *
- */ - protected static final int ENTITY_NODE_BASE_SIZE = 9; - - /** - * The basic extra memory occupied by an MeasurementMNode based on MNode occupation - * - *
    - *
  1. alias reference, 8B - *
  2. tagOffset, 8B - *
  3. estimated schema size, 32B - *
- */ - protected static final int MEASUREMENT_NODE_BASE_SIZE = 48; - // alias length, hashCode and occupation in aliasMap, 4 + 4 + 44 = 52B - protected static final int ALIAS_BASE_SIZE = 52; - - public int estimateSize(IMNode node) { - int size = NODE_BASE_SIZE + node.getName().length(); - if (node.isMeasurement()) { - size += MEASUREMENT_NODE_BASE_SIZE; - IMeasurementMNode measurementMNode = node.getAsMeasurementMNode(); - if (measurementMNode.getAlias() != null) { - size += ALIAS_BASE_SIZE + measurementMNode.getAlias().length(); - } - } else { - size += INTERNAL_NODE_BASE_SIZE; - if (node.isStorageGroup()) { - size += STORAGE_GROUP_NODE_BASE_SIZE; - size += node.getAsStorageGroupMNode().getFullPath().length(); - } - - if (node.isEntity()) { - size += ENTITY_NODE_BASE_SIZE; - } - } - - return size; - } -} diff --git a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/multimode/MTreePartialMemoryTest.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/IMemMNode.java similarity index 72% rename from server/src/test/java/org/apache/iotdb/db/metadata/mtree/multimode/MTreePartialMemoryTest.java rename to server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/IMemMNode.java index d15840c280506..68ef9b86056a8 100644 --- a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/multimode/MTreePartialMemoryTest.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/IMemMNode.java @@ -16,13 +16,8 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.iotdb.db.metadata.mtree.multimode; +package org.apache.iotdb.db.metadata.mnode.mem; -import org.apache.iotdb.db.conf.IoTDBDescriptor; +import org.apache.iotdb.commons.schema.node.IMNode; -public class MTreePartialMemoryTest extends MTreeDiskModeTest { - @Override - protected void setMemSize() { - IoTDBDescriptor.getInstance().getConfig().setCachedMNodeSizeInSchemaFileMode(3); - } -} +public interface IMemMNode extends IMNode {} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/basic/BasicMNode.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/basic/BasicMNode.java new file mode 100644 index 0000000000000..dfd23aa44184b --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/basic/BasicMNode.java @@ -0,0 +1,252 @@ +/* + * 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.metadata.mnode.mem.basic; + +import org.apache.iotdb.commons.conf.IoTDBConstant; +import org.apache.iotdb.commons.path.PartialPath; +import org.apache.iotdb.commons.schema.node.MNodeType; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeContainer; +import org.apache.iotdb.commons.schema.node.visitor.MNodeVisitor; +import org.apache.iotdb.db.metadata.mnode.mem.IMemMNode; +import org.apache.iotdb.db.metadata.mnode.mem.container.MemMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.mem.info.BasicMNodeInfo; + +import java.util.ArrayList; +import java.util.List; + +/** + * This class is the implementation of Metadata Node. One MNode instance represents one node in the + * Metadata Tree + */ +public class BasicMNode implements IMemMNode { + + private static final long serialVersionUID = -770028375899514063L; + + private IMemMNode parent; + private final BasicMNodeInfo basicMNodeInfo; + + /** from root to this node, only be set when used once for InternalMNode */ + private String fullPath; + + /** Constructor of MNode. */ + public BasicMNode(IMemMNode parent, String name) { + this.parent = parent; + this.basicMNodeInfo = new BasicMNodeInfo(name); + } + + @Override + public String getName() { + return basicMNodeInfo.getName(); + } + + @Override + public void setName(String name) { + basicMNodeInfo.setName(name); + } + + @Override + public IMemMNode getParent() { + return parent; + } + + @Override + public void setParent(IMemMNode parent) { + this.parent = parent; + } + + @Override + public String getFullPath() { + if (fullPath == null) { + fullPath = concatFullPath(); + } + return fullPath; + } + + String concatFullPath() { + StringBuilder builder = new StringBuilder(getName()); + IMemMNode curr = this; + while (curr.getParent() != null) { + curr = curr.getParent(); + builder.insert(0, IoTDBConstant.PATH_SEPARATOR).insert(0, curr.getName()); + } + return builder.toString(); + } + + @Override + public void setFullPath(String fullPath) { + this.fullPath = fullPath; + } + + @Override + public PartialPath getPartialPath() { + List detachedPath = new ArrayList<>(); + IMemMNode temp = this; + detachedPath.add(temp.getName()); + while (temp.getParent() != null) { + temp = temp.getParent(); + detachedPath.add(0, temp.getName()); + } + return new PartialPath(detachedPath.toArray(new String[0])); + } + + /** check whether the MNode has a child with the name */ + @Override + public boolean hasChild(String name) { + return false; + } + + /** get the child with the name */ + @Override + public IMemMNode getChild(String name) { + return null; + } + + /** + * add a child to current mnode + * + * @param name child's name + * @param child child's node + * @return the child of this node after addChild + */ + @Override + public IMemMNode addChild(String name, IMemMNode child) { + return null; + } + + /** + * Add a child to the current mnode. + * + *

This method will not take the child's name as one of the inputs and will also make this + * Mnode be child node's parent. All is to reduce the probability of mistaken by users and be more + * convenient for users to use. And the return of this method is used to conveniently construct a + * chain of time series for users. + * + * @param child child's node + * @return return the MNode already added + */ + @Override + public IMemMNode addChild(IMemMNode child) { + return null; + } + + /** delete a child */ + @Override + public IMemMNode deleteChild(String name) { + return null; + } + + /** + * Replace a child of this mnode. New child's name must be the same as old child's name. + * + * @param oldChildName measurement name + * @param newChildNode new child node + */ + @Override + public synchronized void replaceChild(String oldChildName, IMemMNode newChildNode) {} + + @Override + public void moveDataToNewMNode(IMemMNode newMNode) { + newMNode.setParent(parent); + } + + @Override + public IMNodeContainer getChildren() { + return MemMNodeContainer.emptyMNodeContainer(); + } + + @Override + public void setChildren(IMNodeContainer children) {} + + @Override + public boolean isAboveDatabase() { + return false; + } + + @Override + public boolean isDatabase() { + return false; + } + + @Override + public boolean isDevice() { + return false; + } + + @Override + public boolean isMeasurement() { + return false; + } + + @Override + public MNodeType getMNodeType(Boolean isConfig) { + return isConfig ? MNodeType.SG_INTERNAL : MNodeType.INTERNAL; + } + + @Override + public IDatabaseMNode getAsDatabaseMNode() { + throw new UnsupportedOperationException("Wrong MNode Type"); + } + + @Override + public IDeviceMNode getAsDeviceMNode() { + throw new UnsupportedOperationException("Wrong MNode Type"); + } + + @Override + public IMeasurementMNode getAsMeasurementMNode() { + throw new UnsupportedOperationException("Wrong MNode Type"); + } + + @Override + public R accept(MNodeVisitor visitor, C context) { + return visitor.visitBasicMNode(this, context); + } + + /** + * The basic memory occupied by any BasicMNode object + * + *

    + *
  1. object header, 8B + *
  2. node attributes + *
      + *
    1. basicMNodeInfo reference, 8B + *
    2. parent reference, 8B + *
    3. fullPath reference, 8B + *
    + *
  3. MapEntry in parent + *
      + *
    1. key reference, 8B + *
    2. value reference, 8B + *
    3. entry size, see ConcurrentHashMap.Node, 28 + *
    + *
+ */ + @Override + public int estimateSize() { + return 8 + 8 + 8 + 8 + 8 + 8 + 28 + basicMNodeInfo.estimateSize(); + } + + @Override + public IMemMNode getAsMNode() { + return this; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/container/MNodeContainers.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/container/MemMNodeContainer.java similarity index 60% rename from server/src/main/java/org/apache/iotdb/db/metadata/mnode/container/MNodeContainers.java rename to server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/container/MemMNodeContainer.java index ad473edac0399..813793383fc41 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/container/MNodeContainers.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/container/MemMNodeContainer.java @@ -16,12 +16,10 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.iotdb.db.metadata.mnode.container; +package org.apache.iotdb.db.metadata.mnode.mem.container; -import org.apache.iotdb.db.conf.IoTDBDescriptor; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mtree.store.disk.CachedMNodeContainer; -import org.apache.iotdb.db.metadata.schemaregion.SchemaEngineMode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.mem.IMemMNode; import org.jetbrains.annotations.NotNull; @@ -29,36 +27,22 @@ import java.util.Collection; import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import static java.util.Collections.emptySet; -public class MNodeContainers { +public class MemMNodeContainer extends ConcurrentHashMap + implements IMNodeContainer { - @SuppressWarnings("rawtypes") - private static final IMNodeContainer EMPTY_CONTAINER = new EmptyContainer(); + private static final IMNodeContainer EMPTY_CONTAINER = + new MemMNodeContainer.EmptyContainer(); - @SuppressWarnings("unchecked") - public static IMNodeContainer emptyMNodeContainer() { + public static IMNodeContainer emptyMNodeContainer() { return EMPTY_CONTAINER; } - public static boolean isEmptyContainer(IMNodeContainer container) { - return EMPTY_CONTAINER.equals(container); - } - - public static IMNodeContainer getNewMNodeContainer() { - if (IoTDBDescriptor.getInstance() - .getConfig() - .getSchemaEngineMode() - .equals(SchemaEngineMode.Schema_File.toString())) { - return new CachedMNodeContainer(); - } else { - return new MNodeContainerMapImpl(); - } - } - - private static class EmptyContainer extends AbstractMap - implements IMNodeContainer { + private static class EmptyContainer extends AbstractMap + implements IMNodeContainer { @Override public int size() { @@ -81,7 +65,7 @@ public boolean containsValue(Object value) { } @Override - public IMNode get(Object key) { + public IMemMNode get(Object key) { return null; } @@ -93,12 +77,12 @@ public Set keySet() { @Override @NotNull - public Collection values() { + public Collection values() { return emptySet(); } @NotNull - public Set> entrySet() { + public Set> entrySet() { return emptySet(); } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/container/MNodeContainerMapImpl.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/estimator/MNodeSizeEstimator.java similarity index 75% rename from server/src/main/java/org/apache/iotdb/db/metadata/mnode/container/MNodeContainerMapImpl.java rename to server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/estimator/MNodeSizeEstimator.java index a7a043b6deb8d..37f6201fb61ac 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/container/MNodeContainerMapImpl.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/estimator/MNodeSizeEstimator.java @@ -16,11 +16,13 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.iotdb.db.metadata.mnode.container; -import org.apache.iotdb.db.metadata.mnode.IMNode; +package org.apache.iotdb.db.metadata.mnode.mem.estimator; -import java.util.concurrent.ConcurrentHashMap; +public class MNodeSizeEstimator { + private static int ALIAS_BASE_SIZE = 52; -public class MNodeContainerMapImpl extends ConcurrentHashMap - implements IMNodeContainer {} + public static int getAliasBaseSize() { + return ALIAS_BASE_SIZE; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/factory/MemMNodeFactory.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/factory/MemMNodeFactory.java new file mode 100644 index 0000000000000..24215c1e15096 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/factory/MemMNodeFactory.java @@ -0,0 +1,84 @@ +/* + * 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.metadata.mnode.mem.factory; + +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; +import org.apache.iotdb.db.metadata.mnode.mem.IMemMNode; +import org.apache.iotdb.db.metadata.mnode.mem.impl.AboveDatabaseMNode; +import org.apache.iotdb.db.metadata.mnode.mem.impl.BasicInternalMNode; +import org.apache.iotdb.db.metadata.mnode.mem.impl.DatabaseDeviceMNode; +import org.apache.iotdb.db.metadata.mnode.mem.impl.DatabaseMNode; +import org.apache.iotdb.db.metadata.mnode.mem.impl.DeviceMNode; +import org.apache.iotdb.db.metadata.mnode.mem.impl.MeasurementMNode; +import org.apache.iotdb.tsfile.write.schema.IMeasurementSchema; + +public class MemMNodeFactory implements IMNodeFactory { + + private MemMNodeFactory() {} + + private static class MemMNodeFactoryHolder { + private static final MemMNodeFactory INSTANCE = new MemMNodeFactory(); + + private MemMNodeFactoryHolder() {} + } + + public static MemMNodeFactory getInstance() { + return MemMNodeFactory.MemMNodeFactoryHolder.INSTANCE; + } + + @Override + public IMeasurementMNode createMeasurementMNode( + IDeviceMNode parent, String name, IMeasurementSchema schema, String alias) { + return new MeasurementMNode(parent, name, schema, alias); + } + + @Override + public IDeviceMNode createDeviceMNode(IMemMNode parent, String name) { + return new DeviceMNode(parent, name); + } + + @Override + public IDatabaseMNode createDatabaseMNode(IMemMNode parent, String name) { + return new DatabaseMNode(parent, name); + } + + @Override + public IDatabaseMNode createDatabaseMNode( + IMemMNode parent, String name, long dataTTL) { + return new DatabaseMNode(parent, name, dataTTL); + } + + @Override + public IMemMNode createDatabaseDeviceMNode(IMemMNode parent, String name, long dataTTL) { + return new DatabaseDeviceMNode(parent, name, dataTTL); + } + + @Override + public IMemMNode createAboveDatabaseMNode(IMemMNode parent, String name) { + return new AboveDatabaseMNode(parent, name); + } + + @Override + public IMemMNode createInternalMNode(IMemMNode parent, String name) { + return new BasicInternalMNode(parent, name); + } +} diff --git a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/multimode/MTreeMemoryModeTest.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/impl/AboveDatabaseMNode.java similarity index 60% rename from server/src/test/java/org/apache/iotdb/db/metadata/mtree/multimode/MTreeMemoryModeTest.java rename to server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/impl/AboveDatabaseMNode.java index 0156e6c6f0eb5..46c47cd530566 100644 --- a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/multimode/MTreeMemoryModeTest.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/impl/AboveDatabaseMNode.java @@ -16,20 +16,20 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.iotdb.db.metadata.mtree.multimode; +package org.apache.iotdb.db.metadata.mnode.mem.impl; -import org.apache.iotdb.db.conf.IoTDBDescriptor; -import org.apache.iotdb.db.metadata.mtree.MTreeBelowSGTest; -import org.apache.iotdb.db.metadata.schemaregion.SchemaEngineMode; +import org.apache.iotdb.commons.schema.node.common.AbstractAboveDatabaseMNode; +import org.apache.iotdb.db.metadata.mnode.mem.IMemMNode; +import org.apache.iotdb.db.metadata.mnode.mem.basic.BasicMNode; -public class MTreeMemoryModeTest extends MTreeBelowSGTest { - @Override - protected void setConfig() { - IoTDBDescriptor.getInstance() - .getConfig() - .setSchemaEngineMode(SchemaEngineMode.Memory.toString()); +public class AboveDatabaseMNode extends AbstractAboveDatabaseMNode + implements IMemMNode { + public AboveDatabaseMNode(IMemMNode parent, String name) { + super(new BasicInternalMNode(parent, name)); } @Override - protected void rollBackConfig() {} + public IMemMNode getAsMNode() { + return this; + } } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/impl/BasicInternalMNode.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/impl/BasicInternalMNode.java new file mode 100644 index 0000000000000..b790fbf6c5f88 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/impl/BasicInternalMNode.java @@ -0,0 +1,181 @@ +/* + * 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.metadata.mnode.mem.impl; + +import org.apache.iotdb.commons.schema.node.utils.IMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.mem.IMemMNode; +import org.apache.iotdb.db.metadata.mnode.mem.basic.BasicMNode; +import org.apache.iotdb.db.metadata.mnode.mem.container.MemMNodeContainer; + +/** + * This class is the implementation of Metadata Node. One MNode instance represents one node in the + * Metadata Tree + */ +public class BasicInternalMNode extends BasicMNode { + + /** + * suppress warnings reason: volatile for double synchronized check + * + *

This will be a ConcurrentHashMap instance + */ + @SuppressWarnings("squid:S3077") + private transient volatile IMNodeContainer children = null; + + /** Constructor of MNode. */ + public BasicInternalMNode(IMemMNode parent, String name) { + super(parent, name); + } + + /** check whether the MNode has a child with the name */ + @Override + public boolean hasChild(String name) { + return (children != null && children.containsKey(name)); + } + + /** get the child with the name */ + @Override + public IMemMNode getChild(String name) { + IMemMNode child = null; + if (children != null) { + child = children.get(name); + } + return child; + } + + /** + * add a child to current mnode + * + * @param name child's name + * @param child child's node + * @return the child of this node after addChild + */ + @Override + public IMemMNode addChild(String name, IMemMNode child) { + /* use cpu time to exchange memory + * measurementNode's children should be null to save memory + * add child method will only be called when writing MTree, which is not a frequent operation + */ + if (children == null) { + // double check, children is volatile + synchronized (this) { + if (children == null) { + children = new MemMNodeContainer(); + } + } + } + child.setParent(this); + IMemMNode existingChild = children.putIfAbsent(name, child); + return existingChild == null ? child : existingChild; + } + + /** + * Add a child to the current mnode. + * + *

This method will not take the child's name as one of the inputs and will also make this + * Mnode be child node's parent. All is to reduce the probability of mistaken by users and be more + * convenient for users to use. And the return of this method is used to conveniently construct a + * chain of time series for users. + * + * @param child child's node + * @return return the MNode already added + */ + @Override + public IMemMNode addChild(IMemMNode child) { + /* use cpu time to exchange memory + * measurementNode's children should be null to save memory + * add child method will only be called when writing MTree, which is not a frequent operation + */ + if (children == null) { + // double check, children is volatile + synchronized (this) { + if (children == null) { + children = new MemMNodeContainer(); + } + } + } + + child.setParent(this); + children.putIfAbsent(child.getName(), child); + return child; + } + + /** delete a child */ + @Override + public IMemMNode deleteChild(String name) { + if (children != null) { + return children.remove(name); + } + return null; + } + + /** + * Replace a child of this mnode. New child's name must be the same as old child's name. + * + * @param oldChildName measurement name + * @param newChildNode new child node + */ + @Override + public synchronized void replaceChild(String oldChildName, IMemMNode newChildNode) { + if (!oldChildName.equals(newChildNode.getName())) { + throw new RuntimeException("New child's name must be the same as old child's name!"); + } + IMemMNode oldChildNode = this.getChild(oldChildName); + if (oldChildNode == null) { + return; + } + + oldChildNode.moveDataToNewMNode(newChildNode); + + children.replace(newChildNode.getName(), newChildNode); + } + + @Override + public void moveDataToNewMNode(IMemMNode newMNode) { + super.moveDataToNewMNode(newMNode); + + if (children != null) { + newMNode.setChildren(children); + children.forEach((childName, childNode) -> childNode.setParent(newMNode)); + } + } + + @Override + public IMNodeContainer getChildren() { + if (children == null) { + return MemMNodeContainer.emptyMNodeContainer(); + } + return children; + } + + @Override + public void setChildren(IMNodeContainer children) { + this.children = children; + } + + /** MNodeContainer reference and basic occupation, 8 + 80B */ + @Override + public int estimateSize() { + return 8 + 80 + super.estimateSize(); + } + + @Override + public IMemMNode getAsMNode() { + return this; + } +} diff --git a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/multimode/MTreeDiskModeTest.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/impl/DatabaseDeviceMNode.java similarity index 55% rename from server/src/test/java/org/apache/iotdb/db/metadata/mtree/multimode/MTreeDiskModeTest.java rename to server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/impl/DatabaseDeviceMNode.java index 7629d8de12440..c21ded3b82e2b 100644 --- a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/multimode/MTreeDiskModeTest.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/impl/DatabaseDeviceMNode.java @@ -16,27 +16,22 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.iotdb.db.metadata.mtree.multimode; +package org.apache.iotdb.db.metadata.mnode.mem.impl; -import org.apache.iotdb.db.conf.IoTDBDescriptor; -import org.apache.iotdb.db.metadata.mtree.MTreeBelowSGTest; -import org.apache.iotdb.db.metadata.schemaregion.SchemaEngineMode; +import org.apache.iotdb.commons.schema.node.common.AbstractDatabaseDeviceMNode; +import org.apache.iotdb.db.metadata.mnode.mem.IMemMNode; +import org.apache.iotdb.db.metadata.mnode.mem.basic.BasicMNode; +import org.apache.iotdb.db.metadata.mnode.mem.info.DatabaseDeviceInfo; -public abstract class MTreeDiskModeTest extends MTreeBelowSGTest { - @Override - protected void setConfig() { - IoTDBDescriptor.getInstance() - .getConfig() - .setSchemaEngineMode(SchemaEngineMode.Schema_File.toString()); - setMemSize(); +public class DatabaseDeviceMNode extends AbstractDatabaseDeviceMNode + implements IMemMNode { + public DatabaseDeviceMNode(IMemMNode parent, String name, long dataTTL) { + super(new BasicInternalMNode(parent, name), new DatabaseDeviceInfo<>()); + setDataTTL(dataTTL); } @Override - protected void rollBackConfig() { - IoTDBDescriptor.getInstance() - .getConfig() - .setSchemaEngineMode(SchemaEngineMode.Memory.toString()); + public IMemMNode getAsMNode() { + return this; } - - protected abstract void setMemSize(); } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/impl/DatabaseMNode.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/impl/DatabaseMNode.java new file mode 100644 index 0000000000000..6f43d665f4ab6 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/impl/DatabaseMNode.java @@ -0,0 +1,43 @@ +/* + * 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.metadata.mnode.mem.impl; + +import org.apache.iotdb.commons.schema.node.common.AbstractDatabaseMNode; +import org.apache.iotdb.db.metadata.mnode.mem.IMemMNode; +import org.apache.iotdb.db.metadata.mnode.mem.basic.BasicMNode; +import org.apache.iotdb.db.metadata.mnode.mem.info.DatabaseInfo; + +public class DatabaseMNode extends AbstractDatabaseMNode + implements IMemMNode { + + public DatabaseMNode(IMemMNode parent, String name) { + super(new BasicInternalMNode(parent, name), new DatabaseInfo<>()); + } + + // TODO: @yukun, remove this constructor + public DatabaseMNode(IMemMNode parent, String name, long dataTTL) { + this(parent, name); + setDataTTL(dataTTL); + } + + @Override + public IMemMNode getAsMNode() { + return this; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/impl/DeviceMNode.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/impl/DeviceMNode.java new file mode 100644 index 0000000000000..9915b8a49960e --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/impl/DeviceMNode.java @@ -0,0 +1,36 @@ +/* + * 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.metadata.mnode.mem.impl; + +import org.apache.iotdb.commons.schema.node.common.AbstractDeviceMNode; +import org.apache.iotdb.db.metadata.mnode.mem.IMemMNode; +import org.apache.iotdb.db.metadata.mnode.mem.basic.BasicMNode; +import org.apache.iotdb.db.metadata.mnode.mem.info.DeviceInfo; + +public class DeviceMNode extends AbstractDeviceMNode implements IMemMNode { + + public DeviceMNode(IMemMNode parent, String name) { + super(new BasicInternalMNode(parent, name), new DeviceInfo<>()); + } + + @Override + public IMemMNode getAsMNode() { + return this; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/impl/MeasurementMNode.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/impl/MeasurementMNode.java new file mode 100644 index 0000000000000..79e66a366b7c9 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/impl/MeasurementMNode.java @@ -0,0 +1,49 @@ +/* + * 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.metadata.mnode.mem.impl; + +import org.apache.iotdb.commons.schema.node.common.AbstractMeasurementMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.mem.IMemMNode; +import org.apache.iotdb.db.metadata.mnode.mem.basic.BasicMNode; +import org.apache.iotdb.db.metadata.mnode.mem.container.MemMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.mem.info.MeasurementInfo; +import org.apache.iotdb.tsfile.write.schema.IMeasurementSchema; + +public class MeasurementMNode extends AbstractMeasurementMNode + implements IMemMNode { + + public MeasurementMNode( + IDeviceMNode parent, String name, IMeasurementSchema schema, String alias) { + super( + new BasicMNode(parent == null ? null : parent.getAsMNode(), name), + new MeasurementInfo(schema, alias)); + } + + @Override + public IMemMNode getAsMNode() { + return this; + } + + @Override + public IMNodeContainer getChildren() { + return MemMNodeContainer.emptyMNodeContainer(); + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/AboveDatabaseMNode.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/info/BasicMNodeInfo.java similarity index 65% rename from server/src/main/java/org/apache/iotdb/db/metadata/mnode/AboveDatabaseMNode.java rename to server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/info/BasicMNodeInfo.java index 9b2251d40f658..49deac759ae4b 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/AboveDatabaseMNode.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/info/BasicMNodeInfo.java @@ -16,22 +16,26 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.iotdb.db.metadata.mnode; +package org.apache.iotdb.db.metadata.mnode.mem.info; -/** Used to fill the link list of MNode above database in IMTreeBelowSG */ -public class AboveDatabaseMNode extends InternalMNode { - /** - * Constructor of MNode. - * - * @param parent - * @param name - */ - public AboveDatabaseMNode(IMNode parent, String name) { - super(parent, name); +public class BasicMNodeInfo { + private String name; + + public BasicMNodeInfo(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; } - @Override - public boolean isAboveDatabase() { - return true; + public int estimateSize() { + // object header, 8B + // name reference, name length and name hash code, 8 + 4 + 4 = 16B + return 8 + 16; } } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/info/DatabaseDeviceInfo.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/info/DatabaseDeviceInfo.java new file mode 100644 index 0000000000000..587436dba70fb --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/info/DatabaseDeviceInfo.java @@ -0,0 +1,59 @@ +/* + * 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.metadata.mnode.mem.info; + +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.info.IDatabaseDeviceInfo; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; + +public class DatabaseDeviceInfo> extends DeviceInfo + implements IDatabaseDeviceInfo { + /** + * when the data file in a database is older than dataTTL, it is considered invalid and will be + * eventually deleted. + */ + private long dataTTL; + + @Override + public void moveDataToNewMNode(IDatabaseMNode newMNode) { + newMNode.setDataTTL(dataTTL); + } + + @Override + public long getDataTTL() { + return dataTTL; + } + + @Override + public void setDataTTL(long dataTTL) { + this.dataTTL = dataTTL; + } + + /** + * The memory occupied by an DatabaseDeviceInfo based occupation + * + *

    + *
  1. long dataTTL, 8B + *
+ */ + @Override + public int estimateSize() { + return super.estimateSize() + 8; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/info/DatabaseInfo.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/info/DatabaseInfo.java new file mode 100644 index 0000000000000..02ba001888040 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/info/DatabaseInfo.java @@ -0,0 +1,58 @@ +/* + * 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.metadata.mnode.mem.info; + +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.info.IDatabaseInfo; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; + +public class DatabaseInfo> implements IDatabaseInfo { + + private long dataTTL; + + public DatabaseInfo() {} + + @Override + public void moveDataToNewMNode(IDatabaseMNode newMNode) { + newMNode.setDataTTL(dataTTL); + } + + @Override + public long getDataTTL() { + return dataTTL; + } + + @Override + public void setDataTTL(long dataTTL) { + this.dataTTL = dataTTL; + } + + /** + * The memory occupied by an DatabaseDeviceInfo based occupation + * + *
    + *
  1. object header, 8B + *
  2. long dataTTL, 8B + *
+ */ + @Override + public int estimateSize() { + return 8 + 8; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/EntityMNode.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/info/DeviceInfo.java similarity index 57% rename from server/src/main/java/org/apache/iotdb/db/metadata/mnode/EntityMNode.java rename to server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/info/DeviceInfo.java index 31d8112051e45..34182996465e2 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/EntityMNode.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/info/DeviceInfo.java @@ -16,67 +16,52 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.iotdb.db.metadata.mnode; +package org.apache.iotdb.db.metadata.mnode.mem.info; -import org.apache.iotdb.db.metadata.mnode.visitor.MNodeVisitor; +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.info.IDeviceInfo; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; import java.util.Collections; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -public class EntityMNode extends InternalMNode implements IEntityMNode { +import static org.apache.iotdb.db.metadata.MetadataConstant.NON_TEMPLATE; + +public class DeviceInfo> implements IDeviceInfo { /** - * suppress warnings reason: volatile for double synchronized check - * - *

This will be a ConcurrentHashMap instance + * In EntityMNode of MTree in SchemaRegion, this field represents the template activated on this + * node. The normal usage value range is [0, Int.MaxValue], since this is implemented as auto inc + * id. The default value -1 means NON_TEMPLATE. This value will be set negative to implement some + * pre-delete features. */ - @SuppressWarnings("squid:S3077") - private transient volatile Map aliasChildren = null; + protected int schemaTemplateId = NON_TEMPLATE; - private volatile boolean isAligned = false; - - @Override - public String getFullPath() { - if (fullPath == null) { - fullPath = concatFullPath().intern(); - } - return fullPath; - } + private volatile boolean useTemplate = false; /** - * Constructor of MNode. + * suppress warnings reason: volatile for double synchronized check * - * @param parent - * @param name + *

This will be a ConcurrentHashMap instance */ - public EntityMNode(IMNode parent, String name) { - super(parent, name); - } + @SuppressWarnings("squid:S3077") + private transient volatile Map> aliasChildren = null; - /** check whether the MNode has a child with the name */ - @Override - public boolean hasChild(String name) { - return (children != null && children.containsKey(name)) - || (aliasChildren != null && aliasChildren.containsKey(name)); - } + private volatile boolean isAligned = false; - /** get the child with the name */ @Override - public IMNode getChild(String name) { - IMNode child = null; - if (children != null) { - child = children.get(name); - } - if (child != null) { - return child; - } - return aliasChildren == null ? null : aliasChildren.get(name); + public void moveDataToNewMNode(IDeviceMNode newMNode) { + newMNode.setSchemaTemplateId(schemaTemplateId); + newMNode.setUseTemplate(useTemplate); + newMNode.setAliasChildren(aliasChildren); + newMNode.setAligned(isAligned); } /** add an alias */ @Override - public boolean addAlias(String alias, IMeasurementMNode child) { + public boolean addAlias(String alias, IMeasurementMNode child) { if (aliasChildren == null) { // double check, alias children volatile synchronized (this) { @@ -98,7 +83,7 @@ public void deleteAliasChild(String alias) { } @Override - public Map getAliasChildren() { + public Map> getAliasChildren() { if (aliasChildren == null) { return Collections.emptyMap(); } @@ -106,10 +91,24 @@ public Map getAliasChildren() { } @Override - public void setAliasChildren(Map aliasChildren) { + public void setAliasChildren(Map> aliasChildren) { this.aliasChildren = aliasChildren; } + @Override + public boolean hasAliasChild(String name) { + return aliasChildren != null && aliasChildren.containsKey(name); + } + + @Override + public N getAliasChild(String name) { + if (aliasChildren != null) { + IMeasurementMNode child = aliasChildren.get(name); + return child == null ? null : child.getAsMNode(); + } + return null; + } + /** * In EntityMNode(device node), schemaTemplateId represents the template activated on this node. * The pre deactivation mechanism is implemented by making this value negative. Since value 0 and @@ -123,6 +122,11 @@ public int getSchemaTemplateId() { return schemaTemplateId >= -1 ? schemaTemplateId : -schemaTemplateId - 2; } + @Override + public int getSchemaTemplateIdWithState() { + return schemaTemplateId; + } + @Override public boolean isPreDeactivateTemplate() { return schemaTemplateId < -1; @@ -149,40 +153,43 @@ public void deactivateTemplate() { } @Override - public boolean isAligned() { - return isAligned; + public boolean isUseTemplate() { + return useTemplate; } @Override - public void setAligned(boolean isAligned) { - this.isAligned = isAligned; + public void setUseTemplate(boolean useTemplate) { + this.useTemplate = useTemplate; } @Override - public void moveDataToNewMNode(IMNode newMNode) { - super.moveDataToNewMNode(newMNode); - - if (newMNode.isEntity()) { - IEntityMNode newEntityMNode = newMNode.getAsEntityMNode(); - newEntityMNode.setAligned(isAligned); - if (aliasChildren != null) { - newEntityMNode.setAliasChildren(aliasChildren); - } - } + public void setSchemaTemplateId(int schemaTemplateId) { + this.schemaTemplateId = schemaTemplateId; } @Override - public boolean isEntity() { - return true; + public boolean isAligned() { + return isAligned; } @Override - public MNodeType getMNodeType(Boolean isConfig) { - return MNodeType.DEVICE; + public void setAligned(boolean isAligned) { + this.isAligned = isAligned; } + /** + * The memory occupied by an DeviceInfo based occupation + * + *

    + *
  1. object header, 8B + *
  2. int schemaTemplateId, 4B + *
  3. boolean useTemplate, 1B + *
  4. boolean isAligned, 1B + *
  5. aliasChildren reference, 8B + *
+ */ @Override - public R accept(MNodeVisitor visitor, C context) { - return visitor.visitEntityMNode(this, context); + public int estimateSize() { + return 8 + 4 + 1 + 1 + 8; } } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/info/MeasurementInfo.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/info/MeasurementInfo.java new file mode 100644 index 0000000000000..9df799726da8f --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/info/MeasurementInfo.java @@ -0,0 +1,113 @@ +/* + * 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.metadata.mnode.mem.info; + +import org.apache.iotdb.commons.schema.node.info.IMeasurementInfo; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; +import org.apache.iotdb.tsfile.write.schema.IMeasurementSchema; + +public class MeasurementInfo implements IMeasurementInfo { + + /** alias name of this measurement */ + protected String alias; + /** tag/attribute's start offset in tag file */ + private long offset = -1; + /** measurement's Schema for one timeseries represented by current leaf node */ + private IMeasurementSchema schema; + /** whether this measurement is pre deleted and considered in black list */ + private boolean preDeleted = false; + + // alias length, hashCode and occupation in aliasMap, 4 + 4 + 44 = 52B + private static final int ALIAS_BASE_SIZE = 52; + + public MeasurementInfo(IMeasurementSchema schema, String alias) { + this.schema = schema; + this.alias = alias; + } + + @Override + public void moveDataToNewMNode(IMeasurementMNode newMNode) { + newMNode.setSchema(schema); + newMNode.setAlias(alias); + newMNode.setOffset(offset); + newMNode.setPreDeleted(preDeleted); + } + + @Override + public IMeasurementSchema getSchema() { + return schema; + } + + @Override + public void setSchema(IMeasurementSchema schema) { + this.schema = schema; + } + + @Override + public TSDataType getDataType() { + return schema.getType(); + } + + @Override + public String getAlias() { + return alias; + } + + @Override + public void setAlias(String alias) { + this.alias = alias; + } + + @Override + public long getOffset() { + return offset; + } + + @Override + public void setOffset(long offset) { + this.offset = offset; + } + + @Override + public boolean isPreDeleted() { + return preDeleted; + } + + @Override + public void setPreDeleted(boolean preDeleted) { + this.preDeleted = preDeleted; + } + + /** + * The memory occupied by an MeasurementInfo based occupation + * + *
    + *
  1. object header, 8B + *
  2. alias reference, 8B + *
  3. long tagOffset, 8B + *
  4. boolean preDeleted, 1B + *
  5. estimated schema size, 32B + *
+ */ + @Override + public int estimateSize() { + return 8 + 8 + 8 + 1 + 32 + (alias == null ? 0 : ALIAS_BASE_SIZE + alias.length()); + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/iterator/AbstractTraverserIterator.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/iterator/AbstractTraverserIterator.java similarity index 77% rename from server/src/main/java/org/apache/iotdb/db/metadata/mnode/iterator/AbstractTraverserIterator.java rename to server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/iterator/AbstractTraverserIterator.java index 026fd036604c4..dddd3e37448c9 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/iterator/AbstractTraverserIterator.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/iterator/AbstractTraverserIterator.java @@ -16,14 +16,16 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.iotdb.db.metadata.mnode.iterator; +package org.apache.iotdb.db.metadata.mnode.mem.iterator; import org.apache.iotdb.commons.exception.MetadataException; -import org.apache.iotdb.db.metadata.mnode.IEntityMNode; -import org.apache.iotdb.db.metadata.mnode.IMNode; +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; +import org.apache.iotdb.commons.schema.node.utils.IMNodeIterator; +import org.apache.iotdb.db.metadata.mnode.utils.MNodeUtils; import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore; import org.apache.iotdb.db.metadata.template.Template; -import org.apache.iotdb.db.metadata.template.TemplateMNodeGenerator; import java.util.Iterator; import java.util.Map; @@ -35,22 +37,25 @@ * TraverserIterator for traversing device node. The iterator returns the filtered child nodes in * the mtree and the child nodes in the template. */ -public abstract class AbstractTraverserIterator implements IMNodeIterator { - private final IMNodeIterator directChildrenIterator; - private Iterator templateChildrenIterator; - protected IMNode nextMatchedNode; +public abstract class AbstractTraverserIterator> implements IMNodeIterator { + private final IMNodeIterator directChildrenIterator; + private Iterator templateChildrenIterator; + protected N nextMatchedNode; protected boolean usingDirectChildrenIterator = true; // if true, the pre deleted measurement or pre deactivated template won't be processed private boolean skipPreDeletedSchema = false; protected AbstractTraverserIterator( - IMTreeStore store, IEntityMNode parent, Map templateMap) + IMTreeStore store, + IDeviceMNode parent, + Map templateMap, + IMNodeFactory nodeFactory) throws MetadataException { - this.directChildrenIterator = store.getChildrenIterator(parent); + this.directChildrenIterator = store.getChildrenIterator(parent.getAsMNode()); if (templateMap != null && parent.isUseTemplate()) { Template template = getActivatedSchemaTemplate(parent, templateMap); if (template != null) { - templateChildrenIterator = TemplateMNodeGenerator.getChildren(template); + templateChildrenIterator = MNodeUtils.getChildren(template, nodeFactory); } } } @@ -60,10 +65,10 @@ public void setSkipPreDeletedSchema(boolean skipPreDeletedSchema) { } private Template getActivatedSchemaTemplate( - IEntityMNode node, Map templateMap) { + IDeviceMNode node, Map templateMap) { // new cluster, the used template is directly recorded as template id in device mnode if (node.getSchemaTemplateId() != NON_TEMPLATE) { - if (skipPreDeletedSchema && node.getAsEntityMNode().isPreDeactivateTemplate()) { + if (skipPreDeletedSchema && node.getAsDeviceMNode().isPreDeactivateTemplate()) { // skip this pre deactivated template, the invoker will skip this return null; } @@ -99,11 +104,11 @@ public boolean hasNext() { } @Override - public IMNode next() { + public N next() { if (!hasNext()) { throw new NoSuchElementException(); } - IMNode result = nextMatchedNode; + N result = nextMatchedNode; nextMatchedNode = null; return result; } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/iterator/MNodeIterator.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/iterator/MNodeIterator.java similarity index 75% rename from server/src/main/java/org/apache/iotdb/db/metadata/mnode/iterator/MNodeIterator.java rename to server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/iterator/MNodeIterator.java index 77b9d7e23f024..e5864e5b1c14d 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/iterator/MNodeIterator.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/iterator/MNodeIterator.java @@ -17,17 +17,18 @@ * under the License. */ -package org.apache.iotdb.db.metadata.mnode.iterator; +package org.apache.iotdb.db.metadata.mnode.mem.iterator; -import org.apache.iotdb.db.metadata.mnode.IMNode; +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeIterator; import java.util.Iterator; -public class MNodeIterator implements IMNodeIterator { +public class MNodeIterator> implements IMNodeIterator { - private Iterator iterator; + private Iterator iterator; - public MNodeIterator(Iterator iterator) { + public MNodeIterator(Iterator iterator) { this.iterator = iterator; } @@ -37,7 +38,7 @@ public boolean hasNext() { } @Override - public IMNode next() { + public N next() { return iterator.next(); } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/iterator/MemoryTraverserIterator.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/iterator/MemoryTraverserIterator.java similarity index 65% rename from server/src/main/java/org/apache/iotdb/db/metadata/mnode/iterator/MemoryTraverserIterator.java rename to server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/iterator/MemoryTraverserIterator.java index 9a8d9be7bcca8..503d0e4620e34 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/iterator/MemoryTraverserIterator.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/mem/iterator/MemoryTraverserIterator.java @@ -16,19 +16,25 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.iotdb.db.metadata.mnode.iterator; +package org.apache.iotdb.db.metadata.mnode.mem.iterator; import org.apache.iotdb.commons.exception.MetadataException; -import org.apache.iotdb.db.metadata.mnode.IEntityMNode; +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore; import org.apache.iotdb.db.metadata.template.Template; import java.util.Map; -public class MemoryTraverserIterator extends AbstractTraverserIterator { +// only use for IConfigMNode and IMemMNode +public class MemoryTraverserIterator> extends AbstractTraverserIterator { public MemoryTraverserIterator( - IMTreeStore store, IEntityMNode parent, Map templateMap) + IMTreeStore store, + IDeviceMNode parent, + Map templateMap, + IMNodeFactory nodeFactory) throws MetadataException { - super(store, parent, templateMap); + super(store, parent, templateMap, nodeFactory); } } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/ICachedMNode.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/ICachedMNode.java new file mode 100644 index 0000000000000..8c322603a4a3c --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/ICachedMNode.java @@ -0,0 +1,28 @@ +/* + * 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.metadata.mnode.schemafile; + +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.db.metadata.mtree.store.disk.cache.CacheEntry; + +public interface ICachedMNode extends IMNode { + CacheEntry getCacheEntry(); + + void setCacheEntry(CacheEntry cacheEntry); +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/basic/CachedBasicMNode.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/basic/CachedBasicMNode.java new file mode 100644 index 0000000000000..e6f582e5f5b02 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/basic/CachedBasicMNode.java @@ -0,0 +1,264 @@ +/* + * 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.metadata.mnode.schemafile.basic; + +import org.apache.iotdb.commons.conf.IoTDBConstant; +import org.apache.iotdb.commons.path.PartialPath; +import org.apache.iotdb.commons.schema.node.MNodeType; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeContainer; +import org.apache.iotdb.commons.schema.node.visitor.MNodeVisitor; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.container.CachedMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.schemafile.info.CacheMNodeInfo; +import org.apache.iotdb.db.metadata.mtree.store.disk.cache.CacheEntry; + +import java.util.ArrayList; +import java.util.List; + +/** + * This class is the implementation of Metadata Node. One MNode instance represents one node in the + * Metadata Tree + */ +public class CachedBasicMNode implements ICachedMNode { + + private static final long serialVersionUID = -770028375899514063L; + + private ICachedMNode parent; + private final CacheMNodeInfo cacheMNodeInfo; + + /** from root to this node, only be set when used once for InternalMNode */ + private String fullPath; + + /** Constructor of MNode. */ + public CachedBasicMNode(ICachedMNode parent, String name) { + this.parent = parent; + this.cacheMNodeInfo = new CacheMNodeInfo(name); + } + + @Override + public String getName() { + return cacheMNodeInfo.getName(); + } + + @Override + public void setName(String name) { + cacheMNodeInfo.setName(name); + } + + @Override + public ICachedMNode getParent() { + return parent; + } + + @Override + public void setParent(ICachedMNode parent) { + this.parent = parent; + } + + @Override + public String getFullPath() { + if (fullPath == null) { + fullPath = concatFullPath(); + } + return fullPath; + } + + String concatFullPath() { + StringBuilder builder = new StringBuilder(getName()); + ICachedMNode curr = this; + while (curr.getParent() != null) { + curr = curr.getParent(); + builder.insert(0, IoTDBConstant.PATH_SEPARATOR).insert(0, curr.getName()); + } + return builder.toString(); + } + + @Override + public void setFullPath(String fullPath) { + this.fullPath = fullPath; + } + + @Override + public PartialPath getPartialPath() { + List detachedPath = new ArrayList<>(); + ICachedMNode temp = this; + detachedPath.add(temp.getName()); + while (temp.getParent() != null) { + temp = temp.getParent(); + detachedPath.add(0, temp.getName()); + } + return new PartialPath(detachedPath.toArray(new String[0])); + } + + /** check whether the MNode has a child with the name */ + @Override + public boolean hasChild(String name) { + return false; + } + + /** get the child with the name */ + @Override + public ICachedMNode getChild(String name) { + return null; + } + + /** + * add a child to current mnode + * + * @param name child's name + * @param child child's node + * @return the child of this node after addChild + */ + @Override + public ICachedMNode addChild(String name, ICachedMNode child) { + return null; + } + + /** + * Add a child to the current mnode. + * + *

This method will not take the child's name as one of the inputs and will also make this + * Mnode be child node's parent. All is to reduce the probability of mistaken by users and be more + * convenient for users to use. And the return of this method is used to conveniently construct a + * chain of time series for users. + * + * @param child child's node + * @return return the MNode already added + */ + @Override + public ICachedMNode addChild(ICachedMNode child) { + return null; + } + + /** delete a child */ + @Override + public ICachedMNode deleteChild(String name) { + return null; + } + + /** + * Replace a child of this mnode. New child's name must be the same as old child's name. + * + * @param oldChildName measurement name + * @param newChildNode new child node + */ + @Override + public synchronized void replaceChild(String oldChildName, ICachedMNode newChildNode) {} + + @Override + public void moveDataToNewMNode(ICachedMNode newMNode) { + newMNode.setParent(parent); + newMNode.setCacheEntry(getCacheEntry()); + } + + @Override + public IMNodeContainer getChildren() { + return CachedMNodeContainer.emptyMNodeContainer(); + } + + @Override + public void setChildren(IMNodeContainer children) {} + + @Override + public boolean isAboveDatabase() { + return false; + } + + @Override + public boolean isDatabase() { + return false; + } + + @Override + public boolean isDevice() { + return false; + } + + @Override + public boolean isMeasurement() { + return false; + } + + @Override + public MNodeType getMNodeType(Boolean isConfig) { + return isConfig ? MNodeType.SG_INTERNAL : MNodeType.INTERNAL; + } + + @Override + public IDatabaseMNode getAsDatabaseMNode() { + throw new UnsupportedOperationException("Wrong MNode Type"); + } + + @Override + public IDeviceMNode getAsDeviceMNode() { + throw new UnsupportedOperationException("Wrong MNode Type"); + } + + @Override + public IMeasurementMNode getAsMeasurementMNode() { + throw new UnsupportedOperationException("Wrong MNode Type"); + } + + @Override + public R accept(MNodeVisitor visitor, C context) { + return visitor.visitBasicMNode(this, context); + } + + @Override + public CacheEntry getCacheEntry() { + return cacheMNodeInfo.getCacheEntry(); + } + + @Override + public void setCacheEntry(CacheEntry cacheEntry) { + cacheMNodeInfo.setCacheEntry(cacheEntry); + } + + /** + * The basic memory occupied by any CacheBasicMNode object + * + *

    + *
  1. object header, 8B + *
  2. node attributes + *
      + *
    1. basicMNodeInfo reference, 8B + *
    2. parent reference, 8B + *
    3. fullPath reference, 8B + *
    + *
  3. MapEntry in parent + *
      + *
    1. key reference, 8B + *
    2. value reference, 8B + *
    3. entry size, see ConcurrentHashMap.Node, 28 + *
    + *
+ */ + @Override + public int estimateSize() { + return 8 + 8 + 8 + 8 + 8 + 8 + 28 + cacheMNodeInfo.estimateSize(); + } + + @Override + public ICachedMNode getAsMNode() { + return this; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/CachedMNodeContainer.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/container/CachedMNodeContainer.java similarity index 67% rename from server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/CachedMNodeContainer.java rename to server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/container/CachedMNodeContainer.java index 47fecfe9da58f..aed2ae4f8dd2d 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/CachedMNodeContainer.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/container/CachedMNodeContainer.java @@ -16,13 +16,17 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.iotdb.db.metadata.mtree.store.disk; +package org.apache.iotdb.db.metadata.mnode.schemafile.container; -import org.apache.iotdb.db.metadata.mnode.IMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; + +import org.jetbrains.annotations.NotNull; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.util.AbstractMap; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -33,20 +37,29 @@ import java.util.TreeSet; import java.util.concurrent.ConcurrentHashMap; +import static java.util.Collections.emptySet; + public class CachedMNodeContainer implements ICachedMNodeContainer { private long segmentAddress = -1; - private Map childCache = null; - private Map newChildBuffer = null; - private Map updatedChildBuffer = null; + private Map childCache = null; + private Map newChildBuffer = null; + private Map updatedChildBuffer = null; + + private static final IMNodeContainer EMPTY_CONTAINER = + new CachedMNodeContainer.EmptyContainer(); + + public static IMNodeContainer emptyMNodeContainer() { + return EMPTY_CONTAINER; + } @Override public int size() { return getSize(childCache) + getSize(newChildBuffer) + getSize(updatedChildBuffer); } - private int getSize(Map map) { + private int getSize(Map map) { return map == null ? 0 : map.size(); } @@ -55,7 +68,7 @@ public boolean isEmpty() { return isEmpty(childCache) && isEmpty(newChildBuffer) && isEmpty(updatedChildBuffer); } - private boolean isEmpty(Map map) { + private boolean isEmpty(Map map) { return map == null || map.isEmpty(); } @@ -66,7 +79,7 @@ public boolean containsKey(Object key) { || containsKey(updatedChildBuffer, key); } - private boolean containsKey(Map map, Object key) { + private boolean containsKey(Map map, Object key) { return map != null && map.containsKey(key); } @@ -77,13 +90,13 @@ public boolean containsValue(Object value) { || containsValue(updatedChildBuffer, value); } - private boolean containsValue(Map map, Object value) { + private boolean containsValue(Map map, Object value) { return map != null && map.containsValue(value); } @Override - public IMNode get(Object key) { - IMNode result = get(childCache, key); + public ICachedMNode get(Object key) { + ICachedMNode result = get(childCache, key); if (result != null) { return result; } @@ -94,13 +107,13 @@ public IMNode get(Object key) { return get(updatedChildBuffer, key); } - private IMNode get(Map map, Object key) { + private ICachedMNode get(Map map, Object key) { return map == null ? null : map.get(key); } @Nullable @Override - public synchronized IMNode put(String key, IMNode value) { + public synchronized ICachedMNode put(String key, ICachedMNode value) { if (newChildBuffer == null) { newChildBuffer = new ConcurrentHashMap<>(); } @@ -109,9 +122,9 @@ public synchronized IMNode put(String key, IMNode value) { @Nullable @Override - public synchronized IMNode putIfAbsent(String key, IMNode value) { + public synchronized ICachedMNode putIfAbsent(String key, ICachedMNode value) { - IMNode node = get(key); + ICachedMNode node = get(key); if (node == null) { if (newChildBuffer == null) { newChildBuffer = new ConcurrentHashMap<>(); @@ -123,8 +136,8 @@ public synchronized IMNode putIfAbsent(String key, IMNode value) { } @Override - public synchronized IMNode remove(Object key) { - IMNode result = remove(childCache, key); + public synchronized ICachedMNode remove(Object key) { + ICachedMNode result = remove(childCache, key); if (result == null) { result = remove(newChildBuffer, key); } @@ -134,12 +147,12 @@ public synchronized IMNode remove(Object key) { return result; } - private IMNode remove(Map map, Object key) { + private ICachedMNode remove(Map map, Object key) { return map == null ? null : map.remove(key); } @Override - public synchronized void putAll(@Nonnull Map m) { + public synchronized void putAll(@Nonnull Map m) { if (newChildBuffer == null) { newChildBuffer = new ConcurrentHashMap<>(); } @@ -163,42 +176,42 @@ public Set keySet() { return result; } - private Set keySet(Map map) { + private Set keySet(Map map) { return map == null ? Collections.emptySet() : map.keySet(); } @Nonnull @Override - public Collection values() { - Collection result = new ArrayList<>(); + public Collection values() { + Collection result = new ArrayList<>(); result.addAll(getValues(childCache)); result.addAll(getValues(newChildBuffer)); result.addAll(getValues(updatedChildBuffer)); return result; } - private Collection getValues(Map map) { + private Collection getValues(Map map) { return map == null ? Collections.emptyList() : map.values(); } @Nonnull @Override - public Set> entrySet() { - Set> result = new HashSet<>(); + public Set> entrySet() { + Set> result = new HashSet<>(); result.addAll(entrySet(childCache)); result.addAll(entrySet(newChildBuffer)); result.addAll(entrySet(updatedChildBuffer)); return result; } - private Set> entrySet(Map map) { + private Set> entrySet(Map map) { return map == null ? Collections.emptySet() : map.entrySet(); } @Nullable @Override - public synchronized IMNode replace(String key, IMNode value) { - IMNode replacedOne = replace(childCache, key, value); + public synchronized ICachedMNode replace(String key, ICachedMNode value) { + ICachedMNode replacedOne = replace(childCache, key, value); if (replacedOne == null) { replacedOne = replace(newChildBuffer, key, value); } @@ -208,7 +221,7 @@ public synchronized IMNode replace(String key, IMNode value) { return replacedOne; } - private IMNode replace(Map map, String key, IMNode value) { + private ICachedMNode replace(Map map, String key, ICachedMNode value) { return map == null ? null : map.replace(key, value); } @@ -251,37 +264,37 @@ public boolean hasChildInBuffer(String name) { } @Override - public Iterator getChildrenIterator() { + public Iterator getChildrenIterator() { return new CachedMNodeContainerIterator(); } @Override - public Iterator getChildrenBufferIterator() { + public Iterator getChildrenBufferIterator() { return new BufferIterator(); } @Override - public Iterator getNewChildBufferIterator() { + public Iterator getNewChildBufferIterator() { return getNewChildBuffer().values().iterator(); } @Override - public Map getChildCache() { + public Map getChildCache() { return childCache == null ? Collections.emptyMap() : childCache; } @Override - public Map getNewChildBuffer() { + public Map getNewChildBuffer() { return newChildBuffer == null ? Collections.emptyMap() : newChildBuffer; } @Override - public Map getUpdatedChildBuffer() { + public Map getUpdatedChildBuffer() { return updatedChildBuffer == null ? Collections.emptyMap() : updatedChildBuffer; } @Override - public synchronized void loadChildrenFromDisk(Map children) { + public synchronized void loadChildrenFromDisk(Map children) { if (childCache == null) { childCache = new ConcurrentHashMap<>(); } @@ -289,7 +302,7 @@ public synchronized void loadChildrenFromDisk(Map children) { } @Override - public synchronized void addChildToCache(IMNode node) { + public synchronized void addChildToCache(ICachedMNode node) { String name = node.getName(); if (containsKey(name)) { return; @@ -301,7 +314,7 @@ public synchronized void addChildToCache(IMNode node) { } @Override - public synchronized void appendMNode(IMNode node) { + public synchronized void appendMNode(ICachedMNode node) { if (newChildBuffer == null) { newChildBuffer = new ConcurrentHashMap<>(); } @@ -310,7 +323,7 @@ public synchronized void appendMNode(IMNode node) { @Override public synchronized void updateMNode(String name) { - IMNode node = remove(childCache, name); + ICachedMNode node = remove(childCache, name); if (node != null) { if (updatedChildBuffer == null) { updatedChildBuffer = new ConcurrentHashMap<>(); @@ -321,7 +334,7 @@ public synchronized void updateMNode(String name) { @Override public synchronized void moveMNodeToCache(String name) { - IMNode node = remove(newChildBuffer, name); + ICachedMNode node = remove(newChildBuffer, name); if (node == null) { node = remove(updatedChildBuffer, name); } @@ -340,17 +353,17 @@ public synchronized String toString() { StringBuilder builder = new StringBuilder(); builder.append("CachedMNodeContainer:{"); builder.append("childCache:["); - for (IMNode node : getValues(childCache)) { + for (ICachedMNode node : getValues(childCache)) { builder.append(node.getName()).append(","); } builder.append("];"); builder.append("newChildBuffer:["); - for (IMNode node : getValues(newChildBuffer)) { + for (ICachedMNode node : getValues(newChildBuffer)) { builder.append(node.getName()).append(","); } builder.append("];"); builder.append("updateChildBuffer:["); - for (IMNode node : getValues(updatedChildBuffer)) { + for (ICachedMNode node : getValues(updatedChildBuffer)) { builder.append(node.getName()).append(","); } builder.append("];"); @@ -358,9 +371,9 @@ public synchronized String toString() { return builder.toString(); } - private class CachedMNodeContainerIterator implements Iterator { + private class CachedMNodeContainerIterator implements Iterator { - Iterator iterator; + Iterator iterator; byte status = 0; CachedMNodeContainerIterator() { @@ -381,7 +394,7 @@ public boolean hasNext() { } @Override - public IMNode next() { + public ICachedMNode next() { return iterator.next(); } @@ -401,10 +414,10 @@ private boolean changeStatus() { } } - private class BufferIterator implements Iterator { - Iterator iterator; - Iterator newBufferIterator; - Iterator updateBufferIterator; + private class BufferIterator implements Iterator { + Iterator iterator; + Iterator newBufferIterator; + Iterator updateBufferIterator; byte status = 0; BufferIterator() { @@ -427,7 +440,7 @@ public boolean hasNext() { } @Override - public IMNode next() { + public ICachedMNode next() { return iterator.next(); } @@ -440,4 +453,55 @@ private boolean changeStatus() { return false; } } + + private static class EmptyContainer extends AbstractMap + implements IMNodeContainer { + + @Override + public int size() { + return 0; + } + + @Override + public boolean isEmpty() { + return true; + } + + @Override + public boolean containsKey(Object key) { + return false; + } + + @Override + public boolean containsValue(Object value) { + return false; + } + + @Override + public ICachedMNode get(Object key) { + return null; + } + + @Override + @NotNull + public Set keySet() { + return emptySet(); + } + + @Override + @NotNull + public Collection values() { + return emptySet(); + } + + @NotNull + public Set> entrySet() { + return emptySet(); + } + + @Override + public boolean equals(Object o) { + return o == this; + } + } } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/ICachedMNodeContainer.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/container/ICachedMNodeContainer.java similarity index 58% rename from server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/ICachedMNodeContainer.java rename to server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/container/ICachedMNodeContainer.java index 1579f98b793e4..22dcbb8b9e271 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/ICachedMNodeContainer.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/container/ICachedMNodeContainer.java @@ -16,16 +16,15 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.iotdb.db.metadata.mtree.store.disk; +package org.apache.iotdb.db.metadata.mnode.schemafile.container; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.container.IMNodeContainer; -import org.apache.iotdb.db.metadata.mnode.container.MNodeContainers; +import org.apache.iotdb.commons.schema.node.utils.IMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; import java.util.Iterator; import java.util.Map; -public interface ICachedMNodeContainer extends IMNodeContainer { +public interface ICachedMNodeContainer extends IMNodeContainer { long getSegmentAddress(); @@ -41,23 +40,23 @@ public interface ICachedMNodeContainer extends IMNodeContainer { boolean hasChildInBuffer(String name); - Iterator getChildrenIterator(); + Iterator getChildrenIterator(); - Iterator getChildrenBufferIterator(); + Iterator getChildrenBufferIterator(); - Iterator getNewChildBufferIterator(); + Iterator getNewChildBufferIterator(); - Map getChildCache(); + Map getChildCache(); - Map getNewChildBuffer(); + Map getNewChildBuffer(); - Map getUpdatedChildBuffer(); + Map getUpdatedChildBuffer(); - void loadChildrenFromDisk(Map children); + void loadChildrenFromDisk(Map children); - void addChildToCache(IMNode node); + void addChildToCache(ICachedMNode node); - void appendMNode(IMNode node); + void appendMNode(ICachedMNode node); void updateMNode(String name); @@ -65,16 +64,16 @@ public interface ICachedMNodeContainer extends IMNodeContainer { void evictMNode(String name); - static ICachedMNodeContainer getCachedMNodeContainer(IMNode node) { - IMNodeContainer container = node.getChildren(); - if (container.equals(MNodeContainers.emptyMNodeContainer())) { + static ICachedMNodeContainer getCachedMNodeContainer(ICachedMNode node) { + IMNodeContainer container = node.getChildren(); + if (container.equals(CachedMNodeContainer.emptyMNodeContainer())) { container = new CachedMNodeContainer(); node.setChildren(container); } return (ICachedMNodeContainer) container; } - static ICachedMNodeContainer getBelongedContainer(IMNode node) { + static ICachedMNodeContainer getBelongedContainer(ICachedMNode node) { return (ICachedMNodeContainer) node.getParent().getChildren(); } } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/factory/CacheMNodeFactory.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/factory/CacheMNodeFactory.java new file mode 100644 index 0000000000000..5e79e03258c0e --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/factory/CacheMNodeFactory.java @@ -0,0 +1,84 @@ +/* + * 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.metadata.mnode.schemafile.factory; + +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.impl.CachedAboveDatabaseMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.impl.CachedBasicInternalMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.impl.CachedDatabaseDeviceMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.impl.CachedDatabaseMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.impl.CachedDeviceMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.impl.CachedMeasurementMNode; +import org.apache.iotdb.tsfile.write.schema.IMeasurementSchema; + +public class CacheMNodeFactory implements IMNodeFactory { + + private CacheMNodeFactory() {} + + private static class CacheMNodeFactoryHolder { + private static final CacheMNodeFactory INSTANCE = new CacheMNodeFactory(); + + private CacheMNodeFactoryHolder() {} + } + + public static CacheMNodeFactory getInstance() { + return CacheMNodeFactory.CacheMNodeFactoryHolder.INSTANCE; + } + + @Override + public IMeasurementMNode createMeasurementMNode( + IDeviceMNode parent, String name, IMeasurementSchema schema, String alias) { + return new CachedMeasurementMNode(parent, name, schema, alias); + } + + @Override + public IDeviceMNode createDeviceMNode(ICachedMNode parent, String name) { + return new CachedDeviceMNode(parent, name); + } + + @Override + public IDatabaseMNode createDatabaseMNode(ICachedMNode parent, String name) { + return new CachedDatabaseMNode(parent, name); + } + + @Override + public IDatabaseMNode createDatabaseMNode( + ICachedMNode parent, String name, long dataTTL) { + return new CachedDatabaseMNode(parent, name, dataTTL); + } + + @Override + public ICachedMNode createDatabaseDeviceMNode(ICachedMNode parent, String name, long dataTTL) { + return new CachedDatabaseDeviceMNode(parent, name, dataTTL); + } + + @Override + public ICachedMNode createAboveDatabaseMNode(ICachedMNode parent, String name) { + return new CachedAboveDatabaseMNode(parent, name); + } + + @Override + public ICachedMNode createInternalMNode(ICachedMNode parent, String name) { + return new CachedBasicInternalMNode(parent, name); + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/impl/CachedAboveDatabaseMNode.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/impl/CachedAboveDatabaseMNode.java new file mode 100644 index 0000000000000..f8ab57603c840 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/impl/CachedAboveDatabaseMNode.java @@ -0,0 +1,47 @@ +/* + * 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.metadata.mnode.schemafile.impl; + +import org.apache.iotdb.commons.schema.node.common.AbstractAboveDatabaseMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.basic.CachedBasicMNode; +import org.apache.iotdb.db.metadata.mtree.store.disk.cache.CacheEntry; + +public class CachedAboveDatabaseMNode + extends AbstractAboveDatabaseMNode implements ICachedMNode { + + public CachedAboveDatabaseMNode(ICachedMNode parent, String name) { + super(new CachedBasicInternalMNode(parent, name)); + } + + @Override + public CacheEntry getCacheEntry() { + return basicMNode.getCacheEntry(); + } + + @Override + public void setCacheEntry(CacheEntry cacheEntry) { + basicMNode.setCacheEntry(cacheEntry); + } + + @Override + public ICachedMNode getAsMNode() { + return this; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/InternalMNode.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/impl/CachedBasicInternalMNode.java similarity index 51% rename from server/src/main/java/org/apache/iotdb/db/metadata/mnode/InternalMNode.java rename to server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/impl/CachedBasicInternalMNode.java index 47e95f0b9cd5f..b06a98180a27f 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/InternalMNode.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/impl/CachedBasicInternalMNode.java @@ -16,44 +16,29 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.iotdb.db.metadata.mnode; +package org.apache.iotdb.db.metadata.mnode.schemafile.impl; -import org.apache.iotdb.db.metadata.mnode.container.IMNodeContainer; -import org.apache.iotdb.db.metadata.mnode.container.MNodeContainers; -import org.apache.iotdb.db.metadata.mnode.visitor.MNodeVisitor; - -import static org.apache.iotdb.db.metadata.MetadataConstant.NON_TEMPLATE; +import org.apache.iotdb.commons.schema.node.utils.IMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.basic.CachedBasicMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.container.CachedMNodeContainer; /** * This class is the implementation of Metadata Node. One MNode instance represents one node in the * Metadata Tree */ -public class InternalMNode extends MNode { - - private static final long serialVersionUID = -770028375899514063L; +public class CachedBasicInternalMNode extends CachedBasicMNode { /** - * use in Measurement Node so it's protected suppress warnings reason: volatile for double - * synchronized check + * suppress warnings reason: volatile for double synchronized check * *

This will be a ConcurrentHashMap instance */ @SuppressWarnings("squid:S3077") - protected transient volatile IMNodeContainer children = null; - - /** - * This field is mainly used in cluster schema template features. In InternalMNode of ConfigMTree, - * this field represents the template set on this node. In EntityMNode of MTree in SchemaRegion, - * this field represents the template activated on this node. The normal usage value range is [0, - * Int.MaxValue], since this is implemented as auto inc id. The default value -1 means - * NON_TEMPLATE. This value will be set negative to implement some pre-delete features. - */ - protected int schemaTemplateId = NON_TEMPLATE; - - private volatile boolean useTemplate = false; + private transient volatile IMNodeContainer children = null; /** Constructor of MNode. */ - public InternalMNode(IMNode parent, String name) { + public CachedBasicInternalMNode(ICachedMNode parent, String name) { super(parent, name); } @@ -65,8 +50,8 @@ public boolean hasChild(String name) { /** get the child with the name */ @Override - public IMNode getChild(String name) { - IMNode child = null; + public ICachedMNode getChild(String name) { + ICachedMNode child = null; if (children != null) { child = children.get(name); } @@ -81,7 +66,7 @@ public IMNode getChild(String name) { * @return the child of this node after addChild */ @Override - public IMNode addChild(String name, IMNode child) { + public ICachedMNode addChild(String name, ICachedMNode child) { /* use cpu time to exchange memory * measurementNode's children should be null to save memory * add child method will only be called when writing MTree, which is not a frequent operation @@ -90,12 +75,12 @@ public IMNode addChild(String name, IMNode child) { // double check, children is volatile synchronized (this) { if (children == null) { - children = MNodeContainers.getNewMNodeContainer(); + children = new CachedMNodeContainer(); } } } child.setParent(this); - IMNode existingChild = children.putIfAbsent(name, child); + ICachedMNode existingChild = children.putIfAbsent(name, child); return existingChild == null ? child : existingChild; } @@ -111,7 +96,7 @@ public IMNode addChild(String name, IMNode child) { * @return return the MNode already added */ @Override - public IMNode addChild(IMNode child) { + public ICachedMNode addChild(ICachedMNode child) { /* use cpu time to exchange memory * measurementNode's children should be null to save memory * add child method will only be called when writing MTree, which is not a frequent operation @@ -120,7 +105,7 @@ public IMNode addChild(IMNode child) { // double check, children is volatile synchronized (this) { if (children == null) { - children = MNodeContainers.getNewMNodeContainer(); + children = new CachedMNodeContainer(); } } } @@ -132,7 +117,7 @@ public IMNode addChild(IMNode child) { /** delete a child */ @Override - public IMNode deleteChild(String name) { + public ICachedMNode deleteChild(String name) { if (children != null) { return children.remove(name); } @@ -146,11 +131,11 @@ public IMNode deleteChild(String name) { * @param newChildNode new child node */ @Override - public synchronized void replaceChild(String oldChildName, IMNode newChildNode) { + public synchronized void replaceChild(String oldChildName, ICachedMNode newChildNode) { if (!oldChildName.equals(newChildNode.getName())) { throw new RuntimeException("New child's name must be the same as old child's name!"); } - IMNode oldChildNode = this.getChild(oldChildName); + ICachedMNode oldChildNode = this.getChild(oldChildName); if (oldChildNode == null) { return; } @@ -161,12 +146,9 @@ public synchronized void replaceChild(String oldChildName, IMNode newChildNode) } @Override - public void moveDataToNewMNode(IMNode newMNode) { + public void moveDataToNewMNode(ICachedMNode newMNode) { super.moveDataToNewMNode(newMNode); - newMNode.setUseTemplate(useTemplate); - newMNode.setSchemaTemplateId(schemaTemplateId); - if (children != null) { newMNode.setChildren(children); children.forEach((childName, childNode) -> childNode.setParent(newMNode)); @@ -174,86 +156,36 @@ public void moveDataToNewMNode(IMNode newMNode) { } @Override - public IMNodeContainer getChildren() { + public IMNodeContainer getChildren() { if (children == null) { - return MNodeContainers.emptyMNodeContainer(); + return CachedMNodeContainer.emptyMNodeContainer(); } return children; } @Override - public void setChildren(IMNodeContainer children) { + public void setChildren(IMNodeContainer children) { this.children = children; } - @Override - public int getSchemaTemplateId() { - return schemaTemplateId >= -1 ? schemaTemplateId : -schemaTemplateId - 2; - } - - @Override - public int getSchemaTemplateIdWithState() { - return schemaTemplateId; - } - - @Override - public void setSchemaTemplateId(int schemaTemplateId) { - this.schemaTemplateId = schemaTemplateId; - } - /** - * In InternalMNode, schemaTemplateId represents the template set on this node. The pre unset - * mechanism is implemented by making this value negative. Since value 0 and -1 are all occupied, - * the available negative value range is [Int.MIN_VALUE, -2]. The value of a pre unset case equals - * the negative normal value minus 2. For example, if the id of set template is 0, then - 0 - 2 = - * -2 represents the pre unset operation of this template on this node. + * MNodeContainer reference and basic occupation, 8 + 80B. + * + *

    + * Estimated delta size of CachedMNodeContainer compared with other MNodeContainer. + *
  1. address, 8B + *
  2. three map reference (1 cache and 2 buffer), 8 * 3 = 24B + *
  3. estimate occupation of map implementation, minus the basic container occupation, 80 * 3 - + * 80 = 160B + *
*/ @Override - public void preUnsetSchemaTemplate() { - if (this.schemaTemplateId > -1) { - this.schemaTemplateId = -schemaTemplateId - 2; - } - } - - @Override - public void rollbackUnsetSchemaTemplate() { - if (schemaTemplateId < -1) { - schemaTemplateId = -schemaTemplateId - 2; - } - } - - @Override - public boolean isSchemaTemplatePreUnset() { - return schemaTemplateId < -1; - } - - @Override - public void unsetSchemaTemplate() { - this.schemaTemplateId = -1; - } - - @Override - public boolean isAboveDatabase() { - return false; - } - - @Override - public MNodeType getMNodeType(Boolean isConfig) { - return isConfig ? MNodeType.SG_INTERNAL : MNodeType.INTERNAL; - } - - @Override - public boolean isUseTemplate() { - return useTemplate; - } - - @Override - public void setUseTemplate(boolean useTemplate) { - this.useTemplate = useTemplate; + public int estimateSize() { + return 8 + 80 + 192 + super.estimateSize(); } @Override - public R accept(MNodeVisitor visitor, C context) { - return visitor.visitInternalMNode(this, context); + public ICachedMNode getAsMNode() { + return this; } } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/impl/CachedDatabaseDeviceMNode.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/impl/CachedDatabaseDeviceMNode.java new file mode 100644 index 0000000000000..dc4ba9283f547 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/impl/CachedDatabaseDeviceMNode.java @@ -0,0 +1,49 @@ +/* + * 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.metadata.mnode.schemafile.impl; + +import org.apache.iotdb.commons.schema.node.common.AbstractDatabaseDeviceMNode; +import org.apache.iotdb.db.metadata.mnode.mem.info.DatabaseDeviceInfo; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.basic.CachedBasicMNode; +import org.apache.iotdb.db.metadata.mtree.store.disk.cache.CacheEntry; + +public class CachedDatabaseDeviceMNode + extends AbstractDatabaseDeviceMNode implements ICachedMNode { + + public CachedDatabaseDeviceMNode(ICachedMNode parent, String name, long dataTTL) { + super(new CachedBasicInternalMNode(parent, name), new DatabaseDeviceInfo<>()); + setDataTTL(dataTTL); + } + + @Override + public CacheEntry getCacheEntry() { + return basicMNode.getCacheEntry(); + } + + @Override + public void setCacheEntry(CacheEntry cacheEntry) { + basicMNode.setCacheEntry(cacheEntry); + } + + @Override + public ICachedMNode getAsMNode() { + return this; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/impl/CachedDatabaseMNode.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/impl/CachedDatabaseMNode.java new file mode 100644 index 0000000000000..58952882fdcb9 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/impl/CachedDatabaseMNode.java @@ -0,0 +1,54 @@ +/* + * 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.metadata.mnode.schemafile.impl; + +import org.apache.iotdb.commons.schema.node.common.AbstractDatabaseMNode; +import org.apache.iotdb.db.metadata.mnode.mem.info.DatabaseInfo; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.basic.CachedBasicMNode; +import org.apache.iotdb.db.metadata.mtree.store.disk.cache.CacheEntry; + +public class CachedDatabaseMNode extends AbstractDatabaseMNode + implements ICachedMNode { + + public CachedDatabaseMNode(ICachedMNode parent, String name) { + super(new CachedBasicInternalMNode(parent, name), new DatabaseInfo<>()); + } + + // TODO: @yukun, remove this constructor + public CachedDatabaseMNode(ICachedMNode parent, String name, long dataTTL) { + this(parent, name); + setDataTTL(dataTTL); + } + + @Override + public CacheEntry getCacheEntry() { + return basicMNode.getCacheEntry(); + } + + @Override + public void setCacheEntry(CacheEntry cacheEntry) { + basicMNode.setCacheEntry(cacheEntry); + } + + @Override + public ICachedMNode getAsMNode() { + return this; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/impl/CachedDeviceMNode.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/impl/CachedDeviceMNode.java new file mode 100644 index 0000000000000..a9a331c476c01 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/impl/CachedDeviceMNode.java @@ -0,0 +1,48 @@ +/* + * 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.metadata.mnode.schemafile.impl; + +import org.apache.iotdb.commons.schema.node.common.AbstractDeviceMNode; +import org.apache.iotdb.db.metadata.mnode.mem.info.DeviceInfo; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.basic.CachedBasicMNode; +import org.apache.iotdb.db.metadata.mtree.store.disk.cache.CacheEntry; + +public class CachedDeviceMNode extends AbstractDeviceMNode + implements ICachedMNode { + + public CachedDeviceMNode(ICachedMNode parent, String name) { + super(new CachedBasicInternalMNode(parent, name), new DeviceInfo<>()); + } + + @Override + public CacheEntry getCacheEntry() { + return basicMNode.getCacheEntry(); + } + + @Override + public void setCacheEntry(CacheEntry cacheEntry) { + basicMNode.setCacheEntry(cacheEntry); + } + + @Override + public ICachedMNode getAsMNode() { + return this; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/impl/CachedMeasurementMNode.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/impl/CachedMeasurementMNode.java new file mode 100644 index 0000000000000..df10e21f740d8 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/impl/CachedMeasurementMNode.java @@ -0,0 +1,60 @@ +/* + * 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.metadata.mnode.schemafile.impl; + +import org.apache.iotdb.commons.schema.node.common.AbstractMeasurementMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.mem.info.MeasurementInfo; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.basic.CachedBasicMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.container.CachedMNodeContainer; +import org.apache.iotdb.db.metadata.mtree.store.disk.cache.CacheEntry; +import org.apache.iotdb.tsfile.write.schema.IMeasurementSchema; + +public class CachedMeasurementMNode extends AbstractMeasurementMNode + implements ICachedMNode { + + public CachedMeasurementMNode( + IDeviceMNode parent, String name, IMeasurementSchema schema, String alias) { + super( + new CachedBasicMNode(parent == null ? null : parent.getAsMNode(), name), + new MeasurementInfo(schema, alias)); + } + + @Override + public CacheEntry getCacheEntry() { + return basicMNode.getCacheEntry(); + } + + @Override + public void setCacheEntry(CacheEntry cacheEntry) { + basicMNode.setCacheEntry(cacheEntry); + } + + @Override + public ICachedMNode getAsMNode() { + return this; + } + + @Override + public IMNodeContainer getChildren() { + return CachedMNodeContainer.emptyMNodeContainer(); + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/info/CacheMNodeInfo.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/info/CacheMNodeInfo.java new file mode 100644 index 0000000000000..d89dba9bca34d --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/info/CacheMNodeInfo.java @@ -0,0 +1,45 @@ +/* + * 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.metadata.mnode.schemafile.info; + +import org.apache.iotdb.db.metadata.mnode.mem.info.BasicMNodeInfo; +import org.apache.iotdb.db.metadata.mtree.store.disk.cache.CacheEntry; + +public class CacheMNodeInfo extends BasicMNodeInfo { + + private CacheEntry cacheEntry; + + public CacheMNodeInfo(String name) { + super(name); + } + + public CacheEntry getCacheEntry() { + return cacheEntry; + } + + public void setCacheEntry(CacheEntry cacheEntry) { + this.cacheEntry = cacheEntry; + } + + @Override + public int estimateSize() { + // Estimated size of CacheEntry = 40 + return super.estimateSize() + 40; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/iterator/CachedTraverserIterator.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/iterator/CachedTraverserIterator.java similarity index 67% rename from server/src/main/java/org/apache/iotdb/db/metadata/mnode/iterator/CachedTraverserIterator.java rename to server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/iterator/CachedTraverserIterator.java index 501f7483157e5..32ff2d28d3edb 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/iterator/CachedTraverserIterator.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/schemafile/iterator/CachedTraverserIterator.java @@ -16,22 +16,28 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.iotdb.db.metadata.mnode.iterator; +package org.apache.iotdb.db.metadata.mnode.schemafile.iterator; import org.apache.iotdb.commons.exception.MetadataException; -import org.apache.iotdb.db.metadata.mnode.IEntityMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; +import org.apache.iotdb.db.metadata.mnode.mem.iterator.AbstractTraverserIterator; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore; import org.apache.iotdb.db.metadata.template.Template; import java.util.Map; -public class CachedTraverserIterator extends AbstractTraverserIterator { - private final IMTreeStore store; +public class CachedTraverserIterator extends AbstractTraverserIterator { + private final IMTreeStore store; public CachedTraverserIterator( - IMTreeStore store, IEntityMNode parent, Map templateMap) + IMTreeStore store, + IDeviceMNode parent, + Map templateMap, + IMNodeFactory nodeFactory) throws MetadataException { - super(store, parent, templateMap); + super(store, parent, templateMap, nodeFactory); this.store = store; } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/utils/MNodeUtils.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/utils/MNodeUtils.java new file mode 100644 index 0000000000000..f64f270ccee52 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/utils/MNodeUtils.java @@ -0,0 +1,123 @@ +/* + * 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.metadata.mnode.utils; + +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; +import org.apache.iotdb.db.metadata.template.Template; +import org.apache.iotdb.tsfile.write.schema.IMeasurementSchema; + +import java.util.Iterator; + +public class MNodeUtils { + + /** + * When a measurement, represented by template or MeasurementMNode, is going to be added to a + * node, the node should be set to entity. Before invoking this method, lock the related MTree + * part first. + * + * @param node node to be transformed + * @return generated entityMNode + */ + public static > IDeviceMNode setToEntity( + IMNode node, IMNodeFactory nodeFactory) { + IDeviceMNode entityMNode; + if (node.isDevice()) { + entityMNode = node.getAsDeviceMNode(); + } else { + if (node.isDatabase()) { + entityMNode = + nodeFactory + .createDatabaseDeviceMNode( + node.getParent(), node.getName(), node.getAsDatabaseMNode().getDataTTL()) + .getAsDeviceMNode(); + node.moveDataToNewMNode(entityMNode.getAsMNode()); + } else { + // basic node + entityMNode = nodeFactory.createDeviceMNode(node.getParent(), node.getName()); + if (node.getParent() != null) { + node.getParent().replaceChild(node.getName(), entityMNode.getAsMNode()); + } else { + node.moveDataToNewMNode(entityMNode.getAsMNode()); + } + } + } + return entityMNode; + } + + /** + * When there's no measurement, represented by template or MeasurementMNode, is under this + * entityMNode, it should not act as entity anymore. Before invoking this method, lock related + * MTree structure first. + * + * @param entityMNode node to be transformed + * @return generated NoEntity node + */ + public static > N setToInternal( + IDeviceMNode entityMNode, IMNodeFactory nodeFactor) { + N node; + N parent = entityMNode.getParent(); + if (entityMNode.isDatabase()) { + IDatabaseMNode databaseMNode = + nodeFactor.createDatabaseMNode(parent, entityMNode.getName()); + databaseMNode.setDataTTL(entityMNode.getAsDatabaseMNode().getDataTTL()); + node = databaseMNode.getAsMNode(); + } else { + node = nodeFactor.createInternalMNode(parent, entityMNode.getName()); + } + + if (parent != null) { + parent.replaceChild(entityMNode.getName(), node); + } + return node; + } + + public static > N getChild( + Template template, String name, IMNodeFactory nodeFactor) { + IMeasurementSchema schema = template.getSchema(name); + return schema == null + ? null + : nodeFactor + .createMeasurementMNode(null, name, template.getSchema(name), null) + .getAsMNode(); + } + + public static > Iterator getChildren( + Template template, IMNodeFactory nodeFactor) { + return new Iterator() { + private final Iterator schemas = + template.getSchemaMap().values().iterator(); + + @Override + public boolean hasNext() { + return schemas.hasNext(); + } + + @Override + public N next() { + IMeasurementSchema schema = schemas.next(); + return nodeFactor + .createMeasurementMNode(null, schema.getMeasurementId(), schema, null) + .getAsMNode(); + } + }; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/visitor/MNodeVisitor.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/visitor/MNodeVisitor.java deleted file mode 100644 index 53ccc1f059cdd..0000000000000 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/visitor/MNodeVisitor.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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.metadata.mnode.visitor; - -import org.apache.iotdb.db.metadata.mnode.EntityMNode; -import org.apache.iotdb.db.metadata.mnode.InternalMNode; -import org.apache.iotdb.db.metadata.mnode.MeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.StorageGroupEntityMNode; -import org.apache.iotdb.db.metadata.mnode.StorageGroupMNode; - -public abstract class MNodeVisitor { - - public abstract R visitInternalMNode(InternalMNode node, C context); - - public abstract R visitStorageGroupMNode(StorageGroupMNode node, C context); - - public abstract R visitStorageGroupEntityMNode(StorageGroupEntityMNode node, C context); - - public abstract R visitEntityMNode(EntityMNode node, C context); - - public abstract R visitMeasurementMNode(MeasurementMNode node, C context); -} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/ConfigMTree.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/ConfigMTree.java index e427680adea99..a20b4533072eb 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/ConfigMTree.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/ConfigMTree.java @@ -24,16 +24,16 @@ import org.apache.iotdb.commons.exception.IllegalPathException; import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.path.PartialPath; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; +import org.apache.iotdb.commons.schema.node.utils.IMNodeIterator; import org.apache.iotdb.commons.utils.ThriftConfigNodeSerDeUtils; import org.apache.iotdb.db.exception.metadata.DatabaseAlreadySetException; import org.apache.iotdb.db.exception.metadata.DatabaseNotSetException; import org.apache.iotdb.db.exception.metadata.PathNotExistException; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IStorageGroupMNode; -import org.apache.iotdb.db.metadata.mnode.InternalMNode; -import org.apache.iotdb.db.metadata.mnode.StorageGroupMNode; -import org.apache.iotdb.db.metadata.mnode.iterator.IMNodeIterator; -import org.apache.iotdb.db.metadata.mtree.store.MemMTreeStore; +import org.apache.iotdb.db.metadata.mnode.config.IConfigMNode; +import org.apache.iotdb.db.metadata.mnode.config.factory.ConfigMNodeFactory; +import org.apache.iotdb.db.metadata.mtree.store.ConfigMTreeStore; import org.apache.iotdb.db.metadata.mtree.traverser.collector.DatabaseCollector; import org.apache.iotdb.db.metadata.mtree.traverser.collector.MNodeAboveDBCollector; import org.apache.iotdb.db.metadata.mtree.traverser.collector.MNodeCollector; @@ -74,12 +74,14 @@ public class ConfigMTree { private final Logger logger = LoggerFactory.getLogger(ConfigMTree.class); - private IMNode root; + private IConfigMNode root; // this store is only used for traverser invoking - private MemMTreeStore store; + private final ConfigMTreeStore store; + + private final IMNodeFactory nodeFactory = ConfigMNodeFactory.getInstance(); public ConfigMTree() throws MetadataException { - store = new MemMTreeStore(new PartialPath(PATH_ROOT), false); + store = new ConfigMTreeStore(nodeFactory); root = store.getRoot(); } @@ -103,39 +105,39 @@ public void setStorageGroup(PartialPath path) throws MetadataException { if (nodeNames.length <= 1 || !nodeNames[0].equals(root.getName())) { throw new IllegalPathException(path.getFullPath()); } - IMNode cur = root; + IConfigMNode cur = root; int i = 1; // e.g., path = root.a.b.sg, create internal nodes for a, b while (i < nodeNames.length - 1) { - IMNode temp = cur.getChild(nodeNames[i]); + IConfigMNode temp = store.getChild(cur, nodeNames[i]); if (temp == null) { - cur.addChild(nodeNames[i], new InternalMNode(cur, nodeNames[i])); - } else if (temp.isStorageGroup()) { + store.addChild(cur, nodeNames[i], nodeFactory.createInternalMNode(cur, nodeNames[i])); + } else if (temp.isDatabase()) { // before create database, check whether the database already exists throw new DatabaseAlreadySetException(temp.getFullPath()); } - cur = cur.getChild(nodeNames[i]); + cur = store.getChild(cur, nodeNames[i]); i++; } // synchronize check and add, we need addChild operation be atomic. // only write operations on mtree will be synchronized synchronized (this) { - if (cur.hasChild(nodeNames[i])) { + if (store.hasChild(cur, nodeNames[i])) { // node b has child sg - if (cur.getChild(nodeNames[i]).isStorageGroup()) { + if (store.getChild(cur, nodeNames[i]).isDatabase()) { throw new DatabaseAlreadySetException(path.getFullPath()); } else { throw new DatabaseAlreadySetException(path.getFullPath(), true); } } else { - IStorageGroupMNode storageGroupMNode = - new StorageGroupMNode( - cur, nodeNames[i], CommonDescriptor.getInstance().getConfig().getDefaultTTLInMs()); + IDatabaseMNode databaseMNode = + nodeFactory.createDatabaseMNode(cur, nodeNames[i]); + databaseMNode.setDataTTL(CommonDescriptor.getInstance().getConfig().getDefaultTTLInMs()); - IMNode result = cur.addChild(nodeNames[i], storageGroupMNode); + IConfigMNode result = store.addChild(cur, nodeNames[i], databaseMNode.getAsMNode()); - if (result != storageGroupMNode) { + if (result != databaseMNode) { throw new DatabaseAlreadySetException(path.getFullPath(), true); } } @@ -144,11 +146,11 @@ public void setStorageGroup(PartialPath path) throws MetadataException { /** Delete a database */ public void deleteDatabase(PartialPath path) throws MetadataException { - IStorageGroupMNode storageGroupMNode = getDatabaseNodeByDatabasePath(path); - IMNode cur = storageGroupMNode.getParent(); + IDatabaseMNode databaseMNode = getDatabaseNodeByDatabasePath(path); + IConfigMNode cur = databaseMNode.getParent(); // Suppose current system has root.a.b.sg1, root.a.sg2, and delete root.a.b.sg1 // delete the database node sg1 - cur.deleteChild(storageGroupMNode.getName()); + store.deleteChild(cur, databaseMNode.getName()); // delete node a while retain root.a.sg2 while (cur.getParent() != null && cur.getChildren().size() == 0) { @@ -189,10 +191,12 @@ private List collectDatabases( PartialPath pathPattern, boolean isPrefixMatch, boolean collectInternal) throws MetadataException { List result = new LinkedList<>(); - try (DatabaseCollector> collector = - new DatabaseCollector>(root, pathPattern, store, isPrefixMatch) { + try (DatabaseCollector collector = + new DatabaseCollector, IConfigMNode>( + root, pathPattern, store, isPrefixMatch) { + @Override - protected void collectDatabase(IStorageGroupMNode node) { + protected void collectDatabase(IDatabaseMNode node) { result.add(node.getPartialPath()); } }) { @@ -209,11 +213,11 @@ protected void collectDatabase(IStorageGroupMNode node) { */ public List getAllDatabasePaths() { List res = new ArrayList<>(); - Deque nodeStack = new ArrayDeque<>(); + Deque nodeStack = new ArrayDeque<>(); nodeStack.add(root); while (!nodeStack.isEmpty()) { - IMNode current = nodeStack.pop(); - if (current.isStorageGroup()) { + IConfigMNode current = nodeStack.pop(); + if (current.isDatabase()) { res.add(current.getPartialPath()); } else { nodeStack.addAll(current.getChildren().values()); @@ -231,7 +235,8 @@ public List getAllDatabasePaths() { */ public int getDatabaseNum(PartialPath pathPattern, boolean isPrefixMatch) throws MetadataException { - try (DatabaseCounter counter = new DatabaseCounter(root, pathPattern, store, isPrefixMatch)) { + try (DatabaseCounter counter = + new DatabaseCounter<>(root, pathPattern, store, isPrefixMatch)) { return (int) counter.count(); } } @@ -239,31 +244,31 @@ public int getDatabaseNum(PartialPath pathPattern, boolean isPrefixMatch) /** * E.g., root.sg is database given [root, sg], if the give path is not a database, throw exception */ - public IStorageGroupMNode getDatabaseNodeByDatabasePath(PartialPath storageGroupPath) + public IDatabaseMNode getDatabaseNodeByDatabasePath(PartialPath databasePath) throws MetadataException { - String[] nodes = storageGroupPath.getNodes(); + String[] nodes = databasePath.getNodes(); if (nodes.length == 0 || !nodes[0].equals(root.getName())) { - throw new IllegalPathException(storageGroupPath.getFullPath()); + throw new IllegalPathException(databasePath.getFullPath()); } - IMNode cur = root; + IConfigMNode cur = root; for (int i = 1; i < nodes.length - 1; i++) { cur = cur.getChild(nodes[i]); if (cur == null) { - throw new DatabaseNotSetException(storageGroupPath.getFullPath()); + throw new DatabaseNotSetException(databasePath.getFullPath()); } - if (cur.isStorageGroup()) { + if (cur.isDatabase()) { throw new DatabaseAlreadySetException(cur.getFullPath()); } } cur = cur.getChild(nodes[nodes.length - 1]); if (cur == null) { - throw new DatabaseNotSetException(storageGroupPath.getFullPath()); + throw new DatabaseNotSetException(databasePath.getFullPath()); } - if (cur.isStorageGroup()) { - return cur.getAsStorageGroupMNode(); + if (cur.isDatabase()) { + return cur.getAsDatabaseMNode(); } else { - throw new DatabaseAlreadySetException(storageGroupPath.getFullPath(), true); + throw new DatabaseAlreadySetException(databasePath.getFullPath(), true); } } @@ -272,19 +277,20 @@ public IStorageGroupMNode getDatabaseNodeByDatabasePath(PartialPath storageGroup * device], return the MNode of root.sg Get database node, the give path don't need to be database * path. */ - public IStorageGroupMNode getDatabaseNodeByPath(PartialPath path) throws MetadataException { + public IDatabaseMNode getDatabaseNodeByPath(PartialPath path) + throws MetadataException { String[] nodes = path.getNodes(); if (nodes.length == 0 || !nodes[0].equals(root.getName())) { throw new IllegalPathException(path.getFullPath()); } - IMNode cur = root; + IConfigMNode cur = root; for (int i = 1; i < nodes.length; i++) { cur = cur.getChild(nodes[i]); if (cur == null) { break; } - if (cur.isStorageGroup()) { - return cur.getAsStorageGroupMNode(); + if (cur.isDatabase()) { + return cur.getAsDatabaseMNode(); } } throw new DatabaseNotSetException(path.getFullPath()); @@ -298,16 +304,16 @@ public IStorageGroupMNode getDatabaseNodeByPath(PartialPath path) throws Metadat */ public boolean isDatabaseAlreadySet(PartialPath path) { String[] nodeNames = path.getNodes(); - IMNode cur = root; + IConfigMNode cur = root; if (!nodeNames[0].equals(root.getName())) { return false; } for (int i = 1; i < nodeNames.length; i++) { - if (!cur.hasChild(nodeNames[i])) { + if (!store.hasChild(cur, nodeNames[i])) { return false; } - cur = cur.getChild(nodeNames[i]); - if (cur.isStorageGroup()) { + cur = store.getChild(cur, nodeNames[i]); + if (cur.isDatabase()) { return true; } } @@ -322,16 +328,16 @@ public boolean isDatabaseAlreadySet(PartialPath path) { */ public void checkDatabaseAlreadySet(PartialPath path) throws DatabaseAlreadySetException { String[] nodeNames = path.getNodes(); - IMNode cur = root; + IConfigMNode cur = root; if (!nodeNames[0].equals(root.getName())) { return; } for (int i = 1; i < nodeNames.length; i++) { - if (!cur.hasChild(nodeNames[i])) { + if (!store.hasChild(cur, nodeNames[i])) { return; } - cur = cur.getChild(nodeNames[i]); - if (cur.isStorageGroup()) { + cur = store.getChild(cur, nodeNames[i]); + if (cur.isDatabase()) { throw new DatabaseAlreadySetException(cur.getFullPath()); } } @@ -342,20 +348,21 @@ public void checkDatabaseAlreadySet(PartialPath path) throws DatabaseAlreadySetE // region MTree Node Management - public IMNode getNodeWithAutoCreate(PartialPath path) throws DatabaseNotSetException { + public IConfigMNode getNodeWithAutoCreate(PartialPath path) throws DatabaseNotSetException { String[] nodeNames = path.getNodes(); - IMNode cur = root; - IMNode child; + IConfigMNode cur = root; + IConfigMNode child; boolean hasStorageGroup = false; for (int i = 1; i < nodeNames.length; i++) { - child = cur.getChild(nodeNames[i]); + child = store.getChild(cur, nodeNames[i]); if (child == null) { if (hasStorageGroup) { - child = cur.addChild(nodeNames[i], new InternalMNode(cur, nodeNames[i])); + child = + store.addChild(cur, nodeNames[i], nodeFactory.createInternalMNode(cur, nodeNames[i])); } else { throw new DatabaseNotSetException(path.getFullPath()); } - } else if (child.isStorageGroup()) { + } else if (child.isDatabase()) { hasStorageGroup = true; } @@ -371,10 +378,10 @@ public IMNode getNodeWithAutoCreate(PartialPath path) throws DatabaseNotSetExcep public Pair, Set> getNodesListInGivenLevel( PartialPath pathPattern, int nodeLevel, boolean isPrefixMatch) throws MetadataException { List result = new LinkedList<>(); - try (MNodeAboveDBCollector collector = - new MNodeAboveDBCollector(root, pathPattern, store, isPrefixMatch) { + try (MNodeAboveDBCollector collector = + new MNodeAboveDBCollector(root, pathPattern, store, isPrefixMatch) { @Override - protected Void collectMNode(IMNode node) { + protected Void collectMNode(IConfigMNode node) { result.add(getPartialPathFromRootToNode(node)); return null; } @@ -402,11 +409,11 @@ protected Void collectMNode(IMNode node) { public Pair, Set> getChildNodePathInNextLevel( PartialPath pathPattern) throws MetadataException { Set result = new TreeSet<>(); - try (MNodeAboveDBCollector collector = - new MNodeAboveDBCollector( + try (MNodeAboveDBCollector collector = + new MNodeAboveDBCollector( root, pathPattern.concatNode(ONE_LEVEL_PATH_WILDCARD), store, false) { @Override - protected Void collectMNode(IMNode node) { + protected Void collectMNode(IConfigMNode node) { result.add( new TSchemaNode( getPartialPathFromRootToNode(node).getFullPath(), @@ -431,8 +438,8 @@ protected Void collectMNode(IMNode node) { */ public void checkTemplateOnPath(PartialPath path) throws MetadataException { String[] nodeNames = path.getNodes(); - IMNode cur = root; - IMNode child; + IConfigMNode cur = root; + IConfigMNode child; if (cur.getSchemaTemplateId() != NON_TEMPLATE) { throw new MetadataException("Template already exists on " + cur.getFullPath()); @@ -456,12 +463,12 @@ public void checkTemplateOnPath(PartialPath path) throws MetadataException { } // traverse all the descendant of the given path node - private void checkTemplateOnSubtree(IMNode node) throws MetadataException { + private void checkTemplateOnSubtree(IConfigMNode node) throws MetadataException { if (node.isMeasurement()) { return; } - IMNode child; - IMNodeIterator iterator = store.getChildrenIterator(node); + IConfigMNode child; + IMNodeIterator iterator = store.getChildrenIterator(node); while (iterator.hasNext()) { child = iterator.next(); @@ -478,10 +485,11 @@ private void checkTemplateOnSubtree(IMNode node) throws MetadataException { public List getPathsSetOnTemplate(int templateId, boolean filterPreUnset) throws MetadataException { List resSet = new ArrayList<>(); - try (MNodeCollector collector = - new MNodeCollector(root, new PartialPath(ALL_RESULT_NODES), store, false) { + try (MNodeCollector collector = + new MNodeCollector( + root, new PartialPath(ALL_RESULT_NODES), store, false) { @Override - protected boolean acceptFullMatchedNode(IMNode node) { + protected boolean acceptFullMatchedNode(IConfigMNode node) { if (super.acceptFullMatchedNode(node)) { // if node not set template, go on traversing if (node.getSchemaTemplateId() != NON_TEMPLATE) { @@ -497,20 +505,20 @@ protected boolean acceptFullMatchedNode(IMNode node) { } @Override - protected Void collectMNode(IMNode node) { + protected Void collectMNode(IConfigMNode node) { resSet.add(node.getFullPath()); return null; } @Override - protected boolean shouldVisitSubtreeOfFullMatchedNode(IMNode node) { + protected boolean shouldVisitSubtreeOfFullMatchedNode(IConfigMNode node) { // descendants of the node cannot set another template, exit from this branch return (node.getSchemaTemplateId() == NON_TEMPLATE) && super.shouldVisitSubtreeOfFullMatchedNode(node); } @Override - protected boolean shouldVisitSubtreeOfInternalMatchedNode(IMNode node) { + protected boolean shouldVisitSubtreeOfInternalMatchedNode(IConfigMNode node) { // descendants of the node cannot set another template, exit from this branch return (node.getSchemaTemplateId() == NON_TEMPLATE) && super.shouldVisitSubtreeOfFullMatchedNode(node); @@ -525,22 +533,22 @@ protected boolean shouldVisitSubtreeOfInternalMatchedNode(IMNode node) { public Map> getTemplateSetInfo(PartialPath pathPattern) throws MetadataException { Map> result = new HashMap<>(); - try (MNodeCollector collector = - new MNodeCollector(root, pathPattern, store, false) { + try (MNodeCollector collector = + new MNodeCollector(root, pathPattern, store, false) { @Override - protected boolean acceptFullMatchedNode(IMNode node) { + protected boolean acceptFullMatchedNode(IConfigMNode node) { return (node.getSchemaTemplateId() != NON_TEMPLATE) || super.acceptFullMatchedNode(node); } @Override - protected boolean acceptInternalMatchedNode(IMNode node) { + protected boolean acceptInternalMatchedNode(IConfigMNode node) { return (node.getSchemaTemplateId() != NON_TEMPLATE) || super.acceptInternalMatchedNode(node); } @Override - protected Void collectMNode(IMNode node) { + protected Void collectMNode(IConfigMNode node) { result .computeIfAbsent(node.getSchemaTemplateId(), k -> new HashSet<>()) .add(getPartialPathFromRootToNode(node)); @@ -548,14 +556,14 @@ protected Void collectMNode(IMNode node) { } @Override - protected boolean shouldVisitSubtreeOfFullMatchedNode(IMNode node) { + protected boolean shouldVisitSubtreeOfFullMatchedNode(IConfigMNode node) { // descendants of the node cannot set another template, exit from this branch return (node.getSchemaTemplateId() == NON_TEMPLATE) && super.shouldVisitSubtreeOfFullMatchedNode(node); } @Override - protected boolean shouldVisitSubtreeOfInternalMatchedNode(IMNode node) { + protected boolean shouldVisitSubtreeOfInternalMatchedNode(IConfigMNode node) { // descendants of the node cannot set another template, exit from this branch return (node.getSchemaTemplateId() == NON_TEMPLATE) && super.shouldVisitSubtreeOfFullMatchedNode(node); @@ -578,9 +586,10 @@ public void unsetTemplate(int templateId, PartialPath path) throws MetadataExcep getNodeSetTemplate(templateId, path).unsetSchemaTemplate(); } - private IMNode getNodeSetTemplate(int templateId, PartialPath path) throws MetadataException { + private IConfigMNode getNodeSetTemplate(int templateId, PartialPath path) + throws MetadataException { String[] nodeNames = path.getNodes(); - IMNode cur = root; + IConfigMNode cur = root; for (int i = 1; i < nodeNames.length; i++) { cur = cur.getChild(nodeNames[i]); if (cur == null) { @@ -599,10 +608,10 @@ private IMNode getNodeSetTemplate(int templateId, PartialPath path) throws Metad // region Serialization and Deserialization public void serialize(OutputStream outputStream) throws IOException { - serializeInternalNode((InternalMNode) this.root, outputStream); + serializeConfigBasicMNode(this.root, outputStream); } - private void serializeInternalNode(InternalMNode node, OutputStream outputStream) + private void serializeConfigBasicMNode(IConfigMNode node, OutputStream outputStream) throws IOException { serializeChildren(node, outputStream); @@ -612,25 +621,25 @@ private void serializeInternalNode(InternalMNode node, OutputStream outputStream ReadWriteIOUtils.write(node.getChildren().size(), outputStream); } - private void serializeChildren(InternalMNode node, OutputStream outputStream) throws IOException { - for (IMNode child : node.getChildren().values()) { - if (child.isStorageGroup()) { - serializeStorageGroupNode((StorageGroupMNode) child, outputStream); + private void serializeChildren(IConfigMNode node, OutputStream outputStream) throws IOException { + for (IConfigMNode child : node.getChildren().values()) { + if (child.isDatabase()) { + serializeDatabaseNode(child.getAsDatabaseMNode(), outputStream); } else { - serializeInternalNode((InternalMNode) child, outputStream); + serializeConfigBasicMNode(child, outputStream); } } } - private void serializeStorageGroupNode( - StorageGroupMNode storageGroupNode, OutputStream outputStream) throws IOException { - serializeChildren(storageGroupNode, outputStream); + private void serializeDatabaseNode( + IDatabaseMNode storageGroupNode, OutputStream outputStream) throws IOException { + serializeChildren(storageGroupNode.getAsMNode(), outputStream); ReadWriteIOUtils.write(STORAGE_GROUP_MNODE_TYPE, outputStream); ReadWriteIOUtils.write(storageGroupNode.getName(), outputStream); - ReadWriteIOUtils.write(storageGroupNode.getSchemaTemplateId(), outputStream); - ThriftConfigNodeSerDeUtils.serializeTStorageGroupSchema( - storageGroupNode.getStorageGroupSchema(), outputStream); + ReadWriteIOUtils.write(storageGroupNode.getAsMNode().getSchemaTemplateId(), outputStream); + ThriftConfigNodeSerDeUtils.serializeTDatabaseSchema( + storageGroupNode.getAsMNode().getDatabaseSchema(), outputStream); } public void deserialize(InputStream inputStream) throws IOException { @@ -638,14 +647,14 @@ public void deserialize(InputStream inputStream) throws IOException { String name = null; int childNum = 0; - Stack> stack = new Stack<>(); - StorageGroupMNode storageGroupMNode; - InternalMNode internalMNode; + Stack> stack = new Stack<>(); + IConfigMNode databaseMNode; + IConfigMNode internalMNode; if (type == STORAGE_GROUP_MNODE_TYPE) { - storageGroupMNode = deserializeDatabaseMNode(inputStream); - name = storageGroupMNode.getName(); - stack.push(new Pair<>(storageGroupMNode, true)); + databaseMNode = deserializeDatabaseMNode(inputStream); + name = databaseMNode.getName(); + stack.push(new Pair<>(databaseMNode, true)); } else { internalMNode = deserializeInternalMNode(inputStream); childNum = ReadWriteIOUtils.readInt(inputStream); @@ -669,13 +678,13 @@ public void deserialize(InputStream inputStream) throws IOException { name = internalMNode.getName(); break; case STORAGE_GROUP_MNODE_TYPE: - storageGroupMNode = deserializeDatabaseMNode(inputStream); + databaseMNode = deserializeDatabaseMNode(inputStream).getAsMNode(); childNum = 0; while (!stack.isEmpty() && !stack.peek().right) { - storageGroupMNode.addChild(stack.pop().left); + databaseMNode.addChild(stack.pop().left); } - stack.push(new Pair<>(storageGroupMNode, true)); - name = storageGroupMNode.getName(); + stack.push(new Pair<>(databaseMNode, true)); + name = databaseMNode.getName(); break; default: logger.error("Unrecognized node type. Cannot deserialize MTreeAboveSG from given buffer"); @@ -685,19 +694,21 @@ public void deserialize(InputStream inputStream) throws IOException { this.root = stack.peek().left; } - private InternalMNode deserializeInternalMNode(InputStream inputStream) throws IOException { - InternalMNode internalMNode = new InternalMNode(null, ReadWriteIOUtils.readString(inputStream)); - internalMNode.setSchemaTemplateId(ReadWriteIOUtils.readInt(inputStream)); - return internalMNode; + private IConfigMNode deserializeInternalMNode(InputStream inputStream) throws IOException { + IConfigMNode basicMNode = + nodeFactory.createInternalMNode(null, ReadWriteIOUtils.readString(inputStream)); + basicMNode.setSchemaTemplateId(ReadWriteIOUtils.readInt(inputStream)); + return basicMNode; } - private StorageGroupMNode deserializeDatabaseMNode(InputStream inputStream) throws IOException { - StorageGroupMNode storageGroupMNode = - new StorageGroupMNode(null, ReadWriteIOUtils.readString(inputStream)); - storageGroupMNode.setSchemaTemplateId(ReadWriteIOUtils.readInt(inputStream)); - storageGroupMNode.setStorageGroupSchema( - ThriftConfigNodeSerDeUtils.deserializeTStorageGroupSchema(inputStream)); - return storageGroupMNode; + private IConfigMNode deserializeDatabaseMNode(InputStream inputStream) throws IOException { + IDatabaseMNode databaseMNode = + nodeFactory.createDatabaseMNode(null, ReadWriteIOUtils.readString(inputStream)); + databaseMNode.getAsMNode().setSchemaTemplateId(ReadWriteIOUtils.readInt(inputStream)); + databaseMNode + .getAsMNode() + .setDatabaseSchema(ThriftConfigNodeSerDeUtils.deserializeTDatabaseSchema(inputStream)); + return databaseMNode.getAsMNode(); } // endregion diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/IMTreeBelowSG.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/IMTreeBelowSG.java deleted file mode 100644 index de0aa5b8e09db..0000000000000 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/IMTreeBelowSG.java +++ /dev/null @@ -1,195 +0,0 @@ -/* - * 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.metadata.mtree; - -import org.apache.iotdb.commons.exception.MetadataException; -import org.apache.iotdb.commons.path.MeasurementPath; -import org.apache.iotdb.commons.path.PartialPath; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; -import org.apache.iotdb.db.metadata.template.Template; -import org.apache.iotdb.tsfile.file.metadata.enums.CompressionType; -import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; -import org.apache.iotdb.tsfile.file.metadata.enums.TSEncoding; - -import java.io.File; -import java.util.List; -import java.util.Map; -import java.util.Set; - -public interface IMTreeBelowSG { - void clear(); - - /** - * Create MTree snapshot - * - * @param snapshotDir specify snapshot directory - * @return false if failed to create snapshot; true if success - */ - boolean createSnapshot(File snapshotDir); - - IMeasurementMNode createTimeseries( - PartialPath path, - TSDataType dataType, - TSEncoding encoding, - CompressionType compressor, - Map props, - String alias) - throws MetadataException; - - /** - * Create aligned timeseries with full paths from root to one leaf node. Before creating - * timeseries, the * database should be set first, throw exception otherwise - * - * @param devicePath device path - * @param measurements measurements list - * @param dataTypes data types list - * @param encodings encodings list - * @param compressors compressor - */ - List createAlignedTimeseries( - PartialPath devicePath, - List measurements, - List dataTypes, - List encodings, - List compressors, - List aliasList) - throws MetadataException; - - /** - * Check if measurements under device exists in MTree - * - * @param devicePath device full path - * @param measurementList measurements list - * @param aliasList alias of measurement - * @return If all measurements not exists, return empty map. Otherwise, return a map whose key is - * index of measurement in list and value is exception. - */ - Map checkMeasurementExistence( - PartialPath devicePath, List measurementList, List aliasList); - - /** - * Delete path. The path should be a full path from root to leaf node - * - * @param path Format: root.node(.node)+ - */ - IMeasurementMNode deleteTimeseries(PartialPath path) throws MetadataException; - - boolean isEmptyInternalMNode(IMNode node) throws MetadataException; - - /** - * Construct schema black list via setting matched timeseries to pre deleted. - * - * @param pathPattern path pattern - * @return PartialPath of timeseries that has been set to pre deleted - */ - List constructSchemaBlackList(PartialPath pathPattern) throws MetadataException; - - /** - * Rollback schema black list via setting matched timeseries to not pre deleted. - * - * @param pathPattern path pattern - * @return PartialPath of timeseries that has been set to not pre deleted - */ - List rollbackSchemaBlackList(PartialPath pathPattern) throws MetadataException; - - /** - * Get all pre-deleted timeseries matched by given pathPattern. For example, given path pattern - * root.sg.*.s1 and pre-deleted timeseries root.sg.d1.s1, root.sg.d2.s1, then the result set is - * {root.sg.d1.s1, root.sg.d2.s1}. - * - * @param pathPattern path pattern - * @return all pre-deleted timeseries matched by given pathPattern - */ - List getPreDeletedTimeseries(PartialPath pathPattern) throws MetadataException; - - /** - * Get all devices of pre-deleted timeseries matched by given pathPattern. For example, given path - * pattern root.sg.*.s1 and pre-deleted timeseries root.sg.d1.s1, root.sg.d2.s1, then the result - * set is {root.sg.d1, root.sg.d2}. - * - * @param pathPattern path pattern - * @return all devices of pre-deleted timeseries matched by given pathPattern - */ - Set getDevicesOfPreDeletedTimeseries(PartialPath pathPattern) - throws MetadataException; - - void setAlias(IMeasurementMNode measurementMNode, String alias) throws MetadataException; - - /** - * Add an interval path to MTree. This is only used for automatically creating schema - * - *

e.g., get root.sg.d1, get or create all internal nodes and return the node of d1 - */ - IMNode getDeviceNodeWithAutoCreating(PartialPath deviceId) throws MetadataException; - - /** - * Fetch all measurement path - * - * @param pathPattern a path pattern or a full path, may contain wildcard - * @param templateMap - * @param withTags whether returns all the tags of each timeseries as well. - * @return schema - */ - List fetchSchema( - PartialPath pathPattern, Map templateMap, boolean withTags) - throws MetadataException; - - /** - * Get node by the path - * - * @return last node in given seriesPath - */ - IMNode getNodeByPath(PartialPath path) throws MetadataException; - - IMeasurementMNode getMeasurementMNode(PartialPath path) throws MetadataException; - - long countAllMeasurement() throws MetadataException; - - void activateTemplate(PartialPath activatePath, Template template) throws MetadataException; - - /** - * constructSchemaBlackListWithTemplate - * - * @param templateSetInfo PathPattern and templateId to pre-deactivate - * @return Actual full path and templateId that has been pre-deactivated - */ - Map> constructSchemaBlackListWithTemplate( - Map> templateSetInfo) throws MetadataException; - - /** - * rollbackSchemaBlackListWithTemplate - * - * @param templateSetInfo PathPattern and templateId to rollback pre-deactivate - * @return Actual full path and templateId that has been rolled back - */ - Map> rollbackSchemaBlackListWithTemplate( - Map> templateSetInfo) throws MetadataException; - - /** - * deactivateTemplateInBlackList - * - * @param templateSetInfo PathPattern and templateId to rollback deactivate - * @return Actual full path and templateId that has been deactivated - */ - Map> deactivateTemplateInBlackList( - Map> templateSetInfo) throws MetadataException; - - long countPathsUsingTemplate(PartialPath pathPattern, int templateId) throws MetadataException; -} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGCachedImpl.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGCachedImpl.java index 60d70848dad73..e754bbf616da7 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGCachedImpl.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGCachedImpl.java @@ -23,6 +23,11 @@ import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.path.MeasurementPath; import org.apache.iotdb.commons.path.PartialPath; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; +import org.apache.iotdb.commons.schema.node.utils.IMNodeIterator; import org.apache.iotdb.db.exception.metadata.AliasAlreadyExistException; import org.apache.iotdb.db.exception.metadata.AlignedTimeseriesException; import org.apache.iotdb.db.exception.metadata.MNodeTypeMismatchException; @@ -34,13 +39,8 @@ import org.apache.iotdb.db.exception.metadata.template.TemplateImcompatibeException; import org.apache.iotdb.db.exception.metadata.template.TemplateIsInUseException; import org.apache.iotdb.db.metadata.MetadataConstant; -import org.apache.iotdb.db.metadata.mnode.IEntityMNode; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.IStorageGroupMNode; -import org.apache.iotdb.db.metadata.mnode.InternalMNode; -import org.apache.iotdb.db.metadata.mnode.MeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.iterator.IMNodeIterator; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.factory.CacheMNodeFactory; import org.apache.iotdb.db.metadata.mtree.store.CachedMTreeStore; import org.apache.iotdb.db.metadata.mtree.traverser.Traverser; import org.apache.iotdb.db.metadata.mtree.traverser.TraverserWithLimitOffsetWrapper; @@ -101,36 +101,37 @@ *

  • Interfaces and Implementation for Template check * */ -public class MTreeBelowSGCachedImpl implements IMTreeBelowSG { +public class MTreeBelowSGCachedImpl { private final CachedMTreeStore store; - private volatile IStorageGroupMNode storageGroupMNode; - private final IMNode rootNode; - private final Function> tagGetter; + private volatile ICachedMNode storageGroupMNode; + private final ICachedMNode rootNode; + private final Function, Map> tagGetter; + private final IMNodeFactory nodeFactory = CacheMNodeFactory.getInstance(); private final int levelOfSG; // region MTree initialization, clear and serialization public MTreeBelowSGCachedImpl( PartialPath storageGroupPath, - Function> tagGetter, + Function, Map> tagGetter, Runnable flushCallback, - Consumer measurementProcess, + Consumer> measurementProcess, int schemaRegionId, CachedSchemaRegionStatistics regionStatistics) throws MetadataException, IOException { this.tagGetter = tagGetter; store = new CachedMTreeStore(storageGroupPath, schemaRegionId, regionStatistics, flushCallback); - this.storageGroupMNode = store.getRoot().getAsStorageGroupMNode(); + this.storageGroupMNode = store.getRoot(); this.storageGroupMNode.setParent(storageGroupMNode.getParent()); this.rootNode = store.generatePrefix(storageGroupPath); levelOfSG = storageGroupPath.getNodeLength() - 1; // recover measurement - try (MeasurementCollector collector = - new MeasurementCollector( + try (MeasurementCollector collector = + new MeasurementCollector( this.rootNode, new PartialPath(storageGroupMNode.getFullPath()), this.store, true) { - @Override - protected Void collectMeasurement(IMeasurementMNode node) { + + protected Void collectMeasurement(IMeasurementMNode node) { measurementProcess.accept(node); regionStatistics.addTimeseries(1L); return null; @@ -144,21 +145,21 @@ protected Void collectMeasurement(IMeasurementMNode node) { private MTreeBelowSGCachedImpl( PartialPath storageGroupPath, CachedMTreeStore store, - Consumer measurementProcess, - Function> tagGetter) + Consumer> measurementProcess, + Function, Map> tagGetter) throws MetadataException { this.store = store; - this.storageGroupMNode = store.getRoot().getAsStorageGroupMNode(); + this.storageGroupMNode = store.getRoot(); this.rootNode = store.generatePrefix(storageGroupPath); levelOfSG = storageGroupMNode.getPartialPath().getNodeLength() - 1; this.tagGetter = tagGetter; // recover measurement - try (MeasurementCollector collector = - new MeasurementCollector( + try (MeasurementCollector collector = + new MeasurementCollector( this.rootNode, new PartialPath(storageGroupMNode.getFullPath()), this.store, true) { - @Override - protected Void collectMeasurement(IMeasurementMNode node) { + + protected Void collectMeasurement(IMeasurementMNode node) { measurementProcess.accept(node); return null; } @@ -167,18 +168,18 @@ protected Void collectMeasurement(IMeasurementMNode node) { } } - @Override public void clear() { store.clear(); storageGroupMNode = null; } - protected void replaceStorageGroupMNode(IStorageGroupMNode newMNode) { - this.storageGroupMNode.getParent().replaceChild(this.storageGroupMNode.getName(), newMNode); - this.storageGroupMNode = newMNode; + protected void replaceStorageGroupMNode(IDatabaseMNode newMNode) { + this.storageGroupMNode + .getParent() + .replaceChild(this.storageGroupMNode.getName(), newMNode.getAsMNode()); + this.storageGroupMNode = newMNode.getAsMNode(); } - @Override public boolean createSnapshot(File snapshotDir) { return store.createSnapshot(snapshotDir); } @@ -188,8 +189,8 @@ public static MTreeBelowSGCachedImpl loadFromSnapshot( String storageGroupFullPath, int schemaRegionId, CachedSchemaRegionStatistics regionStatistics, - Consumer measurementProcess, - Function> tagGetter, + Consumer> measurementProcess, + Function, Map> tagGetter, Runnable flushCallback) throws IOException, MetadataException { return new MTreeBelowSGCachedImpl( @@ -204,8 +205,7 @@ public static MTreeBelowSGCachedImpl loadFromSnapshot( // region Timeseries operation, including create and delete - @Override - public IMeasurementMNode createTimeseries( + public IMeasurementMNode createTimeseries( PartialPath path, TSDataType dataType, TSEncoding encoding, @@ -213,9 +213,9 @@ public IMeasurementMNode createTimeseries( Map props, String alias) throws MetadataException { - IMeasurementMNode measurementMNode = + IMeasurementMNode measurementMNode = createTimeseriesWithPinnedReturn(path, dataType, encoding, compressor, props, alias); - unPinMNode(measurementMNode); + unPinMNode(measurementMNode.getAsMNode()); return measurementMNode; } @@ -230,7 +230,7 @@ public IMeasurementMNode createTimeseries( * @param props props * @param alias alias of measurement */ - public IMeasurementMNode createTimeseriesWithPinnedReturn( + public IMeasurementMNode createTimeseriesWithPinnedReturn( PartialPath path, TSDataType dataType, TSEncoding encoding, @@ -244,13 +244,13 @@ public IMeasurementMNode createTimeseriesWithPinnedReturn( } MetaFormatUtils.checkTimeseries(path); PartialPath devicePath = path.getDevicePath(); - IMNode deviceParent = checkAndAutoCreateInternalPath(devicePath); + ICachedMNode deviceParent = checkAndAutoCreateInternalPath(devicePath); try { // synchronize check and add, we need addChild and add Alias become atomic operation // only write on mtree will be synchronized synchronized (this) { - IMNode device = checkAndAutoCreateDeviceNode(devicePath.getTailNode(), deviceParent); + ICachedMNode device = checkAndAutoCreateDeviceNode(devicePath.getTailNode(), deviceParent); try { MetaFormatUtils.checkTimeseriesProps(path.getFullPath(), props); @@ -265,30 +265,30 @@ public IMeasurementMNode createTimeseriesWithPinnedReturn( throw new PathAlreadyExistException(path.getFullPath()); } - if (device.isEntity() && device.getAsEntityMNode().isAligned()) { + if (device.isDevice() && device.getAsDeviceMNode().isAligned()) { throw new AlignedTimeseriesException( "timeseries under this entity is aligned, please use createAlignedTimeseries or change entity.", device.getFullPath()); } - IEntityMNode entityMNode; - if (device.isEntity()) { - entityMNode = device.getAsEntityMNode(); + IDeviceMNode entityMNode; + if (device.isDevice()) { + entityMNode = device.getAsDeviceMNode(); } else { entityMNode = store.setToEntity(device); - if (entityMNode.isStorageGroup()) { - replaceStorageGroupMNode(entityMNode.getAsStorageGroupMNode()); + if (entityMNode.isDatabase()) { + replaceStorageGroupMNode(entityMNode.getAsDatabaseMNode()); } - device = entityMNode; + device = entityMNode.getAsMNode(); } - IMeasurementMNode measurementMNode = - MeasurementMNode.getMeasurementMNode( + IMeasurementMNode measurementMNode = + nodeFactory.createMeasurementMNode( entityMNode, leafName, new MeasurementSchema(leafName, dataType, encoding, compressor, props), alias); - store.addChild(entityMNode, leafName, measurementMNode); + store.addChild(entityMNode.getAsMNode(), leafName, measurementMNode.getAsMNode()); // link alias to LeafMNode if (alias != null) { entityMNode.addAlias(alias, measurementMNode); @@ -315,8 +315,7 @@ public IMeasurementMNode createTimeseriesWithPinnedReturn( * @param encodings encodings list * @param compressors compressor */ - @Override - public List createAlignedTimeseries( + public List> createAlignedTimeseries( PartialPath devicePath, List measurements, List dataTypes, @@ -324,15 +323,15 @@ public List createAlignedTimeseries( List compressors, List aliasList) throws MetadataException { - List measurementMNodeList = new ArrayList<>(); + List> measurementMNodeList = new ArrayList<>(); MetaFormatUtils.checkSchemaMeasurementNames(measurements); - IMNode deviceParent = checkAndAutoCreateInternalPath(devicePath); + ICachedMNode deviceParent = checkAndAutoCreateInternalPath(devicePath); try { // synchronize check and add, we need addChild operation be atomic. // only write operations on mtree will be synchronized synchronized (this) { - IMNode device = checkAndAutoCreateDeviceNode(devicePath.getTailNode(), deviceParent); + ICachedMNode device = checkAndAutoCreateDeviceNode(devicePath.getTailNode(), deviceParent); try { for (int i = 0; i < measurements.size(); i++) { @@ -348,27 +347,27 @@ public List createAlignedTimeseries( } } - if (device.isEntity() && !device.getAsEntityMNode().isAligned()) { + if (device.isDevice() && !device.getAsDeviceMNode().isAligned()) { throw new AlignedTimeseriesException( "Timeseries under this entity is not aligned, please use createTimeseries or change entity.", devicePath.getFullPath()); } - IEntityMNode entityMNode; - if (device.isEntity()) { - entityMNode = device.getAsEntityMNode(); + IDeviceMNode entityMNode; + if (device.isDevice()) { + entityMNode = device.getAsDeviceMNode(); } else { entityMNode = store.setToEntity(device); entityMNode.setAligned(true); - if (entityMNode.isStorageGroup()) { - replaceStorageGroupMNode(entityMNode.getAsStorageGroupMNode()); + if (entityMNode.isDatabase()) { + replaceStorageGroupMNode(entityMNode.getAsDatabaseMNode()); } - device = entityMNode; + device = entityMNode.getAsMNode(); } for (int i = 0; i < measurements.size(); i++) { - IMeasurementMNode measurementMNode = - MeasurementMNode.getMeasurementMNode( + IMeasurementMNode measurementMNode = + nodeFactory.createMeasurementMNode( entityMNode, measurements.get(i), new MeasurementSchema( @@ -377,7 +376,8 @@ public List createAlignedTimeseries( encodings.get(i), compressors.get(i)), aliasList == null ? null : aliasList.get(i)); - store.addChild(entityMNode, measurements.get(i), measurementMNode); + store.addChild( + entityMNode.getAsMNode(), measurements.get(i), measurementMNode.getAsMNode()); if (aliasList != null && aliasList.get(i) != null) { entityMNode.addAlias(aliasList.get(i), measurementMNode); } @@ -395,22 +395,21 @@ public List createAlignedTimeseries( } } - @Override public Map checkMeasurementExistence( PartialPath devicePath, List measurementList, List aliasList) { - IMNode device; + ICachedMNode device; try { device = getNodeByPath(devicePath); } catch (MetadataException e) { return Collections.emptyMap(); } try { - if (!device.isEntity()) { + if (!device.isDevice()) { return Collections.emptyMap(); } Map failingMeasurementMap = new HashMap<>(); for (int i = 0; i < measurementList.size(); i++) { - IMNode node = null; + ICachedMNode node = null; try { node = store.getChild(device, measurementList.get(i)); if (node != null) { @@ -456,14 +455,15 @@ public Map checkMeasurementExistence( } } - private IMNode checkAndAutoCreateInternalPath(PartialPath devicePath) throws MetadataException { + private ICachedMNode checkAndAutoCreateInternalPath(PartialPath devicePath) + throws MetadataException { String[] nodeNames = devicePath.getNodes(); MetaFormatUtils.checkTimeseries(devicePath); if (nodeNames.length == levelOfSG + 1) { return null; } - IMNode cur = storageGroupMNode; - IMNode child; + ICachedMNode cur = storageGroupMNode; + ICachedMNode child; String childName; try { // e.g, path = root.sg.d1.s1, create internal nodes and set cur to sg node, parent of d1 @@ -471,7 +471,7 @@ private IMNode checkAndAutoCreateInternalPath(PartialPath devicePath) throws Met childName = nodeNames[i]; child = store.getChild(cur, childName); if (child == null) { - child = store.addChild(cur, childName, new InternalMNode(cur, childName)); + child = store.addChild(cur, childName, nodeFactory.createInternalMNode(cur, childName)); } cur = child; @@ -486,17 +486,18 @@ private IMNode checkAndAutoCreateInternalPath(PartialPath devicePath) throws Met } } - private IMNode checkAndAutoCreateDeviceNode(String deviceName, IMNode deviceParent) + private ICachedMNode checkAndAutoCreateDeviceNode(String deviceName, ICachedMNode deviceParent) throws MetadataException { if (deviceParent == null) { // device is sg pinMNode(storageGroupMNode); return storageGroupMNode; } - IMNode device = store.getChild(deviceParent, deviceName); + ICachedMNode device = store.getChild(deviceParent, deviceName); if (device == null) { device = - store.addChild(deviceParent, deviceName, new InternalMNode(deviceParent, deviceName)); + store.addChild( + deviceParent, deviceName, nodeFactory.createInternalMNode(deviceParent, deviceName)); } if (device.isMeasurement()) { @@ -510,21 +511,21 @@ private IMNode checkAndAutoCreateDeviceNode(String deviceName, IMNode devicePare * * @param path Format: root.node(.node)+ */ - @Override - public IMeasurementMNode deleteTimeseries(PartialPath path) throws MetadataException { + public IMeasurementMNode deleteTimeseries(PartialPath path) + throws MetadataException { String[] nodes = path.getNodes(); if (nodes.length == 0) { throw new IllegalPathException(path.getFullPath()); } - IMeasurementMNode deletedNode = getMeasurementMNode(path); - IEntityMNode parent = deletedNode.getParent(); + IMeasurementMNode deletedNode = getMeasurementMNode(path); + ICachedMNode parent = deletedNode.getParent(); // delete the last node of path store.deleteChild(parent, path.getMeasurement()); if (deletedNode.getAlias() != null) { - parent.deleteAliasChild(deletedNode.getAlias()); + parent.getAsDeviceMNode().deleteAliasChild(deletedNode.getAlias()); } - deleteAndUnpinEmptyInternalMNode(parent); + deleteAndUnpinEmptyInternalMNode(parent.getAsDeviceMNode()); return deletedNode; } @@ -534,12 +535,13 @@ public IMeasurementMNode deleteTimeseries(PartialPath path) throws MetadataExcep * * @param entityMNode delete empty InternalMNode from entityMNode to storageGroupMNode */ - private void deleteAndUnpinEmptyInternalMNode(IEntityMNode entityMNode) throws MetadataException { - IMNode curNode = entityMNode; + private void deleteAndUnpinEmptyInternalMNode(IDeviceMNode entityMNode) + throws MetadataException { + ICachedMNode curNode = entityMNode.getAsMNode(); if (!entityMNode.isUseTemplate()) { boolean hasMeasurement = false; - IMNode child; - IMNodeIterator iterator = store.getChildrenIterator(entityMNode); + ICachedMNode child; + IMNodeIterator iterator = store.getChildrenIterator(curNode); try { while (iterator.hasNext()) { child = iterator.next(); @@ -556,8 +558,8 @@ private void deleteAndUnpinEmptyInternalMNode(IEntityMNode entityMNode) throws M if (!hasMeasurement) { synchronized (this) { curNode = store.setToInternal(entityMNode); - if (curNode.isStorageGroup()) { - replaceStorageGroupMNode(curNode.getAsStorageGroupMNode()); + if (curNode.isDatabase()) { + replaceStorageGroupMNode(curNode.getAsDatabaseMNode()); } } } @@ -566,7 +568,7 @@ private void deleteAndUnpinEmptyInternalMNode(IEntityMNode entityMNode) throws M // delete all empty ancestors except database and MeasurementMNode while (isEmptyInternalMNode(curNode)) { // if current database has no time series, return the database name - if (curNode.isStorageGroup()) { + if (curNode.isDatabase()) { return; } store.deleteChild(curNode.getParent(), curNode.getName()); @@ -575,30 +577,29 @@ private void deleteAndUnpinEmptyInternalMNode(IEntityMNode entityMNode) throws M unPinMNode(curNode); } - @Override - public boolean isEmptyInternalMNode(IMNode node) throws MetadataException { - IMNodeIterator iterator = store.getChildrenIterator(node); + private boolean isEmptyInternalMNode(ICachedMNode node) throws MetadataException { + IMNodeIterator iterator = store.getChildrenIterator(node); try { return !IoTDBConstant.PATH_ROOT.equals(node.getName()) && !node.isMeasurement() - && !node.isUseTemplate() + && !(node.isDevice() && node.getAsDeviceMNode().isUseTemplate()) && !iterator.hasNext(); } finally { iterator.close(); } } - @Override public List constructSchemaBlackList(PartialPath pathPattern) throws MetadataException { List result = new ArrayList<>(); - try (MeasurementUpdater updater = - new MeasurementUpdater(rootNode, pathPattern, store, false) { - @Override - protected void updateMeasurement(IMeasurementMNode node) throws MetadataException { + try (MeasurementUpdater updater = + new MeasurementUpdater(rootNode, pathPattern, store, false) { + + protected void updateMeasurement(IMeasurementMNode node) + throws MetadataException { node.setPreDeleted(true); - store.updateMNode(node); - result.add(getPartialPathFromRootToNode(node)); + store.updateMNode(node.getAsMNode()); + result.add(getPartialPathFromRootToNode(node.getAsMNode())); } }) { updater.update(); @@ -606,17 +607,17 @@ protected void updateMeasurement(IMeasurementMNode node) throws MetadataExceptio return result; } - @Override public List rollbackSchemaBlackList(PartialPath pathPattern) throws MetadataException { List result = new ArrayList<>(); - try (MeasurementUpdater updater = - new MeasurementUpdater(rootNode, pathPattern, store, false) { - @Override - protected void updateMeasurement(IMeasurementMNode node) throws MetadataException { + try (MeasurementUpdater updater = + new MeasurementUpdater(rootNode, pathPattern, store, false) { + + protected void updateMeasurement(IMeasurementMNode node) + throws MetadataException { node.setPreDeleted(false); - store.updateMNode(node); - result.add(getPartialPathFromRootToNode(node)); + store.updateMNode(node.getAsMNode()); + result.add(getPartialPathFromRootToNode(node.getAsMNode())); } }) { updater.update(); @@ -624,16 +625,15 @@ protected void updateMeasurement(IMeasurementMNode node) throws MetadataExceptio return result; } - @Override public List getPreDeletedTimeseries(PartialPath pathPattern) throws MetadataException { List result = new LinkedList<>(); - try (MeasurementCollector collector = - new MeasurementCollector(rootNode, pathPattern, store, false) { - @Override - protected Void collectMeasurement(IMeasurementMNode node) { + try (MeasurementCollector collector = + new MeasurementCollector(rootNode, pathPattern, store, false) { + + protected Void collectMeasurement(IMeasurementMNode node) { if (node.isPreDeleted()) { - result.add(getPartialPathFromRootToNode(node)); + result.add(getPartialPathFromRootToNode(node.getAsMNode())); } return null; } @@ -643,16 +643,15 @@ protected Void collectMeasurement(IMeasurementMNode node) { return result; } - @Override public Set getDevicesOfPreDeletedTimeseries(PartialPath pathPattern) throws MetadataException { Set result = new HashSet<>(); - try (MeasurementCollector collector = - new MeasurementCollector(rootNode, pathPattern, store, false) { - @Override - protected Void collectMeasurement(IMeasurementMNode node) { + try (MeasurementCollector collector = + new MeasurementCollector(rootNode, pathPattern, store, false) { + + protected Void collectMeasurement(IMeasurementMNode node) { if (node.isPreDeleted()) { - result.add(getPartialPathFromRootToNode(node).getDevicePath()); + result.add(getPartialPathFromRootToNode(node.getAsMNode()).getDevicePath()); } return null; } @@ -663,8 +662,8 @@ protected Void collectMeasurement(IMeasurementMNode node) { return result; } - @Override - public void setAlias(IMeasurementMNode measurementMNode, String alias) throws MetadataException { + public void setAlias(IMeasurementMNode measurementMNode, String alias) + throws MetadataException { store.setAlias(measurementMNode, alias); } @@ -677,17 +676,17 @@ public void setAlias(IMeasurementMNode measurementMNode, String alias) throws Me * *

    e.g., get root.sg.d1, get or create all internal nodes and return the node of d1 */ - @Override - public IMNode getDeviceNodeWithAutoCreating(PartialPath deviceId) throws MetadataException { + public ICachedMNode getDeviceNodeWithAutoCreating(PartialPath deviceId) throws MetadataException { String[] nodeNames = deviceId.getNodes(); MetaFormatUtils.checkTimeseries(deviceId); - IMNode cur = storageGroupMNode; - IMNode child; + ICachedMNode cur = storageGroupMNode; + ICachedMNode child; try { for (int i = levelOfSG + 1; i < nodeNames.length; i++) { child = store.getChild(cur, nodeNames[i]); if (child == null) { - child = store.addChild(cur, nodeNames[i], new InternalMNode(cur, nodeNames[i])); + child = + store.addChild(cur, nodeNames[i], nodeFactory.createInternalMNode(cur, nodeNames[i])); } cur = child; } @@ -701,15 +700,14 @@ public IMNode getDeviceNodeWithAutoCreating(PartialPath deviceId) throws Metadat // region Interfaces and Implementation for metadata info Query - @Override public List fetchSchema( PartialPath pathPattern, Map templateMap, boolean withTags) throws MetadataException { List result = new LinkedList<>(); - try (MeasurementCollector collector = - new MeasurementCollector(rootNode, pathPattern, store, false) { - @Override - protected Void collectMeasurement(IMeasurementMNode node) { + try (MeasurementCollector collector = + new MeasurementCollector(rootNode, pathPattern, store, false) { + + protected Void collectMeasurement(IMeasurementMNode node) { if (node.isPreDeleted()) { return null; } @@ -725,7 +723,7 @@ protected Void collectMeasurement(IMeasurementMNode node) { return null; } }) { - collector.setTemplateMap(templateMap); + collector.setTemplateMap(templateMap, nodeFactory); collector.traverse(); } return result; @@ -739,11 +737,10 @@ protected Void collectMeasurement(IMeasurementMNode node) { * * @return last node in given seriesPath */ - @Override - public IMNode getNodeByPath(PartialPath path) throws MetadataException { + public ICachedMNode getNodeByPath(PartialPath path) throws MetadataException { String[] nodes = path.getNodes(); - IMNode cur = storageGroupMNode; - IMNode next; + ICachedMNode cur = storageGroupMNode; + ICachedMNode next; try { for (int i = levelOfSG + 1; i < nodes.length; i++) { next = store.getChild(cur, nodes[i]); @@ -765,9 +762,9 @@ public IMNode getNodeByPath(PartialPath path) throws MetadataException { } } - @Override - public IMeasurementMNode getMeasurementMNode(PartialPath path) throws MetadataException { - IMNode node = getNodeByPath(path); + public IMeasurementMNode getMeasurementMNode(PartialPath path) + throws MetadataException { + ICachedMNode node = getNodeByPath(path); if (node.isMeasurement()) { return node.getAsMeasurementMNode(); } else { @@ -777,10 +774,9 @@ public IMeasurementMNode getMeasurementMNode(PartialPath path) throws MetadataEx } } - @Override public long countAllMeasurement() throws MetadataException { - try (MeasurementCounter measurementCounter = - new MeasurementCounter(rootNode, MetadataConstant.ALL_MATCH_PATTERN, store, false)) { + try (MeasurementCounter measurementCounter = + new MeasurementCounter<>(rootNode, MetadataConstant.ALL_MATCH_PATTERN, store, false)) { return measurementCounter.count(); } } @@ -788,13 +784,12 @@ public long countAllMeasurement() throws MetadataException { // region Interfaces and Implementation for Template check and query - @Override public void activateTemplate(PartialPath activatePath, Template template) throws MetadataException { String[] nodes = activatePath.getNodes(); - IMNode cur = storageGroupMNode; - IMNode child; - IEntityMNode entityMNode; + ICachedMNode cur = storageGroupMNode; + ICachedMNode child; + IDeviceMNode entityMNode; try { for (int i = levelOfSG + 1; i < nodes.length; i++) { @@ -812,20 +807,20 @@ public void activateTemplate(PartialPath activatePath, Template template) } } - if (cur.isUseTemplate()) { - if (template.getId() == cur.getSchemaTemplateId()) { - throw new TemplateIsInUseException(cur.getFullPath()); - } else { - throw new DifferentTemplateException(activatePath.getFullPath(), template.getName()); + if (cur.isDevice()) { + entityMNode = cur.getAsDeviceMNode(); + } else { + entityMNode = store.setToEntity(cur); + if (entityMNode.isDatabase()) { + replaceStorageGroupMNode(entityMNode.getAsDatabaseMNode()); } } - if (cur.isEntity()) { - entityMNode = cur.getAsEntityMNode(); - } else { - entityMNode = store.setToEntity(cur); - if (entityMNode.isStorageGroup()) { - replaceStorageGroupMNode(entityMNode.getAsStorageGroupMNode()); + if (entityMNode.isUseTemplate()) { + if (template.getId() == entityMNode.getSchemaTemplateId()) { + throw new TemplateIsInUseException(cur.getFullPath()); + } else { + throw new DifferentTemplateException(activatePath.getFullPath(), template.getName()); } } } @@ -836,26 +831,25 @@ public void activateTemplate(PartialPath activatePath, Template template) entityMNode.setUseTemplate(true); entityMNode.setSchemaTemplateId(template.getId()); - store.updateMNode(entityMNode); + store.updateMNode(entityMNode.getAsMNode()); } finally { unPinPath(cur); } } - @Override public Map> constructSchemaBlackListWithTemplate( Map> templateSetInfo) throws MetadataException { Map> resultTemplateSetInfo = new HashMap<>(); for (Map.Entry> entry : templateSetInfo.entrySet()) { - try (EntityUpdater updater = - new EntityUpdater(rootNode, entry.getKey(), store, false) { - @Override - protected void updateEntity(IEntityMNode node) throws MetadataException { + try (EntityUpdater updater = + new EntityUpdater(rootNode, entry.getKey(), store, false) { + + protected void updateEntity(IDeviceMNode node) throws MetadataException { if (entry.getValue().contains(node.getSchemaTemplateId())) { resultTemplateSetInfo.put( node.getPartialPath(), Collections.singletonList(node.getSchemaTemplateId())); node.preDeactivateTemplate(); - store.updateMNode(node); + store.updateMNode(node.getAsMNode()); } } }) { @@ -865,21 +859,20 @@ protected void updateEntity(IEntityMNode node) throws MetadataException { return resultTemplateSetInfo; } - @Override public Map> rollbackSchemaBlackListWithTemplate( Map> templateSetInfo) throws MetadataException { Map> resultTemplateSetInfo = new HashMap<>(); for (Map.Entry> entry : templateSetInfo.entrySet()) { - try (EntityUpdater updater = - new EntityUpdater(rootNode, entry.getKey(), store, false) { - @Override - protected void updateEntity(IEntityMNode node) throws MetadataException { + try (EntityUpdater updater = + new EntityUpdater(rootNode, entry.getKey(), store, false) { + + protected void updateEntity(IDeviceMNode node) throws MetadataException { if (entry.getValue().contains(node.getSchemaTemplateId()) && node.isPreDeactivateTemplate()) { resultTemplateSetInfo.put( node.getPartialPath(), Collections.singletonList(node.getSchemaTemplateId())); node.rollbackPreDeactivateTemplate(); - store.updateMNode(node); + store.updateMNode(node.getAsMNode()); } } }) { @@ -889,21 +882,20 @@ protected void updateEntity(IEntityMNode node) throws MetadataException { return resultTemplateSetInfo; } - @Override public Map> deactivateTemplateInBlackList( Map> templateSetInfo) throws MetadataException { Map> resultTemplateSetInfo = new HashMap<>(); for (Map.Entry> entry : templateSetInfo.entrySet()) { - try (EntityUpdater collector = - new EntityUpdater(rootNode, entry.getKey(), store, false) { - @Override - protected void updateEntity(IEntityMNode node) throws MetadataException { + try (EntityUpdater collector = + new EntityUpdater(rootNode, entry.getKey(), store, false) { + + protected void updateEntity(IDeviceMNode node) throws MetadataException { if (entry.getValue().contains(node.getSchemaTemplateId()) && node.isPreDeactivateTemplate()) { resultTemplateSetInfo.put( node.getPartialPath(), Collections.singletonList(node.getSchemaTemplateId())); node.deactivateTemplate(); - store.updateMNode(node); + store.updateMNode(node.getAsMNode()); } } }) { @@ -911,15 +903,15 @@ protected void updateEntity(IEntityMNode node) throws MetadataException { } } for (PartialPath path : resultTemplateSetInfo.keySet()) { - deleteAndUnpinEmptyInternalMNode(getNodeByPath(path).getAsEntityMNode()); + deleteAndUnpinEmptyInternalMNode(getNodeByPath(path).getAsDeviceMNode()); } return resultTemplateSetInfo; } - @Override public long countPathsUsingTemplate(PartialPath pathPattern, int templateId) throws MetadataException { - try (EntityCounter counter = new EntityCounter(rootNode, pathPattern, store, false)) { + try (EntityCounter counter = + new EntityCounter(rootNode, pathPattern, store, false)) { counter.setSchemaTemplateFilter(templateId); return counter.count(); } @@ -935,21 +927,21 @@ public long countPathsUsingTemplate(PartialPath pathPattern, int templateId) * @param node */ // TODO: This interface should not be exposed to SchemaRegion - public void pinMNode(IMNode node) throws MetadataException { + public void pinMNode(ICachedMNode node) throws MetadataException { store.pin(node); } // TODO: This interface should not be exposed to SchemaRegion - public void unPinMNode(IMNode node) { + public void unPinMNode(ICachedMNode node) { store.unPin(node); } - private void unPinPath(IMNode node) { + private void unPinPath(ICachedMNode node) { store.unPinPath(node); } // TODO: This interface should not be exposed to SchemaRegion - public void updateMNode(IMNode node) throws MetadataException { + public void updateMNode(ICachedMNode node) throws MetadataException { store.updateMNode(node); } @@ -958,43 +950,39 @@ public void updateMNode(IMNode node) throws MetadataException { // region Interfaces for schema reader public ISchemaReader getDeviceReader(IShowDevicesPlan showDevicesPlan) throws MetadataException { - EntityCollector collector = - new EntityCollector( + EntityCollector collector = + new EntityCollector( rootNode, showDevicesPlan.getPath(), store, showDevicesPlan.isPrefixMatch()) { - @Override - protected IDeviceSchemaInfo collectEntity(IEntityMNode node) { - PartialPath device = getPartialPathFromRootToNode(node); + + protected IDeviceSchemaInfo collectEntity(IDeviceMNode node) { + PartialPath device = getPartialPathFromRootToNode(node.getAsMNode()); return new ShowDevicesResult(device.getFullPath(), node.isAligned()); } }; if (showDevicesPlan.usingSchemaTemplate()) { collector.setSchemaTemplateFilter(showDevicesPlan.getSchemaTemplateId()); } - TraverserWithLimitOffsetWrapper traverser = + TraverserWithLimitOffsetWrapper traverser = new TraverserWithLimitOffsetWrapper<>( collector, showDevicesPlan.getLimit(), showDevicesPlan.getOffset()); return new ISchemaReader() { - @Override + public boolean isSuccess() { return traverser.isSuccess(); } - @Override public Throwable getFailure() { return traverser.getFailure(); } - @Override public void close() { traverser.close(); } - @Override public boolean hasNext() { return traverser.hasNext(); } - @Override public IDeviceSchemaInfo next() { return traverser.next(); } @@ -1005,26 +993,23 @@ public ISchemaReader getTimeSeriesReader( IShowTimeSeriesPlan showTimeSeriesPlan, Function, Map>> tagAndAttributeProvider) throws MetadataException { - MeasurementCollector collector = - new MeasurementCollector( + MeasurementCollector collector = + new MeasurementCollector( rootNode, showTimeSeriesPlan.getPath(), store, showTimeSeriesPlan.isPrefixMatch()) { - @Override - protected ITimeSeriesSchemaInfo collectMeasurement(IMeasurementMNode node) { + + protected ITimeSeriesSchemaInfo collectMeasurement(IMeasurementMNode node) { return new ITimeSeriesSchemaInfo() { private Pair, Map> tagAndAttribute = null; - @Override public String getAlias() { return node.getAlias(); } - @Override public MeasurementSchema getSchema() { return (MeasurementSchema) node.getSchema(); } - @Override public Map getTags() { if (tagAndAttribute == null) { tagAndAttribute = tagAndAttributeProvider.apply(node.getOffset()); @@ -1032,7 +1017,6 @@ public Map getTags() { return tagAndAttribute.left; } - @Override public Map getAttributes() { if (tagAndAttribute == null) { tagAndAttribute = tagAndAttributeProvider.apply(node.getOffset()); @@ -1040,26 +1024,23 @@ public Map getAttributes() { return tagAndAttribute.right; } - @Override public boolean isUnderAlignedDevice() { - return getParentOfNextMatchedNode().getAsEntityMNode().isAligned(); + return getParentOfNextMatchedNode().getAsDeviceMNode().isAligned(); } - @Override public String getFullPath() { - return getPartialPathFromRootToNode(node).getFullPath(); + return getPartialPathFromRootToNode(node.getAsMNode()).getFullPath(); } - @Override public PartialPath getPartialPath() { - return getPartialPathFromRootToNode(node); + return getPartialPathFromRootToNode(node.getAsMNode()); } }; } }; - collector.setTemplateMap(showTimeSeriesPlan.getRelatedTemplate()); - Traverser traverser; + collector.setTemplateMap(showTimeSeriesPlan.getRelatedTemplate(), nodeFactory); + Traverser traverser; if (showTimeSeriesPlan.getLimit() > 0 || showTimeSeriesPlan.getOffset() > 0) { traverser = new TraverserWithLimitOffsetWrapper<>( @@ -1068,27 +1049,23 @@ public PartialPath getPartialPath() { traverser = collector; } return new ISchemaReader() { - @Override + public boolean isSuccess() { return traverser.isSuccess(); } - @Override public Throwable getFailure() { return traverser.getFailure(); } - @Override public void close() { traverser.close(); } - @Override public boolean hasNext() { return traverser.hasNext(); } - @Override public ITimeSeriesSchemaInfo next() { return traverser.next(); } @@ -1097,38 +1074,34 @@ public ITimeSeriesSchemaInfo next() { public ISchemaReader getNodeReader(IShowNodesPlan showNodesPlan) throws MetadataException { - MNodeCollector collector = - new MNodeCollector( + MNodeCollector collector = + new MNodeCollector( rootNode, showNodesPlan.getPath(), store, showNodesPlan.isPrefixMatch()) { - @Override - protected INodeSchemaInfo collectMNode(IMNode node) { + + protected INodeSchemaInfo collectMNode(ICachedMNode node) { return new ShowNodesResult( getPartialPathFromRootToNode(node).getFullPath(), node.getMNodeType(false)); } }; collector.setTargetLevel(showNodesPlan.getLevel()); return new ISchemaReader() { - @Override + public boolean isSuccess() { return collector.isSuccess(); } - @Override public Throwable getFailure() { return collector.getFailure(); } - @Override public void close() { collector.close(); } - @Override public boolean hasNext() { return collector.hasNext(); } - @Override public INodeSchemaInfo next() { return collector.next(); } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGMemoryImpl.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGMemoryImpl.java index 7d1c397104d98..7e3c558b1c862 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGMemoryImpl.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGMemoryImpl.java @@ -23,6 +23,11 @@ import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.path.MeasurementPath; import org.apache.iotdb.commons.path.PartialPath; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; +import org.apache.iotdb.commons.schema.node.utils.IMNodeIterator; import org.apache.iotdb.db.exception.metadata.AliasAlreadyExistException; import org.apache.iotdb.db.exception.metadata.AlignedTimeseriesException; import org.apache.iotdb.db.exception.metadata.MNodeTypeMismatchException; @@ -34,13 +39,8 @@ import org.apache.iotdb.db.exception.metadata.template.TemplateImcompatibeException; import org.apache.iotdb.db.exception.metadata.template.TemplateIsInUseException; import org.apache.iotdb.db.metadata.MetadataConstant; -import org.apache.iotdb.db.metadata.mnode.IEntityMNode; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.IStorageGroupMNode; -import org.apache.iotdb.db.metadata.mnode.InternalMNode; -import org.apache.iotdb.db.metadata.mnode.MeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.iterator.IMNodeIterator; +import org.apache.iotdb.db.metadata.mnode.mem.IMemMNode; +import org.apache.iotdb.db.metadata.mnode.mem.factory.MemMNodeFactory; import org.apache.iotdb.db.metadata.mtree.store.MemMTreeStore; import org.apache.iotdb.db.metadata.mtree.traverser.Traverser; import org.apache.iotdb.db.metadata.mtree.traverser.TraverserWithLimitOffsetWrapper; @@ -101,22 +101,23 @@ *

  • Interfaces and Implementation for Template check * */ -public class MTreeBelowSGMemoryImpl implements IMTreeBelowSG { +public class MTreeBelowSGMemoryImpl { // this implementation is based on memory, thus only MTree write operation must invoke MTreeStore private final MemMTreeStore store; - private volatile IStorageGroupMNode storageGroupMNode; - private final IMNode rootNode; - private final Function> tagGetter; + private volatile IMemMNode storageGroupMNode; + private final IMemMNode rootNode; + private final Function, Map> tagGetter; + private final IMNodeFactory nodeFactory = MemMNodeFactory.getInstance(); private final int levelOfSG; // region MTree initialization, clear and serialization public MTreeBelowSGMemoryImpl( PartialPath storageGroupPath, - Function> tagGetter, + Function, Map> tagGetter, MemSchemaRegionStatistics regionStatistics) { - store = new MemMTreeStore(storageGroupPath, true, regionStatistics); - this.storageGroupMNode = store.getRoot().getAsStorageGroupMNode(); + store = new MemMTreeStore(storageGroupPath, regionStatistics); + this.storageGroupMNode = store.getRoot(); this.rootNode = store.generatePrefix(storageGroupPath); levelOfSG = storageGroupPath.getNodeLength() - 1; this.tagGetter = tagGetter; @@ -125,26 +126,26 @@ public MTreeBelowSGMemoryImpl( private MTreeBelowSGMemoryImpl( PartialPath storageGroupPath, MemMTreeStore store, - Function> tagGetter) { + Function, Map> tagGetter) { this.store = store; - this.storageGroupMNode = store.getRoot().getAsStorageGroupMNode(); + this.storageGroupMNode = store.getRoot(); this.rootNode = store.generatePrefix(storageGroupPath); levelOfSG = storageGroupPath.getNodeLength() - 1; this.tagGetter = tagGetter; } - @Override public void clear() { store.clear(); storageGroupMNode = null; } - protected void replaceStorageGroupMNode(IStorageGroupMNode newMNode) { - this.storageGroupMNode.getParent().replaceChild(this.storageGroupMNode.getName(), newMNode); - this.storageGroupMNode = newMNode; + protected void replaceStorageGroupMNode(IDatabaseMNode newMNode) { + this.storageGroupMNode + .getParent() + .replaceChild(this.storageGroupMNode.getName(), newMNode.getAsMNode()); + this.storageGroupMNode = newMNode.getAsMNode(); } - @Override public synchronized boolean createSnapshot(File snapshotDir) { return store.createSnapshot(snapshotDir); } @@ -153,8 +154,8 @@ public static MTreeBelowSGMemoryImpl loadFromSnapshot( File snapshotDir, String storageGroupFullPath, MemSchemaRegionStatistics regionStatistics, - Consumer measurementProcess, - Function> tagGetter) + Consumer> measurementProcess, + Function, Map> tagGetter) throws IOException, IllegalPathException { return new MTreeBelowSGMemoryImpl( new PartialPath(storageGroupFullPath), @@ -177,8 +178,7 @@ public static MTreeBelowSGMemoryImpl loadFromSnapshot( * @param props props * @param alias alias of measurement */ - @Override - public IMeasurementMNode createTimeseries( + public IMeasurementMNode createTimeseries( PartialPath path, TSDataType dataType, TSEncoding encoding, @@ -192,12 +192,12 @@ public IMeasurementMNode createTimeseries( } MetaFormatUtils.checkTimeseries(path); PartialPath devicePath = path.getDevicePath(); - IMNode deviceParent = checkAndAutoCreateInternalPath(devicePath); + IMemMNode deviceParent = checkAndAutoCreateInternalPath(devicePath); // synchronize check and add, we need addChild and add Alias become atomic operation // only write on mtree will be synchronized synchronized (this) { - IMNode device = checkAndAutoCreateDeviceNode(devicePath.getTailNode(), deviceParent); + IMemMNode device = checkAndAutoCreateDeviceNode(devicePath.getTailNode(), deviceParent); MetaFormatUtils.checkTimeseriesProps(path.getFullPath(), props); @@ -208,7 +208,7 @@ public IMeasurementMNode createTimeseries( } if (device.hasChild(leafName)) { - IMNode node = device.getChild(leafName); + IMemMNode node = device.getChild(leafName); if (node.isMeasurement()) { if (node.getAsMeasurementMNode().isPreDeleted()) { throw new MeasurementInBlackListException(path); @@ -221,29 +221,29 @@ public IMeasurementMNode createTimeseries( } } - if (device.isEntity() && device.getAsEntityMNode().isAligned()) { + if (device.isDevice() && device.getAsDeviceMNode().isAligned()) { throw new AlignedTimeseriesException( "timeseries under this entity is aligned, please use createAlignedTimeseries or change entity.", device.getFullPath()); } - IEntityMNode entityMNode; - if (device.isEntity()) { - entityMNode = device.getAsEntityMNode(); + IDeviceMNode entityMNode; + if (device.isDevice()) { + entityMNode = device.getAsDeviceMNode(); } else { entityMNode = store.setToEntity(device); - if (entityMNode.isStorageGroup()) { - replaceStorageGroupMNode(entityMNode.getAsStorageGroupMNode()); + if (entityMNode.isDatabase()) { + replaceStorageGroupMNode(entityMNode.getAsDatabaseMNode()); } } - IMeasurementMNode measurementMNode = - MeasurementMNode.getMeasurementMNode( + IMeasurementMNode measurementMNode = + nodeFactory.createMeasurementMNode( entityMNode, leafName, new MeasurementSchema(leafName, dataType, encoding, compressor, props), alias); - store.addChild(entityMNode, leafName, measurementMNode); + store.addChild(entityMNode.getAsMNode(), leafName, measurementMNode.getAsMNode()); // link alias to LeafMNode if (alias != null) { entityMNode.addAlias(alias, measurementMNode); @@ -262,8 +262,7 @@ public IMeasurementMNode createTimeseries( * @param encodings encodings list * @param compressors compressor */ - @Override - public List createAlignedTimeseries( + public List> createAlignedTimeseries( PartialPath devicePath, List measurements, List dataTypes, @@ -271,18 +270,18 @@ public List createAlignedTimeseries( List compressors, List aliasList) throws MetadataException { - List measurementMNodeList = new ArrayList<>(); + List> measurementMNodeList = new ArrayList<>(); MetaFormatUtils.checkSchemaMeasurementNames(measurements); - IMNode deviceParent = checkAndAutoCreateInternalPath(devicePath); + IMemMNode deviceParent = checkAndAutoCreateInternalPath(devicePath); // synchronize check and add, we need addChild operation be atomic. // only write operations on mtree will be synchronized synchronized (this) { - IMNode device = checkAndAutoCreateDeviceNode(devicePath.getTailNode(), deviceParent); + IMemMNode device = checkAndAutoCreateDeviceNode(devicePath.getTailNode(), deviceParent); for (int i = 0; i < measurements.size(); i++) { if (device.hasChild(measurements.get(i))) { - IMNode node = device.getChild(measurements.get(i)); + IMemMNode node = device.getChild(measurements.get(i)); if (node.isMeasurement()) { if (node.getAsMeasurementMNode().isPreDeleted()) { throw new MeasurementInBlackListException(devicePath.concatNode(measurements.get(i))); @@ -302,32 +301,33 @@ public List createAlignedTimeseries( } } - if (device.isEntity() && !device.getAsEntityMNode().isAligned()) { + if (device.isDevice() && !device.getAsDeviceMNode().isAligned()) { throw new AlignedTimeseriesException( "Timeseries under this entity is not aligned, please use createTimeseries or change entity.", devicePath.getFullPath()); } - IEntityMNode entityMNode; - if (device.isEntity()) { - entityMNode = device.getAsEntityMNode(); + IDeviceMNode entityMNode; + if (device.isDevice()) { + entityMNode = device.getAsDeviceMNode(); } else { entityMNode = store.setToEntity(device); entityMNode.setAligned(true); - if (entityMNode.isStorageGroup()) { - replaceStorageGroupMNode(entityMNode.getAsStorageGroupMNode()); + if (entityMNode.isDatabase()) { + replaceStorageGroupMNode(entityMNode.getAsDatabaseMNode()); } } for (int i = 0; i < measurements.size(); i++) { - IMeasurementMNode measurementMNode = - MeasurementMNode.getMeasurementMNode( + IMeasurementMNode measurementMNode = + nodeFactory.createMeasurementMNode( entityMNode, measurements.get(i), new MeasurementSchema( measurements.get(i), dataTypes.get(i), encodings.get(i), compressors.get(i)), aliasList == null ? null : aliasList.get(i)); - store.addChild(entityMNode, measurements.get(i), measurementMNode); + store.addChild( + entityMNode.getAsMNode(), measurements.get(i), measurementMNode.getAsMNode()); if (aliasList != null && aliasList.get(i) != null) { entityMNode.addAlias(aliasList.get(i), measurementMNode); } @@ -337,21 +337,22 @@ public List createAlignedTimeseries( } } - private IMNode checkAndAutoCreateInternalPath(PartialPath devicePath) throws MetadataException { + private IMemMNode checkAndAutoCreateInternalPath(PartialPath devicePath) + throws MetadataException { String[] nodeNames = devicePath.getNodes(); MetaFormatUtils.checkTimeseries(devicePath); if (nodeNames.length == levelOfSG + 1) { return null; } - IMNode cur = storageGroupMNode; - IMNode child; + IMemMNode cur = storageGroupMNode; + IMemMNode child; String childName; // e.g, path = root.sg.d1.s1, create internal nodes and set cur to sg node, parent of d1 for (int i = levelOfSG + 1; i < nodeNames.length - 1; i++) { childName = nodeNames[i]; child = cur.getChild(childName); if (child == null) { - child = store.addChild(cur, childName, new InternalMNode(cur, childName)); + child = store.addChild(cur, childName, nodeFactory.createInternalMNode(cur, childName)); } cur = child; @@ -362,16 +363,17 @@ private IMNode checkAndAutoCreateInternalPath(PartialPath devicePath) throws Met return cur; } - private IMNode checkAndAutoCreateDeviceNode(String deviceName, IMNode deviceParent) + private IMemMNode checkAndAutoCreateDeviceNode(String deviceName, IMemMNode deviceParent) throws PathAlreadyExistException { if (deviceParent == null) { // device is sg return storageGroupMNode; } - IMNode device = store.getChild(deviceParent, deviceName); + IMemMNode device = store.getChild(deviceParent, deviceName); if (device == null) { device = - store.addChild(deviceParent, deviceName, new InternalMNode(deviceParent, deviceName)); + store.addChild( + deviceParent, deviceName, nodeFactory.createInternalMNode(deviceParent, deviceName)); } if (device.isMeasurement()) { @@ -380,23 +382,22 @@ private IMNode checkAndAutoCreateDeviceNode(String deviceName, IMNode devicePare return device; } - @Override public Map checkMeasurementExistence( PartialPath devicePath, List measurementList, List aliasList) { - IMNode device; + IMemMNode device; try { device = getNodeByPath(devicePath); } catch (PathNotExistException e) { return Collections.emptyMap(); } - if (!device.isEntity()) { + if (!device.isDevice()) { return Collections.emptyMap(); } Map failingMeasurementMap = new HashMap<>(); for (int i = 0; i < measurementList.size(); i++) { if (device.hasChild(measurementList.get(i))) { - IMNode node = device.getChild(measurementList.get(i)); + IMemMNode node = device.getChild(measurementList.get(i)); if (node.isMeasurement()) { if (node.getAsMeasurementMNode().isPreDeleted()) { failingMeasurementMap.put( @@ -430,31 +431,30 @@ public Map checkMeasurementExistence( * * @param path Format: root.node(.node)+ */ - @Override - public IMeasurementMNode deleteTimeseries(PartialPath path) throws MetadataException { + public IMeasurementMNode deleteTimeseries(PartialPath path) throws MetadataException { String[] nodes = path.getNodes(); if (nodes.length == 0) { throw new IllegalPathException(path.getFullPath()); } - IMeasurementMNode deletedNode = getMeasurementMNode(path); - IEntityMNode parent = deletedNode.getParent(); + IMeasurementMNode deletedNode = getMeasurementMNode(path); + IMemMNode parent = deletedNode.getParent(); // delete the last node of path store.deleteChild(parent, path.getMeasurement()); if (deletedNode.getAlias() != null) { - parent.deleteAliasChild(deletedNode.getAlias()); + parent.getAsDeviceMNode().deleteAliasChild(deletedNode.getAlias()); } - deleteEmptyInternalMNode(parent); + deleteEmptyInternalMNode(parent.getAsDeviceMNode()); return deletedNode; } /** Used when delete timeseries or deactivate template */ - public void deleteEmptyInternalMNode(IEntityMNode entityMNode) { - IMNode curNode = entityMNode; + public void deleteEmptyInternalMNode(IDeviceMNode entityMNode) { + IMemMNode curNode = entityMNode.getAsMNode(); if (!entityMNode.isUseTemplate()) { boolean hasMeasurement = false; - IMNode child; - IMNodeIterator iterator = store.getChildrenIterator(entityMNode); + IMemMNode child; + IMNodeIterator iterator = store.getChildrenIterator(curNode); while (iterator.hasNext()) { child = iterator.next(); if (child.isMeasurement()) { @@ -466,8 +466,8 @@ public void deleteEmptyInternalMNode(IEntityMNode entityMNode) { if (!hasMeasurement) { synchronized (this) { curNode = store.setToInternal(entityMNode); - if (curNode.isStorageGroup()) { - replaceStorageGroupMNode(curNode.getAsStorageGroupMNode()); + if (curNode.isDatabase()) { + replaceStorageGroupMNode(curNode.getAsDatabaseMNode()); } } } @@ -476,7 +476,7 @@ public void deleteEmptyInternalMNode(IEntityMNode entityMNode) { // delete all empty ancestors except database and MeasurementMNode while (isEmptyInternalMNode(curNode)) { // if current database has no time series, return the database name - if (curNode.isStorageGroup()) { + if (curNode.isDatabase()) { return; } store.deleteChild(curNode.getParent(), curNode.getName()); @@ -484,24 +484,22 @@ public void deleteEmptyInternalMNode(IEntityMNode entityMNode) { } } - @Override - public boolean isEmptyInternalMNode(IMNode node) { + private boolean isEmptyInternalMNode(IMemMNode node) { return !IoTDBConstant.PATH_ROOT.equals(node.getName()) && !node.isMeasurement() - && !node.isUseTemplate() + && !(node.isDevice() && node.getAsDeviceMNode().isUseTemplate()) && node.getChildren().isEmpty(); } - @Override public List constructSchemaBlackList(PartialPath pathPattern) throws MetadataException { List result = new ArrayList<>(); - try (MeasurementUpdater updater = - new MeasurementUpdater(rootNode, pathPattern, store, false) { - @Override - protected void updateMeasurement(IMeasurementMNode node) { + try (MeasurementUpdater updater = + new MeasurementUpdater(rootNode, pathPattern, store, false) { + + protected void updateMeasurement(IMeasurementMNode node) { node.setPreDeleted(true); - result.add(getPartialPathFromRootToNode(node)); + result.add(getPartialPathFromRootToNode(node.getAsMNode())); } }) { updater.update(); @@ -509,16 +507,15 @@ protected void updateMeasurement(IMeasurementMNode node) { return result; } - @Override public List rollbackSchemaBlackList(PartialPath pathPattern) throws MetadataException { List result = new ArrayList<>(); - try (MeasurementUpdater updater = - new MeasurementUpdater(rootNode, pathPattern, store, false) { - @Override - protected void updateMeasurement(IMeasurementMNode node) { + try (MeasurementUpdater updater = + new MeasurementUpdater(rootNode, pathPattern, store, false) { + + protected void updateMeasurement(IMeasurementMNode node) { node.setPreDeleted(false); - result.add(getPartialPathFromRootToNode(node)); + result.add(getPartialPathFromRootToNode(node.getAsMNode())); } }) { updater.update(); @@ -526,16 +523,15 @@ protected void updateMeasurement(IMeasurementMNode node) { return result; } - @Override public List getPreDeletedTimeseries(PartialPath pathPattern) throws MetadataException { List result = new LinkedList<>(); - try (MeasurementCollector collector = - new MeasurementCollector(rootNode, pathPattern, store, false) { - @Override - protected Void collectMeasurement(IMeasurementMNode node) { + try (MeasurementCollector collector = + new MeasurementCollector(rootNode, pathPattern, store, false) { + + protected Void collectMeasurement(IMeasurementMNode node) { if (node.isPreDeleted()) { - result.add(getPartialPathFromRootToNode(node)); + result.add(getPartialPathFromRootToNode(node.getAsMNode())); } return null; } @@ -545,16 +541,15 @@ protected Void collectMeasurement(IMeasurementMNode node) { return result; } - @Override public Set getDevicesOfPreDeletedTimeseries(PartialPath pathPattern) throws MetadataException { Set result = new HashSet<>(); - try (MeasurementCollector collector = - new MeasurementCollector(rootNode, pathPattern, store, false) { - @Override - protected Void collectMeasurement(IMeasurementMNode node) { + try (MeasurementCollector collector = + new MeasurementCollector(rootNode, pathPattern, store, false) { + + protected Void collectMeasurement(IMeasurementMNode node) { if (node.isPreDeleted()) { - result.add(getPartialPathFromRootToNode(node).getDevicePath()); + result.add(getPartialPathFromRootToNode(node.getAsMNode()).getDevicePath()); } return null; } @@ -564,8 +559,8 @@ protected Void collectMeasurement(IMeasurementMNode node) { return result; } - @Override - public void setAlias(IMeasurementMNode measurementMNode, String alias) throws MetadataException { + public void setAlias(IMeasurementMNode measurementMNode, String alias) + throws MetadataException { store.setAlias(measurementMNode, alias); } @@ -578,16 +573,16 @@ public void setAlias(IMeasurementMNode measurementMNode, String alias) throws Me * *

    e.g., get root.sg.d1, get or create all internal nodes and return the node of d1 */ - @Override - public IMNode getDeviceNodeWithAutoCreating(PartialPath deviceId) throws MetadataException { + public IMemMNode getDeviceNodeWithAutoCreating(PartialPath deviceId) throws MetadataException { MetaFormatUtils.checkTimeseries(deviceId); String[] nodeNames = deviceId.getNodes(); - IMNode cur = storageGroupMNode; - IMNode child; + IMemMNode cur = storageGroupMNode; + IMemMNode child; for (int i = levelOfSG + 1; i < nodeNames.length; i++) { child = cur.getChild(nodeNames[i]); if (child == null) { - child = store.addChild(cur, nodeNames[i], new InternalMNode(cur, nodeNames[i])); + child = + store.addChild(cur, nodeNames[i], nodeFactory.createInternalMNode(cur, nodeNames[i])); } cur = child; } @@ -597,15 +592,14 @@ public IMNode getDeviceNodeWithAutoCreating(PartialPath deviceId) throws Metadat // region Interfaces and Implementation for metadata info Query - @Override public List fetchSchema( PartialPath pathPattern, Map templateMap, boolean withTags) throws MetadataException { List result = new LinkedList<>(); - try (MeasurementCollector collector = - new MeasurementCollector(rootNode, pathPattern, store, false) { - @Override - protected Void collectMeasurement(IMeasurementMNode node) { + try (MeasurementCollector collector = + new MeasurementCollector(rootNode, pathPattern, store, false) { + + protected Void collectMeasurement(IMeasurementMNode node) { MeasurementPath path = getCurrentMeasurementPathInTraverse(node); if (nodes[nodes.length - 1].equals(node.getAlias())) { // only when user query with alias, the alias in path will be set @@ -618,7 +612,7 @@ protected Void collectMeasurement(IMeasurementMNode node) { return null; } }) { - collector.setTemplateMap(templateMap); + collector.setTemplateMap(templateMap, nodeFactory); collector.setSkipPreDeletedSchema(true); collector.traverse(); } @@ -633,11 +627,10 @@ protected Void collectMeasurement(IMeasurementMNode node) { * * @return last node in given seriesPath */ - @Override - public IMNode getNodeByPath(PartialPath path) throws PathNotExistException { + public IMemMNode getNodeByPath(PartialPath path) throws PathNotExistException { String[] nodes = path.getNodes(); - IMNode cur = storageGroupMNode; - IMNode next; + IMemMNode cur = storageGroupMNode; + IMemMNode next; for (int i = levelOfSG + 1; i < nodes.length; i++) { next = cur.getChild(nodes[i]); if (next == null) { @@ -654,9 +647,9 @@ public IMNode getNodeByPath(PartialPath path) throws PathNotExistException { return cur; } - @Override - public IMeasurementMNode getMeasurementMNode(PartialPath path) throws MetadataException { - IMNode node = getNodeByPath(path); + public IMeasurementMNode getMeasurementMNode(PartialPath path) + throws MetadataException { + IMemMNode node = getNodeByPath(path); if (node.isMeasurement()) { return node.getAsMeasurementMNode(); } else { @@ -665,10 +658,9 @@ public IMeasurementMNode getMeasurementMNode(PartialPath path) throws MetadataEx } } - @Override public long countAllMeasurement() throws MetadataException { - try (MeasurementCounter measurementCounter = - new MeasurementCounter(rootNode, MetadataConstant.ALL_MATCH_PATTERN, store, false)) { + try (MeasurementCounter measurementCounter = + new MeasurementCounter<>(rootNode, MetadataConstant.ALL_MATCH_PATTERN, store, false)) { return measurementCounter.count(); } } @@ -677,16 +669,15 @@ public long countAllMeasurement() throws MetadataException { // region Interfaces and Implementation for Template check and query - @Override public void activateTemplate(PartialPath activatePath, Template template) throws MetadataException { String[] nodes = activatePath.getNodes(); - IMNode cur = storageGroupMNode; + IMemMNode cur = storageGroupMNode; for (int i = levelOfSG + 1; i < nodes.length; i++) { cur = cur.getChild(nodes[i]); } - IEntityMNode entityMNode; + IDeviceMNode entityMNode; synchronized (this) { for (String measurement : template.getSchemaMap().keySet()) { @@ -696,21 +687,21 @@ public void activateTemplate(PartialPath activatePath, Template template) } } - if (cur.isUseTemplate()) { - if (template.getId() == cur.getSchemaTemplateId()) { - throw new TemplateIsInUseException(cur.getFullPath()); - } else { - throw new DifferentTemplateException(activatePath.getFullPath(), template.getName()); + if (cur.isDevice()) { + entityMNode = cur.getAsDeviceMNode(); + } else { + entityMNode = store.setToEntity(cur); + if (entityMNode.isDatabase()) { + replaceStorageGroupMNode(entityMNode.getAsDatabaseMNode()); } } + } - if (cur.isEntity()) { - entityMNode = cur.getAsEntityMNode(); + if (entityMNode.isUseTemplate()) { + if (template.getId() == entityMNode.getSchemaTemplateId()) { + throw new TemplateIsInUseException(cur.getFullPath()); } else { - entityMNode = store.setToEntity(cur); - if (entityMNode.isStorageGroup()) { - replaceStorageGroupMNode(entityMNode.getAsStorageGroupMNode()); - } + throw new DifferentTemplateException(activatePath.getFullPath(), template.getName()); } } @@ -721,20 +712,18 @@ public void activateTemplate(PartialPath activatePath, Template template) entityMNode.setSchemaTemplateId(template.getId()); } - @Override public Map> constructSchemaBlackListWithTemplate( Map> templateSetInfo) throws MetadataException { Map> resultTemplateSetInfo = new HashMap<>(); for (Map.Entry> entry : templateSetInfo.entrySet()) { - try (EntityUpdater updater = - new EntityUpdater(rootNode, entry.getKey(), store, false) { - @Override - protected void updateEntity(IEntityMNode node) throws MetadataException { + try (EntityUpdater updater = + new EntityUpdater(rootNode, entry.getKey(), store, false) { + + protected void updateEntity(IDeviceMNode node) { if (entry.getValue().contains(node.getSchemaTemplateId())) { resultTemplateSetInfo.put( node.getPartialPath(), Collections.singletonList(node.getSchemaTemplateId())); node.preDeactivateTemplate(); - store.updateMNode(node); } } }) { @@ -744,15 +733,14 @@ protected void updateEntity(IEntityMNode node) throws MetadataException { return resultTemplateSetInfo; } - @Override public Map> rollbackSchemaBlackListWithTemplate( Map> templateSetInfo) throws MetadataException { Map> resultTemplateSetInfo = new HashMap<>(); for (Map.Entry> entry : templateSetInfo.entrySet()) { - try (EntityUpdater updater = - new EntityUpdater(rootNode, entry.getKey(), store, false) { - @Override - protected void updateEntity(IEntityMNode node) { + try (EntityUpdater updater = + new EntityUpdater(rootNode, entry.getKey(), store, false) { + + protected void updateEntity(IDeviceMNode node) { if (entry.getValue().contains(node.getSchemaTemplateId()) && node.isPreDeactivateTemplate()) { resultTemplateSetInfo.put( @@ -767,15 +755,14 @@ protected void updateEntity(IEntityMNode node) { return resultTemplateSetInfo; } - @Override public Map> deactivateTemplateInBlackList( Map> templateSetInfo) throws MetadataException { Map> resultTemplateSetInfo = new HashMap<>(); for (Map.Entry> entry : templateSetInfo.entrySet()) { - try (EntityUpdater collector = - new EntityUpdater(rootNode, entry.getKey(), store, false) { - @Override - protected void updateEntity(IEntityMNode node) { + try (EntityUpdater collector = + new EntityUpdater(rootNode, entry.getKey(), store, false) { + + protected void updateEntity(IDeviceMNode node) { if (entry.getValue().contains(node.getSchemaTemplateId()) && node.isPreDeactivateTemplate()) { resultTemplateSetInfo.put( @@ -794,18 +781,18 @@ protected void updateEntity(IEntityMNode node) { public void activateTemplateWithoutCheck( PartialPath activatePath, int templateId, boolean isAligned) { String[] nodes = activatePath.getNodes(); - IMNode cur = storageGroupMNode; + IMemMNode cur = storageGroupMNode; for (int i = levelOfSG + 1; i < nodes.length; i++) { cur = cur.getChild(nodes[i]); } - IEntityMNode entityMNode; - if (cur.isEntity()) { - entityMNode = cur.getAsEntityMNode(); + IDeviceMNode entityMNode; + if (cur.isDevice()) { + entityMNode = cur.getAsDeviceMNode(); } else { entityMNode = store.setToEntity(cur); - if (entityMNode.isStorageGroup()) { - replaceStorageGroupMNode(entityMNode.getAsStorageGroupMNode()); + if (entityMNode.isDatabase()) { + replaceStorageGroupMNode(entityMNode.getAsDatabaseMNode()); } } @@ -816,10 +803,10 @@ public void activateTemplateWithoutCheck( entityMNode.setSchemaTemplateId(templateId); } - @Override public long countPathsUsingTemplate(PartialPath pathPattern, int templateId) throws MetadataException { - try (EntityCounter counter = new EntityCounter(rootNode, pathPattern, store, false)) { + try (EntityCounter counter = + new EntityCounter<>(rootNode, pathPattern, store, false)) { counter.setSchemaTemplateFilter(templateId); return counter.count(); } @@ -831,43 +818,39 @@ public long countPathsUsingTemplate(PartialPath pathPattern, int templateId) public ISchemaReader getDeviceReader(IShowDevicesPlan showDevicesPlan) throws MetadataException { - EntityCollector collector = - new EntityCollector( + EntityCollector collector = + new EntityCollector( rootNode, showDevicesPlan.getPath(), store, showDevicesPlan.isPrefixMatch()) { - @Override - protected IDeviceSchemaInfo collectEntity(IEntityMNode node) { - PartialPath device = getPartialPathFromRootToNode(node); + + protected IDeviceSchemaInfo collectEntity(IDeviceMNode node) { + PartialPath device = getPartialPathFromRootToNode(node.getAsMNode()); return new ShowDevicesResult(device.getFullPath(), node.isAligned()); } }; if (showDevicesPlan.usingSchemaTemplate()) { collector.setSchemaTemplateFilter(showDevicesPlan.getSchemaTemplateId()); } - TraverserWithLimitOffsetWrapper traverser = + TraverserWithLimitOffsetWrapper traverser = new TraverserWithLimitOffsetWrapper<>( collector, showDevicesPlan.getLimit(), showDevicesPlan.getOffset()); return new ISchemaReader() { - @Override + public boolean isSuccess() { return traverser.isSuccess(); } - @Override public Throwable getFailure() { return traverser.getFailure(); } - @Override public void close() { traverser.close(); } - @Override public boolean hasNext() { return traverser.hasNext(); } - @Override public IDeviceSchemaInfo next() { return traverser.next(); } @@ -878,26 +861,23 @@ public ISchemaReader getTimeSeriesReader( IShowTimeSeriesPlan showTimeSeriesPlan, Function, Map>> tagAndAttributeProvider) throws MetadataException { - MeasurementCollector collector = - new MeasurementCollector( + MeasurementCollector collector = + new MeasurementCollector( rootNode, showTimeSeriesPlan.getPath(), store, showTimeSeriesPlan.isPrefixMatch()) { - @Override - protected ITimeSeriesSchemaInfo collectMeasurement(IMeasurementMNode node) { + + protected ITimeSeriesSchemaInfo collectMeasurement(IMeasurementMNode node) { return new ITimeSeriesSchemaInfo() { private Pair, Map> tagAndAttribute = null; - @Override public String getAlias() { return node.getAlias(); } - @Override public MeasurementSchema getSchema() { return (MeasurementSchema) node.getSchema(); } - @Override public Map getTags() { if (tagAndAttribute == null) { tagAndAttribute = tagAndAttributeProvider.apply(node.getOffset()); @@ -905,7 +885,6 @@ public Map getTags() { return tagAndAttribute.left; } - @Override public Map getAttributes() { if (tagAndAttribute == null) { tagAndAttribute = tagAndAttributeProvider.apply(node.getOffset()); @@ -913,25 +892,22 @@ public Map getAttributes() { return tagAndAttribute.right; } - @Override public boolean isUnderAlignedDevice() { - return getParentOfNextMatchedNode().getAsEntityMNode().isAligned(); + return getParentOfNextMatchedNode().getAsDeviceMNode().isAligned(); } - @Override public String getFullPath() { - return getPartialPathFromRootToNode(node).getFullPath(); + return getPartialPathFromRootToNode(node.getAsMNode()).getFullPath(); } - @Override public PartialPath getPartialPath() { - return getPartialPathFromRootToNode(node); + return getPartialPathFromRootToNode(node.getAsMNode()); } }; } }; - collector.setTemplateMap(showTimeSeriesPlan.getRelatedTemplate()); - Traverser traverser; + collector.setTemplateMap(showTimeSeriesPlan.getRelatedTemplate(), nodeFactory); + Traverser traverser; if (showTimeSeriesPlan.getLimit() > 0 || showTimeSeriesPlan.getOffset() > 0) { traverser = new TraverserWithLimitOffsetWrapper<>( @@ -940,27 +916,23 @@ public PartialPath getPartialPath() { traverser = collector; } return new ISchemaReader() { - @Override + public boolean isSuccess() { return traverser.isSuccess(); } - @Override public Throwable getFailure() { return traverser.getFailure(); } - @Override public void close() { traverser.close(); } - @Override public boolean hasNext() { return traverser.hasNext(); } - @Override public ITimeSeriesSchemaInfo next() { return traverser.next(); } @@ -969,11 +941,11 @@ public ITimeSeriesSchemaInfo next() { public ISchemaReader getNodeReader(IShowNodesPlan showNodesPlan) throws MetadataException { - MNodeCollector collector = - new MNodeCollector( + MNodeCollector collector = + new MNodeCollector( rootNode, showNodesPlan.getPath(), store, showNodesPlan.isPrefixMatch()) { - @Override - protected INodeSchemaInfo collectMNode(IMNode node) { + + protected INodeSchemaInfo collectMNode(IMemMNode node) { return new ShowNodesResult( getPartialPathFromRootToNode(node).getFullPath(), node.getMNodeType(false)); } @@ -981,27 +953,23 @@ protected INodeSchemaInfo collectMNode(IMNode node) { collector.setTargetLevel(showNodesPlan.getLevel()); return new ISchemaReader() { - @Override + public boolean isSuccess() { return collector.isSuccess(); } - @Override public Throwable getFailure() { return collector.getFailure(); } - @Override public void close() { collector.close(); } - @Override public boolean hasNext() { return collector.hasNext(); } - @Override public INodeSchemaInfo next() { return collector.next(); } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/snapshot/MemMTreeSnapshotUtil.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/snapshot/MemMTreeSnapshotUtil.java index b650dc710c232..4b7c29b63dec5 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/snapshot/MemMTreeSnapshotUtil.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/snapshot/MemMTreeSnapshotUtil.java @@ -20,18 +20,19 @@ package org.apache.iotdb.db.metadata.mtree.snapshot; import org.apache.iotdb.commons.file.SystemFileFactory; +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.common.AbstractDatabaseDeviceMNode; +import org.apache.iotdb.commons.schema.node.common.AbstractDatabaseMNode; +import org.apache.iotdb.commons.schema.node.common.AbstractDeviceMNode; +import org.apache.iotdb.commons.schema.node.common.AbstractMeasurementMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; +import org.apache.iotdb.commons.schema.node.utils.IMNodeIterator; +import org.apache.iotdb.commons.schema.node.visitor.MNodeVisitor; import org.apache.iotdb.db.metadata.MetadataConstant; -import org.apache.iotdb.db.metadata.mnode.EntityMNode; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.InternalMNode; -import org.apache.iotdb.db.metadata.mnode.MeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.StorageGroupEntityMNode; -import org.apache.iotdb.db.metadata.mnode.StorageGroupMNode; -import org.apache.iotdb.db.metadata.mnode.estimator.BasicMNodSizeEstimator; -import org.apache.iotdb.db.metadata.mnode.estimator.IMNodeSizeEstimator; -import org.apache.iotdb.db.metadata.mnode.iterator.IMNodeIterator; -import org.apache.iotdb.db.metadata.mnode.visitor.MNodeVisitor; +import org.apache.iotdb.db.metadata.mnode.mem.IMemMNode; +import org.apache.iotdb.db.metadata.mnode.mem.factory.MemMNodeFactory; import org.apache.iotdb.db.metadata.mtree.store.MemMTreeStore; import org.apache.iotdb.db.metadata.rescon.MemSchemaRegionStatistics; import org.apache.iotdb.tsfile.utils.ReadWriteIOUtils; @@ -67,7 +68,7 @@ public class MemMTreeSnapshotUtil { "Error occurred during deserializing MemMTree."; private static final byte VERSION = 0; - private static final IMNodeSizeEstimator ESTIMATOR = new BasicMNodSizeEstimator(); + private static final IMNodeFactory nodeFactory = MemMNodeFactory.getInstance(); public static boolean createSnapshot(File snapshotDir, MemMTreeStore store) { File snapshotTmp = @@ -104,9 +105,9 @@ public static boolean createSnapshot(File snapshotDir, MemMTreeStore store) { } } - public static IMNode loadSnapshot( + public static IMemMNode loadSnapshot( File snapshotDir, - Consumer measurementProcess, + Consumer> measurementProcess, MemSchemaRegionStatistics regionStatistics) throws IOException { File snapshot = @@ -127,17 +128,17 @@ private static void serializeTo(MemMTreeStore store, OutputStream outputStream) inorderSerialize(store.getRoot(), store, outputStream); } - private static void inorderSerialize(IMNode root, MemMTreeStore store, OutputStream outputStream) - throws IOException { + private static void inorderSerialize( + IMemMNode root, MemMTreeStore store, OutputStream outputStream) throws IOException { MNodeSerializer serializer = new MNodeSerializer(); if (!root.accept(serializer, outputStream)) { throw new IOException(SERIALIZE_ERROR_INFO); } - Deque stack = new ArrayDeque<>(); + Deque> stack = new ArrayDeque<>(); stack.push(store.getChildrenIterator(root)); - IMNode node; - IMNodeIterator iterator; + IMemMNode node; + IMNodeIterator iterator; while (!stack.isEmpty()) { iterator = stack.peek(); if (iterator.hasNext()) { @@ -154,22 +155,22 @@ private static void inorderSerialize(IMNode root, MemMTreeStore store, OutputStr } } - private static IMNode deserializeFrom( + private static IMemMNode deserializeFrom( InputStream inputStream, - Consumer measurementProcess, + Consumer> measurementProcess, MemSchemaRegionStatistics regionStatistics) throws IOException { byte version = ReadWriteIOUtils.readByte(inputStream); return inorderDeserialize(inputStream, measurementProcess, regionStatistics); } - private static IMNode inorderDeserialize( + private static IMemMNode inorderDeserialize( InputStream inputStream, - Consumer measurementProcess, + Consumer> measurementProcess, MemSchemaRegionStatistics regionStatistics) throws IOException { MNodeDeserializer deserializer = new MNodeDeserializer(); - Deque ancestors = new ArrayDeque<>(); + Deque ancestors = new ArrayDeque<>(); Deque restChildrenNum = new ArrayDeque<>(); deserializeMNode( ancestors, @@ -179,7 +180,7 @@ private static IMNode inorderDeserialize( measurementProcess, regionStatistics); int childrenNum; - IMNode root = ancestors.peek(); + IMemMNode root = ancestors.peek(); while (!ancestors.isEmpty()) { childrenNum = restChildrenNum.pop(); if (childrenNum == 0) { @@ -199,15 +200,15 @@ private static IMNode inorderDeserialize( } private static void deserializeMNode( - Deque ancestors, + Deque ancestors, Deque restChildrenNum, MNodeDeserializer deserializer, InputStream inputStream, - Consumer measurementProcess, + Consumer> measurementProcess, MemSchemaRegionStatistics regionStatistics) throws IOException { byte type = ReadWriteIOUtils.readByte(inputStream); - IMNode node; + IMemMNode node; int childrenNum; switch (type) { case INTERNAL_MNODE_TYPE: @@ -235,7 +236,7 @@ private static void deserializeMNode( throw new IOException("Unrecognized MNode type " + type); } - regionStatistics.requestMemory(ESTIMATOR.estimateSize(node)); + regionStatistics.requestMemory(node.estimateSize()); if (!ancestors.isEmpty()) { node.setParent(ancestors.peek()); @@ -252,10 +253,12 @@ private static void deserializeMNode( private static class MNodeSerializer extends MNodeVisitor { @Override - public Boolean visitInternalMNode(InternalMNode node, OutputStream outputStream) { + public Boolean visitBasicMNode(IMNode node, OutputStream outputStream) { try { ReadWriteIOUtils.write(INTERNAL_MNODE_TYPE, outputStream); - serializeInternalBasicInfo(node, outputStream); + serializeBasicMNode(node, outputStream); + ReadWriteIOUtils.write(0, outputStream); // for compatibly + ReadWriteIOUtils.write(false, outputStream); // for compatibly return true; } catch (IOException e) { logger.error(SERIALIZE_ERROR_INFO, e); @@ -264,10 +267,13 @@ public Boolean visitInternalMNode(InternalMNode node, OutputStream outputStream) } @Override - public Boolean visitStorageGroupMNode(StorageGroupMNode node, OutputStream outputStream) { + public Boolean visitDatabaseMNode( + AbstractDatabaseMNode> node, OutputStream outputStream) { try { ReadWriteIOUtils.write(STORAGE_GROUP_MNODE_TYPE, outputStream); - serializeInternalBasicInfo(node, outputStream); + serializeBasicMNode(node.getBasicMNode(), outputStream); + ReadWriteIOUtils.write(0, outputStream); // for compatibly + ReadWriteIOUtils.write(false, outputStream); // for compatibly // database node in schemaRegion doesn't store any database schema return true; } catch (IOException e) { @@ -277,11 +283,13 @@ public Boolean visitStorageGroupMNode(StorageGroupMNode node, OutputStream outpu } @Override - public Boolean visitStorageGroupEntityMNode( - StorageGroupEntityMNode node, OutputStream outputStream) { + public Boolean visitDatabaseDeviceMNode( + AbstractDatabaseDeviceMNode> node, OutputStream outputStream) { try { ReadWriteIOUtils.write(STORAGE_GROUP_ENTITY_MNODE_TYPE, outputStream); - serializeInternalBasicInfo(node, outputStream); + serializeBasicMNode(node.getBasicMNode(), outputStream); + ReadWriteIOUtils.write(node.getSchemaTemplateIdWithState(), outputStream); + ReadWriteIOUtils.write(node.isUseTemplate(), outputStream); ReadWriteIOUtils.write(node.isAligned(), outputStream); // database node in schemaRegion doesn't store any database schema return true; @@ -292,10 +300,13 @@ public Boolean visitStorageGroupEntityMNode( } @Override - public Boolean visitEntityMNode(EntityMNode node, OutputStream outputStream) { + public Boolean visitDeviceMNode( + AbstractDeviceMNode> node, OutputStream outputStream) { try { ReadWriteIOUtils.write(ENTITY_MNODE_TYPE, outputStream); - serializeInternalBasicInfo(node, outputStream); + serializeBasicMNode(node.getBasicMNode(), outputStream); + ReadWriteIOUtils.write(node.getSchemaTemplateIdWithState(), outputStream); + ReadWriteIOUtils.write(node.isUseTemplate(), outputStream); ReadWriteIOUtils.write(node.isAligned(), outputStream); return true; } catch (IOException e) { @@ -305,7 +316,8 @@ public Boolean visitEntityMNode(EntityMNode node, OutputStream outputStream) { } @Override - public Boolean visitMeasurementMNode(MeasurementMNode node, OutputStream outputStream) { + public Boolean visitMeasurementMNode( + AbstractMeasurementMNode> node, OutputStream outputStream) { try { ReadWriteIOUtils.write(MEASUREMENT_MNODE_TYPE, outputStream); ReadWriteIOUtils.write(node.getName(), outputStream); @@ -320,65 +332,59 @@ public Boolean visitMeasurementMNode(MeasurementMNode node, OutputStream outputS } } - private void serializeInternalBasicInfo(InternalMNode node, OutputStream outputStream) - throws IOException { + private void serializeBasicMNode(IMNode node, OutputStream outputStream) throws IOException { ReadWriteIOUtils.write(node.getChildren().size(), outputStream); ReadWriteIOUtils.write(node.getName(), outputStream); - ReadWriteIOUtils.write(node.getSchemaTemplateIdWithState(), outputStream); - ReadWriteIOUtils.write(node.isUseTemplate(), outputStream); } } private static class MNodeDeserializer { - public InternalMNode deserializeInternalMNode(InputStream inputStream) throws IOException { + public IMemMNode deserializeInternalMNode(InputStream inputStream) throws IOException { String name = ReadWriteIOUtils.readString(inputStream); - InternalMNode node = new InternalMNode(null, name); - deserializeInternalBasicInfo(node, inputStream); + IMemMNode node = nodeFactory.createInternalMNode(null, name); + int templateId = ReadWriteIOUtils.readInt(inputStream); + boolean useTemplate = ReadWriteIOUtils.readBool(inputStream); return node; } - public StorageGroupMNode deserializeStorageGroupMNode(InputStream inputStream) - throws IOException { + public IMemMNode deserializeStorageGroupMNode(InputStream inputStream) throws IOException { String name = ReadWriteIOUtils.readString(inputStream); - StorageGroupMNode node = new StorageGroupMNode(null, name); - deserializeInternalBasicInfo(node, inputStream); + IMemMNode node = nodeFactory.createDatabaseMNode(null, name).getAsMNode(); + int templateId = ReadWriteIOUtils.readInt(inputStream); + boolean useTemplate = ReadWriteIOUtils.readBool(inputStream); return node; } - public StorageGroupEntityMNode deserializeStorageGroupEntityMNode(InputStream inputStream) + public IMemMNode deserializeStorageGroupEntityMNode(InputStream inputStream) throws IOException { String name = ReadWriteIOUtils.readString(inputStream); - StorageGroupEntityMNode node = new StorageGroupEntityMNode(null, name, 0); - deserializeInternalBasicInfo(node, inputStream); - node.setAligned(ReadWriteIOUtils.readBool(inputStream)); + IMemMNode node = nodeFactory.createDatabaseDeviceMNode(null, name, 0); + node.getAsDeviceMNode().setSchemaTemplateId(ReadWriteIOUtils.readInt(inputStream)); + node.getAsDeviceMNode().setUseTemplate(ReadWriteIOUtils.readBool(inputStream)); + node.getAsDeviceMNode().setAligned(ReadWriteIOUtils.readBool(inputStream)); return node; } - public EntityMNode deserializeEntityMNode(InputStream inputStream) throws IOException { + public IMemMNode deserializeEntityMNode(InputStream inputStream) throws IOException { String name = ReadWriteIOUtils.readString(inputStream); - EntityMNode node = new EntityMNode(null, name); - deserializeInternalBasicInfo(node, inputStream); + IDeviceMNode node = nodeFactory.createDeviceMNode(null, name); + node.setSchemaTemplateId(ReadWriteIOUtils.readInt(inputStream)); + node.setUseTemplate(ReadWriteIOUtils.readBool(inputStream)); node.setAligned(ReadWriteIOUtils.readBool(inputStream)); - return node; + return node.getAsMNode(); } - public MeasurementMNode deserializeMeasurementMNode(InputStream inputStream) - throws IOException { + public IMemMNode deserializeMeasurementMNode(InputStream inputStream) throws IOException { String name = ReadWriteIOUtils.readString(inputStream); MeasurementSchema schema = MeasurementSchema.deserializeFrom(inputStream); String alias = ReadWriteIOUtils.readString(inputStream); long tagOffset = ReadWriteIOUtils.readLong(inputStream); - MeasurementMNode node = new MeasurementMNode(null, name, schema, alias); + IMeasurementMNode node = + nodeFactory.createMeasurementMNode(null, name, schema, alias); node.setOffset(tagOffset); node.setPreDeleted(ReadWriteIOUtils.readBool(inputStream)); - return node; - } - - private void deserializeInternalBasicInfo(InternalMNode node, InputStream inputStream) - throws IOException { - node.setSchemaTemplateId(ReadWriteIOUtils.readInt(inputStream)); - node.setUseTemplate(ReadWriteIOUtils.readBool(inputStream)); + return node.getAsMNode(); } } } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/CachedMTreeStore.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/CachedMTreeStore.java index a3273f72524ae..c7ae851bc9576 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/CachedMTreeStore.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/CachedMTreeStore.java @@ -20,18 +20,19 @@ import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.path.PartialPath; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; +import org.apache.iotdb.commons.schema.node.utils.IMNodeIterator; import org.apache.iotdb.db.exception.metadata.cache.MNodeNotCachedException; -import org.apache.iotdb.db.metadata.mnode.AboveDatabaseMNode; -import org.apache.iotdb.db.metadata.mnode.IEntityMNode; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.IStorageGroupMNode; -import org.apache.iotdb.db.metadata.mnode.MNodeUtils; -import org.apache.iotdb.db.metadata.mnode.estimator.IMNodeSizeEstimator; -import org.apache.iotdb.db.metadata.mnode.iterator.AbstractTraverserIterator; -import org.apache.iotdb.db.metadata.mnode.iterator.CachedTraverserIterator; -import org.apache.iotdb.db.metadata.mnode.iterator.IMNodeIterator; -import org.apache.iotdb.db.metadata.mtree.store.disk.ICachedMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.mem.estimator.MNodeSizeEstimator; +import org.apache.iotdb.db.metadata.mnode.mem.iterator.AbstractTraverserIterator; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.container.ICachedMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.schemafile.factory.CacheMNodeFactory; +import org.apache.iotdb.db.metadata.mnode.schemafile.iterator.CachedTraverserIterator; +import org.apache.iotdb.db.metadata.mnode.utils.MNodeUtils; import org.apache.iotdb.db.metadata.mtree.store.disk.cache.CacheMemoryManager; import org.apache.iotdb.db.metadata.mtree.store.disk.cache.ICacheManager; import org.apache.iotdb.db.metadata.mtree.store.disk.memcontrol.MemManager; @@ -50,9 +51,9 @@ import java.util.Map; import java.util.NoSuchElementException; -import static org.apache.iotdb.db.metadata.mtree.store.disk.ICachedMNodeContainer.getCachedMNodeContainer; +import static org.apache.iotdb.db.metadata.mnode.schemafile.container.ICachedMNodeContainer.getCachedMNodeContainer; -public class CachedMTreeStore implements IMTreeStore { +public class CachedMTreeStore implements IMTreeStore { private static final Logger logger = LoggerFactory.getLogger(CachedMTreeStore.class); @@ -62,10 +63,12 @@ public class CachedMTreeStore implements IMTreeStore { private ISchemaFile file; - private IMNode root; + private ICachedMNode root; private final Runnable flushCallback; + private final IMNodeFactory nodeFactory = CacheMNodeFactory.getInstance(); + private final CachedSchemaRegionStatistics regionStatistics; private final StampedWriterPreferredLock lock = new StampedWriterPreferredLock(); @@ -88,14 +91,14 @@ public CachedMTreeStore( } @Override - public IMNode generatePrefix(PartialPath storageGroupPath) { + public ICachedMNode generatePrefix(PartialPath storageGroupPath) { String[] nodes = storageGroupPath.getNodes(); // nodes[0] must be root - IMNode res = new AboveDatabaseMNode(null, nodes[0]); - IMNode cur = res; - IMNode child; + ICachedMNode res = nodeFactory.createAboveDatabaseMNode(null, nodes[0]); + ICachedMNode cur = res; + ICachedMNode child; for (int i = 1; i < nodes.length - 1; i++) { - child = new AboveDatabaseMNode(cur, nodes[i]); + child = nodeFactory.createAboveDatabaseMNode(cur, nodes[i]); cur.addChild(nodes[i], child); cur = child; } @@ -105,22 +108,22 @@ public IMNode generatePrefix(PartialPath storageGroupPath) { } @Override - public IMNode getRoot() { + public ICachedMNode getRoot() { return root; } @Override - public boolean hasChild(IMNode parent, String name) throws MetadataException { + public boolean hasChild(ICachedMNode parent, String name) throws MetadataException { return hasChild(parent, name, true); } - protected final boolean hasChild(IMNode parent, String name, boolean needLock) + protected final boolean hasChild(ICachedMNode parent, String name, boolean needLock) throws MetadataException { if (needLock) { lock.threadReadLock(); } try { - IMNode child = getChild(parent, name, needLock); + ICachedMNode child = getChild(parent, name, needLock); if (child == null) { return false; } else { @@ -147,17 +150,17 @@ protected final boolean hasChild(IMNode parent, String name, boolean needLock) * @throws MetadataException */ @Override - public IMNode getChild(IMNode parent, String name) throws MetadataException { + public ICachedMNode getChild(ICachedMNode parent, String name) throws MetadataException { return getChild(parent, name, true); } - protected final IMNode getChild(IMNode parent, String name, boolean needLock) + protected final ICachedMNode getChild(ICachedMNode parent, String name, boolean needLock) throws MetadataException { if (needLock) { lock.threadReadLock(); } try { - IMNode node = parent.getChild(name); + ICachedMNode node = parent.getChild(name); if (node == null) { node = loadChildFromDisk(parent, name); } else { @@ -168,7 +171,7 @@ protected final IMNode getChild(IMNode parent, String name, boolean needLock) } } if (node != null && node.isMeasurement()) { - processAlias(parent.getAsEntityMNode(), node.getAsMeasurementMNode()); + processAlias(parent.getAsDeviceMNode(), node.getAsMeasurementMNode()); } return node; @@ -179,8 +182,9 @@ protected final IMNode getChild(IMNode parent, String name, boolean needLock) } } - private IMNode loadChildFromDisk(IMNode parent, String name) throws MetadataException { - IMNode node = null; + private ICachedMNode loadChildFromDisk(ICachedMNode parent, String name) + throws MetadataException { + ICachedMNode node = null; if (!getCachedMNodeContainer(parent).isVolatile()) { try { node = file.getChildNode(parent, name); @@ -194,9 +198,9 @@ private IMNode loadChildFromDisk(IMNode parent, String name) throws MetadataExce return node; } - private IMNode loadChildFromDiskToParent(IMNode parent, IMNode node) { + private ICachedMNode loadChildFromDiskToParent(ICachedMNode parent, ICachedMNode node) { synchronized (parent) { - IMNode nodeAlreadyLoaded = parent.getChild(node.getName()); + ICachedMNode nodeAlreadyLoaded = parent.getChild(node.getName()); if (nodeAlreadyLoaded != null) { try { cacheManager.updateCacheStatusAfterMemoryRead(nodeAlreadyLoaded); @@ -212,7 +216,8 @@ private IMNode loadChildFromDiskToParent(IMNode parent, IMNode node) { } } - private void processAlias(IEntityMNode parent, IMeasurementMNode node) { + private void processAlias( + IDeviceMNode parent, IMeasurementMNode node) { String alias = node.getAlias(); if (alias != null) { parent.addAlias(alias, node); @@ -221,11 +226,12 @@ private void processAlias(IEntityMNode parent, IMeasurementMNode node) { // getChildrenIterator will take readLock, must call iterator.close() after usage @Override - public IMNodeIterator getChildrenIterator(IMNode parent) throws MetadataException { + public IMNodeIterator getChildrenIterator(ICachedMNode parent) + throws MetadataException { return getChildrenIterator(parent, true); } - final IMNodeIterator getChildrenIterator(IMNode parent, boolean needLock) + final IMNodeIterator getChildrenIterator(ICachedMNode parent, boolean needLock) throws MetadataException { try { return new CachedMNodeIterator(parent, needLock); @@ -235,21 +241,21 @@ final IMNodeIterator getChildrenIterator(IMNode parent, boolean needLock) } @Override - public IMNodeIterator getTraverserIterator( - IMNode parent, Map templateMap, boolean skipPreDeletedSchema) + public IMNodeIterator getTraverserIterator( + ICachedMNode parent, Map templateMap, boolean skipPreDeletedSchema) throws MetadataException { return getTraverserIterator(this, parent, templateMap, skipPreDeletedSchema); } - final IMNodeIterator getTraverserIterator( - IMTreeStore store, - IMNode parent, + final IMNodeIterator getTraverserIterator( + IMTreeStore store, + ICachedMNode parent, Map templateMap, boolean skipPreDeletedSchema) throws MetadataException { - if (parent.isEntity()) { - AbstractTraverserIterator iterator = - new CachedTraverserIterator(store, parent.getAsEntityMNode(), templateMap); + if (parent.isDevice()) { + AbstractTraverserIterator iterator = + new CachedTraverserIterator(store, parent.getAsDeviceMNode(), templateMap, nodeFactory); iterator.setSkipPreDeletedSchema(skipPreDeletedSchema); return iterator; } else { @@ -259,7 +265,7 @@ final IMNodeIterator getTraverserIterator( // must pin parent first @Override - public IMNode addChild(IMNode parent, String childName, IMNode child) { + public ICachedMNode addChild(ICachedMNode parent, String childName, ICachedMNode child) { lock.threadReadLock(); try { child.setParent(parent); @@ -283,10 +289,10 @@ public IMNode addChild(IMNode parent, String childName, IMNode child) { * @throws MetadataException */ @Override - public void deleteChild(IMNode parent, String childName) throws MetadataException { + public void deleteChild(ICachedMNode parent, String childName) throws MetadataException { lock.writeLock(); try { - IMNode deletedMNode = getChild(parent, childName, false); + ICachedMNode deletedMNode = getChild(parent, childName, false); ICachedMNodeContainer container = getCachedMNodeContainer(parent); if (!container.isVolatile() && !container.hasChildInNewChildBuffer(childName)) { // the container has been persisted and this child is not a new child, which means the child @@ -312,11 +318,11 @@ public void deleteChild(IMNode parent, String childName) throws MetadataExceptio * @param node the modified node */ @Override - public void updateMNode(IMNode node) { + public void updateMNode(ICachedMNode node) { updateMNode(node, true); } - final void updateMNode(IMNode node, boolean needLock) { + final void updateMNode(ICachedMNode node, boolean needLock) { if (needLock) { lock.threadReadLock(); } @@ -330,42 +336,43 @@ final void updateMNode(IMNode node, boolean needLock) { } @Override - public IEntityMNode setToEntity(IMNode node) { - IEntityMNode result = MNodeUtils.setToEntity(node); + public IDeviceMNode setToEntity(ICachedMNode node) { + IDeviceMNode result = MNodeUtils.setToEntity(node, nodeFactory); if (result != node) { - memManager.updatePinnedSize(IMNodeSizeEstimator.getEntityNodeBaseSize()); + memManager.updatePinnedSize(result.estimateSize() - node.estimateSize()); } - updateMNode(result); + updateMNode(result.getAsMNode()); return result; } @Override - public IMNode setToInternal(IEntityMNode entityMNode) { - IMNode result = MNodeUtils.setToInternal(entityMNode); + public ICachedMNode setToInternal(IDeviceMNode entityMNode) { + ICachedMNode result = MNodeUtils.setToInternal(entityMNode, nodeFactory); if (result != entityMNode) { - memManager.updatePinnedSize(-IMNodeSizeEstimator.getEntityNodeBaseSize()); + memManager.updatePinnedSize(result.estimateSize() - entityMNode.estimateSize()); } updateMNode(result); return result; } @Override - public void setAlias(IMeasurementMNode measurementMNode, String alias) throws MetadataException { + public void setAlias(IMeasurementMNode measurementMNode, String alias) + throws MetadataException { String existingAlias = measurementMNode.getAlias(); if (existingAlias == null && alias == null) { return; } measurementMNode.setAlias(alias); - updateMNode(measurementMNode); + updateMNode(measurementMNode.getAsMNode()); if (existingAlias != null && alias != null) { memManager.updatePinnedSize(alias.length() - existingAlias.length()); } else if (alias == null) { memManager.updatePinnedSize( - -(IMNodeSizeEstimator.getAliasBaseSize() + existingAlias.length())); + -(MNodeSizeEstimator.getAliasBaseSize() + existingAlias.length())); } else { - memManager.updatePinnedSize(IMNodeSizeEstimator.getAliasBaseSize() + alias.length()); + memManager.updatePinnedSize(MNodeSizeEstimator.getAliasBaseSize() + alias.length()); } } @@ -378,11 +385,11 @@ public void setAlias(IMeasurementMNode measurementMNode, String alias) throws Me * @param node */ @Override - public void pin(IMNode node) throws MetadataException { + public void pin(ICachedMNode node) throws MetadataException { pin(node, true); } - final void pin(IMNode node, boolean needLock) throws MetadataException { + final void pin(ICachedMNode node, boolean needLock) throws MetadataException { if (needLock) { lock.threadReadLock(); } @@ -404,11 +411,11 @@ final void pin(IMNode node, boolean needLock) throws MetadataException { * @param node */ @Override - public void unPin(IMNode node) { + public void unPin(ICachedMNode node) { unPin(node, true); } - final void unPin(IMNode node, boolean needLock) { + final void unPin(ICachedMNode node, boolean needLock) { if (needLock) { lock.threadReadLock(); } @@ -424,12 +431,12 @@ final void unPin(IMNode node, boolean needLock) { } @Override - public void unPinPath(IMNode node) { + public void unPinPath(ICachedMNode node) { unPinPath(node, true); } - public void unPinPath(IMNode node, boolean needLock) { - while (!node.isStorageGroup()) { + public void unPinPath(ICachedMNode node, boolean needLock) { + while (!node.isDatabase()) { unPin(node, needLock); node = node.getParent(); } @@ -444,7 +451,7 @@ final void stampedReadUnlock(long stamp) { } @Override - public IMTreeStore getWithReentrantReadLock() { + public IMTreeStore getWithReentrantReadLock() { return new ReentrantReadOnlyCachedMTreeStore(this); } @@ -539,7 +546,8 @@ public boolean executeMemoryRelease() { /** Sync all volatile nodes to schemaFile and execute memory release after flush. */ public void flushVolatileNodes() { try { - IStorageGroupMNode updatedStorageGroupMNode = cacheManager.collectUpdatedStorageGroupMNodes(); + IDatabaseMNode updatedStorageGroupMNode = + cacheManager.collectUpdatedStorageGroupMNodes(); if (updatedStorageGroupMNode != null) { try { file.updateDatabaseNode(updatedStorageGroupMNode); @@ -551,8 +559,8 @@ public void flushVolatileNodes() { return; } } - List nodesToPersist = cacheManager.collectVolatileMNodes(); - for (IMNode volatileNode : nodesToPersist) { + List nodesToPersist = cacheManager.collectVolatileMNodes(); + for (ICachedMNode volatileNode : nodesToPersist) { try { file.writeMNode(volatileNode); } catch (MetadataException | IOException e) { @@ -580,15 +588,16 @@ public void flushVolatileNodes() { * Since any node R/W operation may change the memory status, thus it should be controlled during * iterating child nodes. */ - private class CachedMNodeIterator implements IMNodeIterator { - IMNode parent; - Iterator iterator; - Iterator bufferIterator; + private class CachedMNodeIterator implements IMNodeIterator { + ICachedMNode parent; + Iterator iterator; + Iterator bufferIterator; boolean isIteratingDisk; - IMNode nextNode; + ICachedMNode nextNode; boolean isLocked; - CachedMNodeIterator(IMNode parent, boolean needLock) throws MetadataException, IOException { + CachedMNodeIterator(ICachedMNode parent, boolean needLock) + throws MetadataException, IOException { if (needLock) { lock.threadReadLock(); } @@ -631,17 +640,17 @@ public boolean hasNext() { // must invoke hasNext() first @Override - public IMNode next() { + public ICachedMNode next() { if (nextNode == null && !hasNext()) { throw new NoSuchElementException(); } - IMNode result = nextNode; + ICachedMNode result = nextNode; nextNode = null; return result; } private void readNext() throws MetadataException { - IMNode node = null; + ICachedMNode node = null; if (isIteratingDisk) { ICachedMNodeContainer container = getCachedMNodeContainer(parent); if (iterator.hasNext()) { @@ -656,7 +665,7 @@ private void readNext() throws MetadataException { } } if (node != null) { - IMNode nodeInMem = parent.getChild(node.getName()); + ICachedMNode nodeInMem = parent.getChild(node.getName()); if (nodeInMem != null) { // this branch means the node load from disk is in cache, thus use the instance in // cache diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/ConfigMTreeStore.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/ConfigMTreeStore.java new file mode 100644 index 0000000000000..f74b2d8c5c627 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/ConfigMTreeStore.java @@ -0,0 +1,143 @@ +/* + * 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.metadata.mtree.store; + +import org.apache.iotdb.commons.conf.IoTDBConstant; +import org.apache.iotdb.commons.exception.MetadataException; +import org.apache.iotdb.commons.path.PartialPath; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; +import org.apache.iotdb.commons.schema.node.utils.IMNodeIterator; +import org.apache.iotdb.db.metadata.mnode.config.IConfigMNode; +import org.apache.iotdb.db.metadata.mnode.mem.iterator.AbstractTraverserIterator; +import org.apache.iotdb.db.metadata.mnode.mem.iterator.MNodeIterator; +import org.apache.iotdb.db.metadata.mnode.mem.iterator.MemoryTraverserIterator; +import org.apache.iotdb.db.metadata.template.Template; + +import java.io.File; +import java.util.Map; + +/** This is a memory-based implementation of IMTreeStore. All MNodes are stored in memory. */ +public class ConfigMTreeStore implements IMTreeStore { + + private IConfigMNode root; + private final IMNodeFactory nodeFactory; + + // Only used for ConfigMTree + public ConfigMTreeStore(IMNodeFactory nodeFactory) { + this.root = nodeFactory.createInternalMNode(null, IoTDBConstant.PATH_ROOT); + this.nodeFactory = nodeFactory; + } + + @Override + public IConfigMNode generatePrefix(PartialPath storageGroupPath) { + throw new UnsupportedOperationException(); + } + + @Override + public IConfigMNode getRoot() { + return root; + } + + @Override + public boolean hasChild(IConfigMNode parent, String name) { + return parent.hasChild(name); + } + + @Override + public IConfigMNode getChild(IConfigMNode parent, String name) { + return parent.getChild(name); + } + + @Override + public IMNodeIterator getChildrenIterator(IConfigMNode parent) { + return new MNodeIterator<>(parent.getChildren().values().iterator()); + } + + @Override + public IMNodeIterator getTraverserIterator( + IConfigMNode parent, Map templateMap, boolean skipPreDeletedSchema) + throws MetadataException { + if (parent.isDevice()) { + AbstractTraverserIterator iterator = + new MemoryTraverserIterator<>(this, parent.getAsDeviceMNode(), templateMap, nodeFactory); + iterator.setSkipPreDeletedSchema(skipPreDeletedSchema); + return iterator; + } else { + return getChildrenIterator(parent); + } + } + + @Override + public IConfigMNode addChild(IConfigMNode parent, String childName, IConfigMNode child) { + return parent.addChild(childName, child); + } + + @Override + public void deleteChild(IConfigMNode parent, String childName) { + parent.deleteChild(childName); + } + + @Override + public void updateMNode(IConfigMNode node) {} + + @Override + public IDeviceMNode setToEntity(IConfigMNode node) { + throw new UnsupportedOperationException(); + } + + @Override + public IConfigMNode setToInternal(IDeviceMNode entityMNode) { + throw new UnsupportedOperationException(); + } + + @Override + public void setAlias(IMeasurementMNode measurementMNode, String alias) { + String existingAlias = measurementMNode.getAlias(); + if (existingAlias == null && alias == null) { + return; + } + measurementMNode.setAlias(alias); + } + + @Override + public void pin(IConfigMNode node) {} + + @Override + public void unPin(IConfigMNode node) {} + + @Override + public void unPinPath(IConfigMNode node) {} + + @Override + public IMTreeStore getWithReentrantReadLock() { + return this; + } + + @Override + public void clear() { + root = nodeFactory.createInternalMNode(null, IoTDBConstant.PATH_ROOT); + } + + @Override + public boolean createSnapshot(File snapshotDir) { + throw new UnsupportedOperationException(); + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/IMTreeStore.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/IMTreeStore.java index e15311927f5c7..e5580809e17d5 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/IMTreeStore.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/IMTreeStore.java @@ -20,10 +20,10 @@ import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.path.PartialPath; -import org.apache.iotdb.db.metadata.mnode.IEntityMNode; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.iterator.IMNodeIterator; +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeIterator; import org.apache.iotdb.db.metadata.template.Template; import java.io.File; @@ -34,16 +34,16 @@ * *

    MTreeStore could be implemented as memory-based or disk-based for different scenarios. */ -public interface IMTreeStore { +public interface IMTreeStore> { /** * Generate the ancestor nodes of storageGroupNode * * @return root node */ - IMNode generatePrefix(PartialPath storageGroupPath); + N generatePrefix(PartialPath storageGroupPath); - IMNode getRoot(); + N getRoot(); /** * Check if parent has child @@ -52,7 +52,7 @@ public interface IMTreeStore { * @param name name or alias * @return true if parent has a child whose name or alias matches the condition */ - boolean hasChild(IMNode parent, String name) throws MetadataException; + boolean hasChild(N parent, String name) throws MetadataException; /** * Get child by name or alias @@ -61,33 +61,33 @@ public interface IMTreeStore { * @param name name or alias * @return child node */ - IMNode getChild(IMNode parent, String name) throws MetadataException; + N getChild(N parent, String name) throws MetadataException; - IMNodeIterator getChildrenIterator(IMNode parent) throws MetadataException; + IMNodeIterator getChildrenIterator(N parent) throws MetadataException; - IMNodeIterator getTraverserIterator( - IMNode parent, Map templateMap, boolean skipPreDeletedSchema) + IMNodeIterator getTraverserIterator( + N parent, Map templateMap, boolean skipPreDeletedSchema) throws MetadataException; - IMNode addChild(IMNode parent, String childName, IMNode child); + N addChild(N parent, String childName, N child); - void deleteChild(IMNode parent, String childName) throws MetadataException; + void deleteChild(N parent, String childName) throws MetadataException; - void updateMNode(IMNode node) throws MetadataException; + void updateMNode(N node) throws MetadataException; - IEntityMNode setToEntity(IMNode node) throws MetadataException; + IDeviceMNode setToEntity(N node) throws MetadataException; - IMNode setToInternal(IEntityMNode entityMNode) throws MetadataException; + N setToInternal(IDeviceMNode entityMNode) throws MetadataException; - void setAlias(IMeasurementMNode measurementMNode, String alias) throws MetadataException; + void setAlias(IMeasurementMNode measurementMNode, String alias) throws MetadataException; - void pin(IMNode node) throws MetadataException; + void pin(N node) throws MetadataException; - void unPin(IMNode node); + void unPin(N node); - void unPinPath(IMNode node); + void unPinPath(N node); - IMTreeStore getWithReentrantReadLock(); + IMTreeStore getWithReentrantReadLock(); void clear(); diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/MemMTreeStore.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/MemMTreeStore.java index 49b92f7861262..70d8269bd4a87 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/MemMTreeStore.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/MemMTreeStore.java @@ -22,19 +22,17 @@ import org.apache.iotdb.commons.conf.IoTDBConstant; import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.path.PartialPath; -import org.apache.iotdb.db.metadata.mnode.AboveDatabaseMNode; -import org.apache.iotdb.db.metadata.mnode.IEntityMNode; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.InternalMNode; -import org.apache.iotdb.db.metadata.mnode.MNodeUtils; -import org.apache.iotdb.db.metadata.mnode.StorageGroupMNode; -import org.apache.iotdb.db.metadata.mnode.estimator.BasicMNodSizeEstimator; -import org.apache.iotdb.db.metadata.mnode.estimator.IMNodeSizeEstimator; -import org.apache.iotdb.db.metadata.mnode.iterator.AbstractTraverserIterator; -import org.apache.iotdb.db.metadata.mnode.iterator.IMNodeIterator; -import org.apache.iotdb.db.metadata.mnode.iterator.MNodeIterator; -import org.apache.iotdb.db.metadata.mnode.iterator.MemoryTraverserIterator; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; +import org.apache.iotdb.commons.schema.node.utils.IMNodeIterator; +import org.apache.iotdb.db.metadata.mnode.mem.IMemMNode; +import org.apache.iotdb.db.metadata.mnode.mem.estimator.MNodeSizeEstimator; +import org.apache.iotdb.db.metadata.mnode.mem.factory.MemMNodeFactory; +import org.apache.iotdb.db.metadata.mnode.mem.iterator.AbstractTraverserIterator; +import org.apache.iotdb.db.metadata.mnode.mem.iterator.MNodeIterator; +import org.apache.iotdb.db.metadata.mnode.mem.iterator.MemoryTraverserIterator; +import org.apache.iotdb.db.metadata.mnode.utils.MNodeUtils; import org.apache.iotdb.db.metadata.mtree.snapshot.MemMTreeSnapshotUtil; import org.apache.iotdb.db.metadata.rescon.MemSchemaRegionStatistics; import org.apache.iotdb.db.metadata.template.Template; @@ -45,90 +43,74 @@ import java.util.function.Consumer; /** This is a memory-based implementation of IMTreeStore. All MNodes are stored in memory. */ -public class MemMTreeStore implements IMTreeStore { +public class MemMTreeStore implements IMTreeStore { - private final IMNodeSizeEstimator estimator = new BasicMNodSizeEstimator(); - private MemSchemaRegionStatistics regionStatistics; + private final MemSchemaRegionStatistics regionStatistics; + private final IMNodeFactory nodeFactory = MemMNodeFactory.getInstance(); - private IMNode root; + private IMemMNode root; - // Only used for ConfigMTree - public MemMTreeStore(PartialPath rootPath, boolean isStorageGroup) { - if (isStorageGroup) { - this.root = - new StorageGroupMNode( - null, - rootPath.getTailNode(), - CommonDescriptor.getInstance().getConfig().getDefaultTTLInMs()); - } else { - this.root = new InternalMNode(null, IoTDBConstant.PATH_ROOT); - } - } - - public MemMTreeStore( - PartialPath rootPath, boolean isStorageGroup, MemSchemaRegionStatistics regionStatistics) { - if (isStorageGroup) { - this.root = - new StorageGroupMNode( - null, - rootPath.getTailNode(), - CommonDescriptor.getInstance().getConfig().getDefaultTTLInMs()); - } else { - this.root = new InternalMNode(null, IoTDBConstant.PATH_ROOT); - } + public MemMTreeStore(PartialPath rootPath, MemSchemaRegionStatistics regionStatistics) { + this.root = + nodeFactory + .createDatabaseMNode( + null, + rootPath.getTailNode(), + CommonDescriptor.getInstance().getConfig().getDefaultTTLInMs()) + .getAsMNode(); this.regionStatistics = regionStatistics; } - private MemMTreeStore(IMNode root, MemSchemaRegionStatistics regionStatistics) { + private MemMTreeStore(IMemMNode root, MemSchemaRegionStatistics regionStatistics) { this.root = root; this.regionStatistics = regionStatistics; } @Override - public IMNode generatePrefix(PartialPath storageGroupPath) { + public IMemMNode generatePrefix(PartialPath storageGroupPath) { String[] nodes = storageGroupPath.getNodes(); // nodes[0] must be root - IMNode res = new AboveDatabaseMNode(null, nodes[0]); - IMNode cur = res; - IMNode child; + IMemMNode res = nodeFactory.createAboveDatabaseMNode(null, nodes[0]); + IMemMNode cur = res; + IMemMNode child; for (int i = 1; i < nodes.length - 1; i++) { - child = new AboveDatabaseMNode(cur, nodes[i]); + child = nodeFactory.createAboveDatabaseMNode(cur, nodes[i]); cur.addChild(nodes[i], child); cur = child; } root.setParent(cur); cur.addChild(root); - requestMemory(estimator.estimateSize(root)); + requestMemory(root.estimateSize()); return res; } @Override - public IMNode getRoot() { + public IMemMNode getRoot() { return root; } @Override - public boolean hasChild(IMNode parent, String name) { + public boolean hasChild(IMemMNode parent, String name) { return parent.hasChild(name); } @Override - public IMNode getChild(IMNode parent, String name) { + public IMemMNode getChild(IMemMNode parent, String name) { return parent.getChild(name); } @Override - public IMNodeIterator getChildrenIterator(IMNode parent) { - return new MNodeIterator(parent.getChildren().values().iterator()); + public IMNodeIterator getChildrenIterator(IMemMNode parent) { + return new MNodeIterator<>(parent.getChildren().values().iterator()); } @Override - public IMNodeIterator getTraverserIterator( - IMNode parent, Map templateMap, boolean skipPreDeletedSchema) + public IMNodeIterator getTraverserIterator( + IMemMNode parent, Map templateMap, boolean skipPreDeletedSchema) throws MetadataException { - if (parent.isEntity()) { - AbstractTraverserIterator iterator = - new MemoryTraverserIterator(this, parent.getAsEntityMNode(), templateMap); + if (parent.isDevice()) { + AbstractTraverserIterator iterator = + new MemoryTraverserIterator<>(this, parent.getAsDeviceMNode(), templateMap, nodeFactory); iterator.setSkipPreDeletedSchema(skipPreDeletedSchema); return iterator; } else { @@ -137,56 +119,55 @@ public IMNodeIterator getTraverserIterator( } @Override - public IMNode addChild(IMNode parent, String childName, IMNode child) { - IMNode result = parent.addChild(childName, child); + public IMemMNode addChild(IMemMNode parent, String childName, IMemMNode child) { + IMemMNode result = parent.addChild(childName, child); if (result == child) { - requestMemory(estimator.estimateSize(child)); + requestMemory(child.estimateSize()); } return result; } @Override - public void deleteChild(IMNode parent, String childName) { - releaseMemory(estimator.estimateSize(parent.deleteChild(childName))); + public void deleteChild(IMemMNode parent, String childName) { + releaseMemory(parent.deleteChild(childName).estimateSize()); } @Override - public void updateMNode(IMNode node) {} + public void updateMNode(IMemMNode node) {} @Override - public IEntityMNode setToEntity(IMNode node) { - IEntityMNode result = MNodeUtils.setToEntity(node); + public IDeviceMNode setToEntity(IMemMNode node) { + IDeviceMNode result = MNodeUtils.setToEntity(node, nodeFactory); if (result != node) { - requestMemory(IMNodeSizeEstimator.getEntityNodeBaseSize()); + requestMemory(result.estimateSize() - node.estimateSize()); } - if (result.isStorageGroup()) { - root = result; + if (result.isDatabase()) { + root = result.getAsMNode(); } return result; } @Override - public IMNode setToInternal(IEntityMNode entityMNode) { - IMNode result = MNodeUtils.setToInternal(entityMNode); + public IMemMNode setToInternal(IDeviceMNode entityMNode) { + IMemMNode result = MNodeUtils.setToInternal(entityMNode, nodeFactory); if (result != entityMNode) { - releaseMemory(IMNodeSizeEstimator.getEntityNodeBaseSize()); + releaseMemory(entityMNode.estimateSize() - result.estimateSize()); } - if (result.isStorageGroup()) { + if (result.isDatabase()) { root = result; } return result; } @Override - public void setAlias(IMeasurementMNode measurementMNode, String alias) { + public void setAlias(IMeasurementMNode measurementMNode, String alias) { String existingAlias = measurementMNode.getAlias(); if (existingAlias == null && alias == null) { return; } measurementMNode.setAlias(alias); - updateMNode(measurementMNode); if (existingAlias != null && alias != null) { int delta = alias.length() - existingAlias.length(); @@ -196,29 +177,29 @@ public void setAlias(IMeasurementMNode measurementMNode, String alias) { releaseMemory(-delta); } } else if (alias == null) { - releaseMemory(IMNodeSizeEstimator.getAliasBaseSize() + existingAlias.length()); + releaseMemory(MNodeSizeEstimator.getAliasBaseSize() + existingAlias.length()); } else { - requestMemory(IMNodeSizeEstimator.getAliasBaseSize() + alias.length()); + requestMemory(MNodeSizeEstimator.getAliasBaseSize() + alias.length()); } } @Override - public void pin(IMNode node) {} + public void pin(IMemMNode node) {} @Override - public void unPin(IMNode node) {} + public void unPin(IMemMNode node) {} @Override - public void unPinPath(IMNode node) {} + public void unPinPath(IMemMNode node) {} @Override - public IMTreeStore getWithReentrantReadLock() { + public IMTreeStore getWithReentrantReadLock() { return this; } @Override public void clear() { - root = new InternalMNode(null, IoTDBConstant.PATH_ROOT); + root = nodeFactory.createInternalMNode(null, IoTDBConstant.PATH_ROOT); } @Override @@ -228,7 +209,7 @@ public boolean createSnapshot(File snapshotDir) { public static MemMTreeStore loadFromSnapshot( File snapshotDir, - Consumer measurementProcess, + Consumer> measurementProcess, MemSchemaRegionStatistics regionStatistics) throws IOException { return new MemMTreeStore( diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/ReentrantReadOnlyCachedMTreeStore.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/ReentrantReadOnlyCachedMTreeStore.java index 0c7df571a8de2..3eb09a3e3537f 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/ReentrantReadOnlyCachedMTreeStore.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/ReentrantReadOnlyCachedMTreeStore.java @@ -20,16 +20,16 @@ import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.path.PartialPath; -import org.apache.iotdb.db.metadata.mnode.IEntityMNode; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.iterator.IMNodeIterator; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeIterator; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; import org.apache.iotdb.db.metadata.template.Template; import java.io.File; import java.util.Map; -public class ReentrantReadOnlyCachedMTreeStore implements IMTreeStore { +public class ReentrantReadOnlyCachedMTreeStore implements IMTreeStore { private final CachedMTreeStore store; private final long readLockStamp; @@ -39,84 +39,84 @@ public ReentrantReadOnlyCachedMTreeStore(CachedMTreeStore store) { } @Override - public IMNode generatePrefix(PartialPath storageGroupPath) { + public ICachedMNode generatePrefix(PartialPath storageGroupPath) { throw new UnsupportedOperationException("ReadOnlyReentrantMTreeStore"); } @Override - public IMNode getRoot() { + public ICachedMNode getRoot() { return store.getRoot(); } @Override - public boolean hasChild(IMNode parent, String name) throws MetadataException { + public boolean hasChild(ICachedMNode parent, String name) throws MetadataException { return store.hasChild(parent, name, false); } @Override - public IMNode getChild(IMNode parent, String name) throws MetadataException { + public ICachedMNode getChild(ICachedMNode parent, String name) throws MetadataException { return store.getChild(parent, name, false); } @Override - public IMNodeIterator getChildrenIterator(IMNode parent) throws MetadataException { + public IMNodeIterator getChildrenIterator(ICachedMNode parent) throws MetadataException { return store.getChildrenIterator(parent, false); } @Override - public IMNodeIterator getTraverserIterator( - IMNode parent, Map templateMap, boolean skipPreDeletedSchema) + public IMNodeIterator getTraverserIterator( + ICachedMNode parent, Map templateMap, boolean skipPreDeletedSchema) throws MetadataException { return store.getTraverserIterator(this, parent, templateMap, skipPreDeletedSchema); } @Override - public IMNode addChild(IMNode parent, String childName, IMNode child) { + public ICachedMNode addChild(ICachedMNode parent, String childName, ICachedMNode child) { throw new UnsupportedOperationException("ReadOnlyReentrantMTreeStore"); } @Override - public void deleteChild(IMNode parent, String childName) { + public void deleteChild(ICachedMNode parent, String childName) { throw new UnsupportedOperationException("ReadOnlyReentrantMTreeStore"); } @Override - public void updateMNode(IMNode node) { + public void updateMNode(ICachedMNode node) { store.updateMNode(node, false); } @Override - public IEntityMNode setToEntity(IMNode node) { + public IDeviceMNode setToEntity(ICachedMNode node) { throw new UnsupportedOperationException("ReadOnlyReentrantMTreeStore"); } @Override - public IMNode setToInternal(IEntityMNode entityMNode) { + public ICachedMNode setToInternal(IDeviceMNode entityMNode) { throw new UnsupportedOperationException("ReadOnlyReentrantMTreeStore"); } @Override - public void setAlias(IMeasurementMNode measurementMNode, String alias) { + public void setAlias(IMeasurementMNode measurementMNode, String alias) { throw new UnsupportedOperationException("ReadOnlyReentrantMTreeStore"); } @Override - public void pin(IMNode node) throws MetadataException { + public void pin(ICachedMNode node) throws MetadataException { store.pin(node, false); } @Override - public void unPin(IMNode node) { + public void unPin(ICachedMNode node) { store.unPin(node, false); } @Override - public void unPinPath(IMNode node) { + public void unPinPath(ICachedMNode node) { store.unPinPath(node, false); } @Override - public IMTreeStore getWithReentrantReadLock() { + public IMTreeStore getWithReentrantReadLock() { throw new UnsupportedOperationException("ReadOnlyReentrantMTreeStore"); } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/cache/CacheManager.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/cache/CacheManager.java index e97606262e6eb..dcaf77fdbc50b 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/cache/CacheManager.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/cache/CacheManager.java @@ -18,11 +18,11 @@ */ package org.apache.iotdb.db.metadata.mtree.store.disk.cache; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; import org.apache.iotdb.db.exception.metadata.cache.MNodeNotCachedException; import org.apache.iotdb.db.exception.metadata.cache.MNodeNotPinnedException; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IStorageGroupMNode; -import org.apache.iotdb.db.metadata.mtree.store.disk.ICachedMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.container.ICachedMNodeContainer; import org.apache.iotdb.db.metadata.mtree.store.disk.memcontrol.MemManager; import java.util.ArrayList; @@ -32,8 +32,8 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.function.Consumer; -import static org.apache.iotdb.db.metadata.mtree.store.disk.ICachedMNodeContainer.getBelongedContainer; -import static org.apache.iotdb.db.metadata.mtree.store.disk.ICachedMNodeContainer.getCachedMNodeContainer; +import static org.apache.iotdb.db.metadata.mnode.schemafile.container.ICachedMNodeContainer.getBelongedContainer; +import static org.apache.iotdb.db.metadata.mnode.schemafile.container.ICachedMNodeContainer.getCachedMNodeContainer; /** * This class implemented the cache management, involving the cache status management on per MNode @@ -73,7 +73,7 @@ public CacheManager(MemManager memManager) { this.memManager = memManager; } - public void initRootStatus(IMNode root) { + public void initRootStatus(ICachedMNode root) { pinMNodeWithMemStatusUpdate(root); } @@ -84,7 +84,7 @@ public void initRootStatus(IMNode root) { * @param node */ @Override - public void updateCacheStatusAfterMemoryRead(IMNode node) throws MNodeNotCachedException { + public void updateCacheStatusAfterMemoryRead(ICachedMNode node) throws MNodeNotCachedException { CacheEntry cacheEntry = getCacheEntry(node); if (cacheEntry == null) { throw new MNodeNotCachedException(); @@ -107,7 +107,7 @@ public void updateCacheStatusAfterMemoryRead(IMNode node) throws MNodeNotCachedE * @param node */ @Override - public void updateCacheStatusAfterDiskRead(IMNode node) { + public void updateCacheStatusAfterDiskRead(ICachedMNode node) { pinMNodeWithMemStatusUpdate(node); CacheEntry cacheEntry = getCacheEntry(node); getBelongedContainer(node).addChildToCache(node); @@ -121,7 +121,7 @@ public void updateCacheStatusAfterDiskRead(IMNode node) { * @param node */ @Override - public void updateCacheStatusAfterAppend(IMNode node) { + public void updateCacheStatusAfterAppend(ICachedMNode node) { pinMNodeWithMemStatusUpdate(node); CacheEntry cacheEntry = getCacheEntry(node); cacheEntry.setVolatile(true); @@ -135,9 +135,9 @@ public void updateCacheStatusAfterAppend(IMNode node) { * * @param node */ - private void addToBufferAfterAppend(IMNode node) { + private void addToBufferAfterAppend(ICachedMNode node) { removeAncestorsFromCache(node); - IMNode parent = node.getParent(); + ICachedMNode parent = node.getParent(); CacheEntry cacheEntry = getCacheEntry(parent); if (!cacheEntry.isVolatile()) { // the cacheEntry may be set to volatile concurrently, the unVolatile node should not be added @@ -154,11 +154,11 @@ private void addToBufferAfterAppend(IMNode node) { * The ancestors of volatile node should not stay in nodeCache in which the node will be evicted. * When invoking this method, all the ancestors have been pinned. */ - private void removeAncestorsFromCache(IMNode node) { - IMNode parent = node.getParent(); - IMNode current = node; + private void removeAncestorsFromCache(ICachedMNode node) { + ICachedMNode parent = node.getParent(); + ICachedMNode current = node; CacheEntry cacheEntry = getCacheEntry(parent); - while (!current.isStorageGroup() && isInNodeCache(cacheEntry)) { + while (!current.isDatabase() && isInNodeCache(cacheEntry)) { removeFromNodeCache(cacheEntry); current = parent; parent = parent.getParent(); @@ -173,10 +173,10 @@ private void removeAncestorsFromCache(IMNode node) { * @param node */ @Override - public void updateCacheStatusAfterUpdate(IMNode node) { + public void updateCacheStatusAfterUpdate(ICachedMNode node) { CacheEntry cacheEntry = getCacheEntry(node); if (!cacheEntry.isVolatile()) { - if (!node.isStorageGroup()) { + if (!node.isDatabase()) { synchronized (cacheEntry) { // the status change affects the subTre collect in nodeBuffer cacheEntry.setVolatile(true); @@ -199,14 +199,14 @@ public void updateCacheStatusAfterUpdate(IMNode node) { * * @param node */ - private void addToBufferAfterUpdate(IMNode node) { - if (node.isStorageGroup()) { - nodeBuffer.setUpdatedStorageGroupMNode(node.getAsStorageGroupMNode()); + private void addToBufferAfterUpdate(ICachedMNode node) { + if (node.isDatabase()) { + nodeBuffer.setUpdatedStorageGroupMNode(node.getAsDatabaseMNode()); return; } removeAncestorsFromCache(node); - IMNode parent = node.getParent(); + ICachedMNode parent = node.getParent(); CacheEntry cacheEntry = getCacheEntry(parent); /* @@ -235,25 +235,25 @@ private void addToBufferAfterUpdate(IMNode node) { * @param node */ @Override - public void updateCacheStatusAfterPersist(IMNode node) { - IMNode tmp = node; - while (!tmp.isStorageGroup() && !isInNodeCache(getCacheEntry(tmp))) { + public void updateCacheStatusAfterPersist(ICachedMNode node) { + ICachedMNode tmp = node; + while (!tmp.isDatabase() && !isInNodeCache(getCacheEntry(tmp))) { addToNodeCache(getCacheEntry(tmp), tmp); tmp = tmp.getParent(); } ICachedMNodeContainer container = getCachedMNodeContainer(node); - Map persistedChildren = container.getNewChildBuffer(); - for (IMNode child : persistedChildren.values()) { + Map persistedChildren = container.getNewChildBuffer(); + for (ICachedMNode child : persistedChildren.values()) { updateCacheStatusAfterPersist(child, container); } persistedChildren = container.getUpdatedChildBuffer(); - for (IMNode child : persistedChildren.values()) { + for (ICachedMNode child : persistedChildren.values()) { updateCacheStatusAfterPersist(child, container); } } - private void updateCacheStatusAfterPersist(IMNode node, ICachedMNodeContainer container) { + private void updateCacheStatusAfterPersist(ICachedMNode node, ICachedMNodeContainer container) { CacheEntry cacheEntry = getCacheEntry(node); cacheEntry.setVolatile(false); container.moveMNodeToCache(node.getName()); @@ -266,8 +266,8 @@ private void updateCacheStatusAfterPersist(IMNode node, ICachedMNodeContainer co * @return null if not exist */ @Override - public IStorageGroupMNode collectUpdatedStorageGroupMNodes() { - IStorageGroupMNode storageGroupMNode = nodeBuffer.getUpdatedStorageGroupMNode(); + public IDatabaseMNode collectUpdatedStorageGroupMNodes() { + IDatabaseMNode storageGroupMNode = nodeBuffer.getUpdatedStorageGroupMNode(); nodeBuffer.setUpdatedStorageGroupMNode(null); return storageGroupMNode; } @@ -279,21 +279,22 @@ public IStorageGroupMNode collectUpdatedStorageGroupMNodes() { * @return */ @Override - public List collectVolatileMNodes() { - List nodesToPersist = new ArrayList<>(); + public List collectVolatileMNodes() { + List nodesToPersist = new ArrayList<>(); nodeBuffer.forEachNode(node -> collectVolatileNodes(node, nodesToPersist)); nodeBuffer.clear(); return nodesToPersist; } - private void collectVolatileNodes(IMNode node, List nodesToPersist) { - Iterator bufferIterator = getCachedMNodeContainer(node).getChildrenBufferIterator(); + private void collectVolatileNodes(ICachedMNode node, List nodesToPersist) { + Iterator bufferIterator = + getCachedMNodeContainer(node).getChildrenBufferIterator(); if (bufferIterator.hasNext()) { nodesToPersist.add(node); } - IMNode child; + ICachedMNode child; while (bufferIterator.hasNext()) { child = bufferIterator.next(); collectVolatileNodes(child, nodesToPersist); @@ -301,11 +302,11 @@ private void collectVolatileNodes(IMNode node, List nodesToPersist) { } @Override - public void remove(IMNode node) { + public void remove(ICachedMNode node) { removeRecursively(node); } - private void removeOne(CacheEntry cacheEntry, IMNode node) { + private void removeOne(CacheEntry cacheEntry, ICachedMNode node) { if (cacheEntry.isVolatile()) { nodeBuffer.remove(cacheEntry); } else { @@ -320,13 +321,13 @@ private void removeOne(CacheEntry cacheEntry, IMNode node) { memManager.releaseMemResource(node); } - private void removeRecursively(IMNode node) { + private void removeRecursively(ICachedMNode node) { CacheEntry cacheEntry = getCacheEntry(node); if (cacheEntry == null) { return; } removeOne(cacheEntry, node); - for (IMNode child : node.getChildren().values()) { + for (ICachedMNode child : node.getChildren().values()) { removeRecursively(child); } } @@ -339,9 +340,9 @@ private void removeRecursively(IMNode node) { */ @Override public synchronized boolean evict() { - IMNode node = null; + ICachedMNode node = null; CacheEntry cacheEntry = null; - List evictedMNodes = new ArrayList<>(); + List evictedMNodes = new ArrayList<>(); boolean isSuccess = false; while (!isSuccess) { node = getPotentialNodeTobeEvicted(); @@ -356,7 +357,7 @@ public synchronized boolean evict() { if (node.isMeasurement()) { String alias = node.getAsMeasurementMNode().getAlias(); if (alias != null) { - node.getParent().getAsEntityMNode().deleteAliasChild(alias); + node.getParent().getAsDeviceMNode().deleteAliasChild(alias); } } removeFromNodeCache(getCacheEntry(node)); @@ -375,8 +376,8 @@ public synchronized boolean evict() { return !evictedMNodes.isEmpty(); } - private void collectEvictedMNodes(IMNode node, List evictedMNodes) { - for (IMNode child : node.getChildren().values()) { + private void collectEvictedMNodes(ICachedMNode node, List evictedMNodes) { + for (ICachedMNode child : node.getChildren().values()) { removeFromNodeCache(getCacheEntry(child)); child.setCacheEntry(null); evictedMNodes.add(child); @@ -393,7 +394,7 @@ private void collectEvictedMNodes(IMNode node, List evictedMNodes) { * @param node */ @Override - public void pinMNode(IMNode node) throws MNodeNotPinnedException { + public void pinMNode(ICachedMNode node) throws MNodeNotPinnedException { CacheEntry cacheEntry = getCacheEntry(node); if (cacheEntry == null || !cacheEntry.isPinned()) { throw new MNodeNotPinnedException(); @@ -408,7 +409,7 @@ public void pinMNode(IMNode node) throws MNodeNotPinnedException { } } - private void pinMNodeWithMemStatusUpdate(IMNode node) { + private void pinMNodeWithMemStatusUpdate(ICachedMNode node) { CacheEntry cacheEntry = getCacheEntry(node); // update memory status first if (cacheEntry == null) { @@ -420,12 +421,12 @@ private void pinMNodeWithMemStatusUpdate(IMNode node) { doPin(node); } - private void doPin(IMNode node) { + private void doPin(ICachedMNode node) { CacheEntry cacheEntry = getCacheEntry(node); // do pin MNode in memory if (!cacheEntry.isPinned()) { - IMNode parent = node.getParent(); - if (!node.isStorageGroup()) { + ICachedMNode parent = node.getParent(); + if (!node.isDatabase()) { getCacheEntry(parent).pin(); } } @@ -442,7 +443,7 @@ private void doPin(IMNode node) { * @return */ @Override - public boolean unPinMNode(IMNode node) { + public boolean unPinMNode(ICachedMNode node) { CacheEntry cacheEntry = getCacheEntry(node); if (cacheEntry == null) { return false; @@ -451,7 +452,7 @@ public boolean unPinMNode(IMNode node) { return doUnPin(node); } - private boolean doUnPin(IMNode node) { + private boolean doUnPin(ICachedMNode node) { CacheEntry cacheEntry = getCacheEntry(node); boolean isPinStatusChanged = false; @@ -463,7 +464,7 @@ private boolean doUnPin(IMNode node) { } } - if (isPinStatusChanged && !node.isStorageGroup()) { + if (isPinStatusChanged && !node.isDatabase()) { doUnPin(node.getParent()); } @@ -471,13 +472,13 @@ private boolean doUnPin(IMNode node) { } @Override - public void clear(IMNode root) { + public void clear(ICachedMNode root) { clearMNodeInMemory(root); clearNodeCache(); nodeBuffer.clear(); } - private void clearMNodeInMemory(IMNode node) { + private void clearMNodeInMemory(ICachedMNode node) { CacheEntry cacheEntry = getCacheEntry(node); if (cacheEntry == null) { return; @@ -488,13 +489,13 @@ private void clearMNodeInMemory(IMNode node) { } memManager.releaseMemResource(node); - Iterator iterator = getCachedMNodeContainer(node).getChildrenIterator(); + Iterator iterator = getCachedMNodeContainer(node).getChildrenIterator(); while (iterator.hasNext()) { clearMNodeInMemory(iterator.next()); } } - protected CacheEntry getCacheEntry(IMNode node) { + protected CacheEntry getCacheEntry(ICachedMNode node) { return node.getCacheEntry(); } @@ -503,7 +504,7 @@ public long getBufferNodeNum() { return nodeBuffer.getBufferNodeNum(); } - protected void initCacheEntryForNode(IMNode node) { + protected void initCacheEntryForNode(ICachedMNode node) { node.setCacheEntry(new CacheEntry()); } @@ -511,15 +512,15 @@ protected void initCacheEntryForNode(IMNode node) { // MNode update operation like node replace may reset the mapping between cacheEntry and node, // thus it should be updated - protected abstract void updateCacheStatusAfterUpdate(CacheEntry cacheEntry, IMNode node); + protected abstract void updateCacheStatusAfterUpdate(CacheEntry cacheEntry, ICachedMNode node); protected abstract boolean isInNodeCache(CacheEntry cacheEntry); - protected abstract void addToNodeCache(CacheEntry cacheEntry, IMNode node); + protected abstract void addToNodeCache(CacheEntry cacheEntry, ICachedMNode node); protected abstract void removeFromNodeCache(CacheEntry cacheEntry); - protected abstract IMNode getPotentialNodeTobeEvicted(); + protected abstract ICachedMNode getPotentialNodeTobeEvicted(); protected abstract void clearNodeCache(); @@ -527,8 +528,8 @@ private static class NodeBuffer { private static final int MAP_NUM = 17; - private IStorageGroupMNode updatedStorageGroupMNode; - private Map[] maps = new Map[MAP_NUM]; + private IDatabaseMNode updatedStorageGroupMNode; + private Map[] maps = new Map[MAP_NUM]; NodeBuffer() { for (int i = 0; i < MAP_NUM; i++) { @@ -536,15 +537,15 @@ private static class NodeBuffer { } } - public IStorageGroupMNode getUpdatedStorageGroupMNode() { + public IDatabaseMNode getUpdatedStorageGroupMNode() { return updatedStorageGroupMNode; } - public void setUpdatedStorageGroupMNode(IStorageGroupMNode updatedStorageGroupMNode) { + public void setUpdatedStorageGroupMNode(IDatabaseMNode updatedStorageGroupMNode) { this.updatedStorageGroupMNode = updatedStorageGroupMNode; } - void put(CacheEntry cacheEntry, IMNode node) { + void put(CacheEntry cacheEntry, ICachedMNode node) { maps[getLoc(cacheEntry)].put(cacheEntry, node); } @@ -552,9 +553,9 @@ void remove(CacheEntry cacheEntry) { maps[getLoc(cacheEntry)].remove(cacheEntry); } - void forEachNode(Consumer action) { - for (Map map : maps) { - for (IMNode node : map.values()) { + void forEachNode(Consumer action) { + for (Map map : maps) { + for (ICachedMNode node : map.values()) { action.accept(node); } } @@ -569,7 +570,7 @@ long getBufferNodeNum() { } void clear() { - for (Map map : maps) { + for (Map map : maps) { map.clear(); } } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/cache/ICacheManager.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/cache/ICacheManager.java index 395db8044fc93..b66777ff3c86f 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/cache/ICacheManager.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/cache/ICacheManager.java @@ -18,42 +18,42 @@ */ package org.apache.iotdb.db.metadata.mtree.store.disk.cache; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; import org.apache.iotdb.db.exception.metadata.cache.MNodeNotCachedException; import org.apache.iotdb.db.exception.metadata.cache.MNodeNotPinnedException; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IStorageGroupMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; import java.util.List; public interface ICacheManager { - void initRootStatus(IMNode root); + void initRootStatus(ICachedMNode root); - void updateCacheStatusAfterMemoryRead(IMNode node) throws MNodeNotCachedException; + void updateCacheStatusAfterMemoryRead(ICachedMNode node) throws MNodeNotCachedException; - void updateCacheStatusAfterDiskRead(IMNode node); + void updateCacheStatusAfterDiskRead(ICachedMNode node); - void updateCacheStatusAfterAppend(IMNode node); + void updateCacheStatusAfterAppend(ICachedMNode node); - void updateCacheStatusAfterUpdate(IMNode node); + void updateCacheStatusAfterUpdate(ICachedMNode node); - void updateCacheStatusAfterPersist(IMNode node); + void updateCacheStatusAfterPersist(ICachedMNode node); - IStorageGroupMNode collectUpdatedStorageGroupMNodes(); + IDatabaseMNode collectUpdatedStorageGroupMNodes(); - List collectVolatileMNodes(); + List collectVolatileMNodes(); - void remove(IMNode node); + void remove(ICachedMNode node); boolean evict(); - void pinMNode(IMNode node) throws MNodeNotPinnedException; + void pinMNode(ICachedMNode node) throws MNodeNotPinnedException; - boolean unPinMNode(IMNode node); + boolean unPinMNode(ICachedMNode node); long getBufferNodeNum(); long getCacheNodeNum(); - void clear(IMNode root); + void clear(ICachedMNode root); } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/cache/LRUCacheManager.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/cache/LRUCacheManager.java index ae21c836c70cb..258ae87ed7fbf 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/cache/LRUCacheManager.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/cache/LRUCacheManager.java @@ -18,7 +18,7 @@ */ package org.apache.iotdb.db.metadata.mtree.store.disk.cache; -import org.apache.iotdb.db.metadata.mnode.IMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; import org.apache.iotdb.db.metadata.mtree.store.disk.memcontrol.MemManager; import java.util.concurrent.atomic.AtomicLong; @@ -47,12 +47,12 @@ public void updateCacheStatusAfterAccess(CacheEntry cacheEntry) { // MNode update operation like node replace may reset the mapping between cacheEntry and node, // thus it should be updated @Override - protected void updateCacheStatusAfterUpdate(CacheEntry cacheEntry, IMNode node) { + protected void updateCacheStatusAfterUpdate(CacheEntry cacheEntry, ICachedMNode node) { getAsLRUCacheEntry(cacheEntry).setNode(node); } @Override - protected void initCacheEntryForNode(IMNode node) { + protected void initCacheEntryForNode(ICachedMNode node) { LRUCacheEntry cacheEntry = new LRUCacheEntry(node); node.setCacheEntry(cacheEntry); } @@ -64,7 +64,7 @@ protected boolean isInNodeCache(CacheEntry cacheEntry) { } @Override - protected void addToNodeCache(CacheEntry cacheEntry, IMNode node) { + protected void addToNodeCache(CacheEntry cacheEntry, ICachedMNode node) { LRUCacheEntry lruCacheEntry = getAsLRUCacheEntry(cacheEntry); getTargetCacheList(lruCacheEntry).addToCacheList(lruCacheEntry, node); } @@ -76,8 +76,8 @@ protected void removeFromNodeCache(CacheEntry cacheEntry) { } @Override - protected IMNode getPotentialNodeTobeEvicted() { - IMNode result = null; + protected ICachedMNode getPotentialNodeTobeEvicted() { + ICachedMNode result = null; for (LRUCacheList cacheList : lruCacheLists) { result = cacheList.getPotentialNodeTobeEvicted(); if (result != null) { @@ -120,21 +120,21 @@ private static class LRUCacheEntry extends CacheEntry { // although the node instance may be replaced, the name and full path of the node won't be // changed, which means the cacheEntry always map to only one logic node - protected volatile IMNode node; + protected volatile ICachedMNode node; private volatile LRUCacheEntry pre = null; private volatile LRUCacheEntry next = null; - public LRUCacheEntry(IMNode node) { + public LRUCacheEntry(ICachedMNode node) { this.node = node; } - public IMNode getNode() { + public ICachedMNode getNode() { return node; } - public void setNode(IMNode node) { + public void setNode(ICachedMNode node) { this.node = node; } @@ -181,7 +181,7 @@ private void updateCacheStatusAfterAccess(LRUCacheEntry lruCacheEntry) { } } - private void addToCacheList(LRUCacheEntry lruCacheEntry, IMNode node) { + private void addToCacheList(LRUCacheEntry lruCacheEntry, ICachedMNode node) { lock.lock(); try { lruCacheEntry.setNode(node); @@ -202,7 +202,7 @@ private void removeFromCacheList(LRUCacheEntry lruCacheEntry) { } } - private IMNode getPotentialNodeTobeEvicted() { + private ICachedMNode getPotentialNodeTobeEvicted() { lock.lock(); try { LRUCacheEntry target = last; diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/cache/PlainCacheManager.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/cache/PlainCacheManager.java index 33cd944aa979a..bc6cdaeafaaee 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/cache/PlainCacheManager.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/cache/PlainCacheManager.java @@ -18,7 +18,7 @@ */ package org.apache.iotdb.db.metadata.mtree.store.disk.cache; -import org.apache.iotdb.db.metadata.mnode.IMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; import org.apache.iotdb.db.metadata.mtree.store.disk.memcontrol.MemManager; import java.util.Map; @@ -29,7 +29,7 @@ public class PlainCacheManager extends CacheManager { // The nodes in nodeCache are all evictable if not pinned and may be selected to be evicted during // cache // eviction. - private volatile Map nodeCache = new ConcurrentHashMap<>(); + private volatile Map nodeCache = new ConcurrentHashMap<>(); public PlainCacheManager(MemManager memManager) { super(memManager); @@ -41,7 +41,7 @@ protected void updateCacheStatusAfterAccess(CacheEntry cacheEntry) {} // MNode update operation like node replace may reset the mapping between cacheEntry and node, // thus it should be updated @Override - protected void updateCacheStatusAfterUpdate(CacheEntry cacheEntry, IMNode node) { + protected void updateCacheStatusAfterUpdate(CacheEntry cacheEntry, ICachedMNode node) { nodeCache.replace(cacheEntry, node); } @@ -51,7 +51,7 @@ protected boolean isInNodeCache(CacheEntry cacheEntry) { } @Override - protected void addToNodeCache(CacheEntry cacheEntry, IMNode node) { + protected void addToNodeCache(CacheEntry cacheEntry, ICachedMNode node) { nodeCache.put(cacheEntry, node); } @@ -61,7 +61,7 @@ protected void removeFromNodeCache(CacheEntry cacheEntry) { } @Override - protected IMNode getPotentialNodeTobeEvicted() { + protected ICachedMNode getPotentialNodeTobeEvicted() { for (CacheEntry cacheEntry : nodeCache.keySet()) { if (!cacheEntry.isPinned()) { return nodeCache.get(cacheEntry); diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/memcontrol/CachedMNodeSizeEstimator.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/memcontrol/CachedMNodeSizeEstimator.java deleted file mode 100644 index 54f7567eecc04..0000000000000 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/memcontrol/CachedMNodeSizeEstimator.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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.metadata.mtree.store.disk.memcontrol; - -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.estimator.BasicMNodSizeEstimator; - -public class CachedMNodeSizeEstimator extends BasicMNodSizeEstimator { - - /** Estimated size of CacheEntry */ - private static final int CACHE_ENTRY_SIZE = 40; - - /** - * Estimated delta size of CachedMNodeContainer compared with MNodeContainerMapImpl - * - *

      - *
    1. address, 8B - *
    2. three map reference (1 cache and 2 buffer), 8 * 3 = 24B - *
    3. estimate occupation of map implementation, minus the basic container occupation, 80 * 3 - - * 80 = 160B - *
    - */ - private static final int CACHED_MNODE_CONTAINER_SIZE_DELTA = 192; - - private static final int NODE_BASE_SIZE = - BasicMNodSizeEstimator.NODE_BASE_SIZE + CACHE_ENTRY_SIZE; - - private static final int INTERNAL_NODE_BASE_SIZE = - BasicMNodSizeEstimator.INTERNAL_NODE_BASE_SIZE + CACHED_MNODE_CONTAINER_SIZE_DELTA; - - @Override - public int estimateSize(IMNode node) { - int size = NODE_BASE_SIZE + node.getName().length(); - if (node.isMeasurement()) { - size += MEASUREMENT_NODE_BASE_SIZE; - IMeasurementMNode measurementMNode = node.getAsMeasurementMNode(); - if (measurementMNode.getAlias() != null) { - size += ALIAS_BASE_SIZE + measurementMNode.getAlias().length(); - } - } else { - size += INTERNAL_NODE_BASE_SIZE; - if (node.isStorageGroup()) { - size += STORAGE_GROUP_NODE_BASE_SIZE; - size += node.getAsStorageGroupMNode().getFullPath().length(); - } - - if (node.isEntity()) { - size += ENTITY_NODE_BASE_SIZE; - } - } - - return size; - } -} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/memcontrol/MemManager.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/memcontrol/MemManager.java index a8216725d4f94..8dbef37ff0bc6 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/memcontrol/MemManager.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/memcontrol/MemManager.java @@ -19,7 +19,7 @@ package org.apache.iotdb.db.metadata.mtree.store.disk.memcontrol; -import org.apache.iotdb.db.metadata.mnode.IMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; import org.apache.iotdb.db.metadata.rescon.CachedSchemaRegionStatistics; import java.util.List; @@ -29,46 +29,44 @@ public class MemManager { private final CachedSchemaRegionStatistics regionStatistics; - private final CachedMNodeSizeEstimator estimator = new CachedMNodeSizeEstimator(); - public MemManager(CachedSchemaRegionStatistics regionStatistics) { this.regionStatistics = regionStatistics; } - public void requestPinnedMemResource(IMNode node) { - int size = estimator.estimateSize(node); + public void requestPinnedMemResource(ICachedMNode node) { + int size = node.estimateSize(); regionStatistics.requestMemory(size); regionStatistics.updatePinnedMemorySize(size); regionStatistics.updatePinnedMNodeNum(1); } - public void upgradeMemResource(IMNode node) { - int size = estimator.estimateSize(node); + public void upgradeMemResource(ICachedMNode node) { + int size = node.estimateSize(); regionStatistics.updatePinnedMemorySize(size); regionStatistics.updatePinnedMNodeNum(1); regionStatistics.updateUnpinnedMemorySize(-size); regionStatistics.updateUnpinnedMNodeNum(-1); } - public void releasePinnedMemResource(IMNode node) { - int size = estimator.estimateSize(node); + public void releasePinnedMemResource(ICachedMNode node) { + int size = node.estimateSize(); regionStatistics.updateUnpinnedMemorySize(size); regionStatistics.updateUnpinnedMNodeNum(1); regionStatistics.updatePinnedMemorySize(-size); regionStatistics.updatePinnedMNodeNum(-1); } - public void releaseMemResource(IMNode node) { - int size = estimator.estimateSize(node); + public void releaseMemResource(ICachedMNode node) { + int size = node.estimateSize(); regionStatistics.updateUnpinnedMemorySize(-size); regionStatistics.updateUnpinnedMNodeNum(-1); regionStatistics.releaseMemory(size); } - public void releaseMemResource(List evictedNodes) { + public void releaseMemResource(List evictedNodes) { int size = 0; - for (IMNode node : evictedNodes) { - size += estimator.estimateSize(node); + for (ICachedMNode node : evictedNodes) { + size += node.estimateSize(); } regionStatistics.updateUnpinnedMNodeNum(-evictedNodes.size()); regionStatistics.updateUnpinnedMemorySize(-size); diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/ISchemaFile.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/ISchemaFile.java index 93bf6426a2c6f..4ba759f4f6734 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/ISchemaFile.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/ISchemaFile.java @@ -19,8 +19,8 @@ package org.apache.iotdb.db.metadata.mtree.store.disk.schemafile; import org.apache.iotdb.commons.exception.MetadataException; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IStorageGroupMNode; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; import java.io.File; import java.io.IOException; @@ -33,7 +33,7 @@ public interface ISchemaFile { * * @return node instance, template name as hash code */ - IMNode init() throws MetadataException; + ICachedMNode init() throws MetadataException; /** * Modify header of schema file corresponding to the database node synchronously @@ -41,16 +41,16 @@ public interface ISchemaFile { * @param sgNode node to be updated * @return true if success */ - boolean updateDatabaseNode(IStorageGroupMNode sgNode) throws IOException; + boolean updateDatabaseNode(IDatabaseMNode sgNode) throws IOException; /** * Only database node along with its descendents could be flushed into schema file. * * @param node */ - void writeMNode(IMNode node) throws MetadataException, IOException; + void writeMNode(ICachedMNode node) throws MetadataException, IOException; - void delete(IMNode node) throws IOException, MetadataException; + void delete(ICachedMNode node) throws IOException, MetadataException; void close() throws IOException; @@ -58,9 +58,10 @@ public interface ISchemaFile { void sync() throws IOException; - IMNode getChildNode(IMNode parent, String childName) throws MetadataException, IOException; + ICachedMNode getChildNode(ICachedMNode parent, String childName) + throws MetadataException, IOException; - Iterator getChildren(IMNode parent) throws MetadataException, IOException; + Iterator getChildren(ICachedMNode parent) throws MetadataException, IOException; boolean createSnapshot(File snapshotDir); } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/ISegmentedPage.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/ISegmentedPage.java index fb915084e1942..7f511653ea243 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/ISegmentedPage.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/ISegmentedPage.java @@ -21,7 +21,7 @@ import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.db.exception.metadata.schemafile.SchemaPageOverflowException; import org.apache.iotdb.db.exception.metadata.schemafile.SegmentNotFoundException; -import org.apache.iotdb.db.metadata.mnode.IMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; import java.io.IOException; import java.nio.ByteBuffer; @@ -39,9 +39,9 @@ public interface ISegmentedPage extends ISchemaPage { */ long write(short segIdx, String key, ByteBuffer buffer) throws MetadataException; - IMNode read(short segIdx, String key) throws MetadataException; + ICachedMNode read(short segIdx, String key) throws MetadataException; - IMNode readByAlias(short segIdx, String alias) throws MetadataException; + ICachedMNode readByAlias(short segIdx, String alias) throws MetadataException; /** * The record is definitely inside specified segment. {@link WrappedSegment} will compare existed @@ -52,7 +52,7 @@ public interface ISegmentedPage extends ISchemaPage { */ void update(short segIdx, String key, ByteBuffer buffer) throws MetadataException; - Queue getChildren(short segId) throws MetadataException; + Queue getChildren(short segId) throws MetadataException; void removeRecord(short segId, String key) throws SegmentNotFoundException; diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/MockSchemaFile.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/MockSchemaFile.java index 618534e16fe98..6674745fd4af1 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/MockSchemaFile.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/MockSchemaFile.java @@ -20,17 +20,13 @@ import org.apache.iotdb.commons.conf.CommonDescriptor; import org.apache.iotdb.commons.path.PartialPath; -import org.apache.iotdb.db.metadata.mnode.EntityMNode; -import org.apache.iotdb.db.metadata.mnode.IEntityMNode; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.IStorageGroupMNode; -import org.apache.iotdb.db.metadata.mnode.InternalMNode; -import org.apache.iotdb.db.metadata.mnode.MeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.StorageGroupEntityMNode; -import org.apache.iotdb.db.metadata.mnode.StorageGroupMNode; -import org.apache.iotdb.db.metadata.mtree.store.disk.CachedMNodeContainer; -import org.apache.iotdb.db.metadata.mtree.store.disk.ICachedMNodeContainer; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.container.CachedMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.schemafile.container.ICachedMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.schemafile.factory.CacheMNodeFactory; import java.io.File; import java.io.IOException; @@ -40,45 +36,46 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import static org.apache.iotdb.db.metadata.mtree.store.disk.ICachedMNodeContainer.getCachedMNodeContainer; +import static org.apache.iotdb.db.metadata.mnode.schemafile.container.ICachedMNodeContainer.getCachedMNodeContainer; public class MockSchemaFile implements ISchemaFile { private PartialPath storageGroupPath; - private IStorageGroupMNode storageGroupMNode; + private IDatabaseMNode storageGroupMNode; + private static final IMNodeFactory nodeFactory = CacheMNodeFactory.getInstance(); private long fileTail = 0; - private final Map> mockFile = new HashMap<>(); + private final Map> mockFile = new HashMap<>(); public MockSchemaFile(PartialPath storageGroupPath) { this.storageGroupPath = storageGroupPath; } @Override - public IMNode init() { + public ICachedMNode init() { storageGroupMNode = - new StorageGroupMNode( + nodeFactory.createDatabaseMNode( null, storageGroupPath.getTailNode(), CommonDescriptor.getInstance().getConfig().getDefaultTTLInMs()); - writeMNode(storageGroupMNode); - return cloneMNode(storageGroupMNode); + writeMNode(storageGroupMNode.getAsMNode()); + return cloneMNode(storageGroupMNode.getAsMNode()); } @Override - public boolean updateDatabaseNode(IStorageGroupMNode sgNode) throws IOException { - this.storageGroupMNode = cloneMNode(sgNode).getAsStorageGroupMNode(); + public boolean updateDatabaseNode(IDatabaseMNode sgNode) throws IOException { + this.storageGroupMNode = cloneMNode(sgNode.getAsMNode()).getAsDatabaseMNode(); return true; } @Override - public IMNode getChildNode(IMNode parent, String childName) { - Map segment = getSegment(parent); - IMNode result = null; + public ICachedMNode getChildNode(ICachedMNode parent, String childName) { + Map segment = getSegment(parent); + ICachedMNode result = null; if (segment != null) { result = cloneMNode(segment.get(childName)); - if (result == null && parent.isEntity()) { - for (IMNode node : segment.values()) { + if (result == null && parent.isDevice()) { + for (ICachedMNode node : segment.values()) { if (node.isMeasurement() && childName.equals(node.getAsMeasurementMNode().getAlias())) { result = cloneMNode(node); break; @@ -90,9 +87,9 @@ public IMNode getChildNode(IMNode parent, String childName) { } @Override - public Iterator getChildren(IMNode parent) { + public Iterator getChildren(ICachedMNode parent) { - Map segment = getSegment(parent); + Map segment = getSegment(parent); if (segment == null) { return Collections.emptyIterator(); } @@ -105,7 +102,7 @@ public boolean createSnapshot(File snapshotDir) { } @Override - public void writeMNode(IMNode parent) { + public void writeMNode(ICachedMNode parent) { ICachedMNodeContainer container = getCachedMNodeContainer(parent); long address = container.getSegmentAddress(); if (container.isVolatile()) { @@ -116,8 +113,8 @@ public void writeMNode(IMNode parent) { write(address, container.getNewChildBuffer()); } - private void write(long address, Map nodeMap) { - for (IMNode node : nodeMap.values()) { + private void write(long address, Map nodeMap) { + for (ICachedMNode node : nodeMap.values()) { if (!node.isMeasurement()) { ICachedMNodeContainer container = getCachedMNodeContainer(node); if (container.isVolatile()) { @@ -129,19 +126,19 @@ private void write(long address, Map nodeMap) { } @Override - public void delete(IMNode targetNode) { - IMNode removedNode = getSegment(targetNode.getParent()).remove(targetNode.getName()); + public void delete(ICachedMNode targetNode) { + ICachedMNode removedNode = getSegment(targetNode.getParent()).remove(targetNode.getName()); if (removedNode == null || removedNode.isMeasurement()) { return; } deleteMNodeRecursively(removedNode); } - private void deleteMNodeRecursively(IMNode node) { + private void deleteMNodeRecursively(ICachedMNode node) { ICachedMNodeContainer container = getCachedMNodeContainer(node); - Map removedSegment = mockFile.remove(container.getSegmentAddress()); + Map removedSegment = mockFile.remove(container.getSegmentAddress()); if (removedSegment != null) { - for (IMNode child : removedSegment.values()) { + for (ICachedMNode child : removedSegment.values()) { deleteMNodeRecursively(child); } } @@ -158,11 +155,11 @@ public void clear() { mockFile.clear(); } - private long getSegmentAddress(IMNode node) { + private long getSegmentAddress(ICachedMNode node) { return getCachedMNodeContainer(node).getSegmentAddress(); } - private Map getSegment(IMNode node) { + private Map getSegment(ICachedMNode node) { return mockFile.get(getSegmentAddress(node)); } @@ -172,56 +169,59 @@ private long allocateSegment() { return address; } - static IMNode cloneMNode(IMNode node) { + static ICachedMNode cloneMNode(ICachedMNode node) { if (node == null) { return null; } if (node.isMeasurement()) { - IMeasurementMNode measurementMNode = node.getAsMeasurementMNode(); - IMeasurementMNode result = - MeasurementMNode.getMeasurementMNode( - null, - measurementMNode.getName(), - measurementMNode.getSchema(), - measurementMNode.getAlias()); - result.setOffset(measurementMNode.getOffset()); + IMeasurementMNode measurementMNode = node.getAsMeasurementMNode(); + ICachedMNode result = + nodeFactory + .createMeasurementMNode( + null, + measurementMNode.getName(), + measurementMNode.getSchema(), + measurementMNode.getAlias()) + .getAsMNode(); + result.getAsMeasurementMNode().setOffset(measurementMNode.getOffset()); return result; - } else if (node.isStorageGroup() && node.isEntity()) { - StorageGroupEntityMNode result = - new StorageGroupEntityMNode( - null, node.getName(), node.getAsStorageGroupMNode().getDataTTL()); - result.setAligned(node.getAsEntityMNode().isAligned()); + } else if (node.isDatabase() && node.isDevice()) { + ICachedMNode result = + nodeFactory.createDatabaseDeviceMNode( + null, node.getName(), node.getAsDatabaseMNode().getDataTTL()); + result.getAsDeviceMNode().setAligned(node.getAsDeviceMNode().isAligned()); cloneInternalMNodeData(node, result); return result; - } else if (node.isEntity()) { - IEntityMNode result = new EntityMNode(null, node.getName()); - result.setAligned(node.getAsEntityMNode().isAligned()); + } else if (node.isDevice()) { + ICachedMNode result = nodeFactory.createDeviceMNode(null, node.getName()).getAsMNode(); + result.getAsDeviceMNode().setAligned(node.getAsDeviceMNode().isAligned()); cloneInternalMNodeData(node, result); return result; - } else if (node.isStorageGroup()) { - StorageGroupMNode result = - new StorageGroupMNode(null, node.getName(), node.getAsStorageGroupMNode().getDataTTL()); + } else if (node.isDatabase()) { + ICachedMNode result = + nodeFactory + .createDatabaseMNode(null, node.getName(), node.getAsDatabaseMNode().getDataTTL()) + .getAsMNode(); cloneInternalMNodeData(node, result); return result; } else { - InternalMNode result = new InternalMNode(null, node.getName()); + ICachedMNode result = nodeFactory.createInternalMNode(null, node.getName()); cloneInternalMNodeData(node, result); return result; } } - private static void cloneInternalMNodeData(IMNode node, IMNode result) { - result.setUseTemplate(node.isUseTemplate()); + private static void cloneInternalMNodeData(ICachedMNode node, ICachedMNode result) { ICachedMNodeContainer container = new CachedMNodeContainer(); container.setSegmentAddress((getCachedMNodeContainer(node)).getSegmentAddress()); result.setChildren(container); } - private class MockSchemaFileIterator implements Iterator { + private class MockSchemaFileIterator implements Iterator { - Iterator iterator; + Iterator iterator; - MockSchemaFileIterator(Iterator iterator) { + MockSchemaFileIterator(Iterator iterator) { this.iterator = iterator; } @@ -231,7 +231,7 @@ public boolean hasNext() { } @Override - public IMNode next() { + public ICachedMNode next() { return cloneMNode(iterator.next()); } } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/RecordUtils.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/RecordUtils.java index a64b0afd4c7a8..e648c673acfbd 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/RecordUtils.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/RecordUtils.java @@ -19,13 +19,13 @@ package org.apache.iotdb.db.metadata.mtree.store.disk.schemafile; import org.apache.iotdb.commons.exception.MetadataException; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; import org.apache.iotdb.commons.utils.TestOnly; -import org.apache.iotdb.db.metadata.mnode.EntityMNode; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.InternalMNode; -import org.apache.iotdb.db.metadata.mnode.MeasurementMNode; -import org.apache.iotdb.db.metadata.mtree.store.disk.ICachedMNodeContainer; +import org.apache.iotdb.db.metadata.MetadataConstant; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.container.ICachedMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.schemafile.factory.CacheMNodeFactory; import org.apache.iotdb.tsfile.file.metadata.enums.CompressionType; import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; import org.apache.iotdb.tsfile.file.metadata.enums.TSEncoding; @@ -37,15 +37,15 @@ import java.util.Map; /** - * This class translate an IMNode into a bytebuffer, or vice versa. Expected to support record as - * entry of segment-level index further. Coupling with IMNode structure.
    + * This class translate an ICacheMNode into a bytebuffer, or vice versa. Expected to support record + * as entry of segment-level index further. Coupling with ICacheMNode structure.
    *
    * TODO: Guardian statements on higher stack NEEDED. The longest ALIAS is limited to 0x7fff(32767) * bytes for that a Short is used to record the length, hence a colossal record may collapse the * stack. */ public class RecordUtils { - // Offsets of IMNode infos in a record buffer + // Offsets of ICacheMNode infos in a record buffer private static final short INTERNAL_NODE_LENGTH = (short) 1 + 2 + 8 + 4 + 1; // always fixed length record private static final short MEASUREMENT_BASIC_LENGTH = @@ -63,7 +63,9 @@ public class RecordUtils { private static final byte ENTITY_TYPE = 1; private static final byte MEASUREMENT_TYPE = 4; - public static ByteBuffer node2Buffer(IMNode node) { + private static final IMNodeFactory nodeFactory = CacheMNodeFactory.getInstance(); + + public static ByteBuffer node2Buffer(ICachedMNode node) { if (node.isMeasurement()) { return measurement2Buffer(node.getAsMeasurementMNode()); } else { @@ -93,13 +95,17 @@ public static ByteBuffer node2Buffer(IMNode node) { * @param node * @return */ - private static ByteBuffer internal2Buffer(IMNode node) { + private static ByteBuffer internal2Buffer(ICachedMNode node) { byte nodeType = INTERNAL_TYPE; boolean isAligned = false; + int schemaTemplateIdWithState = MetadataConstant.NON_TEMPLATE; + boolean isUseTemplate = false; - if (node.isEntity()) { + if (node.isDevice()) { nodeType = ENTITY_TYPE; - isAligned = node.getAsEntityMNode().isAligned(); + isAligned = node.getAsDeviceMNode().isAligned(); + schemaTemplateIdWithState = node.getAsDeviceMNode().getSchemaTemplateIdWithState(); + isUseTemplate = node.getAsDeviceMNode().isUseTemplate(); } ByteBuffer buffer = ByteBuffer.allocate(INTERNAL_NODE_LENGTH); @@ -107,10 +113,10 @@ private static ByteBuffer internal2Buffer(IMNode node) { ReadWriteIOUtils.write(INTERNAL_NODE_LENGTH, buffer); ReadWriteIOUtils.write( ICachedMNodeContainer.getCachedMNodeContainer(node).getSegmentAddress(), buffer); - ReadWriteIOUtils.write(node.getSchemaTemplateIdWithState(), buffer); + ReadWriteIOUtils.write(schemaTemplateIdWithState, buffer); // encode bitwise flag - byte useAndAligned = encodeInternalStatus(node.isUseTemplate(), isAligned); + byte useAndAligned = encodeInternalStatus(isUseTemplate, isAligned); ReadWriteIOUtils.write(useAndAligned, buffer); return buffer; @@ -134,7 +140,7 @@ private static ByteBuffer internal2Buffer(IMNode node) { * *

    It doesn't use MeasurementSchema.serializeTo for duplication of measurementId */ - private static ByteBuffer measurement2Buffer(IMeasurementMNode node) { + private static ByteBuffer measurement2Buffer(IMeasurementMNode node) { int bufferLength = node.getAlias() == null ? 4 + MEASUREMENT_BASIC_LENGTH @@ -172,8 +178,9 @@ private static ByteBuffer measurement2Buffer(IMeasurementMNode node) { * @param buffer content of the node * @return node constructed from buffer */ - public static IMNode buffer2Node(String nodeName, ByteBuffer buffer) throws MetadataException { - IMNode resNode; + public static ICachedMNode buffer2Node(String nodeName, ByteBuffer buffer) + throws MetadataException { + ICachedMNode resNode; byte nodeType = ReadWriteIOUtils.readByte(buffer); if (nodeType < 2) { @@ -188,15 +195,15 @@ public static IMNode buffer2Node(String nodeName, ByteBuffer buffer) throws Meta boolean isAligned = isAligned(bitFlag); if (nodeType == 0) { - resNode = new InternalMNode(null, nodeName); + resNode = nodeFactory.createInternalMNode(null, nodeName); } else { - resNode = new EntityMNode(null, nodeName); - resNode.getAsEntityMNode().setAligned(isAligned); + resNode = nodeFactory.createDeviceMNode(null, nodeName).getAsMNode(); + resNode.getAsDeviceMNode().setAligned(isAligned); + resNode.getAsDeviceMNode().setUseTemplate(usingTemplate); + resNode.getAsDeviceMNode().setSchemaTemplateId(templateId); } ICachedMNodeContainer.getCachedMNodeContainer(resNode).setSegmentAddress(segAddr); - resNode.setUseTemplate(usingTemplate); - resNode.setSchemaTemplateId(templateId); return resNode; } else { @@ -283,7 +290,7 @@ public static void updateSegAddr(ByteBuffer recBuf, long newSegAddr) { @TestOnly public static String buffer2String(ByteBuffer buffer) throws MetadataException { StringBuilder builder = new StringBuilder("["); - IMNode node = buffer2Node("unspecified", buffer); + ICachedMNode node = buffer2Node("unspecified", buffer); if (node.isMeasurement()) { builder.append("measurementNode, "); builder.append( @@ -293,7 +300,7 @@ public static String buffer2String(ByteBuffer buffer) throws MetadataException { ? "" : node.getAsMeasurementMNode().getAlias())); builder.append( - String.format("type: %s, ", node.getAsMeasurementMNode().getDataType("").toString())); + String.format("type: %s, ", node.getAsMeasurementMNode().getDataType().toString())); builder.append( String.format( "encoding: %s, ", @@ -303,36 +310,36 @@ public static String buffer2String(ByteBuffer buffer) throws MetadataException { "compressor: %s]", node.getAsMeasurementMNode().getSchema().getCompressor().toString())); return builder.toString(); - } else if (node.isEntity()) { + } else if (node.isDevice()) { builder.append("entityNode, "); - if (node.getAsEntityMNode().isAligned()) { + if (node.getAsDeviceMNode().isAligned()) { builder.append("aligned, "); } else { builder.append("not aligned, "); } - } else { - builder.append("internalNode, "); - } + if (node.getAsDeviceMNode().isUseTemplate()) { + builder.append("using template.]"); + } else { + builder.append("not using template.]"); + } - if (node.isUseTemplate()) { - builder.append("using template.]"); } else { - builder.append("not using template.]"); + builder.append("internalNode, "); } return builder.toString(); } - // region padding with IMNode + // region padding with ICacheMNode /** These 2 convert methods are coupling with tag, template module respectively. */ - private static long convertTags2Long(IMeasurementMNode node) { + private static long convertTags2Long(IMeasurementMNode node) { return node.getOffset(); } /** Including schema and pre-delete flag of a measurement, could be expanded further. */ - private static long convertMeasStat2Long(IMeasurementMNode node) { + private static long convertMeasStat2Long(IMeasurementMNode node) { byte dataType = node.getSchema().getTypeInByte(); byte encoding = node.getSchema().getEncodingType().serialize(); byte compressor = node.getSchema().getCompressor().serialize(); @@ -341,7 +348,7 @@ private static long convertMeasStat2Long(IMeasurementMNode node) { return (preDelete << 24 | dataType << 16 | encoding << 8 | compressor); } - private static IMNode paddingMeasurement( + private static ICachedMNode paddingMeasurement( String nodeName, long tagIndex, long statsBytes, String alias, Map props) { byte preDel = (byte) (statsBytes >>> 24); byte dataType = (byte) (statsBytes >>> 16); @@ -356,7 +363,8 @@ private static IMNode paddingMeasurement( CompressionType.deserialize(compressor), props); - IMNode res = MeasurementMNode.getMeasurementMNode(null, nodeName, schema, alias); + ICachedMNode res = + nodeFactory.createMeasurementMNode(null, nodeName, schema, alias).getAsMNode(); res.getAsMeasurementMNode().setOffset(tagIndex); if (preDel > 0) { diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/SchemaFile.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/SchemaFile.java index 212e81dc61b0c..f4a25a0625c65 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/SchemaFile.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/SchemaFile.java @@ -21,15 +21,15 @@ import org.apache.iotdb.commons.conf.CommonDescriptor; import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.file.SystemFileFactory; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; import org.apache.iotdb.commons.utils.PathUtils; import org.apache.iotdb.commons.utils.TestOnly; import org.apache.iotdb.db.exception.metadata.schemafile.SchemaFileNotExists; import org.apache.iotdb.db.metadata.MetadataConstant; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IStorageGroupMNode; -import org.apache.iotdb.db.metadata.mnode.StorageGroupEntityMNode; -import org.apache.iotdb.db.metadata.mnode.StorageGroupMNode; -import org.apache.iotdb.db.metadata.mtree.store.disk.ICachedMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.container.ICachedMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.schemafile.factory.CacheMNodeFactory; import org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.pagemgr.BTreePageManager; import org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.pagemgr.IPageManager; import org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.pagemgr.PageManager; @@ -77,12 +77,13 @@ public class SchemaFile implements ISchemaFile { private File pmtFile; private FileChannel channel; + private final IMNodeFactory nodeFactory = CacheMNodeFactory.getInstance(); + // todo refactor constructor for schema file in Jan. private SchemaFile( String sgName, int schemaRegionId, boolean override, long ttl, boolean isEntity) throws IOException, MetadataException { String dirPath = getDirPath(sgName, schemaRegionId); - this.storageGroupName = sgName; this.filePath = dirPath + File.separator + MetadataConstant.SCHEMA_FILE_NAME; this.logPath = dirPath + File.separator + MetadataConstant.SCHEMA_LOG_FILE_NAME; @@ -167,8 +168,8 @@ private static String getDirPath(String sgName, int schemaRegionId) { // region Interface Implementation @Override - public IMNode init() throws MetadataException { - IMNode resNode; + public ICachedMNode init() throws MetadataException { + ICachedMNode resNode; String[] sgPathNodes = storageGroupName == null ? new String[] {"noName"} @@ -176,30 +177,37 @@ public IMNode init() throws MetadataException { if (isEntity) { resNode = setNodeAddress( - new StorageGroupEntityMNode(null, sgPathNodes[sgPathNodes.length - 1], dataTTL), 0L); + nodeFactory.createDatabaseDeviceMNode( + null, sgPathNodes[sgPathNodes.length - 1], dataTTL), + 0L); + resNode.getAsDeviceMNode().setSchemaTemplateId(sgNodeTemplateIdWithState); + resNode.getAsDeviceMNode().setUseTemplate(sgNodeTemplateIdWithState > -1); } else { resNode = setNodeAddress( - new StorageGroupMNode(null, sgPathNodes[sgPathNodes.length - 1], dataTTL), 0L); + nodeFactory + .createDatabaseMNode(null, sgPathNodes[sgPathNodes.length - 1], dataTTL) + .getAsMNode(), + 0L); } resNode.setFullPath(storageGroupName); - resNode.setSchemaTemplateId(sgNodeTemplateIdWithState); - resNode.setUseTemplate(sgNodeTemplateIdWithState > -1); return resNode; } @Override - public boolean updateDatabaseNode(IStorageGroupMNode sgNode) throws IOException { + public boolean updateDatabaseNode(IDatabaseMNode sgNode) throws IOException { this.dataTTL = sgNode.getDataTTL(); - this.isEntity = sgNode.isEntity(); - this.sgNodeTemplateIdWithState = sgNode.getSchemaTemplateIdWithState(); + this.isEntity = sgNode.isDevice(); + if (sgNode.isDevice()) { + this.sgNodeTemplateIdWithState = sgNode.getAsDeviceMNode().getSchemaTemplateIdWithState(); + } updateHeaderBuffer(); return true; } @Override - public void delete(IMNode node) throws IOException, MetadataException { - if (node.isStorageGroup()) { + public void delete(ICachedMNode node) throws IOException, MetadataException { + if (node.isDatabase()) { // should clear this file clear(); } else { @@ -208,11 +216,11 @@ public void delete(IMNode node) throws IOException, MetadataException { } @Override - public void writeMNode(IMNode node) throws MetadataException, IOException { + public void writeMNode(ICachedMNode node) throws MetadataException, IOException { long curSegAddr = getNodeAddress(node); - if (node.isStorageGroup()) { - isEntity = node.isEntity(); + if (node.isDatabase()) { + isEntity = node.isDevice(); setNodeAddress(node, lastSGAddr); } else { if (curSegAddr < 0L) { @@ -231,13 +239,14 @@ public void writeMNode(IMNode node) throws MetadataException, IOException { } @Override - public IMNode getChildNode(IMNode parent, String childName) + public ICachedMNode getChildNode(ICachedMNode parent, String childName) throws MetadataException, IOException { return pageManager.getChildNode(parent, childName); } @Override - public Iterator getChildren(IMNode parent) throws MetadataException, IOException { + public Iterator getChildren(ICachedMNode parent) + throws MetadataException, IOException { if (parent.isMeasurement() || getNodeAddress(parent) < 0) { throw new MetadataException( String.format("Node [%s] has no child in schema file.", parent.getFullPath())); @@ -408,11 +417,11 @@ public static long getPageAddress(int pageIndex) { + SchemaFileConfig.FILE_HEADER_SIZE; } - public static long getNodeAddress(IMNode node) { + public static long getNodeAddress(ICachedMNode node) { return ICachedMNodeContainer.getCachedMNodeContainer(node).getSegmentAddress(); } - public static IMNode setNodeAddress(IMNode node, long addr) { + public static ICachedMNode setNodeAddress(ICachedMNode node, long addr) { ICachedMNodeContainer.getCachedMNodeContainer(node).setSegmentAddress(addr); return node; } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/SegmentedPage.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/SegmentedPage.java index bf9303d750f86..0c3628ad3a445 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/SegmentedPage.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/SegmentedPage.java @@ -24,7 +24,7 @@ import org.apache.iotdb.db.exception.metadata.schemafile.SchemaPageOverflowException; import org.apache.iotdb.db.exception.metadata.schemafile.SegmentNotFoundException; import org.apache.iotdb.db.exception.metadata.schemafile.SegmentOverflowException; -import org.apache.iotdb.db.metadata.mnode.IMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; import org.apache.iotdb.tsfile.utils.ReadWriteIOUtils; import java.nio.ByteBuffer; @@ -42,7 +42,7 @@ public class SegmentedPage extends SchemaPage implements ISegmentedPage { // maintains leaf segment instance inside this page, lazily instantiated // map segmentIndex -> segmentInstance - private final transient Map> segCacheMap; + private final transient Map> segCacheMap; /** * This class is aimed to manage space inside one page. @@ -60,7 +60,7 @@ public class SegmentedPage extends SchemaPage implements ISegmentedPage { *

    Page Body Structure: * *

      - *
    • var length * memberNum: {@linkplain WrappedSegment} contains serialized IMNodes. + *
    • var length * memberNum: {@linkplain WrappedSegment} contains serialized ICacheMNodes. *
    • ... spare space... *
    • 2 bytes * memberNum: offset of segments, using marking deletion as {@linkplain * #deleteSegment} mentioned. @@ -81,7 +81,7 @@ public SegmentedPage(ByteBuffer pageBuffer) { @Override public long write(short segIdx, String key, ByteBuffer buffer) throws MetadataException { - ISegment tarSeg = getSegment(segIdx); + ISegment tarSeg = getSegment(segIdx); if (tarSeg.insertRecord(key, buffer) < 0) { // relocate inside page, if not enough space for new size segment, throw exception @@ -100,18 +100,18 @@ public long write(short segIdx, String key, ByteBuffer buffer) throws MetadataEx } @Override - public IMNode read(short segIdx, String key) throws MetadataException { + public ICachedMNode read(short segIdx, String key) throws MetadataException { return getSegment(segIdx).getRecordByKey(key); } @Override - public IMNode readByAlias(short segIdx, String alias) throws MetadataException { + public ICachedMNode readByAlias(short segIdx, String alias) throws MetadataException { return getSegment(segIdx).getRecordByAlias(alias); } @Override public void update(short segIdx, String key, ByteBuffer buffer) throws MetadataException { - ISegment seg = getSegment(segIdx); + ISegment seg = getSegment(segIdx); try { if (seg.updateRecord(key, buffer) < 0) { throw new MetadataException("Record to update not found."); @@ -131,7 +131,7 @@ public void update(short segIdx, String key, ByteBuffer buffer) throws MetadataE } @Override - public Queue getChildren(short segId) throws MetadataException { + public Queue getChildren(short segId) throws MetadataException { return getSegment(segId).getAllRecords(); } @@ -189,7 +189,8 @@ public void getPageBuffer(ByteBuffer dst) { @Override public synchronized short allocNewSegment(short size) throws MetadataException { - ISegment newSeg = WrappedSegment.initAsSegment(allocSpareBufferSlice(size)); + ISegment newSeg = + WrappedSegment.initAsSegment(allocSpareBufferSlice(size)); if (newSeg == null) { compactSegments(); @@ -223,7 +224,8 @@ public long transplantSegment(ISegmentedPage srcPage, short segId, short newSegS this.pageBuffer.position(spareOffset); this.pageBuffer.limit(spareOffset + newSegSize); - ISegment newSeg = WrappedSegment.loadAsSegment(this.pageBuffer.slice()); + ISegment newSeg = + WrappedSegment.loadAsSegment(this.pageBuffer.slice()); // registerNewSegment will modify page status considering the new segment return SchemaFile.getGlobalIndex(pageIndex, registerNewSegment(newSeg)); @@ -289,7 +291,7 @@ public String inspect() throws SegmentNotFoundException { @Override public synchronized void syncPageBuffer() { super.syncPageBuffer(); - for (Map.Entry> entry : segCacheMap.entrySet()) { + for (Map.Entry> entry : segCacheMap.entrySet()) { entry.getValue().syncBuffer(); } @@ -330,7 +332,8 @@ public ByteBuffer getEntireSegmentSlice() throws MetadataException { * @param index index rather than offset of the segment * @return null if InternalSegment, otherwise instance */ - private ISegment getSegment(short index) throws SegmentNotFoundException { + private ISegment getSegment(short index) + throws SegmentNotFoundException { if (segOffsetLst.size() <= index || segOffsetLst.get(index) < 0) { throw new SegmentNotFoundException(pageIndex, index); } @@ -347,7 +350,7 @@ private ISegment getSegment(short index) throws SegmentNotFo bufferR.position(getSegmentOffset(index)); bufferR.limit(bufferR.position() + WrappedSegment.getSegBufLen(bufferR)); - ISegment res; + ISegment res; try { res = WrappedSegment.loadAsSegment(bufferR.slice()); } catch (RecordDuplicatedException e) { @@ -386,7 +389,7 @@ private short getSegmentOffset(short index) throws SegmentNotFoundException { * @return reallocated segment instance * @throws SchemaPageOverflowException if this page has no enough space */ - private ISegment relocateSegment( + private ISegment relocateSegment( ISegment seg, short segIdx, short newSize) throws MetadataException { if (seg.size() == SchemaFileConfig.SEG_MAX_SIZ || getSpareSize() + seg.size() < newSize) { throw new SchemaPageOverflowException(pageIndex); @@ -401,7 +404,7 @@ private ISegment relocateSegment( // allocate buffer slice successfully seg.extendsTo(newBuffer); - ISegment newSeg = WrappedSegment.loadAsSegment(newBuffer); + ISegment newSeg = WrappedSegment.loadAsSegment(newBuffer); // since this buffer is allocated from pageSpareOffset, new spare offset can simply add size up segOffsetLst.set(segIdx, spareOffset); @@ -513,7 +516,7 @@ private synchronized void rearrangeSegments(short idx) { * @param newSize extended size * @return extended segment based on page buffer */ - private ISegment extendSegmentInPlace( + private ISegment extendSegmentInPlace( short segId, short oriSegSize, short newSize) throws MetadataException { // extend segment, modify pageSpareOffset, segCacheMap short offset = getSegmentOffset(segId); @@ -536,7 +539,7 @@ private ISegment extendSegmentInPlace( // pass page buffer slice to instantiate segment pageBuffer.position(offset); pageBuffer.limit(offset + newSize); - ISegment newSeg = WrappedSegment.loadAsSegment(pageBuffer.slice()); + ISegment newSeg = WrappedSegment.loadAsSegment(pageBuffer.slice()); // modify status segOffsetLst.set(segId, offset); @@ -549,7 +552,7 @@ private ISegment extendSegmentInPlace( public void updateRecordSegAddr(short segId, String key, long newSegAddr) throws SegmentNotFoundException { - ISegment seg = getSegment(segId); + ISegment seg = getSegment(segId); // TODO: add to interface ((WrappedSegment) seg).updateRecordSegAddr(key, newSegAddr); } @@ -561,7 +564,7 @@ public void updateRecordSegAddr(short segId, String key, long newSegAddr) * @param seg the segment to register * @return index of the segment */ - private synchronized short registerNewSegment(ISegment seg) + private synchronized short registerNewSegment(ISegment seg) throws MetadataException { short thisIndex = (short) segOffsetLst.size(); if (segCacheMap.containsKey(thisIndex)) { diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/WrappedSegment.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/WrappedSegment.java index 55551d6104139..ed888902b7f67 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/WrappedSegment.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/WrappedSegment.java @@ -22,7 +22,7 @@ import org.apache.iotdb.commons.utils.TestOnly; import org.apache.iotdb.db.exception.metadata.schemafile.RecordDuplicatedException; import org.apache.iotdb.db.exception.metadata.schemafile.SegmentOverflowException; -import org.apache.iotdb.db.metadata.mnode.IMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; import org.apache.iotdb.tsfile.file.metadata.enums.CompressionType; import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; import org.apache.iotdb.tsfile.file.metadata.enums.TSEncoding; @@ -38,11 +38,11 @@ /** * This class initiate a segment object with corresponding bytes. Implements add, get, remove - * records which is serialized IMNode.
      + * records which is serialized ICacheMNode.
      * Act like a wrapper of a bytebuffer which reflects a segment.
      * And itself is wrapped inside a SchemaPage. */ -public class WrappedSegment extends Segment { +public class WrappedSegment extends Segment { // reconstruct every initiation after keyAddressList but not write into buffer private List> aliasKeyList; @@ -86,7 +86,7 @@ public WrappedSegment(int size) throws RecordDuplicatedException { this(ByteBuffer.allocate(size)); } - public static ISegment initAsSegment(ByteBuffer buffer) + public static ISegment initAsSegment(ByteBuffer buffer) throws RecordDuplicatedException { if (buffer == null) { return null; @@ -94,7 +94,7 @@ public static ISegment initAsSegment(ByteBuffer buffer) return new WrappedSegment(buffer, true); } - public static ISegment loadAsSegment(ByteBuffer buffer) + public static ISegment loadAsSegment(ByteBuffer buffer) throws RecordDuplicatedException { if (buffer == null) { return null; @@ -168,7 +168,7 @@ public synchronized String splitByKey( } @Override - public IMNode getRecordByKey(String key) throws MetadataException { + public ICachedMNode getRecordByKey(String key) throws MetadataException { // index means order for target node in keyAddressList, NOT aliasKeyList int index = getRecordIndexByKey(key); @@ -187,7 +187,7 @@ public IMNode getRecordByKey(String key) throws MetadataException { } @Override - public IMNode getRecordByAlias(String alias) throws MetadataException { + public ICachedMNode getRecordByAlias(String alias) throws MetadataException { int ix = getRecordIndexByAlias(alias); if (ix < 0) { @@ -206,8 +206,8 @@ public boolean hasRecordAlias(String alias) { } @Override - public Queue getAllRecords() throws MetadataException { - Queue res = new ArrayDeque<>(keyAddressList.size()); + public Queue getAllRecords() throws MetadataException { + Queue res = new ArrayDeque<>(keyAddressList.size()); ByteBuffer roBuffer = this.buffer.asReadOnlyBuffer(); roBuffer.clear(); for (Pair p : keyAddressList) { @@ -274,7 +274,7 @@ public int updateRecord(String key, ByteBuffer uBuffer) public int removeRecord(String key) { int idx = getRecordIndexByKey(key); - // deletion only seeks for name of IMNode + // deletion only seeks for name of ICacheMNode if (idx < 0) { return -1; } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/pagemgr/BTreePageManager.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/pagemgr/BTreePageManager.java index 7474e2313a844..6861f0678a6c2 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/pagemgr/BTreePageManager.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/pagemgr/BTreePageManager.java @@ -20,7 +20,7 @@ import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.db.exception.metadata.schemafile.ColossalRecordException; -import org.apache.iotdb.db.metadata.mnode.IMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; import org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.ISchemaPage; import org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.ISegmentedPage; import org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.SchemaFileConfig; @@ -95,7 +95,7 @@ protected void multiPageUpdateOverflowOperation( * @throws IOException */ @Override - protected void buildSubIndex(IMNode parNode) throws MetadataException, IOException { + protected void buildSubIndex(ICachedMNode parNode) throws MetadataException, IOException { ISchemaPage cursorPage = getPageInstance(getPageIndex(getNodeAddress(parNode))); if (cursorPage.getAsInternalPage() == null) { @@ -114,8 +114,8 @@ protected void buildSubIndex(IMNode parNode) throws MetadataException, IOExcepti } long nextAddr = cursorPage.getAsSegmentedPage().getNextSegAddress((short) 0); - Queue children = cursorPage.getAsSegmentedPage().getChildren((short) 0); - IMNode child; + Queue children = cursorPage.getAsSegmentedPage().getChildren((short) 0); + ICachedMNode child; // TODO: inefficient to build B+Tree up-to-bottom, improve further while (!children.isEmpty() || nextAddr != -1L) { if (children.isEmpty()) { @@ -293,7 +293,7 @@ private void insertIndexEntryRecursiveUpwards(int treeTraceIndex, String key, in } @Override - public void delete(IMNode node) throws IOException, MetadataException { + public void delete(ICachedMNode node) throws IOException, MetadataException { // remove corresponding record long recSegAddr = getNodeAddress(node.getParent()); recSegAddr = getTargetSegmentAddress(recSegAddr, node.getName()); @@ -351,7 +351,7 @@ public void delete(IMNode node) throws IOException, MetadataException { } @Override - public IMNode getChildNode(IMNode parent, String childName) + public ICachedMNode getChildNode(ICachedMNode parent, String childName) throws MetadataException, IOException { if (getNodeAddress(parent) < 0) { throw new MetadataException( @@ -360,12 +360,12 @@ public IMNode getChildNode(IMNode parent, String childName) } long actualSegAddr = getTargetSegmentAddress(getNodeAddress(parent), childName); - IMNode child = + ICachedMNode child = getPageInstance(getPageIndex(actualSegAddr)) .getAsSegmentedPage() .read(getSegIndex(actualSegAddr), childName); - if (child == null && parent.isEntity()) { + if (child == null && parent.isDevice()) { // try read alias directly first child = getPageInstance(getPageIndex(actualSegAddr)) @@ -381,7 +381,8 @@ public IMNode getChildNode(IMNode parent, String childName) return child; } - private IMNode getChildWithAlias(IMNode par, String alias) throws IOException, MetadataException { + private ICachedMNode getChildWithAlias(ICachedMNode par, String alias) + throws IOException, MetadataException { long srtAddr = getNodeAddress(par); ISchemaPage page = getPageInstance(getPageIndex(srtAddr)); @@ -399,7 +400,8 @@ private IMNode getChildWithAlias(IMNode par, String alias) throws IOException, M } @Override - public Iterator getChildren(IMNode parent) throws MetadataException, IOException { + public Iterator getChildren(ICachedMNode parent) + throws MetadataException, IOException { int pageIdx = getPageIndex(getNodeAddress(parent)); short segId = getSegIndex(getNodeAddress(parent)); ISchemaPage page = getPageInstance(pageIdx); @@ -409,10 +411,10 @@ public Iterator getChildren(IMNode parent) throws MetadataException, IOE } long actualSegAddr = page.getAsSegmentedPage().getNextSegAddress(segId); - Queue initChildren = page.getAsSegmentedPage().getChildren(segId); - return new Iterator() { + Queue initChildren = page.getAsSegmentedPage().getChildren(segId); + return new Iterator() { long nextSeg = actualSegAddr; - Queue children = initChildren; + Queue children = initChildren; @Override public boolean hasNext() { @@ -439,7 +441,7 @@ public boolean hasNext() { } @Override - public IMNode next() { + public ICachedMNode next() { return children.poll(); } }; diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/pagemgr/IPageManager.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/pagemgr/IPageManager.java index 05fdce3f85f82..3c6695efb1307 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/pagemgr/IPageManager.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/pagemgr/IPageManager.java @@ -19,7 +19,7 @@ package org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.pagemgr; import org.apache.iotdb.commons.exception.MetadataException; -import org.apache.iotdb.db.metadata.mnode.IMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; import org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.SchemaFile; import org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.SchemaPage; @@ -38,15 +38,16 @@ */ public interface IPageManager { - void writeNewChildren(IMNode parNode) throws MetadataException, IOException; + void writeNewChildren(ICachedMNode parNode) throws MetadataException, IOException; - void writeUpdatedChildren(IMNode parNode) throws MetadataException, IOException; + void writeUpdatedChildren(ICachedMNode parNode) throws MetadataException, IOException; - void delete(IMNode node) throws IOException, MetadataException; + void delete(ICachedMNode node) throws IOException, MetadataException; - IMNode getChildNode(IMNode parent, String childName) throws MetadataException, IOException; + ICachedMNode getChildNode(ICachedMNode parent, String childName) + throws MetadataException, IOException; - Iterator getChildren(IMNode parent) throws MetadataException, IOException; + Iterator getChildren(ICachedMNode parent) throws MetadataException, IOException; void clear() throws IOException, MetadataException; diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/pagemgr/PageManager.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/pagemgr/PageManager.java index ca5b04c8554f4..d85e0e94ab939 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/pagemgr/PageManager.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/disk/schemafile/pagemgr/PageManager.java @@ -21,8 +21,8 @@ import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.utils.TestOnly; import org.apache.iotdb.db.exception.metadata.schemafile.SchemaPageOverflowException; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mtree.store.disk.ICachedMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.container.ICachedMNodeContainer; import org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.ISchemaPage; import org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.ISegmentedPage; import org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.RecordUtils; @@ -150,16 +150,16 @@ private long recoverFromLog(String logPath) throws IOException, MetadataExceptio // region Framework Methods @Override - public void writeNewChildren(IMNode node) throws MetadataException, IOException { + public void writeNewChildren(ICachedMNode node) throws MetadataException, IOException { int subIndex; long curSegAddr = getNodeAddress(node); long actualAddress; // actual segment to write record - IMNode child; + ICachedMNode child; ISchemaPage curPage; ByteBuffer childBuffer; String alias; // TODO: reserve order of insert in container may be better - for (Map.Entry entry : + for (Map.Entry entry : ICachedMNodeContainer.getCachedMNodeContainer(node).getNewChildBuffer().entrySet().stream() .sorted(Map.Entry.comparingByKey()) .collect(Collectors.toList())) { @@ -204,7 +204,7 @@ public void writeNewChildren(IMNode node) throws MetadataException, IOException multiPageInsertOverflowOperation(curPage, entry.getKey(), childBuffer); subIndex = subIndexRootPage(curSegAddr); - if (node.isEntity() && subIndex < 0) { + if (node.isDevice() && subIndex < 0) { // the record occurred overflow had been inserted already buildSubIndex(node); } else if (alias != null) { @@ -238,16 +238,16 @@ public void writeNewChildren(IMNode node) throws MetadataException, IOException } @Override - public void writeUpdatedChildren(IMNode node) throws MetadataException, IOException { + public void writeUpdatedChildren(ICachedMNode node) throws MetadataException, IOException { boolean removeOldSubEntry = false, insertNewSubEntry = false; int subIndex; long curSegAddr = getNodeAddress(node); long actualAddress; // actual segment to write record String alias, oldAlias; // key of the sub-index entry now - IMNode child, oldChild; + ICachedMNode child, oldChild; ISchemaPage curPage; ByteBuffer childBuffer; - for (Map.Entry entry : + for (Map.Entry entry : ICachedMNodeContainer.getCachedMNodeContainer(node).getUpdatedChildBuffer().entrySet()) { child = entry.getValue(); actualAddress = getTargetSegmentAddress(curSegAddr, entry.getKey()); @@ -266,7 +266,7 @@ public void writeUpdatedChildren(IMNode node) throws MetadataException, IOExcept } else { alias = null; } - if (node.isEntity()) { + if (node.isDevice()) { oldChild = curPage.getAsSegmentedPage().read(getSegIndex(actualAddress), entry.getKey()); oldAlias = oldChild.isMeasurement() ? oldChild.getAsMeasurementMNode().getAlias() : null; } else { @@ -312,7 +312,7 @@ public void writeUpdatedChildren(IMNode node) throws MetadataException, IOExcept multiPageUpdateOverflowOperation(curPage, entry.getKey(), childBuffer); subIndex = subIndexRootPage(curSegAddr); - if (node.isEntity() && subIndex < 0) { + if (node.isDevice() && subIndex < 0) { buildSubIndex(node); } else if (insertNewSubEntry || removeOldSubEntry) { if (removeOldSubEntry) { @@ -376,7 +376,7 @@ protected abstract void multiPageUpdateOverflowOperation( * * @param parNode node needs to build subordinate index. */ - protected abstract void buildSubIndex(IMNode parNode) throws MetadataException, IOException; + protected abstract void buildSubIndex(ICachedMNode parNode) throws MetadataException, IOException; /** * Insert an entry of subordinate index of the target node. @@ -571,9 +571,9 @@ private synchronized int loadFromFile(ByteBuffer dst, int pageIndex) throws IOEx return readChannel.read(dst, getPageAddress(pageIndex)); } - private void updateParentalRecord(IMNode parent, String key, long newSegAddr) + private void updateParentalRecord(ICachedMNode parent, String key, long newSegAddr) throws IOException, MetadataException { - if (parent == null || parent.getChild(key).isStorageGroup()) { + if (parent == null || parent.getChild(key).isDatabase()) { throw new MetadataException("Root page shall not be migrated."); } long parSegAddr = parent.getParent() == null ? 0L : getNodeAddress(parent); @@ -601,12 +601,12 @@ private static long getPageAddress(int pageIndex) { * @param node * @return */ - private static short estimateSegmentSize(IMNode node) { + private static short estimateSegmentSize(ICachedMNode node) { int childNum = node.getChildren().size(); if (childNum < SEG_SIZE_METRIC[0]) { // for record offset, length of string key int totalSize = SEG_HEADER_SIZE + 6 * childNum; - for (IMNode child : node.getChildren().values()) { + for (ICachedMNode child : node.getChildren().values()) { totalSize += child.getName().getBytes().length; if (child.isMeasurement()) { totalSize += @@ -640,7 +640,7 @@ private static short estimateSegmentSize(IMNode node) { * here. Supposed to merge with SchemaFile#reEstimateSegSize. * * @param expSize expected size calculated from next new record - * @param batchSize size of children within one {@linkplain #writeNewChildren(IMNode)} + * @param batchSize size of children within one {@linkplain #writeNewChildren(ICachedMNode)} * @return estimated size * @throws MetadataException */ diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/Traverser.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/Traverser.java index 485915d9880a0..9e588a1152fbc 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/Traverser.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/Traverser.java @@ -21,14 +21,15 @@ import org.apache.iotdb.commons.exception.IllegalPathException; import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.path.PartialPath; +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; +import org.apache.iotdb.commons.schema.node.utils.IMNodeIterator; import org.apache.iotdb.commons.schema.tree.AbstractTreeVisitor; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.iterator.IMNodeIterator; -import org.apache.iotdb.db.metadata.mnode.iterator.MNodeIterator; +import org.apache.iotdb.db.metadata.mnode.mem.iterator.MNodeIterator; +import org.apache.iotdb.db.metadata.mnode.utils.MNodeUtils; import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore; import org.apache.iotdb.db.metadata.mtree.store.ReentrantReadOnlyCachedMTreeStore; import org.apache.iotdb.db.metadata.template.Template; -import org.apache.iotdb.db.metadata.template.TemplateMNodeGenerator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -49,17 +50,18 @@ *
    • collector: to collect customized results of the matched node or measurement * */ -public abstract class Traverser extends AbstractTreeVisitor { +public abstract class Traverser> extends AbstractTreeVisitor { private static final Logger logger = LoggerFactory.getLogger(Traverser.class); - protected IMTreeStore store; + protected IMTreeStore store; - protected IMNode startNode; + protected N startNode; protected String[] nodes; // measurement in template should be processed only if templateMap is not null protected Map templateMap; + protected IMNodeFactory nodeFactory; // if true, the pre deleted measurement or pre deactivated template won't be processed protected boolean skipPreDeletedSchema = false; @@ -78,7 +80,7 @@ protected Traverser() {} * @param isPrefixMatch prefix match or not * @throws MetadataException path does not meet the expected rules */ - protected Traverser(IMNode startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) + protected Traverser(N startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) throws MetadataException { super(startNode, path, isPrefixMatch); this.store = store.getWithReentrantReadLock(); @@ -108,25 +110,26 @@ public void traverse() throws MetadataException { } @Override - protected IMNode getChild(IMNode parent, String childName) throws MetadataException { - IMNode child = null; + protected N getChild(N parent, String childName) throws MetadataException { + N child = null; if (parent.isAboveDatabase()) { child = parent.getChild(childName); } else { if (templateMap != null && !templateMap.isEmpty() // this task will cover some timeseries represented by template - && parent.getSchemaTemplateId() != NON_TEMPLATE // the device is using template + && (parent.isDevice() + && parent.getAsDeviceMNode().getSchemaTemplateId() + != NON_TEMPLATE) // the device is using template && !(skipPreDeletedSchema && parent - .getAsEntityMNode() + .getAsDeviceMNode() .isPreDeactivateTemplate())) { // the template should not skip - Template template = templateMap.get(parent.getSchemaTemplateId()); + int templateId = parent.getAsDeviceMNode().getSchemaTemplateId(); + Template template = templateMap.get(templateId); // if null, it means the template on this device is not covered in this query, refer to the // mpp analyzing stage - if (template != null) { - child = - TemplateMNodeGenerator.getChild( - templateMap.get(parent.getSchemaTemplateId()), childName); + if (template != null && nodeFactory != null) { + child = MNodeUtils.getChild(templateMap.get(templateId), childName, nodeFactory); } } } @@ -137,8 +140,8 @@ protected IMNode getChild(IMNode parent, String childName) throws MetadataExcept } @Override - protected void releaseNode(IMNode node) { - if (!node.isAboveDatabase() && !node.isStorageGroup()) { + protected void releaseNode(N node) { + if (!node.isAboveDatabase() && !node.isDatabase()) { // In any case we can call store#inpin directly because the unpin method will not do anything // if it is an IMNode in template or in memory mode. store.unPin(node); @@ -146,17 +149,17 @@ protected void releaseNode(IMNode node) { } @Override - protected Iterator getChildrenIterator(IMNode parent) throws MetadataException { + protected Iterator getChildrenIterator(N parent) throws MetadataException { if (parent.isAboveDatabase()) { - return new MNodeIterator(parent.getChildren().values().iterator()); + return new MNodeIterator<>(parent.getChildren().values().iterator()); } else { return store.getTraverserIterator(parent, templateMap, skipPreDeletedSchema); } } @Override - protected void releaseNodeIterator(Iterator nodeIterator) { - ((IMNodeIterator) nodeIterator).close(); + protected void releaseNodeIterator(Iterator nodeIterator) { + ((IMNodeIterator) nodeIterator).close(); } @Override @@ -168,8 +171,9 @@ public void close() { } } - public void setTemplateMap(Map templateMap) { + public void setTemplateMap(Map templateMap, IMNodeFactory nodeFactory) { this.templateMap = templateMap; + this.nodeFactory = nodeFactory; } public void setSkipPreDeletedSchema(boolean skipPreDeletedSchema) { diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/TraverserWithLimitOffsetWrapper.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/TraverserWithLimitOffsetWrapper.java index b6fb42b6eee78..c497885102ea7 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/TraverserWithLimitOffsetWrapper.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/TraverserWithLimitOffsetWrapper.java @@ -20,12 +20,12 @@ package org.apache.iotdb.db.metadata.mtree.traverser; import org.apache.iotdb.commons.exception.MetadataException; -import org.apache.iotdb.db.metadata.mnode.IMNode; +import org.apache.iotdb.commons.schema.node.IMNode; import java.util.NoSuchElementException; -public class TraverserWithLimitOffsetWrapper extends Traverser { - private final Traverser traverser; +public class TraverserWithLimitOffsetWrapper> extends Traverser { + private final Traverser traverser; private final long limit; private final long offset; private final boolean hasLimit; @@ -33,7 +33,7 @@ public class TraverserWithLimitOffsetWrapper extends Traverser { private int count = 0; int curOffset = 0; - public TraverserWithLimitOffsetWrapper(Traverser traverser, long limit, long offset) { + public TraverserWithLimitOffsetWrapper(Traverser traverser, long limit, long offset) { this.traverser = traverser; this.limit = limit; this.offset = offset; @@ -84,27 +84,27 @@ public Throwable getFailure() { } @Override - protected boolean shouldVisitSubtreeOfInternalMatchedNode(IMNode node) { + protected boolean shouldVisitSubtreeOfInternalMatchedNode(N node) { return false; } @Override - protected boolean shouldVisitSubtreeOfFullMatchedNode(IMNode node) { + protected boolean shouldVisitSubtreeOfFullMatchedNode(N node) { return false; } @Override - protected boolean acceptInternalMatchedNode(IMNode node) { + protected boolean acceptInternalMatchedNode(N node) { return false; } @Override - protected boolean acceptFullMatchedNode(IMNode node) { + protected boolean acceptFullMatchedNode(N node) { return false; } @Override - protected R generateResult(IMNode nextMatchedNode) { + protected R generateResult(N nextMatchedNode) { return null; } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/basic/DatabaseTraverser.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/basic/DatabaseTraverser.java index 6ff51cc7d0b8c..7e5f704d40e59 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/basic/DatabaseTraverser.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/basic/DatabaseTraverser.java @@ -20,11 +20,11 @@ import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.path.PartialPath; -import org.apache.iotdb.db.metadata.mnode.IMNode; +import org.apache.iotdb.commons.schema.node.IMNode; import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore; import org.apache.iotdb.db.metadata.mtree.traverser.Traverser; -public abstract class DatabaseTraverser extends Traverser { +public abstract class DatabaseTraverser> extends Traverser { protected boolean collectInternal = false; @@ -38,29 +38,29 @@ public abstract class DatabaseTraverser extends Traverser { * @throws MetadataException path does not meet the expected rules */ public DatabaseTraverser( - IMNode startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) + N startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) throws MetadataException { super(startNode, path, store, isPrefixMatch); } @Override - protected boolean acceptFullMatchedNode(IMNode node) { - return node.isStorageGroup(); + protected boolean acceptFullMatchedNode(N node) { + return node.isDatabase(); } @Override - protected boolean acceptInternalMatchedNode(IMNode node) { - return collectInternal && node.isStorageGroup(); + protected boolean acceptInternalMatchedNode(N node) { + return collectInternal && node.isDatabase(); } @Override - protected boolean shouldVisitSubtreeOfFullMatchedNode(IMNode node) { - return !node.isStorageGroup(); + protected boolean shouldVisitSubtreeOfFullMatchedNode(N node) { + return !node.isDatabase(); } @Override - protected boolean shouldVisitSubtreeOfInternalMatchedNode(IMNode node) { - return !node.isStorageGroup(); + protected boolean shouldVisitSubtreeOfInternalMatchedNode(N node) { + return !node.isDatabase(); } public void setCollectInternal(boolean collectInternal) { diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/basic/EntityTraverser.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/basic/EntityTraverser.java index feafa079830c6..6ead59dad4eee 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/basic/EntityTraverser.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/basic/EntityTraverser.java @@ -20,11 +20,11 @@ import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.path.PartialPath; -import org.apache.iotdb.db.metadata.mnode.IMNode; +import org.apache.iotdb.commons.schema.node.IMNode; import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore; import org.apache.iotdb.db.metadata.mtree.traverser.Traverser; -public abstract class EntityTraverser extends Traverser { +public abstract class EntityTraverser> extends Traverser { private boolean usingTemplate = false; private int schemaTemplateId = -1; @@ -38,32 +38,31 @@ public abstract class EntityTraverser extends Traverser { * @param isPrefixMatch prefix match or not * @throws MetadataException path does not meet the expected rules */ - public EntityTraverser( - IMNode startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) + public EntityTraverser(N startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) throws MetadataException { super(startNode, path, store, isPrefixMatch); } @Override - protected boolean acceptFullMatchedNode(IMNode node) { - if (node.isEntity()) { - return !usingTemplate || schemaTemplateId == node.getSchemaTemplateId(); + protected boolean acceptFullMatchedNode(N node) { + if (node.isDevice()) { + return !usingTemplate || schemaTemplateId == node.getAsDeviceMNode().getSchemaTemplateId(); } return false; } @Override - protected boolean acceptInternalMatchedNode(IMNode node) { + protected boolean acceptInternalMatchedNode(N node) { return false; } @Override - protected boolean shouldVisitSubtreeOfFullMatchedNode(IMNode node) { + protected boolean shouldVisitSubtreeOfFullMatchedNode(N node) { return !node.isMeasurement(); } @Override - protected boolean shouldVisitSubtreeOfInternalMatchedNode(IMNode node) { + protected boolean shouldVisitSubtreeOfInternalMatchedNode(N node) { return !node.isMeasurement(); } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/basic/MNodeTraverser.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/basic/MNodeTraverser.java index acf8fd8f429ee..e2bcf3156760d 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/basic/MNodeTraverser.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/basic/MNodeTraverser.java @@ -20,7 +20,7 @@ import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.path.PartialPath; -import org.apache.iotdb.db.metadata.mnode.IMNode; +import org.apache.iotdb.commons.schema.node.IMNode; import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore; import org.apache.iotdb.db.metadata.mtree.traverser.Traverser; @@ -30,11 +30,11 @@ * MNodeTraverser finds the node of the specified level on the path and process it. The same node * will not be processed more than once. If a level is not given, the current node is processed. */ -public abstract class MNodeTraverser extends Traverser { +public abstract class MNodeTraverser> extends Traverser { // Level query option started from 0. For example, level of root.sg.d1.s1 is 3. protected int targetLevel = -1; - protected IMNode lastVisitNode = null; + protected N lastVisitNode = null; /** * To traverse subtree under root.sg, e.g., init Traverser(root, "root.sg.**") @@ -45,14 +45,13 @@ public abstract class MNodeTraverser extends Traverser { * @param isPrefixMatch prefix match or not * @throws MetadataException path does not meet the expected rules */ - public MNodeTraverser( - IMNode startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) + public MNodeTraverser(N startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) throws MetadataException { super(startNode, path, store, isPrefixMatch); } @Override - protected boolean acceptFullMatchedNode(IMNode node) { + protected boolean acceptFullMatchedNode(N node) { if (targetLevel >= 0) { if (getSizeOfAncestor() > targetLevel) { return getAncestorNodeByLevel(targetLevel) != lastVisitNode; @@ -67,17 +66,17 @@ protected boolean acceptFullMatchedNode(IMNode node) { } @Override - protected boolean acceptInternalMatchedNode(IMNode node) { + protected boolean acceptInternalMatchedNode(N node) { return false; } @Override - protected boolean shouldVisitSubtreeOfFullMatchedNode(IMNode node) { + protected boolean shouldVisitSubtreeOfFullMatchedNode(N node) { return !node.isMeasurement(); } @Override - protected boolean shouldVisitSubtreeOfInternalMatchedNode(IMNode node) { + protected boolean shouldVisitSubtreeOfInternalMatchedNode(N node) { return !node.isMeasurement(); } @@ -86,7 +85,7 @@ public void setTargetLevel(int targetLevel) { } @Override - protected final R generateResult(IMNode nextMatchedNode) { + protected final R generateResult(N nextMatchedNode) { if (targetLevel >= 0) { if (getLevelOfNextMatchedNode() == targetLevel) { lastVisitNode = nextMatchedNode; @@ -99,5 +98,5 @@ protected final R generateResult(IMNode nextMatchedNode) { } } - protected abstract R transferToResult(IMNode node); + protected abstract R transferToResult(N node); } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/basic/MeasurementTraverser.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/basic/MeasurementTraverser.java index d559ff6d77ecf..6ced2e9cbba97 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/basic/MeasurementTraverser.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/basic/MeasurementTraverser.java @@ -20,11 +20,11 @@ import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.path.PartialPath; -import org.apache.iotdb.db.metadata.mnode.IMNode; +import org.apache.iotdb.commons.schema.node.IMNode; import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore; import org.apache.iotdb.db.metadata.mtree.traverser.Traverser; -public abstract class MeasurementTraverser extends Traverser { +public abstract class MeasurementTraverser> extends Traverser { /** * To traverse subtree under root.sg, e.g., init Traverser(root, "root.sg.**") @@ -36,28 +36,28 @@ public abstract class MeasurementTraverser extends Traverser { * @throws MetadataException path does not meet the expected rules */ public MeasurementTraverser( - IMNode startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) + N startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) throws MetadataException { super(startNode, path, store, isPrefixMatch); } @Override - protected boolean acceptFullMatchedNode(IMNode node) { + protected boolean acceptFullMatchedNode(N node) { return node.isMeasurement(); } @Override - protected boolean acceptInternalMatchedNode(IMNode node) { + protected boolean acceptInternalMatchedNode(N node) { return false; } @Override - protected boolean shouldVisitSubtreeOfFullMatchedNode(IMNode node) { + protected boolean shouldVisitSubtreeOfFullMatchedNode(N node) { return !node.isMeasurement(); } @Override - protected boolean shouldVisitSubtreeOfInternalMatchedNode(IMNode node) { + protected boolean shouldVisitSubtreeOfInternalMatchedNode(N node) { return !node.isMeasurement(); } } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/collector/DatabaseCollector.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/collector/DatabaseCollector.java index ea3762f1dcc75..1c71ae27f63b1 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/collector/DatabaseCollector.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/collector/DatabaseCollector.java @@ -20,25 +20,25 @@ import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.path.PartialPath; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IStorageGroupMNode; +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore; import org.apache.iotdb.db.metadata.mtree.traverser.basic.DatabaseTraverser; // This class implements database path collection function. -public abstract class DatabaseCollector extends DatabaseTraverser { +public abstract class DatabaseCollector> extends DatabaseTraverser { protected DatabaseCollector( - IMNode startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) + N startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) throws MetadataException { super(startNode, path, store, isPrefixMatch); } @Override - protected R generateResult(IMNode nextMatchedNode) { - collectDatabase(nextMatchedNode.getAsStorageGroupMNode()); + protected R generateResult(N nextMatchedNode) { + collectDatabase(nextMatchedNode.getAsDatabaseMNode()); return null; } // TODO: make collectDatabase return R - protected abstract void collectDatabase(IStorageGroupMNode node); + protected abstract void collectDatabase(IDatabaseMNode node); } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/collector/EntityCollector.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/collector/EntityCollector.java index 1756daf271de0..1c95ca05936a1 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/collector/EntityCollector.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/collector/EntityCollector.java @@ -20,25 +20,25 @@ import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.path.PartialPath; -import org.apache.iotdb.db.metadata.mnode.IEntityMNode; -import org.apache.iotdb.db.metadata.mnode.IMNode; +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore; import org.apache.iotdb.db.metadata.mtree.traverser.basic.EntityTraverser; // This class defines EntityMNode as target node and defines the Entity process framework. // TODO: set R is IDeviceSchemaInfo -public abstract class EntityCollector extends EntityTraverser { +public abstract class EntityCollector> extends EntityTraverser { protected EntityCollector( - IMNode startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) + N startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) throws MetadataException { super(startNode, path, store, isPrefixMatch); } @Override - protected R generateResult(IMNode nextMatchedNode) { - return collectEntity(nextMatchedNode.getAsEntityMNode()); + protected R generateResult(N nextMatchedNode) { + return collectEntity(nextMatchedNode.getAsDeviceMNode()); } - protected abstract R collectEntity(IEntityMNode node); + protected abstract R collectEntity(IDeviceMNode node); } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/collector/MNodeAboveDBCollector.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/collector/MNodeAboveDBCollector.java index 5a996ce8854e1..22b74a8ce5137 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/collector/MNodeAboveDBCollector.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/collector/MNodeAboveDBCollector.java @@ -20,25 +20,25 @@ import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.path.PartialPath; -import org.apache.iotdb.db.metadata.mnode.IMNode; +import org.apache.iotdb.commons.schema.node.IMNode; import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore; import java.util.HashSet; import java.util.Set; -public abstract class MNodeAboveDBCollector extends MNodeCollector { +public abstract class MNodeAboveDBCollector> extends MNodeCollector { protected Set involvedDatabaseMNodes = new HashSet<>(); protected MNodeAboveDBCollector( - IMNode startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) + N startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) throws MetadataException { super(startNode, path, store, isPrefixMatch); } @Override - protected boolean shouldVisitSubtreeOfFullMatchedNode(IMNode node) { - if (node.isStorageGroup()) { + protected boolean shouldVisitSubtreeOfFullMatchedNode(N node) { + if (node.isDatabase()) { involvedDatabaseMNodes.add(getParentPartialPath().concatNode(node.getName())); return false; } else { @@ -47,8 +47,8 @@ protected boolean shouldVisitSubtreeOfFullMatchedNode(IMNode node) { } @Override - protected boolean shouldVisitSubtreeOfInternalMatchedNode(IMNode node) { - if (node.isStorageGroup()) { + protected boolean shouldVisitSubtreeOfInternalMatchedNode(N node) { + if (node.isDatabase()) { involvedDatabaseMNodes.add(getParentPartialPath().concatNode(node.getName())); return false; } else { diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/collector/MNodeCollector.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/collector/MNodeCollector.java index 5104f778b74f3..fe7087e2a1037 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/collector/MNodeCollector.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/collector/MNodeCollector.java @@ -20,7 +20,7 @@ import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.path.PartialPath; -import org.apache.iotdb.db.metadata.mnode.IMNode; +import org.apache.iotdb.commons.schema.node.IMNode; import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore; import org.apache.iotdb.db.metadata.mtree.traverser.basic.MNodeTraverser; @@ -31,17 +31,17 @@ * will not be processed more than once. If a level is not given, the current node is processed. */ // TODO: set R to IMNodeInfo -public abstract class MNodeCollector extends MNodeTraverser { +public abstract class MNodeCollector> extends MNodeTraverser { protected MNodeCollector( - IMNode startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) + N startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) throws MetadataException { super(startNode, path, store, isPrefixMatch); } - protected final R transferToResult(IMNode node) { + protected final R transferToResult(N node) { return collectMNode(node); } - protected abstract R collectMNode(IMNode node); + protected abstract R collectMNode(N node); } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/collector/MeasurementCollector.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/collector/MeasurementCollector.java index ff488befbb559..2c42161abb05e 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/collector/MeasurementCollector.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/collector/MeasurementCollector.java @@ -21,23 +21,24 @@ import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.path.MeasurementPath; import org.apache.iotdb.commons.path.PartialPath; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore; import org.apache.iotdb.db.metadata.mtree.traverser.basic.MeasurementTraverser; // This class defines MeasurementMNode as target node and defines the measurement process framework. // TODO: set R is ITimeseriesInfo -public abstract class MeasurementCollector extends MeasurementTraverser { +public abstract class MeasurementCollector> + extends MeasurementTraverser { protected MeasurementCollector( - IMNode startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) + N startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) throws MetadataException { super(startNode, path, store, isPrefixMatch); } @Override - protected R generateResult(IMNode nextMatchedNode) { + protected R generateResult(N nextMatchedNode) { return collectMeasurement(nextMatchedNode.getAsMeasurementMNode()); } @@ -46,18 +47,19 @@ protected R generateResult(IMNode nextMatchedNode) { * * @param node MeasurementMNode holding the measurement schema */ - protected abstract R collectMeasurement(IMeasurementMNode node); + protected abstract R collectMeasurement(IMeasurementMNode node); /** * When traverse goes into a template, IMNode.getPartialPath may not work as nodes in template has * no parent on MTree. So this methods will construct a path from root to node in template using a * stack traverseContext. */ - protected MeasurementPath getCurrentMeasurementPathInTraverse(IMeasurementMNode currentNode) { - IMNode par = getParentOfNextMatchedNode(); + protected MeasurementPath getCurrentMeasurementPathInTraverse(IMeasurementMNode currentNode) { + N par = getParentOfNextMatchedNode(); MeasurementPath retPath = - new MeasurementPath(getPartialPathFromRootToNode(currentNode), currentNode.getSchema()); - retPath.setUnderAlignedEntity(par.getAsEntityMNode().isAligned()); + new MeasurementPath( + getPartialPathFromRootToNode(currentNode.getAsMNode()), currentNode.getSchema()); + retPath.setUnderAlignedEntity(par.getAsDeviceMNode().isAligned()); return retPath; } } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/counter/DatabaseCounter.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/counter/DatabaseCounter.java index 0e7b589317974..4daaabe76800b 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/counter/DatabaseCounter.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/counter/DatabaseCounter.java @@ -20,23 +20,23 @@ import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.path.PartialPath; -import org.apache.iotdb.db.metadata.mnode.IMNode; +import org.apache.iotdb.commons.schema.node.IMNode; import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore; import org.apache.iotdb.db.metadata.mtree.traverser.basic.DatabaseTraverser; // This class implement database counter. -public class DatabaseCounter extends DatabaseTraverser implements Counter { +public class DatabaseCounter> extends DatabaseTraverser + implements Counter { private int count; - public DatabaseCounter( - IMNode startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) + public DatabaseCounter(N startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) throws MetadataException { super(startNode, path, store, isPrefixMatch); } @Override - protected Void generateResult(IMNode nextMatchedNode) { + protected Void generateResult(N nextMatchedNode) { count++; return null; } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/counter/EntityCounter.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/counter/EntityCounter.java index 3feaa46b58139..5e5cd03f2c75a 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/counter/EntityCounter.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/counter/EntityCounter.java @@ -20,21 +20,22 @@ import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.path.PartialPath; -import org.apache.iotdb.db.metadata.mnode.IMNode; +import org.apache.iotdb.commons.schema.node.IMNode; import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore; import org.apache.iotdb.db.metadata.mtree.traverser.basic.EntityTraverser; // This class implement entity counter. -public class EntityCounter extends EntityTraverser implements Counter { +public class EntityCounter> extends EntityTraverser + implements Counter { private int count; - public EntityCounter(IMNode startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) + public EntityCounter(N startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) throws MetadataException { super(startNode, path, store, isPrefixMatch); } @Override - protected Void generateResult(IMNode nextMatchedNode) { + protected Void generateResult(N nextMatchedNode) { count++; return null; } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/counter/MeasurementCounter.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/counter/MeasurementCounter.java index 62d28646415f7..7e5387fbf194c 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/counter/MeasurementCounter.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/counter/MeasurementCounter.java @@ -20,22 +20,23 @@ import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.path.PartialPath; -import org.apache.iotdb.db.metadata.mnode.IMNode; +import org.apache.iotdb.commons.schema.node.IMNode; import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore; import org.apache.iotdb.db.metadata.mtree.traverser.basic.MeasurementTraverser; // This class implement measurement counter. -public class MeasurementCounter extends MeasurementTraverser implements Counter { +public class MeasurementCounter> extends MeasurementTraverser + implements Counter { private int count; public MeasurementCounter( - IMNode startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) + N startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) throws MetadataException { super(startNode, path, store, isPrefixMatch); } @Override - protected Void generateResult(IMNode nextMatchedNode) { + protected Void generateResult(N nextMatchedNode) { count++; return null; } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/updater/EntityUpdater.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/updater/EntityUpdater.java index 4fa58aff0484d..3fbee6e2c0438 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/updater/EntityUpdater.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/updater/EntityUpdater.java @@ -20,12 +20,13 @@ import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.path.PartialPath; -import org.apache.iotdb.db.metadata.mnode.IEntityMNode; -import org.apache.iotdb.db.metadata.mnode.IMNode; +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore; import org.apache.iotdb.db.metadata.mtree.traverser.basic.EntityTraverser; -public abstract class EntityUpdater extends EntityTraverser implements Updater { +public abstract class EntityUpdater> extends EntityTraverser + implements Updater { /** * To traverse subtree under root.sg, e.g., init Traverser(root, "root.sg.**") * @@ -35,15 +36,15 @@ public abstract class EntityUpdater extends EntityTraverser implements Upd * @param isPrefixMatch prefix match or not * @throws MetadataException path does not meet the expected rules */ - public EntityUpdater(IMNode startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) + public EntityUpdater(N startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) throws MetadataException { super(startNode, path, store, isPrefixMatch); } @Override - protected Void generateResult(IMNode nextMatchedNode) { + protected Void generateResult(N nextMatchedNode) { try { - updateEntity(nextMatchedNode.getAsEntityMNode()); + updateEntity(nextMatchedNode.getAsDeviceMNode()); } catch (MetadataException e) { setFailure(e); } @@ -61,5 +62,5 @@ public void update() throws MetadataException { } } - protected abstract void updateEntity(IEntityMNode node) throws MetadataException; + protected abstract void updateEntity(IDeviceMNode node) throws MetadataException; } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/updater/MeasurementUpdater.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/updater/MeasurementUpdater.java index c779663b8ecbb..b7a669bdf50b8 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/updater/MeasurementUpdater.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/updater/MeasurementUpdater.java @@ -20,12 +20,13 @@ import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.path.PartialPath; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore; import org.apache.iotdb.db.metadata.mtree.traverser.basic.MeasurementTraverser; -public abstract class MeasurementUpdater extends MeasurementTraverser implements Updater { +public abstract class MeasurementUpdater> extends MeasurementTraverser + implements Updater { /** * To traverse subtree under root.sg, e.g., init Traverser(root, "root.sg.**") * @@ -36,13 +37,13 @@ public abstract class MeasurementUpdater extends MeasurementTraverser impl * @throws MetadataException path does not meet the expected rules */ public MeasurementUpdater( - IMNode startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) + N startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch) throws MetadataException { super(startNode, path, store, isPrefixMatch); } @Override - protected Void generateResult(IMNode nextMatchedNode) { + protected Void generateResult(N nextMatchedNode) { try { updateMeasurement(nextMatchedNode.getAsMeasurementMNode()); } catch (MetadataException e) { @@ -62,5 +63,5 @@ public void update() throws MetadataException { } } - protected abstract void updateMeasurement(IMeasurementMNode node) throws MetadataException; + protected abstract void updateMeasurement(IMeasurementMNode node) throws MetadataException; } diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/plan/schemaregion/result/ShowNodesResult.java b/server/src/main/java/org/apache/iotdb/db/metadata/plan/schemaregion/result/ShowNodesResult.java index e07d5dcee6d05..e0e5edab4dab9 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/plan/schemaregion/result/ShowNodesResult.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/plan/schemaregion/result/ShowNodesResult.java @@ -19,7 +19,7 @@ package org.apache.iotdb.db.metadata.plan.schemaregion.result; -import org.apache.iotdb.db.metadata.mnode.MNodeType; +import org.apache.iotdb.commons.schema.node.MNodeType; import org.apache.iotdb.db.metadata.query.info.INodeSchemaInfo; import java.util.Objects; diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/query/info/INodeSchemaInfo.java b/server/src/main/java/org/apache/iotdb/db/metadata/query/info/INodeSchemaInfo.java index f7aa604009109..cdc449c6524d7 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/query/info/INodeSchemaInfo.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/query/info/INodeSchemaInfo.java @@ -19,7 +19,7 @@ package org.apache.iotdb.db.metadata.query.info; -import org.apache.iotdb.db.metadata.mnode.MNodeType; +import org.apache.iotdb.commons.schema.node.MNodeType; public interface INodeSchemaInfo extends ISchemaInfo { diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/SchemaRegionMemoryImpl.java b/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/SchemaRegionMemoryImpl.java index 59383c6f01360..5bec389692d34 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/SchemaRegionMemoryImpl.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/SchemaRegionMemoryImpl.java @@ -25,6 +25,8 @@ import org.apache.iotdb.commons.path.MeasurementPath; import org.apache.iotdb.commons.path.PartialPath; import org.apache.iotdb.commons.path.PathPatternTree; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; import org.apache.iotdb.commons.utils.FileUtils; import org.apache.iotdb.consensus.ConsensusFactory; import org.apache.iotdb.db.conf.IoTDBConfig; @@ -41,8 +43,7 @@ import org.apache.iotdb.db.metadata.logfile.SchemaLogWriter; import org.apache.iotdb.db.metadata.metric.ISchemaRegionMetric; import org.apache.iotdb.db.metadata.metric.SchemaRegionMemMetric; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; +import org.apache.iotdb.db.metadata.mnode.mem.IMemMNode; import org.apache.iotdb.db.metadata.mtree.MTreeBelowSGMemoryImpl; import org.apache.iotdb.db.metadata.plan.schemaregion.ISchemaRegionPlan; import org.apache.iotdb.db.metadata.plan.schemaregion.SchemaRegionPlanVisitor; @@ -530,7 +531,7 @@ public void createTimeseries(ICreateTimeSeriesPlan plan, long offset) throws Met } try { - IMeasurementMNode leafMNode; + IMeasurementMNode leafMNode; // using try-catch to restore seriesNumberMonitor's state while create failed try { @@ -616,7 +617,7 @@ public void createAlignedTimeSeries(ICreateAlignedTimeSeriesPlan plan) throws Me List encodings = plan.getEncodings(); List> tagsList = plan.getTagsList(); List> attributesList = plan.getAttributesList(); - List measurementMNodeList; + List> measurementMNodeList; // using try-catch to restore seriesNumberMonitor's state while create failed try { @@ -725,7 +726,7 @@ public long constructSchemaBlackList(PathPatternTree patternTree) throws Metadat } private void recoverPreDeleteTimeseries(PartialPath path) throws MetadataException { - IMeasurementMNode measurementMNode = mtree.getMeasurementMNode(path); + IMeasurementMNode measurementMNode = mtree.getMeasurementMNode(path); measurementMNode.setPreDeleted(true); } @@ -773,7 +774,7 @@ public void deleteTimeseriesInBlackList(PathPatternTree patternTree) throws Meta private void deleteSingleTimeseriesInBlackList(PartialPath path) throws MetadataException, IOException { - IMeasurementMNode measurementMNode = mtree.deleteTimeseries(path); + IMeasurementMNode measurementMNode = mtree.deleteTimeseries(path); removeFromTagInvertedIndex(measurementMNode); regionStatistics.deleteTimeseries(1L); @@ -783,14 +784,14 @@ private void deleteSingleTimeseriesInBlackList(PartialPath path) } private void recoverRollbackPreDeleteTimeseries(PartialPath path) throws MetadataException { - IMeasurementMNode measurementMNode = mtree.getMeasurementMNode(path); + IMeasurementMNode measurementMNode = mtree.getMeasurementMNode(path); measurementMNode.setPreDeleted(false); } /** @param path full path from root to leaf node */ private void deleteOneTimeseriesUpdateStatistics(PartialPath path) throws MetadataException, IOException { - IMeasurementMNode measurementMNode = mtree.deleteTimeseries(path); + IMeasurementMNode measurementMNode = mtree.deleteTimeseries(path); removeFromTagInvertedIndex(measurementMNode); regionStatistics.deleteTimeseries(1L); @@ -808,9 +809,9 @@ private void deleteOneTimeseriesUpdateStatistics(PartialPath path) * * @param path path */ - private IMNode getDeviceNodeWithAutoCreate(PartialPath path) + private IMemMNode getDeviceNodeWithAutoCreate(PartialPath path) throws IOException, MetadataException { - IMNode node = mtree.getDeviceNodeWithAutoCreating(path); + IMemMNode node = mtree.getDeviceNodeWithAutoCreating(path); writeToMLog(SchemaRegionWritePlanFactory.getAutoCreateDeviceMNodePlan(node.getPartialPath())); return node; } @@ -851,7 +852,7 @@ public List fetchSchema( * @param offset offset in the tag file */ private void changeOffset(PartialPath path, long offset) throws MetadataException { - IMeasurementMNode measurementMNode = mtree.getMeasurementMNode(path); + IMeasurementMNode measurementMNode = mtree.getMeasurementMNode(path); measurementMNode.setOffset(offset); if (isRecovering) { @@ -864,11 +865,12 @@ private void changeOffset(PartialPath path, long offset) throws MetadataExceptio } private void changeAlias(PartialPath path, String alias) throws MetadataException { - IMeasurementMNode leafMNode = mtree.getMeasurementMNode(path); + IMeasurementMNode leafMNode = mtree.getMeasurementMNode(path); + IDeviceMNode device = leafMNode.getParent().getAsDeviceMNode(); if (leafMNode.getAlias() != null) { - leafMNode.getParent().deleteAliasChild(leafMNode.getAlias()); + device.deleteAliasChild(leafMNode.getAlias()); } - leafMNode.getParent().addAlias(alias, leafMNode); + device.addAlias(alias, leafMNode); mtree.setAlias(leafMNode, alias); try { @@ -895,7 +897,7 @@ public void upsertAliasAndTagsAndAttributes( Map attributesMap, PartialPath fullPath) throws MetadataException, IOException { - IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath); + IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath); // upsert alias upsertAlias(alias, fullPath, leafMNode); @@ -919,16 +921,18 @@ public void upsertAliasAndTagsAndAttributes( tagManager.updateTagsAndAttributes(tagsMap, attributesMap, leafMNode); } - private void upsertAlias(String alias, PartialPath fullPath, IMeasurementMNode leafMNode) + private void upsertAlias( + String alias, PartialPath fullPath, IMeasurementMNode leafMNode) throws MetadataException, IOException { // upsert alias if (alias != null && !alias.equals(leafMNode.getAlias())) { - if (!leafMNode.getParent().addAlias(alias, leafMNode)) { + IDeviceMNode deviceMNode = leafMNode.getParent().getAsDeviceMNode(); + if (!deviceMNode.addAlias(alias, leafMNode)) { throw new MetadataException("The alias already exists."); } if (leafMNode.getAlias() != null) { - leafMNode.getParent().deleteAliasChild(leafMNode.getAlias()); + deviceMNode.deleteAliasChild(leafMNode.getAlias()); } mtree.setAlias(leafMNode, alias); @@ -947,7 +951,7 @@ private void upsertAlias(String alias, PartialPath fullPath, IMeasurementMNode l @Override public void addAttributes(Map attributesMap, PartialPath fullPath) throws MetadataException, IOException { - IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath); + IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath); // no tag or attribute, we need to add a new record in log if (leafMNode.getOffset() < 0) { @@ -970,7 +974,7 @@ public void addAttributes(Map attributesMap, PartialPath fullPat @Override public void addTags(Map tagsMap, PartialPath fullPath) throws MetadataException, IOException { - IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath); + IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath); // no tag or attribute, we need to add a new record in log if (leafMNode.getOffset() < 0) { long offset = tagManager.writeTagFile(tagsMap, Collections.emptyMap()); @@ -995,7 +999,7 @@ public void addTags(Map tagsMap, PartialPath fullPath) @SuppressWarnings("squid:S3776") // Suppress high Cognitive Complexity warning public void dropTagsOrAttributes(Set keySet, PartialPath fullPath) throws MetadataException, IOException { - IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath); + IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath); // no tag or attribute, just do nothing. if (leafMNode.getOffset() != -1) { tagManager.dropTagsOrAttributes(keySet, fullPath, leafMNode); @@ -1013,7 +1017,7 @@ public void dropTagsOrAttributes(Set keySet, PartialPath fullPath) @SuppressWarnings("squid:S3776") // Suppress high Cognitive Complexity warning public void setTagsOrAttributesValue(Map alterMap, PartialPath fullPath) throws MetadataException, IOException { - IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath); + IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath); if (leafMNode.getOffset() < 0) { throw new MetadataException( String.format("TimeSeries [%s] does not have any tag/attribute.", fullPath)); @@ -1036,7 +1040,7 @@ public void setTagsOrAttributesValue(Map alterMap, PartialPath f @SuppressWarnings("squid:S3776") // Suppress high Cognitive Complexity warning public void renameTagOrAttributeKey(String oldKey, String newKey, PartialPath fullPath) throws MetadataException, IOException { - IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath); + IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath); if (leafMNode.getOffset() < 0) { throw new MetadataException( String.format("TimeSeries [%s] does not have [%s] tag/attribute.", fullPath, oldKey), @@ -1048,7 +1052,7 @@ public void renameTagOrAttributeKey(String oldKey, String newKey, PartialPath fu /** remove the node from the tag inverted index */ @SuppressWarnings("squid:S3776") // Suppress high Cognitive Complexity warning - private void removeFromTagInvertedIndex(IMeasurementMNode node) throws IOException { + private void removeFromTagInvertedIndex(IMeasurementMNode node) throws IOException { tagManager.removeFromTagInvertedIndex(node); } // endregion diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/SchemaRegionSchemaFileImpl.java b/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/SchemaRegionSchemaFileImpl.java index dbf2e59185b9e..38319a833e36e 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/SchemaRegionSchemaFileImpl.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/SchemaRegionSchemaFileImpl.java @@ -24,6 +24,8 @@ import org.apache.iotdb.commons.path.MeasurementPath; import org.apache.iotdb.commons.path.PartialPath; import org.apache.iotdb.commons.path.PathPatternTree; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; import org.apache.iotdb.consensus.ConsensusFactory; import org.apache.iotdb.db.conf.IoTDBConfig; import org.apache.iotdb.db.conf.IoTDBDescriptor; @@ -43,8 +45,7 @@ import org.apache.iotdb.db.metadata.logfile.SchemaLogWriter; import org.apache.iotdb.db.metadata.metric.ISchemaRegionMetric; import org.apache.iotdb.db.metadata.metric.SchemaRegionCachedMetric; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; import org.apache.iotdb.db.metadata.mtree.MTreeBelowSGCachedImpl; import org.apache.iotdb.db.metadata.mtree.store.disk.cache.CacheMemoryManager; import org.apache.iotdb.db.metadata.plan.schemaregion.ISchemaRegionPlan; @@ -598,7 +599,7 @@ public void createTimeseries(ICreateTimeSeriesPlan plan, long offset) throws Met try { PartialPath path = plan.getPath(); - IMeasurementMNode leafMNode; + IMeasurementMNode leafMNode; // using try-catch to restore seriesNumberMonitor's state while create failed try { SchemaUtils.checkDataTypeWithEncoding(plan.getDataType(), plan.getEncoding()); @@ -631,11 +632,11 @@ public void createTimeseries(ICreateTimeSeriesPlan plan, long offset) throws Met // info // in tagFile to recover index directly tagManager.recoverIndex(offset, leafMNode); - mtree.pinMNode(leafMNode); + mtree.pinMNode(leafMNode.getAsMNode()); } else if (plan.getTags() != null) { // tag key, tag value tagManager.addIndex(plan.getTags(), leafMNode); - mtree.pinMNode(leafMNode); + mtree.pinMNode(leafMNode.getAsMNode()); } // write log @@ -650,11 +651,11 @@ public void createTimeseries(ICreateTimeSeriesPlan plan, long offset) throws Met } if (offset != -1) { leafMNode.setOffset(offset); - mtree.updateMNode(leafMNode); + mtree.updateMNode(leafMNode.getAsMNode()); } } finally { - mtree.unPinMNode(leafMNode); + mtree.unPinMNode(leafMNode.getAsMNode()); } } catch (IOException e) { @@ -725,7 +726,7 @@ public void createAlignedTimeSeries(ICreateAlignedTimeSeriesPlan plan) throws Me List encodings = plan.getEncodings(); List> tagsList = plan.getTagsList(); List> attributesList = plan.getAttributesList(); - List measurementMNodeList; + List> measurementMNodeList; // using try-catch to restore seriesNumberMonitor's state while create failed try { for (int i = 0; i < measurements.size(); i++) { @@ -758,13 +759,13 @@ public void createAlignedTimeSeries(ICreateAlignedTimeSeriesPlan plan) throws Me if (tagOffsets != null && !plan.getTagOffsets().isEmpty() && isRecovering) { if (tagOffsets.get(i) != -1) { tagManager.recoverIndex(plan.getTagOffsets().get(i), measurementMNodeList.get(i)); - mtree.pinMNode(measurementMNodeList.get(i)); + mtree.pinMNode(measurementMNodeList.get(i).getAsMNode()); } } else if (tagsList != null && !tagsList.isEmpty()) { if (tagsList.get(i) != null) { // tag key, tag value tagManager.addIndex(tagsList.get(i), measurementMNodeList.get(i)); - mtree.pinMNode(measurementMNodeList.get(i)); + mtree.pinMNode(measurementMNodeList.get(i).getAsMNode()); } } } @@ -797,12 +798,12 @@ public void createAlignedTimeSeries(ICreateAlignedTimeSeriesPlan plan) throws Me for (int i = 0; i < measurements.size(); i++) { if (tagOffsets.get(i) != -1) { measurementMNodeList.get(i).setOffset(tagOffsets.get(i)); - mtree.updateMNode(measurementMNodeList.get(i)); + mtree.updateMNode(measurementMNodeList.get(i).getAsMNode()); } } } finally { - for (IMeasurementMNode measurementMNode : measurementMNodeList) { - mtree.unPinMNode(measurementMNode); + for (IMeasurementMNode measurementMNode : measurementMNodeList) { + mtree.unPinMNode(measurementMNode.getAsMNode()); } } } catch (IOException e) { @@ -886,7 +887,7 @@ public void deleteTimeseriesInBlackList(PathPatternTree patternTree) throws Meta private void deleteSingleTimeseriesInBlackList(PartialPath path) throws MetadataException, IOException { - IMeasurementMNode measurementMNode = mtree.deleteTimeseries(path); + IMeasurementMNode measurementMNode = mtree.deleteTimeseries(path); removeFromTagInvertedIndex(measurementMNode); regionStatistics.deleteTimeseries(1L); @@ -898,7 +899,7 @@ private void deleteSingleTimeseriesInBlackList(PartialPath path) /** @param path full path from root to leaf node */ private void deleteOneTimeseriesUpdateStatistics(PartialPath path) throws MetadataException, IOException { - IMeasurementMNode measurementMNode = mtree.deleteTimeseries(path); + IMeasurementMNode measurementMNode = mtree.deleteTimeseries(path); removeFromTagInvertedIndex(measurementMNode); regionStatistics.deleteTimeseries(1L); @@ -916,15 +917,15 @@ private void deleteOneTimeseriesUpdateStatistics(PartialPath path) * * @param path path */ - private IMNode getDeviceNodeWithAutoCreate(PartialPath path) + private ICachedMNode getDeviceNodeWithAutoCreate(PartialPath path) throws IOException, MetadataException { - IMNode node = mtree.getDeviceNodeWithAutoCreating(path); + ICachedMNode node = mtree.getDeviceNodeWithAutoCreating(path); writeToMLog(SchemaRegionWritePlanFactory.getAutoCreateDeviceMNodePlan(node.getPartialPath())); return node; } private void autoCreateDeviceMNode(IAutoCreateDeviceMNodePlan plan) throws MetadataException { - IMNode node = mtree.getDeviceNodeWithAutoCreating(plan.getPath()); + ICachedMNode node = mtree.getDeviceNodeWithAutoCreating(plan.getPath()); mtree.unPinMNode(node); try { writeToMLog(plan); @@ -958,35 +959,36 @@ public List fetchSchema( * @param offset offset in the tag file */ private void changeOffset(PartialPath path, long offset) throws MetadataException { - IMeasurementMNode measurementMNode = mtree.getMeasurementMNode(path); + IMeasurementMNode measurementMNode = mtree.getMeasurementMNode(path); try { measurementMNode.setOffset(offset); - mtree.updateMNode(measurementMNode); + mtree.updateMNode(measurementMNode.getAsMNode()); if (isRecovering) { try { if (tagManager.recoverIndex(offset, measurementMNode)) { - mtree.pinMNode(measurementMNode); + mtree.pinMNode(measurementMNode.getAsMNode()); } } catch (IOException e) { throw new MetadataException(e); } } } finally { - mtree.unPinMNode(measurementMNode); + mtree.unPinMNode(measurementMNode.getAsMNode()); } } private void changeAlias(PartialPath path, String alias) throws MetadataException { - IMeasurementMNode leafMNode = mtree.getMeasurementMNode(path); + IMeasurementMNode leafMNode = mtree.getMeasurementMNode(path); try { + IDeviceMNode device = leafMNode.getParent().getAsDeviceMNode(); if (leafMNode.getAlias() != null) { - leafMNode.getParent().deleteAliasChild(leafMNode.getAlias()); + device.deleteAliasChild(leafMNode.getAlias()); } - leafMNode.getParent().addAlias(alias, leafMNode); + device.addAlias(alias, leafMNode); mtree.setAlias(leafMNode, alias); } finally { - mtree.unPinMNode(leafMNode); + mtree.unPinMNode(leafMNode.getAsMNode()); } try { @@ -1013,7 +1015,7 @@ public void upsertAliasAndTagsAndAttributes( Map attributesMap, PartialPath fullPath) throws MetadataException, IOException { - IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath); + IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath); try { // upsert alias upsertAlias(alias, fullPath, leafMNode); @@ -1027,31 +1029,33 @@ public void upsertAliasAndTagsAndAttributes( long offset = tagManager.writeTagFile(tagsMap, attributesMap); writeToMLog(SchemaRegionWritePlanFactory.getChangeTagOffsetPlan(fullPath, offset)); leafMNode.setOffset(offset); - mtree.updateMNode(leafMNode); + mtree.updateMNode(leafMNode.getAsMNode()); // update inverted Index map if (tagsMap != null && !tagsMap.isEmpty()) { tagManager.addIndex(tagsMap, leafMNode); - mtree.pinMNode(leafMNode); + mtree.pinMNode(leafMNode.getAsMNode()); } return; } tagManager.updateTagsAndAttributes(tagsMap, attributesMap, leafMNode); } finally { - mtree.unPinMNode(leafMNode); + mtree.unPinMNode(leafMNode.getAsMNode()); } } - private void upsertAlias(String alias, PartialPath fullPath, IMeasurementMNode leafMNode) + private void upsertAlias( + String alias, PartialPath fullPath, IMeasurementMNode leafMNode) throws MetadataException, IOException { // upsert alias if (alias != null && !alias.equals(leafMNode.getAlias())) { - if (!leafMNode.getParent().addAlias(alias, leafMNode)) { + IDeviceMNode device = leafMNode.getParent().getAsDeviceMNode(); + if (!device.addAlias(alias, leafMNode)) { throw new MetadataException("The alias already exists."); } if (leafMNode.getAlias() != null) { - leafMNode.getParent().deleteAliasChild(leafMNode.getAlias()); + device.deleteAliasChild(leafMNode.getAlias()); } mtree.setAlias(leafMNode, alias); @@ -1070,20 +1074,20 @@ private void upsertAlias(String alias, PartialPath fullPath, IMeasurementMNode l @Override public void addAttributes(Map attributesMap, PartialPath fullPath) throws MetadataException, IOException { - IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath); + IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath); try { // no tag or attribute, we need to add a new record in log if (leafMNode.getOffset() < 0) { long offset = tagManager.writeTagFile(Collections.emptyMap(), attributesMap); writeToMLog(SchemaRegionWritePlanFactory.getChangeTagOffsetPlan(fullPath, offset)); leafMNode.setOffset(offset); - mtree.updateMNode(leafMNode); + mtree.updateMNode(leafMNode.getAsMNode()); return; } tagManager.addAttributes(attributesMap, fullPath, leafMNode); } finally { - mtree.updateMNode(leafMNode); + mtree.updateMNode(leafMNode.getAsMNode()); } } @@ -1097,23 +1101,23 @@ public void addAttributes(Map attributesMap, PartialPath fullPat @Override public void addTags(Map tagsMap, PartialPath fullPath) throws MetadataException, IOException { - IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath); + IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath); try { // no tag or attribute, we need to add a new record in log if (leafMNode.getOffset() < 0) { long offset = tagManager.writeTagFile(tagsMap, Collections.emptyMap()); writeToMLog(SchemaRegionWritePlanFactory.getChangeTagOffsetPlan(fullPath, offset)); leafMNode.setOffset(offset); - mtree.updateMNode(leafMNode); + mtree.updateMNode(leafMNode.getAsMNode()); // update inverted Index map tagManager.addIndex(tagsMap, leafMNode); - mtree.pinMNode(leafMNode); + mtree.pinMNode(leafMNode.getAsMNode()); return; } tagManager.addTags(tagsMap, fullPath, leafMNode); } finally { - mtree.unPinMNode(leafMNode); + mtree.unPinMNode(leafMNode.getAsMNode()); } } @@ -1128,16 +1132,16 @@ public void addTags(Map tagsMap, PartialPath fullPath) @SuppressWarnings("squid:S3776") // Suppress high Cognitive Complexity warning public void dropTagsOrAttributes(Set keySet, PartialPath fullPath) throws MetadataException, IOException { - IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath); + IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath); try { // no tag or attribute, just do nothing. if (leafMNode.getOffset() != -1) { tagManager.dropTagsOrAttributes(keySet, fullPath, leafMNode); // when the measurementMNode was added to tagIndex, it was pinned - mtree.unPinMNode(leafMNode); + mtree.unPinMNode(leafMNode.getAsMNode()); } } finally { - mtree.unPinMNode(leafMNode); + mtree.unPinMNode(leafMNode.getAsMNode()); } } @@ -1152,7 +1156,7 @@ public void dropTagsOrAttributes(Set keySet, PartialPath fullPath) @SuppressWarnings("squid:S3776") // Suppress high Cognitive Complexity warning public void setTagsOrAttributesValue(Map alterMap, PartialPath fullPath) throws MetadataException, IOException { - IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath); + IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath); try { if (leafMNode.getOffset() < 0) { throw new MetadataException( @@ -1162,7 +1166,7 @@ public void setTagsOrAttributesValue(Map alterMap, PartialPath f // tags, attributes tagManager.setTagsOrAttributesValue(alterMap, fullPath, leafMNode); } finally { - mtree.unPinMNode(leafMNode); + mtree.unPinMNode(leafMNode.getAsMNode()); } } @@ -1179,7 +1183,7 @@ public void setTagsOrAttributesValue(Map alterMap, PartialPath f @SuppressWarnings("squid:S3776") // Suppress high Cognitive Complexity warning public void renameTagOrAttributeKey(String oldKey, String newKey, PartialPath fullPath) throws MetadataException, IOException { - IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath); + IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath); try { if (leafMNode.getOffset() < 0) { throw new MetadataException( @@ -1189,13 +1193,13 @@ public void renameTagOrAttributeKey(String oldKey, String newKey, PartialPath fu // tags, attributes tagManager.renameTagOrAttributeKey(oldKey, newKey, fullPath, leafMNode); } finally { - mtree.unPinMNode(leafMNode); + mtree.unPinMNode(leafMNode.getAsMNode()); } } /** remove the node from the tag inverted index */ @SuppressWarnings("squid:S3776") // Suppress high Cognitive Complexity warning - private void removeFromTagInvertedIndex(IMeasurementMNode node) throws IOException { + private void removeFromTagInvertedIndex(IMeasurementMNode node) throws IOException { tagManager.removeFromTagInvertedIndex(node); } // endregion @@ -1206,7 +1210,7 @@ public void activateSchemaTemplate(IActivateTemplateInClusterPlan plan, Template throws MetadataException { try { - IMNode deviceNode = getDeviceNodeWithAutoCreate(plan.getActivatePath()); + ICachedMNode deviceNode = getDeviceNodeWithAutoCreate(plan.getActivatePath()); try { mtree.activateTemplate(plan.getActivatePath(), template); writeToMLog(plan); diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/tag/TagManager.java b/server/src/main/java/org/apache/iotdb/db/metadata/tag/TagManager.java index 206e0bb80c343..d52e8ecc72d70 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/tag/TagManager.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/tag/TagManager.java @@ -21,11 +21,11 @@ import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.file.SystemFileFactory; import org.apache.iotdb.commons.path.PartialPath; +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; import org.apache.iotdb.db.conf.IoTDBConfig; import org.apache.iotdb.db.conf.IoTDBDescriptor; import org.apache.iotdb.db.metadata.MetadataConstant; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; import org.apache.iotdb.db.metadata.plan.schemaregion.read.IShowTimeSeriesPlan; import org.apache.iotdb.db.metadata.plan.schemaregion.result.ShowTimeSeriesResult; import org.apache.iotdb.db.metadata.query.info.ITimeSeriesSchemaInfo; @@ -68,7 +68,7 @@ public class TagManager { private String sgSchemaDirPath; private TagLogFile tagLogFile; // tag key -> tag value -> LeafMNode - private Map>> tagIndex = new ConcurrentHashMap<>(); + private Map>>> tagIndex = new ConcurrentHashMap<>(); public TagManager(String sgSchemaDirPath) throws IOException { this.sgSchemaDirPath = sgSchemaDirPath; @@ -125,7 +125,8 @@ public static TagManager loadFromSnapshot(File snapshotDir, String sgSchemaDirPa } } - public boolean recoverIndex(long offset, IMeasurementMNode measurementMNode) throws IOException { + public boolean recoverIndex(long offset, IMeasurementMNode measurementMNode) + throws IOException { Map tags = tagLogFile.readTag(config.getTagAttributeTotalSize(), offset); if (tags == null || tags.isEmpty()) { return false; @@ -135,7 +136,7 @@ public boolean recoverIndex(long offset, IMeasurementMNode measurementMNode) thr } } - public void addIndex(String tagKey, String tagValue, IMeasurementMNode measurementMNode) { + public void addIndex(String tagKey, String tagValue, IMeasurementMNode measurementMNode) { if (tagKey == null || tagValue == null || measurementMNode == null) { return; } @@ -145,7 +146,7 @@ public void addIndex(String tagKey, String tagValue, IMeasurementMNode measureme .add(measurementMNode); } - public void addIndex(Map tagsMap, IMeasurementMNode measurementMNode) { + public void addIndex(Map tagsMap, IMeasurementMNode measurementMNode) { if (tagsMap != null && measurementMNode != null) { for (Map.Entry entry : tagsMap.entrySet()) { addIndex(entry.getKey(), entry.getValue(), measurementMNode); @@ -153,25 +154,25 @@ public void addIndex(Map tagsMap, IMeasurementMNode measurementM } } - public void removeIndex(String tagKey, String tagValue, IMeasurementMNode measurementMNode) { + public void removeIndex(String tagKey, String tagValue, IMeasurementMNode measurementMNode) { tagIndex.get(tagKey).get(tagValue).remove(measurementMNode); if (tagIndex.get(tagKey).get(tagValue).isEmpty()) { tagIndex.get(tagKey).remove(tagValue); } } - private List getMatchedTimeseriesInIndex(IShowTimeSeriesPlan plan) { + private List> getMatchedTimeseriesInIndex(IShowTimeSeriesPlan plan) { if (!tagIndex.containsKey(plan.getKey())) { return Collections.emptyList(); } - Map> value2Node = tagIndex.get(plan.getKey()); + Map>> value2Node = tagIndex.get(plan.getKey()); if (value2Node.isEmpty()) { return Collections.emptyList(); } - List allMatchedNodes = new ArrayList<>(); + List> allMatchedNodes = new ArrayList<>(); if (plan.isContains()) { - for (Map.Entry> entry : value2Node.entrySet()) { + for (Map.Entry>> entry : value2Node.entrySet()) { if (entry.getKey() == null || entry.getValue() == null) { continue; } @@ -181,7 +182,7 @@ private List getMatchedTimeseriesInIndex(IShowTimeSeriesPlan } } } else { - for (Map.Entry> entry : value2Node.entrySet()) { + for (Map.Entry>> entry : value2Node.entrySet()) { if (entry.getKey() == null || entry.getValue() == null) { continue; } @@ -202,7 +203,7 @@ private List getMatchedTimeseriesInIndex(IShowTimeSeriesPlan public ISchemaReader getTimeSeriesReaderWithIndex( IShowTimeSeriesPlan plan) { - Iterator allMatchedNodes = getMatchedTimeseriesInIndex(plan).iterator(); + Iterator> allMatchedNodes = getMatchedTimeseriesInIndex(plan).iterator(); PartialPath pathPattern = plan.getPath(); int curOffset = 0; int count = 0; @@ -210,7 +211,7 @@ public ISchemaReader getTimeSeriesReaderWithIndex( long offset = plan.getOffset(); boolean hasLimit = limit > 0 || offset > 0; while (curOffset < offset && allMatchedNodes.hasNext()) { - IMeasurementMNode node = allMatchedNodes.next(); + IMeasurementMNode node = allMatchedNodes.next(); if (plan.isPrefixMatch() ? pathPattern.prefixMatchFullPath(node.getPartialPath()) : pathPattern.matchFullPath(node.getPartialPath())) { @@ -263,7 +264,7 @@ public ITimeSeriesSchemaInfo next() { private void getNext() throws IOException { nextMatched = null; while (allMatchedNodes.hasNext()) { - IMeasurementMNode node = allMatchedNodes.next(); + IMeasurementMNode node = allMatchedNodes.next(); if (plan.isPrefixMatch() ? pathPattern.prefixMatchFullPath(node.getPartialPath()) : pathPattern.matchFullPath(node.getPartialPath())) { @@ -276,7 +277,7 @@ private void getNext() throws IOException { (MeasurementSchema) node.getSchema(), tagAndAttributePair.left, tagAndAttributePair.right, - node.getParent().isAligned()); + node.getParent().getAsDeviceMNode().isAligned()); break; } } @@ -285,7 +286,7 @@ private void getNext() throws IOException { } /** remove the node from the tag inverted index */ - public void removeFromTagInvertedIndex(IMeasurementMNode node) throws IOException { + public void removeFromTagInvertedIndex(IMeasurementMNode node) throws IOException { if (node.getOffset() < 0) { return; } @@ -330,7 +331,9 @@ public void removeFromTagInvertedIndex(IMeasurementMNode node) throws IOExceptio * new value to update it. */ public void updateTagsAndAttributes( - Map tagsMap, Map attributesMap, IMeasurementMNode leafMNode) + Map tagsMap, + Map attributesMap, + IMeasurementMNode leafMNode) throws MetadataException, IOException { Pair, Map> pair = @@ -394,7 +397,7 @@ public void updateTagsAndAttributes( * @throws MetadataException tagLogFile write error or attributes already exist */ public void addAttributes( - Map attributesMap, PartialPath fullPath, IMeasurementMNode leafMNode) + Map attributesMap, PartialPath fullPath, IMeasurementMNode leafMNode) throws MetadataException, IOException { Pair, Map> pair = @@ -422,7 +425,7 @@ public void addAttributes( * @throws MetadataException tagLogFile write error or tag already exists */ public void addTags( - Map tagsMap, PartialPath fullPath, IMeasurementMNode leafMNode) + Map tagsMap, PartialPath fullPath, IMeasurementMNode leafMNode) throws MetadataException, IOException { Pair, Map> pair = @@ -452,7 +455,7 @@ public void addTags( * @param keySet tags key or attributes key */ public void dropTagsOrAttributes( - Set keySet, PartialPath fullPath, IMeasurementMNode leafMNode) + Set keySet, PartialPath fullPath, IMeasurementMNode leafMNode) throws MetadataException, IOException { Pair, Map> pair = tagLogFile.read(config.getTagAttributeTotalSize(), leafMNode.getOffset()); @@ -475,8 +478,8 @@ public void dropTagsOrAttributes( // persist the change to disk tagLogFile.write(pair.left, pair.right, leafMNode.getOffset()); - Map> tagVal2LeafMNodeSet; - Set MMNodes; + Map>> tagVal2LeafMNodeSet; + Set> MMNodes; for (Map.Entry entry : deleteTag.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); @@ -523,7 +526,7 @@ public void dropTagsOrAttributes( * @throws MetadataException tagLogFile write error or tags/attributes do not exist */ public void setTagsOrAttributesValue( - Map alterMap, PartialPath fullPath, IMeasurementMNode leafMNode) + Map alterMap, PartialPath fullPath, IMeasurementMNode leafMNode) throws MetadataException, IOException { // tags, attributes Pair, Map> pair = @@ -593,7 +596,7 @@ public void setTagsOrAttributesValue( * a tag/attribute named newKey */ public void renameTagOrAttributeKey( - String oldKey, String newKey, PartialPath fullPath, IMeasurementMNode leafMNode) + String oldKey, String newKey, PartialPath fullPath, IMeasurementMNode leafMNode) throws MetadataException, IOException { // tags, attributes Pair, Map> pair = @@ -669,7 +672,7 @@ public Pair, Map> readTagFile(long tagFileOf * @return the tag key-value map. * @throws RuntimeException If any IOException happens. */ - public Map readTags(IMeasurementMNode node) { + public Map readTags(IMeasurementMNode node) { try { return readTagFile(node.getOffset()).getLeft(); } catch (IOException e) { diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/template/TemplateMNodeGenerator.java b/server/src/main/java/org/apache/iotdb/db/metadata/template/TemplateMNodeGenerator.java deleted file mode 100644 index b22ed3ab3fcc6..0000000000000 --- a/server/src/main/java/org/apache/iotdb/db/metadata/template/TemplateMNodeGenerator.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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.metadata.template; - -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.MeasurementMNode; -import org.apache.iotdb.tsfile.write.schema.IMeasurementSchema; - -import java.util.Iterator; - -// TODO: it will be generic later -public class TemplateMNodeGenerator { - public static IMNode getChild(Template template, String name) { - IMeasurementSchema schema = template.getSchema(name); - return schema == null ? null : new MeasurementMNode(null, name, template.getSchema(name), null); - } - - public static Iterator getChildren(Template template) { - return new Iterator() { - private final Iterator schemas = - template.getSchemaMap().values().iterator(); - - @Override - public boolean hasNext() { - return schemas.hasNext(); - } - - @Override - public IMNode next() { - IMeasurementSchema schema = schemas.next(); - return new MeasurementMNode(null, schema.getMeasurementId(), schema, null); - } - }; - } -} diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/utils/MetaUtils.java b/server/src/main/java/org/apache/iotdb/db/metadata/utils/MetaUtils.java index cf0141cbefd0e..9a4bc694d5c59 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/utils/MetaUtils.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/utils/MetaUtils.java @@ -24,8 +24,8 @@ import org.apache.iotdb.commons.path.AlignedPath; import org.apache.iotdb.commons.path.MeasurementPath; import org.apache.iotdb.commons.path.PartialPath; +import org.apache.iotdb.commons.schema.node.IMNode; import org.apache.iotdb.commons.utils.TestOnly; -import org.apache.iotdb.db.metadata.mnode.IMNode; import org.apache.iotdb.db.mpp.plan.planner.plan.parameter.AggregationDescriptor; import org.apache.iotdb.db.mpp.plan.planner.plan.parameter.OrderByParameter; import org.apache.iotdb.db.mpp.plan.statement.component.Ordering; diff --git a/server/src/main/java/org/apache/iotdb/db/mpp/execution/operator/schema/NodeManageMemoryMergeOperator.java b/server/src/main/java/org/apache/iotdb/db/mpp/execution/operator/schema/NodeManageMemoryMergeOperator.java index d9315fcdbc4f5..7124b6d27c3b0 100644 --- a/server/src/main/java/org/apache/iotdb/db/mpp/execution/operator/schema/NodeManageMemoryMergeOperator.java +++ b/server/src/main/java/org/apache/iotdb/db/mpp/execution/operator/schema/NodeManageMemoryMergeOperator.java @@ -20,7 +20,7 @@ package org.apache.iotdb.db.mpp.execution.operator.schema; import org.apache.iotdb.common.rpc.thrift.TSchemaNode; -import org.apache.iotdb.db.metadata.mnode.MNodeType; +import org.apache.iotdb.commons.schema.node.MNodeType; import org.apache.iotdb.db.mpp.common.header.ColumnHeader; import org.apache.iotdb.db.mpp.common.header.ColumnHeaderConstant; import org.apache.iotdb.db.mpp.execution.operator.Operator; diff --git a/server/src/main/java/org/apache/iotdb/db/mpp/plan/execution/memory/StatementMemorySourceVisitor.java b/server/src/main/java/org/apache/iotdb/db/mpp/plan/execution/memory/StatementMemorySourceVisitor.java index d1f15a9534f13..40e29c8cb43da 100644 --- a/server/src/main/java/org/apache/iotdb/db/mpp/plan/execution/memory/StatementMemorySourceVisitor.java +++ b/server/src/main/java/org/apache/iotdb/db/mpp/plan/execution/memory/StatementMemorySourceVisitor.java @@ -23,8 +23,8 @@ import org.apache.iotdb.commons.conf.IoTDBConstant; import org.apache.iotdb.commons.exception.IllegalPathException; import org.apache.iotdb.commons.path.PartialPath; +import org.apache.iotdb.commons.schema.node.MNodeType; import org.apache.iotdb.commons.sync.pipesink.PipeSink; -import org.apache.iotdb.db.metadata.mnode.MNodeType; import org.apache.iotdb.db.mpp.common.header.ColumnHeader; import org.apache.iotdb.db.mpp.common.header.ColumnHeaderConstant; import org.apache.iotdb.db.mpp.common.header.DatasetHeader; diff --git a/server/src/test/java/org/apache/iotdb/db/metadata/MetaUtilsTest.java b/server/src/test/java/org/apache/iotdb/db/metadata/MetaUtilsTest.java index d2b720a91154d..67b700641e10f 100644 --- a/server/src/test/java/org/apache/iotdb/db/metadata/MetaUtilsTest.java +++ b/server/src/test/java/org/apache/iotdb/db/metadata/MetaUtilsTest.java @@ -22,7 +22,9 @@ import org.apache.iotdb.commons.path.AlignedPath; import org.apache.iotdb.commons.path.MeasurementPath; import org.apache.iotdb.commons.path.PartialPath; -import org.apache.iotdb.db.metadata.mnode.InternalMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; +import org.apache.iotdb.db.metadata.mnode.mem.IMemMNode; +import org.apache.iotdb.db.metadata.mnode.mem.factory.MemMNodeFactory; import org.apache.iotdb.db.metadata.utils.MetaUtils; import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; import org.apache.iotdb.tsfile.utils.Pair; @@ -41,24 +43,26 @@ public class MetaUtilsTest { + private final IMNodeFactory nodeFactory = MemMNodeFactory.getInstance(); + @Test public void testGetMultiFullPaths() { - InternalMNode rootNode = new InternalMNode(null, "root"); + IMemMNode rootNode = nodeFactory.createInternalMNode(null, "root"); // builds the relationship of root.a and root.aa - InternalMNode aNode = new InternalMNode(rootNode, "a"); + IMemMNode aNode = nodeFactory.createInternalMNode(rootNode, "a"); rootNode.addChild(aNode.getName(), aNode); - InternalMNode aaNode = new InternalMNode(rootNode, "aa"); + IMemMNode aaNode = nodeFactory.createInternalMNode(rootNode, "aa"); rootNode.addChild(aaNode.getName(), aaNode); // builds the relationship of root.a.b and root.aa.bb - InternalMNode bNode = new InternalMNode(aNode, "b"); + IMemMNode bNode = nodeFactory.createInternalMNode(aNode, "b"); aNode.addChild(bNode.getName(), bNode); - InternalMNode bbNode = new InternalMNode(aaNode, "bb"); + IMemMNode bbNode = nodeFactory.createInternalMNode(aaNode, "bb"); aaNode.addChild(bbNode.getName(), bbNode); // builds the relationship of root.aa.bb.cc - InternalMNode ccNode = new InternalMNode(bbNode, "cc"); + IMemMNode ccNode = nodeFactory.createInternalMNode(bbNode, "cc"); bbNode.addChild(ccNode.getName(), ccNode); List multiFullPaths = MetaUtils.getMultiFullPaths(rootNode); diff --git a/server/src/test/java/org/apache/iotdb/db/metadata/idtable/IDTableTest.java b/server/src/test/java/org/apache/iotdb/db/metadata/idtable/IDTableTest.java index 33ef4d106689c..a7dfe55c3d816 100644 --- a/server/src/test/java/org/apache/iotdb/db/metadata/idtable/IDTableTest.java +++ b/server/src/test/java/org/apache/iotdb/db/metadata/idtable/IDTableTest.java @@ -30,7 +30,7 @@ // import org.apache.iotdb.db.metadata.idtable.entry.DiskSchemaEntry; // import org.apache.iotdb.db.metadata.idtable.entry.IDeviceID; // import org.apache.iotdb.db.metadata.idtable.entry.SchemaEntry; -// import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; +// import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; // import org.apache.iotdb.db.qp.Planner; // import org.apache.iotdb.db.qp.physical.crud.InsertRowPlan; // import org.apache.iotdb.db.qp.physical.sys.CreateAlignedTimeSeriesPlan; diff --git a/server/src/test/java/org/apache/iotdb/db/metadata/mnode/MNodeTest.java b/server/src/test/java/org/apache/iotdb/db/metadata/mnode/MNodeTest.java index 4b33592632008..42fde861adfb8 100644 --- a/server/src/test/java/org/apache/iotdb/db/metadata/mnode/MNodeTest.java +++ b/server/src/test/java/org/apache/iotdb/db/metadata/mnode/MNodeTest.java @@ -18,45 +18,39 @@ */ package org.apache.iotdb.db.metadata.mnode; +import org.apache.iotdb.commons.schema.node.role.IDeviceMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; +import org.apache.iotdb.db.metadata.mnode.mem.IMemMNode; +import org.apache.iotdb.db.metadata.mnode.mem.factory.MemMNodeFactory; import org.apache.iotdb.db.metadata.utils.MetaUtils; -import com.google.common.util.concurrent.ThreadFactoryBuilder; -import org.junit.Before; import org.junit.Test; import java.util.List; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertSame; public class MNodeTest { - private static ExecutorService service; - @Before - public void setUp() throws Exception { - service = - Executors.newFixedThreadPool( - Runtime.getRuntime().availableProcessors(), - new ThreadFactoryBuilder().setDaemon(false).setNameFormat("replaceChild-%d").build()); - } + private final IMNodeFactory nodeFactory = MemMNodeFactory.getInstance(); @Test public void testReplaceChild() { - InternalMNode rootNode = new InternalMNode(null, "root"); + IMemMNode rootNode = nodeFactory.createInternalMNode(null, "root"); - IEntityMNode aNode = new EntityMNode(rootNode, "a"); - rootNode.addChild(aNode.getName(), aNode); + IDeviceMNode aNode = nodeFactory.createDeviceMNode(rootNode, "a"); + rootNode.addChild(aNode.getName(), aNode.getAsMNode()); - IMeasurementMNode bNode = MeasurementMNode.getMeasurementMNode(aNode, "b", null, null); + IMeasurementMNode bNode = nodeFactory.createMeasurementMNode(aNode, "b", null, null); - aNode.addChild(bNode.getName(), bNode); + aNode.addChild(bNode.getName(), bNode.getAsMNode()); aNode.addAlias("aliasOfb", bNode); - IEntityMNode newANode = new EntityMNode(null, "a"); - rootNode.replaceChild(aNode.getName(), newANode); + IDeviceMNode newANode = nodeFactory.createDeviceMNode(null, "a"); + rootNode.replaceChild(aNode.getName(), newANode.getAsMNode()); List multiFullPaths = MetaUtils.getMultiFullPaths(rootNode); assertEquals("root.a.b", multiFullPaths.get(0)); @@ -67,28 +61,28 @@ public void testReplaceChild() { @Test public void testAddChild() { - InternalMNode rootNode = new InternalMNode(null, "root"); + IMemMNode rootNode = nodeFactory.createInternalMNode(null, "root"); - IMNode speedNode = + IMemMNode speedNode = rootNode - .addChild(new InternalMNode(null, "sg1")) - .addChild(new InternalMNode(null, "a")) - .addChild(new InternalMNode(null, "b")) - .addChild(new InternalMNode(null, "c")) - .addChild(new InternalMNode(null, "d")) - .addChild(new InternalMNode(null, "device")) - .addChild(new InternalMNode(null, "speed")); + .addChild(nodeFactory.createInternalMNode(null, "sg1")) + .addChild(nodeFactory.createInternalMNode(null, "a")) + .addChild(nodeFactory.createInternalMNode(null, "b")) + .addChild(nodeFactory.createInternalMNode(null, "c")) + .addChild(nodeFactory.createInternalMNode(null, "d")) + .addChild(nodeFactory.createInternalMNode(null, "device")) + .addChild(nodeFactory.createInternalMNode(null, "speed")); assertEquals("root.sg1.a.b.c.d.device.speed", speedNode.getFullPath()); - IMNode temperatureNode = + IMemMNode temperatureNode = rootNode .getChild("sg1") - .addChild(new InternalMNode(null, "aa")) - .addChild(new InternalMNode(null, "bb")) - .addChild(new InternalMNode(null, "cc")) - .addChild(new InternalMNode(null, "dd")) - .addChild(new InternalMNode(null, "device11")) - .addChild(new InternalMNode(null, "temperature")); + .addChild(nodeFactory.createInternalMNode(null, "aa")) + .addChild(nodeFactory.createInternalMNode(null, "bb")) + .addChild(nodeFactory.createInternalMNode(null, "cc")) + .addChild(nodeFactory.createInternalMNode(null, "dd")) + .addChild(nodeFactory.createInternalMNode(null, "device11")) + .addChild(nodeFactory.createInternalMNode(null, "temperature")); assertEquals("root.sg1.aa.bb.cc.dd.device11.temperature", temperatureNode.getFullPath()); } } diff --git a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/ConfigMTreeTest.java b/server/src/test/java/org/apache/iotdb/db/metadata/mtree/ConfigMTreeTest.java index 686025361bf6a..3a224321a0bbb 100644 --- a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/ConfigMTreeTest.java +++ b/server/src/test/java/org/apache/iotdb/db/metadata/mtree/ConfigMTreeTest.java @@ -21,9 +21,9 @@ import org.apache.iotdb.commons.exception.IllegalPathException; import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.path.PartialPath; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; import org.apache.iotdb.confignode.rpc.thrift.TDatabaseSchema; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IStorageGroupMNode; +import org.apache.iotdb.db.metadata.mnode.config.IConfigMNode; import org.apache.iotdb.db.utils.EnvironmentUtils; import org.apache.iotdb.tsfile.utils.Pair; @@ -254,11 +254,12 @@ public void testSerialization() throws Exception { }; for (int i = 0; i < pathList.length; i++) { root.setStorageGroup(pathList[i]); - IStorageGroupMNode storageGroupMNode = root.getDatabaseNodeByDatabasePath(pathList[i]); + IDatabaseMNode storageGroupMNode = + root.getDatabaseNodeByDatabasePath(pathList[i]); storageGroupMNode.setDataTTL(i); - storageGroupMNode.setDataReplicationFactor(i); - storageGroupMNode.setSchemaReplicationFactor(i); - storageGroupMNode.setTimePartitionInterval(i); + storageGroupMNode.getAsMNode().getDatabaseSchema().setDataReplicationFactor(i); + storageGroupMNode.getAsMNode().getDatabaseSchema().setSchemaReplicationFactor(i); + storageGroupMNode.getAsMNode().getDatabaseSchema().setTimePartitionInterval(i); root.getNodeWithAutoCreate(pathList[i].concatNode("a")).setSchemaTemplateId(i); } @@ -271,7 +272,7 @@ public void testSerialization() throws Exception { for (int i = 0; i < pathList.length; i++) { TDatabaseSchema storageGroupSchema = - newTree.getDatabaseNodeByDatabasePath(pathList[i]).getStorageGroupSchema(); + newTree.getDatabaseNodeByDatabasePath(pathList[i]).getAsMNode().getDatabaseSchema(); Assert.assertEquals(i, storageGroupSchema.getTTL()); Assert.assertEquals(i, storageGroupSchema.getSchemaReplicationFactor()); Assert.assertEquals(i, storageGroupSchema.getDataReplicationFactor()); @@ -298,7 +299,7 @@ public void testSetTemplate() throws MetadataException { fail(); } - IMNode node = root.getNodeWithAutoCreate(path); + IConfigMNode node = root.getNodeWithAutoCreate(path); node.setSchemaTemplateId(0); try { diff --git a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGTest.java b/server/src/test/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGTest.java deleted file mode 100644 index 8b22cf580ab04..0000000000000 --- a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGTest.java +++ /dev/null @@ -1,257 +0,0 @@ -/* - * 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.metadata.mtree; - -import org.apache.iotdb.commons.exception.MetadataException; -import org.apache.iotdb.commons.path.PartialPath; -import org.apache.iotdb.db.conf.IoTDBDescriptor; -import org.apache.iotdb.db.exception.metadata.AliasAlreadyExistException; -import org.apache.iotdb.db.exception.metadata.PathAlreadyExistException; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.MeasurementMNode; -import org.apache.iotdb.db.metadata.mtree.store.disk.cache.CacheMemoryManager; -import org.apache.iotdb.db.metadata.rescon.CachedSchemaEngineStatistics; -import org.apache.iotdb.db.metadata.rescon.CachedSchemaRegionStatistics; -import org.apache.iotdb.db.metadata.rescon.MemSchemaEngineStatistics; -import org.apache.iotdb.db.metadata.rescon.MemSchemaRegionStatistics; -import org.apache.iotdb.db.metadata.schemaregion.SchemaEngineMode; -import org.apache.iotdb.db.utils.EnvironmentUtils; -import org.apache.iotdb.tsfile.common.conf.TSFileDescriptor; -import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; -import org.apache.iotdb.tsfile.file.metadata.enums.TSEncoding; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import java.io.IOException; -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -public abstract class MTreeBelowSGTest { - - ConfigMTree root; - IMTreeBelowSG storageGroup; - - Set usedMTree = new HashSet<>(); - - protected abstract void setConfig(); - - protected abstract void rollBackConfig(); - - @Before - public void setUp() throws Exception { - setConfig(); - EnvironmentUtils.envSetUp(); - root = new ConfigMTree(); - } - - @After - public void tearDown() throws Exception { - CacheMemoryManager.getInstance().clear(); - root.clear(); - root = null; - for (IMTreeBelowSG mtree : usedMTree) { - mtree.clear(); - } - usedMTree.clear(); - storageGroup = null; - EnvironmentUtils.cleanEnv(); - rollBackConfig(); - } - - private IMTreeBelowSG getStorageGroup(PartialPath path) throws MetadataException { - try { - root.setStorageGroup(path); - IMTreeBelowSG mtree; - if (SchemaEngineMode.valueOf(IoTDBDescriptor.getInstance().getConfig().getSchemaEngineMode()) - .equals(SchemaEngineMode.Schema_File)) { - mtree = - new MTreeBelowSGCachedImpl( - path, - null, - () -> { - // do nothing - }, - node -> { - // do nothing - }, - 0, - new CachedSchemaRegionStatistics(0, new CachedSchemaEngineStatistics())); - } else { - mtree = - new MTreeBelowSGMemoryImpl( - path, null, new MemSchemaRegionStatistics(0, new MemSchemaEngineStatistics())); - } - usedMTree.add(mtree); - return mtree; - } catch (IOException e) { - throw new MetadataException(e); - } - } - - @Test - public void testAddLeftNodePathWithAlias() throws MetadataException { - storageGroup = getStorageGroup(new PartialPath("root.laptop")); - try { - storageGroup.createTimeseries( - new PartialPath("root.laptop.d1.s1"), - TSDataType.INT32, - TSEncoding.RLE, - TSFileDescriptor.getInstance().getConfig().getCompressor(), - Collections.emptyMap(), - "status"); - } catch (MetadataException e) { - e.printStackTrace(); - fail(e.getMessage()); - } - try { - storageGroup.createTimeseries( - new PartialPath("root.laptop.d1.s2"), - TSDataType.INT32, - TSEncoding.RLE, - TSFileDescriptor.getInstance().getConfig().getCompressor(), - Collections.emptyMap(), - "status"); - } catch (MetadataException e) { - assertTrue(e instanceof AliasAlreadyExistException); - } - } - - @Test - public void testSetStorageGroup() throws MetadataException { - try { - storageGroup = getStorageGroup(new PartialPath("root.laptop.d1")); - assertTrue(root.isDatabaseAlreadySet(new PartialPath("root.laptop.d1"))); - - } catch (MetadataException e) { - e.printStackTrace(); - fail(e.getMessage()); - } - try { - root.setStorageGroup(new PartialPath("root.laptop")); - } catch (MetadataException e) { - Assert.assertEquals( - "some children of root.laptop have already been created as database", e.getMessage()); - } - - try { - storageGroup.createTimeseries( - new PartialPath("root.laptop.d1.s0"), - TSDataType.INT32, - TSEncoding.RLE, - TSFileDescriptor.getInstance().getConfig().getCompressor(), - Collections.emptyMap(), - null); - storageGroup.createTimeseries( - new PartialPath("root.laptop.d1.s1"), - TSDataType.INT32, - TSEncoding.RLE, - TSFileDescriptor.getInstance().getConfig().getCompressor(), - Collections.emptyMap(), - null); - } catch (MetadataException e) { - e.printStackTrace(); - fail(e.getMessage()); - } - try { - storageGroup.deleteTimeseries(new PartialPath("root.laptop.d1.s0")); - } catch (MetadataException e) { - e.printStackTrace(); - fail(e.getMessage()); - } - - try { - root.deleteDatabase(new PartialPath("root.laptop.d1")); - } catch (MetadataException e) { - e.printStackTrace(); - fail(e.getMessage()); - } - assertFalse(root.isDatabaseAlreadySet(new PartialPath("root.laptop.d1.s1"))); - assertFalse(root.isDatabaseAlreadySet(new PartialPath("root.laptop.d1"))); - assertFalse(root.isDatabaseAlreadySet(new PartialPath("root.laptop"))); - } - - @Test - public void testSearchStorageGroup() throws MetadataException { - String path1 = "root"; - String sgPath1 = "root.vehicle"; - storageGroup = getStorageGroup(new PartialPath(sgPath1)); - assertTrue(root.isDatabaseAlreadySet(new PartialPath(path1))); - try { - storageGroup.createTimeseries( - new PartialPath("root.vehicle.d1.s1"), - TSDataType.INT32, - TSEncoding.RLE, - TSFileDescriptor.getInstance().getConfig().getCompressor(), - Collections.emptyMap(), - null); - storageGroup.createTimeseries( - new PartialPath("root.vehicle.d1.s2"), - TSDataType.INT32, - TSEncoding.RLE, - TSFileDescriptor.getInstance().getConfig().getCompressor(), - Collections.emptyMap(), - null); - } catch (MetadataException e1) { - fail(e1.getMessage()); - } - - assertEquals( - root.getBelongedDatabases(new PartialPath("root.vehicle.d1.s1")), - Collections.singletonList(new PartialPath(sgPath1))); - } - - @Test - public void testCreateTimeseries() throws MetadataException { - String sgPath = "root.sg1"; - storageGroup = getStorageGroup(new PartialPath(sgPath)); - - storageGroup.createTimeseries( - new PartialPath("root.sg1.a.b.c"), - TSDataType.INT32, - TSEncoding.RLE, - TSFileDescriptor.getInstance().getConfig().getCompressor(), - Collections.emptyMap(), - null); - - try { - // mtree doesn't support nested timeseries which means MeasurementMNode is leaf of the tree. - storageGroup.createTimeseries( - new PartialPath("root.sg1.a.b"), - TSDataType.INT32, - TSEncoding.RLE, - TSFileDescriptor.getInstance().getConfig().getCompressor(), - Collections.emptyMap(), - null); - } catch (PathAlreadyExistException e) { - assertEquals("Path [root.sg1.a.b] already exist", e.getMessage()); - } - - IMNode node = storageGroup.getNodeByPath(new PartialPath("root.sg1.a.b")); - assertFalse(node instanceof MeasurementMNode); - } -} diff --git a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/disk/CachedMNodeContainerTest.java b/server/src/test/java/org/apache/iotdb/db/metadata/mtree/disk/CachedMNodeContainerTest.java index 02fa73da69b4b..9312c67d3a877 100644 --- a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/disk/CachedMNodeContainerTest.java +++ b/server/src/test/java/org/apache/iotdb/db/metadata/mtree/disk/CachedMNodeContainerTest.java @@ -18,9 +18,10 @@ */ package org.apache.iotdb.db.metadata.mtree.disk; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.InternalMNode; -import org.apache.iotdb.db.metadata.mtree.store.disk.CachedMNodeContainer; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.container.CachedMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.schemafile.factory.CacheMNodeFactory; import org.junit.Test; @@ -30,20 +31,22 @@ public class CachedMNodeContainerTest { + private final IMNodeFactory nodeFactory = CacheMNodeFactory.getInstance(); + @Test public void testIterator() { CachedMNodeContainer container = new CachedMNodeContainer(); - Map childCache = new HashMap<>(); - childCache.put("1", new InternalMNode(null, "1")); - childCache.put("2", new InternalMNode(null, "2")); - childCache.put("5", new InternalMNode(null, "5")); + Map childCache = new HashMap<>(); + childCache.put("1", nodeFactory.createInternalMNode(null, "1")); + childCache.put("2", nodeFactory.createInternalMNode(null, "2")); + childCache.put("5", nodeFactory.createInternalMNode(null, "5")); container.loadChildrenFromDisk(childCache); - container.put("3", new InternalMNode(null, "3")); - container.put("4", new InternalMNode(null, "4")); - container.put("6", new InternalMNode(null, "6")); + container.put("3", nodeFactory.createInternalMNode(null, "3")); + container.put("4", nodeFactory.createInternalMNode(null, "4")); + container.put("6", nodeFactory.createInternalMNode(null, "6")); container.updateMNode("5"); container.updateMNode("6"); - Iterator iterator = container.getChildrenIterator(); + Iterator iterator = container.getChildrenIterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } diff --git a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/schemafile/RecordUtilTests.java b/server/src/test/java/org/apache/iotdb/db/metadata/mtree/schemafile/RecordUtilTests.java index f0544b6796730..31d6bca72096b 100644 --- a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/schemafile/RecordUtilTests.java +++ b/server/src/test/java/org/apache/iotdb/db/metadata/mtree/schemafile/RecordUtilTests.java @@ -19,10 +19,10 @@ package org.apache.iotdb.db.metadata.mtree.schemafile; import org.apache.iotdb.commons.exception.MetadataException; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.InternalMNode; -import org.apache.iotdb.db.metadata.mnode.MeasurementMNode; -import org.apache.iotdb.db.metadata.mtree.store.disk.ICachedMNodeContainer; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.container.ICachedMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.schemafile.factory.CacheMNodeFactory; import org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.RecordUtils; import org.apache.iotdb.db.utils.EnvironmentUtils; import org.apache.iotdb.tsfile.file.metadata.enums.CompressionType; @@ -42,6 +42,8 @@ public class RecordUtilTests { + private final IMNodeFactory nodeFactory = CacheMNodeFactory.getInstance(); + @Before public void setUp() { EnvironmentUtils.envSetUp(); @@ -54,15 +56,22 @@ public void tearDown() throws Exception { @Test public void internalNodeTest() throws MetadataException { - IMNode oneNode = new InternalMNode(null, "abcd"); + ICachedMNode oneNode = nodeFactory.createInternalMNode(null, "abcd"); + ICachedMNode twoNode = nodeFactory.createDeviceMNode(null, "efgh").getAsMNode(); ICachedMNodeContainer.getCachedMNodeContainer(oneNode).setSegmentAddress(1234567L); - oneNode.setUseTemplate(true); + ICachedMNodeContainer.getCachedMNodeContainer(twoNode).setSegmentAddress(66666L); + twoNode.getAsDeviceMNode().setUseTemplate(true); ByteBuffer buffer = RecordUtils.node2Buffer(oneNode); buffer.clear(); - IMNode node2 = RecordUtils.buffer2Node("abcd", buffer); + ICachedMNode node1 = RecordUtils.buffer2Node("abcd", buffer); + Assert.assertEquals( + 1234567L, ICachedMNodeContainer.getCachedMNodeContainer(node1).getSegmentAddress()); + buffer = RecordUtils.node2Buffer(twoNode); + buffer.clear(); + node1 = RecordUtils.buffer2Node("efgh", buffer); Assert.assertEquals( - ICachedMNodeContainer.getCachedMNodeContainer(node2).getSegmentAddress(), 1234567L); - Assert.assertEquals(node2.isUseTemplate(), oneNode.isUseTemplate()); + 66666L, ICachedMNodeContainer.getCachedMNodeContainer(node1).getSegmentAddress()); + Assert.assertTrue(node1.getAsDeviceMNode().isUseTemplate()); } @Test @@ -74,7 +83,8 @@ public void measurementTest() throws MetadataException { IMeasurementSchema schema = new MeasurementSchema( "amn", TSDataType.FLOAT, TSEncoding.BITMAP, CompressionType.GZIP, props); - IMNode amn = MeasurementMNode.getMeasurementMNode(null, "amn", schema, "anothername"); + ICachedMNode amn = + nodeFactory.createMeasurementMNode(null, "amn", schema, "anothername").getAsMNode(); ByteBuffer tBuf = RecordUtils.node2Buffer(amn); tBuf.clear(); @@ -89,7 +99,7 @@ public void measurementTest() throws MetadataException { ByteBuffer buffer = RecordUtils.node2Buffer(amn); buffer.clear(); - IMNode node2 = RecordUtils.buffer2Node("amn", buffer); + ICachedMNode node2 = RecordUtils.buffer2Node("amn", buffer); Assert.assertTrue( amn.getAsMeasurementMNode().getSchema().equals(node2.getAsMeasurementMNode().getSchema())); diff --git a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/schemafile/SchemaFileLogTest.java b/server/src/test/java/org/apache/iotdb/db/metadata/mtree/schemafile/SchemaFileLogTest.java index b96a3120cee6a..777fdc9410bcf 100644 --- a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/schemafile/SchemaFileLogTest.java +++ b/server/src/test/java/org/apache/iotdb/db/metadata/mtree/schemafile/SchemaFileLogTest.java @@ -19,10 +19,11 @@ package org.apache.iotdb.db.metadata.mtree.schemafile; import org.apache.iotdb.commons.exception.MetadataException; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; import org.apache.iotdb.db.conf.IoTDBDescriptor; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IStorageGroupMNode; -import org.apache.iotdb.db.metadata.mnode.StorageGroupEntityMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.factory.CacheMNodeFactory; import org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.ISchemaPage; import org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.SchemaFile; import org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.SchemaFileConfig; @@ -50,6 +51,7 @@ public class SchemaFileLogTest { private static final int TEST_SCHEMA_REGION_ID = 0; + private final IMNodeFactory nodeFactory = CacheMNodeFactory.getInstance(); @Before public void setUp() { @@ -71,15 +73,16 @@ public void tearDown() throws Exception { public void essentialLogTest() throws IOException, MetadataException { SchemaFile sf = (SchemaFile) SchemaFile.initSchemaFile("root.test.vRoot1", TEST_SCHEMA_REGION_ID); - IStorageGroupMNode newSGNode = new StorageGroupEntityMNode(null, "newSG", 10000L); + IDatabaseMNode newSGNode = + nodeFactory.createDatabaseDeviceMNode(null, "newSG", 10000L).getAsDatabaseMNode(); sf.updateDatabaseNode(newSGNode); - IMNode root = virtualTriangleMTree(5, "root.test"); + ICachedMNode root = virtualTriangleMTree(5, "root.test"); - Iterator ite = getTreeBFT(root); - IMNode lastNode = null; + Iterator ite = getTreeBFT(root); + ICachedMNode lastNode = null; while (ite.hasNext()) { - IMNode curNode = ite.next(); + ICachedMNode curNode = ite.next(); if (!curNode.isMeasurement()) { sf.writeMNode(curNode); lastNode = curNode; @@ -94,7 +97,7 @@ public void essentialLogTest() throws IOException, MetadataException { ByteBuffer.allocate(SchemaFileConfig.PAGE_LENGTH), corruptPageIndex); // record number of children now - Iterator res = sf.getChildren(lastNode); + Iterator res = sf.getChildren(lastNode); int cnt = 0; while (res.hasNext()) { cnt++; diff --git a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/schemafile/SchemaFileTest.java b/server/src/test/java/org/apache/iotdb/db/metadata/mtree/schemafile/SchemaFileTest.java index d739b567de373..bcdd9d6ec0b11 100644 --- a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/schemafile/SchemaFileTest.java +++ b/server/src/test/java/org/apache/iotdb/db/metadata/mtree/schemafile/SchemaFileTest.java @@ -19,18 +19,16 @@ package org.apache.iotdb.db.metadata.mtree.schemafile; import org.apache.iotdb.commons.exception.MetadataException; +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode; +import org.apache.iotdb.commons.schema.node.role.IMeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; import org.apache.iotdb.commons.utils.PathUtils; import org.apache.iotdb.db.conf.IoTDBDescriptor; -import org.apache.iotdb.db.metadata.mnode.EntityMNode; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.IStorageGroupMNode; -import org.apache.iotdb.db.metadata.mnode.InternalMNode; -import org.apache.iotdb.db.metadata.mnode.MNodeUtils; -import org.apache.iotdb.db.metadata.mnode.MeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.StorageGroupEntityMNode; -import org.apache.iotdb.db.metadata.mnode.StorageGroupMNode; -import org.apache.iotdb.db.metadata.mtree.store.disk.ICachedMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.container.ICachedMNodeContainer; +import org.apache.iotdb.db.metadata.mnode.schemafile.factory.CacheMNodeFactory; +import org.apache.iotdb.db.metadata.mnode.utils.MNodeUtils; import org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.ISchemaFile; import org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.RecordUtils; import org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.SchemaFile; @@ -64,6 +62,7 @@ public class SchemaFileTest { private static final int TEST_SCHEMA_REGION_ID = 0; + private static final IMNodeFactory nodeFactory = CacheMNodeFactory.getInstance(); @Before public void setUp() { @@ -84,21 +83,22 @@ public void tearDown() throws Exception { @Test public void essentialTestSchemaFile() throws IOException, MetadataException { ISchemaFile sf = SchemaFile.initSchemaFile("root.test.vRoot1", TEST_SCHEMA_REGION_ID); - IStorageGroupMNode newSGNode = new StorageGroupEntityMNode(null, "newSG", 10000L); + IDatabaseMNode newSGNode = + nodeFactory.createDatabaseDeviceMNode(null, "newSG", 10000L).getAsDatabaseMNode(); sf.updateDatabaseNode(newSGNode); - IMNode root = virtualTriangleMTree(5, "root.test"); - IMNode int0 = root.getChild("int0"); - IMNode int1 = root.getChild("int0").getChild("int1"); - IMNode int4 = + ICachedMNode root = virtualTriangleMTree(5, "root.test"); + ICachedMNode int0 = root.getChild("int0"); + ICachedMNode int1 = root.getChild("int0").getChild("int1"); + ICachedMNode int4 = root.getChild("int0").getChild("int1").getChild("int2").getChild("int3").getChild("int4"); ICachedMNodeContainer.getCachedMNodeContainer(int0) .getNewChildBuffer() .put("mint1", getMeasurementNode(int0, "mint1", "alas")); - Iterator ite = getTreeBFT(root); + Iterator ite = getTreeBFT(root); while (ite.hasNext()) { - IMNode curNode = ite.next(); + ICachedMNode curNode = ite.next(); if (!curNode.isMeasurement()) { sf.writeMNode(curNode); } @@ -158,22 +158,23 @@ public void essentialTestSchemaFile() throws IOException, MetadataException { @Test public void testVerticalTree() throws MetadataException, IOException { ISchemaFile sf = SchemaFile.initSchemaFile("root.sgvt.vt", TEST_SCHEMA_REGION_ID); - IStorageGroupMNode sgNode = new StorageGroupEntityMNode(null, "sg", 11_111L); + IDatabaseMNode sgNode = + nodeFactory.createDatabaseDeviceMNode(null, "sg", 11_111L).getAsDatabaseMNode(); sf.updateDatabaseNode(sgNode); - IMNode root = getVerticalTree(100, "VT"); - Iterator ite = getTreeBFT(root); + ICachedMNode root = getVerticalTree(100, "VT"); + Iterator ite = getTreeBFT(root); while (ite.hasNext()) { sf.writeMNode(ite.next()); } - IMNode vt1 = getNode(root, "root.VT_0.VT_1"); - IMNode vt4 = getNode(root, "root.VT_0.VT_1.VT_2.VT_3.VT_4"); + ICachedMNode vt1 = getNode(root, "root.VT_0.VT_1"); + ICachedMNode vt4 = getNode(root, "root.VT_0.VT_1.VT_2.VT_3.VT_4"); ICachedMNodeContainer.getCachedMNodeContainer(vt1).getNewChildBuffer().clear(); addMeasurementChild(vt1, "newM"); sf.writeMNode(vt1); - IMNode vt0 = getNode(root, "root.VT_0"); + ICachedMNode vt0 = getNode(root, "root.VT_0"); Assert.assertEquals( ICachedMNodeContainer.getCachedMNodeContainer(vt1).getSegmentAddress(), RecordUtils.getRecordSegAddr( @@ -203,8 +204,8 @@ public void testVerticalTree() throws MetadataException, IOException { nsf.close(); nsf = SchemaFile.loadSchemaFile("root.sgvt.vt", TEST_SCHEMA_REGION_ID); - Iterator vt1Children = nsf.getChildren(vt1); - Iterator vt4Children = nsf.getChildren(vt4); + Iterator vt1Children = nsf.getChildren(vt1); + Iterator vt4Children = nsf.getChildren(vt4); while (vt1Children.hasNext()) { newNodes.remove(vt1Children.next().getName()); @@ -227,7 +228,7 @@ public void testVerticalTree() throws MetadataException, IOException { nsf.writeMNode(vt1); nsf.writeMNode(vt4); - Assert.assertEquals(11111L, nsf.init().getAsStorageGroupMNode().getDataTTL()); + Assert.assertEquals(11111L, nsf.init().getAsDatabaseMNode().getDataTTL()); nsf.close(); } @@ -236,19 +237,19 @@ public void testVerticalTree() throws MetadataException, IOException { public void testFaltTree() throws MetadataException, IOException { ISchemaFile sf = SchemaFile.initSchemaFile("root.test.vRoot1", TEST_SCHEMA_REGION_ID); - Iterator ite = getTreeBFT(getFlatTree(50000, "aa")); + Iterator ite = getTreeBFT(getFlatTree(50000, "aa")); while (ite.hasNext()) { - IMNode cur = ite.next(); + ICachedMNode cur = ite.next(); if (!cur.isMeasurement()) { sf.writeMNode(cur); } } - IMNode node = new InternalMNode(null, "a"); + ICachedMNode node = nodeFactory.createInternalMNode(null, "a"); ICachedMNodeContainer.getCachedMNodeContainer(node).setSegmentAddress(0L); List tryReadList = Arrays.asList(199, 1999, 2999, 3999, 4999, 5999); for (Integer rid : tryReadList) { - IMNode target = sf.getChildNode(node, "aa" + rid); + ICachedMNode target = sf.getChildNode(node, "aa" + rid); Assert.assertEquals("aa" + rid + "als", target.getAsMeasurementMNode().getAlias()); } sf.close(); @@ -258,12 +259,12 @@ public void testFaltTree() throws MetadataException, IOException { public void testGetChildren() throws MetadataException, IOException { essentialTestSchemaFile(); - IMNode node = new InternalMNode(null, "test"); + ICachedMNode node = nodeFactory.createInternalMNode(null, "test"); ICachedMNodeContainer.getCachedMNodeContainer(node) .setSegmentAddress(SchemaFile.getGlobalIndex(2, (short) 0)); ISchemaFile sf = SchemaFile.loadSchemaFile("root.test.vRoot1", TEST_SCHEMA_REGION_ID); - Iterator res = sf.getChildren(node); + Iterator res = sf.getChildren(node); int cnt = 0; while (res.hasNext()) { res.next(); @@ -276,19 +277,19 @@ public void testGetChildren() throws MetadataException, IOException { @Test public void test200KMeasurement() throws MetadataException, IOException { int i = 200000, j = 20; - IMNode sgNode = new StorageGroupMNode(null, "sgRoot", 11111111L); + ICachedMNode sgNode = nodeFactory.createDatabaseMNode(null, "sgRoot", 11111111L).getAsMNode(); ISchemaFile sf = SchemaFile.initSchemaFile(sgNode.getName(), TEST_SCHEMA_REGION_ID); while (j >= 0) { - IMNode aDevice = new EntityMNode(sgNode, "dev_" + j); + ICachedMNode aDevice = nodeFactory.createDeviceMNode(sgNode, "dev_" + j).getAsMNode(); sgNode.addChild(aDevice); j--; } sf.writeMNode(sgNode); - IMNode meas; - IMNode dev = sgNode.getChildren().get("dev_2"); + ICachedMNode meas; + ICachedMNode dev = sgNode.getChildren().get("dev_2"); while (i >= 0) { meas = getMeasurementNode(dev, "m_" + i, "ma_" + i); dev.addChild(meas); @@ -309,19 +310,19 @@ public void test200KMeasurement() throws MetadataException, IOException { @Test public void test10KDevices() throws MetadataException, IOException { int i = 1000; - IMNode sgNode = new StorageGroupMNode(null, "sgRoot", 11111111L); + ICachedMNode sgNode = nodeFactory.createDatabaseMNode(null, "sgRoot", 11111111L).getAsMNode(); // write with empty entitiy while (i >= 0) { - IMNode aDevice = new InternalMNode(sgNode, "dev_" + i); + ICachedMNode aDevice = nodeFactory.createInternalMNode(sgNode, "dev_" + i); sgNode.addChild(aDevice); i--; } - Iterator orderedTree = getTreeBFT(sgNode); + Iterator orderedTree = getTreeBFT(sgNode); ISchemaFile sf = SchemaFile.initSchemaFile(sgNode.getName(), TEST_SCHEMA_REGION_ID); ICachedMNodeContainer.getCachedMNodeContainer(sgNode).setSegmentAddress(0L); - IMNode node = null; + ICachedMNode node = null; try { while (orderedTree.hasNext()) { node = orderedTree.next(); @@ -334,7 +335,7 @@ public void test10KDevices() throws MetadataException, IOException { i = 1000; while (i >= 0) { long addr = getSegAddrInContainer(sgNode.getChild("dev_" + i)); - IMNode aDevice = new EntityMNode(sgNode, "dev_" + i); + ICachedMNode aDevice = nodeFactory.createDeviceMNode(sgNode, "dev_" + i).getAsMNode(); sgNode.deleteChild(aDevice.getName()); sgNode.addChild(aDevice); moveToUpdateBuffer(sgNode, "dev_" + i); @@ -357,7 +358,7 @@ public void test10KDevices() throws MetadataException, IOException { } // write with few measurement - for (IMNode etn : sgNode.getChildren().values()) { + for (ICachedMNode etn : sgNode.getChildren().values()) { int j = 10; while (j >= 0) { addMeasurementChild(etn, String.format("mtc_%d_%d", i, j)); @@ -370,7 +371,7 @@ public void test10KDevices() throws MetadataException, IOException { try { while (orderedTree.hasNext()) { node = orderedTree.next(); - if (!node.isMeasurement() && !node.isStorageGroup()) { + if (!node.isMeasurement() && !node.isDatabase()) { sf.writeMNode(node); ICachedMNodeContainer.getCachedMNodeContainer(node).getNewChildBuffer().clear(); } @@ -384,7 +385,7 @@ public void test10KDevices() throws MetadataException, IOException { Set resName = new HashSet<>(); // more measurement - for (IMNode etn : sgNode.getChildren().values()) { + for (ICachedMNode etn : sgNode.getChildren().values()) { int j = 1000; while (j >= 0) { addMeasurementChild(etn, String.format("mtc2_%d_%d", i, j)); @@ -397,11 +398,11 @@ public void test10KDevices() throws MetadataException, IOException { orderedTree = getTreeBFT(sgNode); sf = SchemaFile.loadSchemaFile(sgNode.getName(), TEST_SCHEMA_REGION_ID); - List arbitraryNode = new ArrayList<>(); + List arbitraryNode = new ArrayList<>(); try { while (orderedTree.hasNext()) { node = orderedTree.next(); - if (!node.isMeasurement() && !node.isStorageGroup()) { + if (!node.isMeasurement() && !node.isDatabase()) { sf.writeMNode(node); if (arbitraryNode.size() < 50) { arbitraryNode.add(node); @@ -418,12 +419,12 @@ public void test10KDevices() throws MetadataException, IOException { sf = SchemaFile.loadSchemaFile("sgRoot", TEST_SCHEMA_REGION_ID); for (String key : resName) { - IMNode resNode = sf.getChildNode(arbitraryNode.get(arbitraryNode.size() - 3), key); + ICachedMNode resNode = sf.getChildNode(arbitraryNode.get(arbitraryNode.size() - 3), key); Assert.assertTrue( resNode.getAsMeasurementMNode().getAlias().equals(resNode.getName() + "alias")); } - Iterator res = sf.getChildren(arbitraryNode.get(arbitraryNode.size() - 1)); + Iterator res = sf.getChildren(arbitraryNode.get(arbitraryNode.size() - 1)); int i2 = 0; while (res.hasNext()) { resName.remove(res.next().getName()); @@ -436,10 +437,10 @@ public void test10KDevices() throws MetadataException, IOException { @Test public void testUpdateOnFullPageSegment() throws MetadataException, IOException { ISchemaFile sf = SchemaFile.initSchemaFile("root.sg", TEST_SCHEMA_REGION_ID); - IMNode root = getFlatTree(783, "aa"); - Iterator ite = getTreeBFT(root); + ICachedMNode root = getFlatTree(783, "aa"); + Iterator ite = getTreeBFT(root); while (ite.hasNext()) { - IMNode cur = ite.next(); + ICachedMNode cur = ite.next(); if (!cur.isMeasurement()) { sf.writeMNode(cur); } @@ -458,9 +459,9 @@ public void testUpdateOnFullPageSegment() throws MetadataException, IOException root.getChildren().clear(); - root.addChild(new EntityMNode(root, "ent1")); + root.addChild(nodeFactory.createDeviceMNode(root, "ent1").getAsMNode()); - IMNode ent1 = root.getChild("ent1"); + ICachedMNode ent1 = root.getChild("ent1"); ent1.addChild(getMeasurementNode(ent1, "m1", "m1a")); sf.writeMNode(root); @@ -511,8 +512,8 @@ public void testEstimateSegSize() throws Exception { * related methods shall be merged further: {@linkplain SchemaFile#reEstimateSegSize} * ,{@linkplain PageManager#reEstimateSegSize} */ - IMNode sgNode = new StorageGroupMNode(null, "mma", 111111111L); - IMNode d1 = fillChildren(sgNode, 300, "d", this::supplyEntity); + ICachedMNode sgNode = nodeFactory.createDatabaseMNode(null, "mma", 111111111L).getAsMNode(); + ICachedMNode d1 = fillChildren(sgNode, 300, "d", this::supplyEntity); ISchemaFile sf = SchemaFile.initSchemaFile("root.sg", TEST_SCHEMA_REGION_ID); try { sf.writeMNode(sgNode); @@ -531,7 +532,7 @@ public void testEstimateSegSize() throws Exception { fillChildren(d1, 20, "ss", this::supplyMeasurement); sf.writeMNode(d1); - Iterator verifyChildren = sf.getChildren(d1); + Iterator verifyChildren = sf.getChildren(d1); int cnt = 0; while (verifyChildren.hasNext()) { cnt++; @@ -546,21 +547,21 @@ public void testEstimateSegSize() throws Exception { @Test public void test200KAlias() throws Exception { ISchemaFile sf = SchemaFile.initSchemaFile("root.sg", TEST_SCHEMA_REGION_ID); - IMNode sgNode = new StorageGroupMNode(null, "mma", 111111111L); + ICachedMNode sgNode = nodeFactory.createDatabaseMNode(null, "mma", 111111111L).getAsMNode(); // 5 devices, each for 200k measurements int factor20K = 20000; - List devs = new ArrayList<>(); + List devs = new ArrayList<>(); List senList = new ArrayList<>(); Map aliasAns = new HashMap<>(); try { for (int i = 0; i < 5; i++) { - devs.add(new EntityMNode(sgNode, "d_" + i)); + devs.add(nodeFactory.createDeviceMNode(sgNode, "d_" + i).getAsMNode()); sgNode.addChild(devs.get(i)); } - for (IMNode dev : devs) { - List sens = new ArrayList<>(); + for (ICachedMNode dev : devs) { + List sens = new ArrayList<>(); for (int i = 0; i < factor20K; i++) { sens.add(getMeasurementNode(dev, "s_" + i, null)); dev.addChild(sens.get(i)); @@ -572,9 +573,9 @@ public void test200KAlias() throws Exception { senList.add(sens); } - Iterator ite = getTreeBFT(sgNode); + Iterator ite = getTreeBFT(sgNode); - IMNode curNode; + ICachedMNode curNode; while (ite.hasNext()) { curNode = ite.next(); if (!curNode.isMeasurement()) { @@ -588,8 +589,8 @@ public void test200KAlias() throws Exception { sf = SchemaFile.loadSchemaFile("root.sg", TEST_SCHEMA_REGION_ID); try { - IMNode dev2 = devs.get(2); - for (IMNode child : dev2.getChildren().values()) { + ICachedMNode dev2 = devs.get(2); + for (ICachedMNode child : dev2.getChildren().values()) { child.getAsMeasurementMNode().setAlias(aliasAns.get(child.getName())); } @@ -607,7 +608,7 @@ public void test200KAlias() throws Exception { Assert.assertEquals(entry.getKey(), sf.getChildNode(dev2, entry.getValue()).getName()); } - Iterator children = sf.getChildren(dev2); + Iterator children = sf.getChildren(dev2); int cnt = 0; while (children.hasNext()) { cnt++; @@ -623,12 +624,12 @@ public void test200KAlias() throws Exception { @Test public void testRearrangementWhenInsert() throws MetadataException, IOException { ISchemaFile sf = SchemaFile.initSchemaFile("root.sg", TEST_SCHEMA_REGION_ID); - IMNode root = new StorageGroupEntityMNode(null, "sgRoot", 0L); + ICachedMNode root = nodeFactory.createDatabaseDeviceMNode(null, "sgRoot", 0L); root.getChildren().clear(); - IMNode ent2 = new EntityMNode(root, "ent2"); - IMNode ent3 = new EntityMNode(root, "ent3"); - IMNode ent4 = new EntityMNode(root, "ent4"); + ICachedMNode ent2 = nodeFactory.createDeviceMNode(root, "ent2").getAsMNode(); + ICachedMNode ent3 = nodeFactory.createDeviceMNode(root, "ent3").getAsMNode(); + ICachedMNode ent4 = nodeFactory.createDeviceMNode(root, "ent4").getAsMNode(); root.addChild(ent2); root.addChild(ent3); root.addChild(ent4); @@ -680,7 +681,7 @@ public void testRearrangementWhenInsert() throws MetadataException, IOException getSegAddr(sf, getSegAddrInContainer(ent4), "e4m0")); root.getChildren().clear(); - IMNode ent5 = new EntityMNode(root, "ent5"); + ICachedMNode ent5 = nodeFactory.createDeviceMNode(root, "ent5").getAsMNode(); root.addChild(ent5); while (ent5.getChildren().size() < 19) { ent5.addChild( @@ -748,7 +749,7 @@ public void bitwiseTest() { public void basicTest() throws IOException, MetadataException { SchemaFileConfig.INTERNAL_SPLIT_VALVE = 16000; int i = 10000; - IMNode sgNode = new StorageGroupEntityMNode(null, "sgRoot", 11111111L); + ICachedMNode sgNode = nodeFactory.createDatabaseDeviceMNode(null, "sgRoot", 11111111L); Set checkSet = new HashSet<>(); // write with empty entitiy while (i >= 0) { @@ -758,17 +759,17 @@ public void basicTest() throws IOException, MetadataException { } else if (i < 100) { name = "0" + name; } - IMNode aMeas = getMeasurementNode(sgNode, "s_" + name, null); + ICachedMNode aMeas = getMeasurementNode(sgNode, "s_" + name, null); checkSet.add(aMeas.getName()); sgNode.addChild(aMeas); i--; } - Iterator orderedTree = getTreeBFT(sgNode); + Iterator orderedTree = getTreeBFT(sgNode); ISchemaFile sf = SchemaFile.initSchemaFile(sgNode.getName(), TEST_SCHEMA_REGION_ID); sf.writeMNode(sgNode); - Iterator res = sf.getChildren(sgNode); + Iterator res = sf.getChildren(sgNode); while (res.hasNext()) { checkSet.remove(res.next().getName()); } @@ -782,7 +783,7 @@ public void basicSplitTest() throws MetadataException, IOException { SchemaFileConfig.INTERNAL_SPLIT_VALVE = 16230; SchemaFileConfig.DETAIL_SKETCH = true; int i = 999; - IMNode sgNode = new StorageGroupEntityMNode(null, "sgRoot", 11111111L); + ICachedMNode sgNode = nodeFactory.createDatabaseDeviceMNode(null, "sgRoot", 11111111L); Set checkSet = new HashSet<>(); // write with empty entitiy while (i >= 0) { @@ -792,7 +793,7 @@ public void basicSplitTest() throws MetadataException, IOException { } else if (i < 100) { name = "0" + name; } - IMNode aMeas = getMeasurementNode(sgNode, "s_" + name, null); + ICachedMNode aMeas = getMeasurementNode(sgNode, "s_" + name, null); checkSet.add(aMeas.getName()); sgNode.addChild(aMeas); i--; @@ -801,7 +802,7 @@ public void basicSplitTest() throws MetadataException, IOException { ISchemaFile sf = SchemaFile.initSchemaFile(sgNode.getName(), TEST_SCHEMA_REGION_ID); sf.writeMNode(sgNode); - Iterator res = sf.getChildren(sgNode); + Iterator res = sf.getChildren(sgNode); while (res.hasNext()) { checkSet.remove(res.next().getName()); @@ -817,7 +818,7 @@ public void basicSplitTest() throws MetadataException, IOException { } else if (j < 100) { name = "0" + name; } - IMNode aMeas = new InternalMNode(sgNode, "d_" + name); + ICachedMNode aMeas = nodeFactory.createInternalMNode(sgNode, "d_" + name); sgNode.addChild(aMeas); } @@ -828,14 +829,14 @@ public void basicSplitTest() throws MetadataException, IOException { } else if (j < 100) { name = "0" + name; } - IMNode aMeas = new InternalMNode(sgNode, "dd2_" + name); + ICachedMNode aMeas = nodeFactory.createInternalMNode(sgNode, "dd2_" + name); checkSet.add(aMeas.getName()); sgNode.getChildren().get("d_010").addChild(aMeas); } - IMNode d010 = sgNode.getChildren().get("d_010"); - d010 = MNodeUtils.setToEntity(d010); - IMNode ano = getMeasurementNode(d010, "splitover", "aliaslasialsai"); + ICachedMNode d010 = sgNode.getChildren().get("d_010"); + d010 = MNodeUtils.setToEntity(d010, nodeFactory).getAsMNode(); + ICachedMNode ano = getMeasurementNode(d010, "splitover", "aliaslasialsai"); d010.addChild(ano); sgNode.addChild(d010); @@ -851,7 +852,7 @@ public void basicSplitTest() throws MetadataException, IOException { sf.writeMNode(d010); int d010cs = 0; - Iterator res2 = sf.getChildren(d010); + Iterator res2 = sf.getChildren(d010); while (res2.hasNext()) { checkSet.add(res2.next().getName()); d010cs++; @@ -916,24 +917,27 @@ private static long getSegAddr(ISchemaFile sf, long curAddr, String key) { // endregion - // region IMNode Shortcut + // region ICacheMNode Shortcut - private IMNode supplyMeasurement(IMNode par, String name) { + private ICachedMNode supplyMeasurement(ICachedMNode par, String name) { return getMeasurementNode(par, name, name + "_als"); } - private IMNode supplyInternal(IMNode par, String name) { - return new InternalMNode(par, name); + private ICachedMNode supplyInternal(ICachedMNode par, String name) { + return nodeFactory.createInternalMNode(par, name); } - private IMNode supplyEntity(IMNode par, String name) { - return new EntityMNode(par, name); + private ICachedMNode supplyEntity(ICachedMNode par, String name) { + return nodeFactory.createDeviceMNode(par, name).getAsMNode(); } - private IMNode fillChildren( - IMNode par, int number, String prefix, BiFunction nodeFactory) { + private ICachedMNode fillChildren( + ICachedMNode par, + int number, + String prefix, + BiFunction nodeFactory) { String childName; - IMNode lastChild = null; + ICachedMNode lastChild = null; for (int i = 0; i < number; i++) { childName = prefix + "_" + i; lastChild = nodeFactory.apply(par, childName); @@ -943,7 +947,7 @@ private IMNode fillChildren( } // open for package - static void addMeasurementChild(IMNode par, String mid) { + static void addMeasurementChild(ICachedMNode par, String mid) { par.addChild(getMeasurementNode(par, mid, mid + "alias")); } @@ -951,9 +955,9 @@ static IMeasurementSchema getSchema(String id) { return new MeasurementSchema(id, TSDataType.FLOAT); } - private IMNode getNode(IMNode root, String path) throws MetadataException { + private ICachedMNode getNode(ICachedMNode root, String path) throws MetadataException { String[] pathNodes = PathUtils.splitPathToDetachedNodes(path); - IMNode cur = root; + ICachedMNode cur = root; for (String node : pathNodes) { if (!node.equals("root")) { cur = cur.getChild(node); @@ -962,33 +966,33 @@ private IMNode getNode(IMNode root, String path) throws MetadataException { return cur; } - static IMNode getInternalWithSegAddr(IMNode par, String name, long segAddr) { - IMNode node = new EntityMNode(par, name); + static ICachedMNode getInternalWithSegAddr(ICachedMNode par, String name, long segAddr) { + ICachedMNode node = nodeFactory.createDeviceMNode(par, name).getAsMNode(); ICachedMNodeContainer.getCachedMNodeContainer(node).setSegmentAddress(segAddr); return node; } - static IMNode getMeasurementNode(IMNode par, String name, String alias) { + static ICachedMNode getMeasurementNode(ICachedMNode par, String name, String alias) { IMeasurementSchema schema = new MeasurementSchema(name, TSDataType.FLOAT); - IMeasurementMNode mNode = - MeasurementMNode.getMeasurementMNode(par.getAsEntityMNode(), name, schema, alias); - return mNode; + return nodeFactory + .createMeasurementMNode(par.getAsDeviceMNode(), name, schema, alias) + .getAsMNode(); } - static void addNodeToUpdateBuffer(IMNode par, IMNode child) { + static void addNodeToUpdateBuffer(ICachedMNode par, ICachedMNode child) { ICachedMNodeContainer.getCachedMNodeContainer(par).remove(child.getName()); ICachedMNodeContainer.getCachedMNodeContainer(par).appendMNode(child); ICachedMNodeContainer.getCachedMNodeContainer(par).moveMNodeToCache(child.getName()); ICachedMNodeContainer.getCachedMNodeContainer(par).updateMNode(child.getName()); } - static void moveToUpdateBuffer(IMNode par, String childName) { + static void moveToUpdateBuffer(ICachedMNode par, String childName) { ICachedMNodeContainer.getCachedMNodeContainer(par).appendMNode(par.getChild(childName)); ICachedMNodeContainer.getCachedMNodeContainer(par).moveMNodeToCache(childName); ICachedMNodeContainer.getCachedMNodeContainer(par).updateMNode(childName); } - static void moveAllToUpdate(IMNode par) { + static void moveAllToUpdate(ICachedMNode par) { List childNames = par.getChildren().values().stream().map(IMNode::getName).collect(Collectors.toList()); for (String name : childNames) { @@ -997,7 +1001,7 @@ static void moveAllToUpdate(IMNode par) { } } - static void moveAllToBuffer(IMNode par) { + static void moveAllToBuffer(ICachedMNode par) { List childNames = par.getChildren().values().stream().map(IMNode::getName).collect(Collectors.toList()); for (String name : childNames) { @@ -1005,7 +1009,7 @@ static void moveAllToBuffer(IMNode par) { } } - static long getSegAddrInContainer(IMNode par) { + static long getSegAddrInContainer(ICachedMNode par) { return ICachedMNodeContainer.getCachedMNodeContainer(par).getSegmentAddress(); } @@ -1013,74 +1017,73 @@ static long getSegAddrInContainer(IMNode par) { // region Tree Constructor - static IMNode virtualTriangleMTree(int size, String sgPath) throws MetadataException { + static ICachedMNode virtualTriangleMTree(int size, String sgPath) throws MetadataException { String[] sgPathNodes = PathUtils.splitPathToDetachedNodes(sgPath); - IMNode upperNode = null; + ICachedMNode upperNode = null; for (String name : sgPathNodes) { - IMNode child = new InternalMNode(upperNode, name); - upperNode = child; + upperNode = nodeFactory.createInternalMNode(upperNode, name); } - IMNode internalNode = new StorageGroupEntityMNode(upperNode, "vRoot1", 0L); + ICachedMNode internalNode = nodeFactory.createDatabaseDeviceMNode(upperNode, "vRoot1", 0L); for (int idx = 0; idx < size; idx++) { String measurementId = "mid" + idx; IMeasurementSchema schema = new MeasurementSchema(measurementId, TSDataType.FLOAT); - IMeasurementMNode mNode = - MeasurementMNode.getMeasurementMNode( - internalNode.getAsEntityMNode(), measurementId, schema, measurementId + "als"); - internalNode.addChild(mNode); + IMeasurementMNode mNode = + nodeFactory.createMeasurementMNode( + internalNode.getAsDeviceMNode(), measurementId, schema, measurementId + "als"); + internalNode.addChild(mNode.getAsMNode()); } - IMNode curNode = internalNode; + ICachedMNode curNode = internalNode; for (int idx = 0; idx < size; idx++) { String nodeName = "int" + idx; - IMNode newNode = new EntityMNode(curNode, nodeName); + ICachedMNode newNode = nodeFactory.createDeviceMNode(curNode, nodeName).getAsMNode(); curNode.addChild(newNode); curNode = newNode; } for (int idx = 0; idx < 1000; idx++) { IMeasurementSchema schema = new MeasurementSchema("finalM" + idx, TSDataType.FLOAT); - IMeasurementMNode mNode = - MeasurementMNode.getMeasurementMNode( - internalNode.getAsEntityMNode(), "finalM" + idx, schema, "finalals" + idx); - curNode.addChild(mNode); + IMeasurementMNode mNode = + nodeFactory.createMeasurementMNode( + internalNode.getAsDeviceMNode(), "finalM" + idx, schema, "finalals" + idx); + curNode.addChild(mNode.getAsMNode()); } IMeasurementSchema schema = new MeasurementSchema("finalM", TSDataType.FLOAT); - IMeasurementMNode mNode = - MeasurementMNode.getMeasurementMNode( - internalNode.getAsEntityMNode(), "finalM", schema, "finalals"); - curNode.addChild(mNode); + IMeasurementMNode mNode = + nodeFactory.createMeasurementMNode( + internalNode.getAsDeviceMNode(), "finalM", schema, "finalals"); + curNode.addChild(mNode.getAsMNode()); upperNode.addChild(internalNode); return internalNode; } - static IMNode getFlatTree(int flatSize, String id) { - IMNode root = new InternalMNode(null, "root"); - IMNode test = new InternalMNode(root, "test"); - IMNode internalNode = new StorageGroupEntityMNode(null, "vRoot1", 0L); + static ICachedMNode getFlatTree(int flatSize, String id) { + ICachedMNode root = nodeFactory.createInternalMNode(null, "root"); + ICachedMNode test = nodeFactory.createInternalMNode(root, "test"); + ICachedMNode internalNode = nodeFactory.createDatabaseDeviceMNode(null, "vRoot1", 0L); for (int idx = 0; idx < flatSize; idx++) { String measurementId = id + idx; IMeasurementSchema schema = new MeasurementSchema(measurementId, TSDataType.FLOAT); - IMeasurementMNode mNode = - MeasurementMNode.getMeasurementMNode( - internalNode.getAsEntityMNode(), measurementId, schema, measurementId + "als"); - internalNode.addChild(mNode); + IMeasurementMNode mNode = + nodeFactory.createMeasurementMNode( + internalNode.getAsDeviceMNode(), measurementId, schema, measurementId + "als"); + internalNode.addChild(mNode.getAsMNode()); } test.addChild(internalNode); return internalNode; } - static IMNode getVerticalTree(int height, String id) { - IMNode trueRoot = new InternalMNode(null, "root"); - trueRoot.addChild(new InternalMNode(trueRoot, "sgvt")); - IMNode root = new StorageGroupEntityMNode(null, "vt", 0L); + static ICachedMNode getVerticalTree(int height, String id) { + ICachedMNode trueRoot = nodeFactory.createInternalMNode(null, "root"); + trueRoot.addChild(nodeFactory.createInternalMNode(trueRoot, "sgvt")); + ICachedMNode root = nodeFactory.createDatabaseDeviceMNode(null, "vt", 0L); int cnt = 0; - IMNode cur = root; + ICachedMNode cur = root; while (cnt < height) { - cur.addChild(new EntityMNode(cur, id + "_" + cnt)); + cur.addChild(nodeFactory.createDeviceMNode(cur, id + "_" + cnt).getAsMNode()); cur = cur.getChild(id + "_" + cnt); cnt++; } @@ -1088,9 +1091,9 @@ static IMNode getVerticalTree(int height, String id) { return root; } - static Iterator getTreeBFT(IMNode root) { - return new Iterator() { - Queue queue = new LinkedList(); + static Iterator getTreeBFT(ICachedMNode root) { + return new Iterator() { + final Queue queue = new LinkedList<>(); { this.queue.add(root); @@ -1102,10 +1105,10 @@ public boolean hasNext() { } @Override - public IMNode next() { - IMNode curNode = queue.poll(); + public ICachedMNode next() { + ICachedMNode curNode = queue.poll(); if (!curNode.isMeasurement() && curNode.getChildren().size() > 0) { - for (IMNode child : curNode.getChildren().values()) { + for (ICachedMNode child : curNode.getChildren().values()) { queue.add(child); } } diff --git a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/schemafile/SchemaPageTest.java b/server/src/test/java/org/apache/iotdb/db/metadata/mtree/schemafile/SchemaPageTest.java index 5e7db846aaa01..0294ab5f53deb 100644 --- a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/schemafile/SchemaPageTest.java +++ b/server/src/test/java/org/apache/iotdb/db/metadata/mtree/schemafile/SchemaPageTest.java @@ -19,10 +19,9 @@ package org.apache.iotdb.db.metadata.mtree.schemafile; import org.apache.iotdb.commons.exception.MetadataException; -import org.apache.iotdb.db.metadata.mnode.EntityMNode; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.MeasurementMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.factory.CacheMNodeFactory; import org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.ISchemaPage; import org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.RecordUtils; import org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.SchemaFileConfig; @@ -42,6 +41,8 @@ public class SchemaPageTest { + private final IMNodeFactory nodeFactory = CacheMNodeFactory.getInstance(); + @Before public void setUp() { EnvironmentUtils.envSetUp(); @@ -56,11 +57,11 @@ public void tearDown() throws Exception { public void flatTreeInsert() throws IOException, MetadataException { ISchemaPage page = ISchemaPage.initSegmentedPage(ByteBuffer.allocate(SchemaFileConfig.PAGE_LENGTH), 0); - IMNode root = virtualFlatMTree(15); + ICachedMNode root = virtualFlatMTree(15); for (int i = 0; i < 7; i++) { page.getAsSegmentedPage().allocNewSegment(SchemaFileConfig.SEG_SIZE_LST[0]); int cnt = 0; - for (IMNode child : root.getChildren().values()) { + for (ICachedMNode child : root.getChildren().values()) { cnt++; try { page.getAsSegmentedPage() @@ -104,16 +105,17 @@ public void essentialPageTest() throws MetadataException, IOException { Assert.assertEquals(256, (int) nPage.getAsInternalPage().getRecordByKey("aab")); } - private IMNode virtualFlatMTree(int childSize) { - IMNode internalNode = new EntityMNode(null, "vRoot1"); + private ICachedMNode virtualFlatMTree(int childSize) { + ICachedMNode internalNode = nodeFactory.createDeviceMNode(null, "vRoot1").getAsMNode(); for (int idx = 0; idx < childSize; idx++) { String measurementId = "mid" + idx; IMeasurementSchema schema = new MeasurementSchema(measurementId, TSDataType.FLOAT); - IMeasurementMNode mNode = - MeasurementMNode.getMeasurementMNode( - internalNode.getAsEntityMNode(), measurementId, schema, measurementId + "als"); - internalNode.addChild(mNode); + internalNode.addChild( + nodeFactory + .createMeasurementMNode( + internalNode.getAsDeviceMNode(), measurementId, schema, measurementId + "als") + .getAsMNode()); } return internalNode; } diff --git a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/schemafile/WrappedSegmentTest.java b/server/src/test/java/org/apache/iotdb/db/metadata/mtree/schemafile/WrappedSegmentTest.java index 3696abd0e8bcf..3bfc57fef6b46 100644 --- a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/schemafile/WrappedSegmentTest.java +++ b/server/src/test/java/org/apache/iotdb/db/metadata/mtree/schemafile/WrappedSegmentTest.java @@ -19,12 +19,10 @@ package org.apache.iotdb.db.metadata.mtree.schemafile; import org.apache.iotdb.commons.exception.MetadataException; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; import org.apache.iotdb.db.conf.IoTDBDescriptor; -import org.apache.iotdb.db.metadata.mnode.EntityMNode; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.InternalMNode; -import org.apache.iotdb.db.metadata.mnode.MeasurementMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.factory.CacheMNodeFactory; import org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.ISegment; import org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.RecordUtils; import org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.SchemaFileConfig; @@ -44,6 +42,8 @@ public class WrappedSegmentTest { + private static final IMNodeFactory nodeFactory = CacheMNodeFactory.getInstance(); + @Before public void setUp() { IoTDBDescriptor.getInstance() @@ -63,8 +63,8 @@ public void tearDown() throws Exception { @Test public void flatTreeInsert() throws MetadataException { WrappedSegment sf = new WrappedSegment(500); - IMNode rNode = virtualFlatMTree(10); - for (IMNode node : rNode.getChildren().values()) { + ICachedMNode rNode = virtualFlatMTree(10); + for (ICachedMNode node : rNode.getChildren().values()) { sf.insertRecord(node.getName(), RecordUtils.node2Buffer(node)); } sf.syncBuffer(); @@ -101,16 +101,17 @@ public void flatTreeInsert() throws MetadataException { Assert.assertEquals(sf.getRecord("aaa"), nsf.getRecord("aaa")); } - private IMNode virtualFlatMTree(int childSize) { - IMNode internalNode = new EntityMNode(null, "vRoot1"); + private ICachedMNode virtualFlatMTree(int childSize) { + ICachedMNode internalNode = nodeFactory.createDeviceMNode(null, "vRoot1").getAsMNode(); for (int idx = 0; idx < childSize; idx++) { String measurementId = "mid" + idx; IMeasurementSchema schema = new MeasurementSchema(measurementId, TSDataType.FLOAT); - IMeasurementMNode mNode = - MeasurementMNode.getMeasurementMNode( - internalNode.getAsEntityMNode(), measurementId, schema, measurementId + "als"); - internalNode.addChild(mNode); + internalNode.addChild( + nodeFactory + .createMeasurementMNode( + internalNode.getAsDeviceMNode(), measurementId, schema, measurementId + "als") + .getAsMNode()); } return internalNode; } @@ -118,9 +119,9 @@ private IMNode virtualFlatMTree(int childSize) { @Test public void evenSplitTest() throws MetadataException { ByteBuffer buffer = ByteBuffer.allocate(500); - ISegment seg = WrappedSegment.initAsSegment(buffer); + ISegment seg = WrappedSegment.initAsSegment(buffer); String[] test = new String[] {"a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9"}; - IMNode mNode = getMeasurementNode(null, "m", null); + ICachedMNode mNode = getMeasurementNode(null, "m", null); ByteBuffer buf = RecordUtils.node2Buffer(mNode); for (int i = 0; i < test.length; i++) { @@ -155,9 +156,9 @@ public void evenSplitTest() throws MetadataException { public void increasingSplitTest() throws MetadataException { ByteBuffer buffer = ByteBuffer.allocate(500); ByteBuffer buf2 = ByteBuffer.allocate(500); - ISegment seg = WrappedSegment.initAsSegment(buffer); + ISegment seg = WrappedSegment.initAsSegment(buffer); String[] test = new String[] {"a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8"}; - IMNode mNode = new InternalMNode(null, "m"); + ICachedMNode mNode = nodeFactory.createInternalMNode(null, "m"); ByteBuffer buf = RecordUtils.node2Buffer(mNode); for (int i = 0; i < test.length; i++) { @@ -189,9 +190,9 @@ public void increasingSplitTest() throws MetadataException { public void decreasingSplitTest() throws MetadataException { ByteBuffer buffer = ByteBuffer.allocate(500); ByteBuffer buf2 = ByteBuffer.allocate(500); - ISegment seg = WrappedSegment.initAsSegment(buffer); + ISegment seg = WrappedSegment.initAsSegment(buffer); String[] test = new String[] {"a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8"}; - IMNode mNode = new InternalMNode(null, "m"); + ICachedMNode mNode = nodeFactory.createInternalMNode(null, "m"); ByteBuffer buf = RecordUtils.node2Buffer(mNode); for (int i = test.length - 1; i >= 0; i--) { @@ -238,11 +239,10 @@ public void print(Object s) { System.out.println(s); } - private IMNode getMeasurementNode(IMNode par, String name, String alias) { + private ICachedMNode getMeasurementNode(ICachedMNode par, String name, String alias) { IMeasurementSchema schema = new MeasurementSchema(name, TSDataType.FLOAT); - IMeasurementMNode mNode = - MeasurementMNode.getMeasurementMNode( - par != null ? par.getAsEntityMNode() : null, name, schema, alias); - return mNode; + return nodeFactory + .createMeasurementMNode(par == null ? null : par.getAsDeviceMNode(), name, schema, alias) + .getAsMNode(); } } diff --git a/server/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaRegionBasicTest.java b/server/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaRegionBasicTest.java index 9f64421280783..879884ca5c0e7 100644 --- a/server/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaRegionBasicTest.java +++ b/server/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaRegionBasicTest.java @@ -22,10 +22,10 @@ import org.apache.iotdb.commons.path.MeasurementPath; import org.apache.iotdb.commons.path.PartialPath; import org.apache.iotdb.commons.path.PathPatternTree; +import org.apache.iotdb.commons.schema.node.MNodeType; import org.apache.iotdb.db.exception.metadata.AliasAlreadyExistException; import org.apache.iotdb.db.exception.metadata.MeasurementAlreadyExistException; import org.apache.iotdb.db.exception.metadata.PathAlreadyExistException; -import org.apache.iotdb.db.metadata.mnode.MNodeType; import org.apache.iotdb.db.metadata.plan.schemaregion.impl.read.SchemaRegionReadPlanFactory; import org.apache.iotdb.db.metadata.plan.schemaregion.impl.write.SchemaRegionWritePlanFactory; import org.apache.iotdb.db.metadata.plan.schemaregion.result.ShowDevicesResult; diff --git a/server/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaStatisticsTest.java b/server/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaStatisticsTest.java index 6e1383275fb85..4318c13f163e5 100644 --- a/server/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaStatisticsTest.java +++ b/server/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaStatisticsTest.java @@ -21,15 +21,10 @@ import org.apache.iotdb.commons.conf.CommonDescriptor; import org.apache.iotdb.commons.path.PartialPath; import org.apache.iotdb.commons.path.PathPatternTree; -import org.apache.iotdb.db.metadata.mnode.EntityMNode; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.InternalMNode; -import org.apache.iotdb.db.metadata.mnode.MeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.StorageGroupEntityMNode; -import org.apache.iotdb.db.metadata.mnode.StorageGroupMNode; -import org.apache.iotdb.db.metadata.mnode.estimator.BasicMNodSizeEstimator; -import org.apache.iotdb.db.metadata.mnode.estimator.IMNodeSizeEstimator; -import org.apache.iotdb.db.metadata.mtree.store.disk.memcontrol.CachedMNodeSizeEstimator; +import org.apache.iotdb.commons.schema.node.IMNode; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; +import org.apache.iotdb.db.metadata.mnode.mem.factory.MemMNodeFactory; +import org.apache.iotdb.db.metadata.mnode.schemafile.factory.CacheMNodeFactory; import org.apache.iotdb.db.metadata.rescon.CachedSchemaEngineStatistics; import org.apache.iotdb.db.metadata.rescon.CachedSchemaRegionStatistics; import org.apache.iotdb.db.metadata.rescon.ISchemaEngineStatistics; @@ -72,82 +67,86 @@ public void testMemoryStatistics() throws Exception { if (testParams.getTestModeName().equals("SchemaFile-PartialMemory") || testParams.getTestModeName().equals("SchemaFile-NonMemory")) { + + IMNodeFactory nodeFactory = CacheMNodeFactory.getInstance(); // wait release and flush task Thread.sleep(1000); - IMNodeSizeEstimator estimator = new CachedMNodeSizeEstimator(); // schemaRegion1 - IMNode sg1 = - new StorageGroupEntityMNode( + IMNode sg1 = + nodeFactory.createDatabaseDeviceMNode( null, "sg1", CommonDescriptor.getInstance().getConfig().getDefaultTTLInMs()); sg1.setFullPath("root.sg1"); - long size1 = estimator.estimateSize(sg1); + long size1 = sg1.estimateSize(); Assert.assertEquals(size1, schemaRegion1.getSchemaRegionStatistics().getRegionMemoryUsage()); // schemaRegion2 - IMNode sg2 = - new StorageGroupMNode( + IMNode sg2 = + nodeFactory.createDatabaseMNode( null, "sg2", CommonDescriptor.getInstance().getConfig().getDefaultTTLInMs()); sg2.setFullPath("root.sg2"); - long size2 = estimator.estimateSize(sg2); + long size2 = sg2.estimateSize(); Assert.assertEquals(size2, schemaRegion2.getSchemaRegionStatistics().getRegionMemoryUsage()); Assert.assertEquals(size1 + size2, engineStatistics.getMemoryUsage()); } else { - IMNodeSizeEstimator estimator = + IMNodeFactory nodeFactory = testParams.getSchemaEngineMode().equals("Memory") - ? new BasicMNodSizeEstimator() - : new CachedMNodeSizeEstimator(); + ? MemMNodeFactory.getInstance() + : CacheMNodeFactory.getInstance(); // schemaRegion1 - IMNode sg1 = - new StorageGroupEntityMNode( + IMNode sg1 = + nodeFactory.createDatabaseDeviceMNode( null, "sg1", CommonDescriptor.getInstance().getConfig().getDefaultTTLInMs()); sg1.setFullPath("root.sg1"); - long size1 = estimator.estimateSize(sg1); - IMNode tmp = - new MeasurementMNode( - sg1, + long size1 = sg1.estimateSize(); + IMNode tmp = + nodeFactory.createMeasurementMNode( + sg1.getAsDeviceMNode(), "d0", new MeasurementSchema( "d0", TSDataType.INT64, TSEncoding.PLAIN, CompressionType.SNAPPY), null); - size1 += estimator.estimateSize(tmp); - tmp = new InternalMNode(sg1, "d1"); - size1 += estimator.estimateSize(tmp); - tmp = new EntityMNode(tmp, "s2"); - size1 += estimator.estimateSize(tmp); + size1 += tmp.estimateSize(); + tmp = nodeFactory.createInternalMNode(sg1.getAsMNode(), "d1"); + size1 += tmp.estimateSize(); + tmp = nodeFactory.createDeviceMNode(tmp, "s2"); + size1 += tmp.estimateSize(); size1 += - estimator.estimateSize( - new MeasurementMNode( - tmp, + nodeFactory + .createMeasurementMNode( + tmp.getAsDeviceMNode(), "t1", new MeasurementSchema( "t1", TSDataType.INT64, TSEncoding.PLAIN, CompressionType.SNAPPY), - null)); + null) + .estimateSize(); Assert.assertEquals(size1, schemaRegion1.getSchemaRegionStatistics().getRegionMemoryUsage()); // schemaRegion2 - IMNode sg2 = - new StorageGroupMNode( + IMNode sg2 = + nodeFactory.createDatabaseMNode( null, "sg2", CommonDescriptor.getInstance().getConfig().getDefaultTTLInMs()); sg2.setFullPath("root.sg2"); - long size2 = estimator.estimateSize(sg2); - tmp = new EntityMNode(sg2, "d1"); - size2 += estimator.estimateSize(tmp); + long size2 = sg2.estimateSize(); + tmp = nodeFactory.createDeviceMNode(sg2, "d1"); + size2 += tmp.estimateSize(); size2 += - estimator.estimateSize( - new MeasurementMNode( - tmp, + nodeFactory + .createMeasurementMNode( + tmp.getAsDeviceMNode(), "s3", new MeasurementSchema( "s3", TSDataType.INT64, TSEncoding.PLAIN, CompressionType.SNAPPY), - null)); - tmp = new EntityMNode(sg2, "d2"); - size2 += estimator.estimateSize(tmp); + null) + .estimateSize(); + tmp = nodeFactory.createDeviceMNode(sg2, "d2"); + size2 += tmp.estimateSize(); size2 += - estimator.estimateSize( - new MeasurementMNode( - tmp, + nodeFactory + .createMeasurementMNode( + tmp.getAsDeviceMNode(), "s2", new MeasurementSchema( "s2", TSDataType.INT64, TSEncoding.PLAIN, CompressionType.SNAPPY), - null)); + null) + .estimateSize(); Assert.assertEquals(size2, schemaRegion2.getSchemaRegionStatistics().getRegionMemoryUsage()); Assert.assertEquals(size1 + size2, engineStatistics.getMemoryUsage()); } diff --git a/server/src/test/java/org/apache/iotdb/db/mpp/plan/plan/node/metadata/read/NodeManagementMemoryMergeNodeSerdeTest.java b/server/src/test/java/org/apache/iotdb/db/mpp/plan/plan/node/metadata/read/NodeManagementMemoryMergeNodeSerdeTest.java index 2a598b1f64039..8737b34ec937a 100644 --- a/server/src/test/java/org/apache/iotdb/db/mpp/plan/plan/node/metadata/read/NodeManagementMemoryMergeNodeSerdeTest.java +++ b/server/src/test/java/org/apache/iotdb/db/mpp/plan/plan/node/metadata/read/NodeManagementMemoryMergeNodeSerdeTest.java @@ -23,7 +23,7 @@ import org.apache.iotdb.common.rpc.thrift.TSchemaNode; import org.apache.iotdb.commons.exception.IllegalPathException; import org.apache.iotdb.commons.path.PartialPath; -import org.apache.iotdb.db.metadata.mnode.MNodeType; +import org.apache.iotdb.commons.schema.node.MNodeType; import org.apache.iotdb.db.mpp.common.FragmentInstanceId; import org.apache.iotdb.db.mpp.common.PlanFragmentId; import org.apache.iotdb.db.mpp.execution.exchange.sink.DownStreamChannelLocation; diff --git a/server/src/test/java/org/apache/iotdb/db/tools/SchemaFileSketchTest.java b/server/src/test/java/org/apache/iotdb/db/tools/SchemaFileSketchTest.java index 67a6be5a8a264..63a52e8e5a48f 100644 --- a/server/src/test/java/org/apache/iotdb/db/tools/SchemaFileSketchTest.java +++ b/server/src/test/java/org/apache/iotdb/db/tools/SchemaFileSketchTest.java @@ -20,13 +20,11 @@ package org.apache.iotdb.db.tools; import org.apache.iotdb.commons.exception.MetadataException; +import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory; import org.apache.iotdb.db.conf.IoTDBDescriptor; import org.apache.iotdb.db.metadata.MetadataConstant; -import org.apache.iotdb.db.metadata.mnode.IMNode; -import org.apache.iotdb.db.metadata.mnode.IMeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.InternalMNode; -import org.apache.iotdb.db.metadata.mnode.MeasurementMNode; -import org.apache.iotdb.db.metadata.mnode.StorageGroupEntityMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.ICachedMNode; +import org.apache.iotdb.db.metadata.mnode.schemafile.factory.CacheMNodeFactory; import org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.ISchemaFile; import org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.SchemaFile; import org.apache.iotdb.db.metadata.schemaregion.SchemaEngineMode; @@ -53,6 +51,8 @@ public class SchemaFileSketchTest { + private final IMNodeFactory nodeFactory = CacheMNodeFactory.getInstance(); + @Before public void setUp() { IoTDBDescriptor.getInstance() @@ -75,9 +75,9 @@ private void prepareData() throws IOException, MetadataException { int TEST_SCHEMA_REGION_ID = 0; ISchemaFile sf = SchemaFile.initSchemaFile("root.test.vRoot1", TEST_SCHEMA_REGION_ID); - Iterator ite = getTreeBFT(getFlatTree(500, "aa")); + Iterator ite = getTreeBFT(getFlatTree(500, "aa")); while (ite.hasNext()) { - IMNode cur = ite.next(); + ICachedMNode cur = ite.next(); if (!cur.isMeasurement()) { sf.writeMNode(cur); } @@ -118,9 +118,9 @@ public void testSchemaFileSketch() throws Exception { } } - private Iterator getTreeBFT(IMNode root) { - return new Iterator() { - Queue queue = new LinkedList(); + private Iterator getTreeBFT(ICachedMNode root) { + return new Iterator() { + Queue queue = new LinkedList<>(); { this.queue.add(root); @@ -132,10 +132,10 @@ public boolean hasNext() { } @Override - public IMNode next() { - IMNode curNode = queue.poll(); + public ICachedMNode next() { + ICachedMNode curNode = queue.poll(); if (!curNode.isMeasurement() && curNode.getChildren().size() > 0) { - for (IMNode child : curNode.getChildren().values()) { + for (ICachedMNode child : curNode.getChildren().values()) { queue.add(child); } } @@ -144,18 +144,19 @@ public IMNode next() { }; } - private IMNode getFlatTree(int flatSize, String id) { - IMNode root = new InternalMNode(null, "root"); - IMNode test = new InternalMNode(root, "test"); - IMNode internalNode = new StorageGroupEntityMNode(null, "vRoot1", 0L); + private ICachedMNode getFlatTree(int flatSize, String id) { + ICachedMNode root = nodeFactory.createInternalMNode(null, "root"); + ICachedMNode test = nodeFactory.createInternalMNode(root, "test"); + ICachedMNode internalNode = nodeFactory.createDatabaseDeviceMNode(null, "vRoot1", 0L); for (int idx = 0; idx < flatSize; idx++) { String measurementId = id + idx; IMeasurementSchema schema = new MeasurementSchema(measurementId, TSDataType.FLOAT); - IMeasurementMNode mNode = - MeasurementMNode.getMeasurementMNode( - internalNode.getAsEntityMNode(), measurementId, schema, measurementId + "als"); - internalNode.addChild(mNode); + internalNode.addChild( + nodeFactory + .createMeasurementMNode( + internalNode.getAsDeviceMNode(), measurementId, schema, measurementId + "als") + .getAsMNode()); } test.addChild(internalNode);