Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,46 @@
}
}

private IDeviceMNode<IMemMNode> setToEntityAndUpdateFlags(final IMemMNode node) {
final boolean wasDevice = node.isDevice();
final IDeviceMNode<IMemMNode> deviceMNode = store.setToEntity(node);
if (!wasDevice) {
markAncestorsHavingDeviceDescendant(node);
}
return deviceMNode;
}

private void markAncestorsHavingDeviceDescendant(final IMemMNode deviceNode) {
IMemMNode current = deviceNode.getParent();
while (current != null && !current.hasDeviceDescendant()) {
current.setHasDeviceDescendant(true);
current = current.getParent();
}
}

private boolean hasDeviceDescendantInChildren(final IMemMNode node) {
final IMNodeIterator<IMemMNode> iterator = store.getChildrenIterator(node);
try {
while (iterator.hasNext()) {
final IMemMNode child = iterator.next();
if (child.isDevice() || child.hasDeviceDescendant()) {
return true;
}
}
return false;
} finally {
iterator.close();
}
}

private void refreshAncestorsHavingDeviceDescendant(IMemMNode startNode) {
IMemMNode current = startNode;
while (current != null) {
current.setHasDeviceDescendant(hasDeviceDescendantInChildren(current));
current = current.getParent();
}
}

private long getTemplateMeasurementCount(final int templateId) {
final Template template = ClusterTemplateManager.getInstance().getTemplate(templateId);
return template == null ? 0L : template.getMeasurementNumber();
Expand Down Expand Up @@ -315,7 +355,7 @@
if (device.isDevice()) {
entityMNode = device.getAsDeviceMNode();
} else {
entityMNode = store.setToEntity(device);
entityMNode = setToEntityAndUpdateFlags(device);
}

// create a non-aligned time series
Expand Down Expand Up @@ -352,7 +392,7 @@
* @param encodings encodings list
* @param compressors compressor
*/
public List<IMeasurementMNode<IMemMNode>> createAlignedTimeSeries(

Check warning on line 395 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/mem/MTreeBelowSGMemoryImpl.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

A "Brain Method" was detected. Refactor it to reduce at least one of the following metrics: LOC from 69 to 64, Complexity from 20 to 14, Nesting Level from 4 to 2, Number of Variables from 18 to 6.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ4mlnE8I-vGf32cLFKF&open=AZ4mlnE8I-vGf32cLFKF&pullRequest=17672
final PartialPath devicePath,
final List<String> measurements,
final List<TSDataType> dataTypes,
Expand Down Expand Up @@ -409,7 +449,7 @@
if (device.isDevice()) {
entityMNode = device.getAsDeviceMNode();
} else {
entityMNode = store.setToEntity(device);
entityMNode = setToEntityAndUpdateFlags(device);
entityMNode.setAligned(true);
}

Expand Down Expand Up @@ -673,6 +713,7 @@
synchronized (this) {
curNode = store.setToInternal(entityMNode);
}
refreshAncestorsHavingDeviceDescendant(curNode.getParent());
} else if (!hasNonViewMeasurement) {
// has some measurement but they are all logical view
entityMNode.setAligned(null);
Expand Down Expand Up @@ -1033,7 +1074,7 @@
if (cur.isDevice()) {
entityMNode = cur.getAsDeviceMNode();
} else {
entityMNode = store.setToEntity(cur);
entityMNode = setToEntityAndUpdateFlags(cur);
}
}

Expand Down Expand Up @@ -1141,7 +1182,7 @@
if (cur.isDevice()) {
entityMNode = cur.getAsDeviceMNode();
} else {
entityMNode = store.setToEntity(cur);
entityMNode = setToEntityAndUpdateFlags(cur);
}

if (!entityMNode.isAligned()) {
Expand Down Expand Up @@ -1196,14 +1237,24 @@

private long rebuildSubtreeMeasurementCountFromNode(final IMemMNode node) {
long count = node.isMeasurement() ? 1L : 0L;
boolean hasDeviceDescendant = false;
final IMNodeIterator<IMemMNode> iterator = store.getChildrenIterator(node);
while (iterator.hasNext()) {
count += rebuildSubtreeMeasurementCountFromNode(iterator.next());
try {
while (iterator.hasNext()) {
final IMemMNode child = iterator.next();
count += rebuildSubtreeMeasurementCountFromNode(child);
if (child.isDevice() || child.hasDeviceDescendant()) {
hasDeviceDescendant = true;
}
}
} finally {
iterator.close();
}
if (node.isDevice() && node.getAsDeviceMNode().isUseTemplate()) {
count += getTemplateMeasurementCount(node.getAsDeviceMNode().getSchemaTemplateId());
}
node.setSubtreeMeasurementCount(count);
node.setHasDeviceDescendant(hasDeviceDescendant);
return count;
}

Expand Down Expand Up @@ -1786,7 +1837,7 @@
if (device.isDevice()) {
entityMNode = device.getAsDeviceMNode();
} else {
entityMNode = store.setToEntity(device);
entityMNode = setToEntityAndUpdateFlags(device);
// this parent has no measurement before. The leafName is his first child who is a logical
// view.
entityMNode.setAligned(null);
Expand Down Expand Up @@ -1923,7 +1974,7 @@
(TableDeviceInfo<IMemMNode>) entityMNode.getDeviceInfo();
attributeUpdater.accept(deviceInfo.getAttributePointer());
} else {
entityMNode = store.setToEntity(cur);
entityMNode = setToEntityAndUpdateFlags(cur);
final TableDeviceInfo<IMemMNode> deviceInfo = new TableDeviceInfo<>();
deviceInfo.setAttributePointer(attributePointerGetter.getAsInt());
entityMNode.getAsInternalMNode().setDeviceInfo(deviceInfo);
Expand Down Expand Up @@ -2040,6 +2091,7 @@
collector.traverse();
}
databaseMNode.deleteChild(tableName);
refreshAncestorsHavingDeviceDescendant(databaseMNode);
regionStatistics.resetTableDevice(tableName);
store.releaseMemory(memoryReleased.get());
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ public interface IMemMNode extends IMNode<IMemMNode> {
long getSubtreeMeasurementCount();

void setSubtreeMeasurementCount(long subtreeMeasurementCount);

/**
* Whether there is any device node in the subtree rooted at this node, excluding the node itself.
* This flag is maintained in memory only.
*/
boolean hasDeviceDescendant();

void setHasDeviceDescendant(boolean hasDeviceDescendant);
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public class BasicMNode implements IMemMNode {
/** Cached count of measurements in this node's subtree, rebuilt on restart. */
private long subtreeMeasurementCount = 0L;

/** Cached flag showing whether there is any device in the subtree below this node. */
private boolean hasDeviceDescendant = false;

/** from root to this node, only be set when used once for InternalMNode */
private String fullPath;

Expand Down Expand Up @@ -112,6 +115,16 @@ public void setSubtreeMeasurementCount(final long subtreeMeasurementCount) {
this.subtreeMeasurementCount = subtreeMeasurementCount;
}

@Override
public boolean hasDeviceDescendant() {
return hasDeviceDescendant;
}

@Override
public void setHasDeviceDescendant(final boolean hasDeviceDescendant) {
this.hasDeviceDescendant = hasDeviceDescendant;
}

@Override
public PartialPath getPartialPath() {
final List<String> detachedPath = new ArrayList<>();
Expand Down Expand Up @@ -239,6 +252,7 @@ public <R, C> R accept(final MNodeVisitor<R, C> visitor, final C context) {
* <li>parent reference, 8B
* <li>fullPath reference, 8B
* <li>subtreeMeasurementCount, 8B
* <li>hasDeviceDescendant, 1B
* </ol>
* <li>MapEntry in parent
* <ol>
Expand All @@ -250,7 +264,7 @@ public <R, C> R accept(final MNodeVisitor<R, C> visitor, final C context) {
*/
@Override
public int estimateSize() {
return 8 + 8 + 8 + 8 + 8 + 8 + 8 + 28 + basicMNodeInfo.estimateSize();
return 8 + 8 + 8 + 8 + 8 + 1 + 8 + 8 + 28 + basicMNodeInfo.estimateSize();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,14 @@ public long getSubtreeMeasurementCount() {
public void setSubtreeMeasurementCount(long subtreeMeasurementCount) {
basicMNode.setSubtreeMeasurementCount(subtreeMeasurementCount);
}

@Override
public boolean hasDeviceDescendant() {
return basicMNode.hasDeviceDescendant();
}

@Override
public void setHasDeviceDescendant(final boolean hasDeviceDescendant) {
basicMNode.setHasDeviceDescendant(hasDeviceDescendant);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,14 @@ public long getSubtreeMeasurementCount() {
public void setSubtreeMeasurementCount(long subtreeMeasurementCount) {
basicMNode.setSubtreeMeasurementCount(subtreeMeasurementCount);
}

@Override
public boolean hasDeviceDescendant() {
return basicMNode.hasDeviceDescendant();
}

@Override
public void setHasDeviceDescendant(final boolean hasDeviceDescendant) {
basicMNode.setHasDeviceDescendant(hasDeviceDescendant);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,14 @@ public long getSubtreeMeasurementCount() {
public void setSubtreeMeasurementCount(long subtreeMeasurementCount) {
basicMNode.setSubtreeMeasurementCount(subtreeMeasurementCount);
}

@Override
public boolean hasDeviceDescendant() {
return basicMNode.hasDeviceDescendant();
}

@Override
public void setHasDeviceDescendant(final boolean hasDeviceDescendant) {
basicMNode.setHasDeviceDescendant(hasDeviceDescendant);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,71 @@
private final int levelOfDB;
private final CachedSchemaRegionStatistics regionStatistics;

private IDeviceMNode<ICachedMNode> setToEntityAndUpdateFlags(final ICachedMNode node)
throws MetadataException {
final boolean wasDevice = node.isDevice();
if (!node.isDeviceDescendantComputed()) {
node.setHasDeviceDescendant(hasDeviceDescendantInChildren(node));
node.setDeviceDescendantComputed(true);
}
final IDeviceMNode<ICachedMNode> deviceMNode = store.setToEntity(node);
if (!wasDevice) {
markAncestorsHavingDeviceDescendant(node);
}
return deviceMNode;
}

private void markAncestorsHavingDeviceDescendant(final ICachedMNode deviceNode) {
ICachedMNode current = deviceNode.getParent();
while (current != null && !current.hasDeviceDescendant()) {
current.setHasDeviceDescendant(true);
current.setDeviceDescendantComputed(true);
current = current.getParent();
}
}

private boolean hasDeviceDescendantInChildren(final ICachedMNode node) throws MetadataException {
final IMNodeIterator<ICachedMNode> iterator = store.getChildrenIterator(node);
try {
while (iterator.hasNext()) {
final ICachedMNode child = iterator.next();
try {
if (child.isDevice() || hasDeviceDescendant(child)) {
return true;
}
} finally {
unPinMNode(child);
}
}
return false;
} finally {
iterator.close();
}
}

private boolean hasDeviceDescendant(final ICachedMNode node) throws MetadataException {
if (node.isMeasurement()) {
node.setHasDeviceDescendant(false);
node.setDeviceDescendantComputed(true);
return false;
}
if (!node.isDeviceDescendantComputed()) {
node.setHasDeviceDescendant(hasDeviceDescendantInChildren(node));
node.setDeviceDescendantComputed(true);
}
return node.hasDeviceDescendant();
}

private void refreshAncestorsHavingDeviceDescendant(ICachedMNode startNode)
throws MetadataException {
ICachedMNode current = startNode;
while (current != null) {
current.setHasDeviceDescendant(hasDeviceDescendantInChildren(current));
current.setDeviceDescendantComputed(true);
current = current.getParent();
}
}

// region MTree initialization, clear and serialization
public MTreeBelowSGCachedImpl(
PartialPath storageGroupPath,
Expand Down Expand Up @@ -291,7 +356,7 @@
* @param props props
* @param alias alias of measurement
*/
public IMeasurementMNode<ICachedMNode> createTimeSeriesWithPinnedReturn(

Check warning on line 359 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/MTreeBelowSGCachedImpl.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

A "Brain Method" was detected. Refactor it to reduce at least one of the following metrics: LOC from 69 to 64, Complexity from 17 to 14, Nesting Level from 5 to 2, Number of Variables from 17 to 6.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ4m06snrfNQ6yOhHtD1&open=AZ4m06snrfNQ6yOhHtD1&pullRequest=17672
final PartialPath path,
final TSDataType dataType,
final TSEncoding encoding,
Expand Down Expand Up @@ -353,7 +418,7 @@
if (device.isDevice()) {
entityMNode = device.getAsDeviceMNode();
} else {
entityMNode = store.setToEntity(device);
entityMNode = setToEntityAndUpdateFlags(device);
device = entityMNode.getAsMNode();
}

Expand Down Expand Up @@ -395,7 +460,7 @@
* @param encodings encodings list
* @param compressors compressor
*/
public List<IMeasurementMNode<ICachedMNode>> createAlignedTimeSeries(

Check warning on line 463 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/MTreeBelowSGCachedImpl.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

A "Brain Method" was detected. Refactor it to reduce at least one of the following metrics: LOC from 85 to 64, Complexity from 21 to 14, Nesting Level from 6 to 2, Number of Variables from 18 to 6.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ4m06snrfNQ6yOhHtD2&open=AZ4m06snrfNQ6yOhHtD2&pullRequest=17672
final PartialPath devicePath,
final List<String> measurements,
final List<TSDataType> dataTypes,
Expand Down Expand Up @@ -458,7 +523,7 @@
if (device.isDevice()) {
entityMNode = device.getAsDeviceMNode();
} else {
entityMNode = store.setToEntity(device);
entityMNode = setToEntityAndUpdateFlags(device);
entityMNode.setAligned(true);
device = entityMNode.getAsMNode();
}
Expand Down Expand Up @@ -703,6 +768,7 @@

if (!hasMeasurement) {
curNode = store.setToInternal(entityMNode);
refreshAncestorsHavingDeviceDescendant(curNode.getParent());
} else if (!hasNonViewMeasurement) {
// has some measurement but they are all logical view
store.updateMNode(entityMNode.getAsMNode(), o -> o.getAsDeviceMNode().setAligned(null));
Expand Down Expand Up @@ -1075,7 +1141,7 @@
if (device.isDevice()) {
entityMNode = device.getAsDeviceMNode();
} else {
entityMNode = store.setToEntity(device);
entityMNode = setToEntityAndUpdateFlags(device);
// this parent has no measurement before. The leafName is his first child who is a
// logical
// view.
Expand Down Expand Up @@ -1197,7 +1263,7 @@
if (cur.isDevice()) {
entityMNode = cur.getAsDeviceMNode();
} else {
entityMNode = store.setToEntity(cur);
entityMNode = setToEntityAndUpdateFlags(cur);
}

if (entityMNode.isUseTemplate()) {
Expand Down Expand Up @@ -1243,7 +1309,7 @@
if (cur.isDevice()) {
entityMNode = cur.getAsDeviceMNode();
} else {
entityMNode = store.setToEntity(cur);
entityMNode = setToEntityAndUpdateFlags(cur);
}

store.updateMNode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
import org.apache.iotdb.db.schemaengine.schemaregion.mtree.impl.pbtree.memory.cache.CacheEntry;

public interface ICachedMNode extends IMNode<ICachedMNode> {
boolean hasDeviceDescendant();

void setHasDeviceDescendant(boolean hasDeviceDescendant);

boolean isDeviceDescendantComputed();

void setDeviceDescendantComputed(boolean deviceDescendantComputed);

CacheEntry getCacheEntry();

void setCacheEntry(CacheEntry cacheEntry);
Expand Down
Loading
Loading