Skip to content

Commit

Permalink
refactor: moved logic for truncate cluster to the database layer
Browse files Browse the repository at this point in the history
  • Loading branch information
tglman committed Sep 14, 2023
1 parent 1e0457c commit cc7bff0
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ private void fetchTransacion() {
}

@Override
public OResultSet query(String query, Object[] args) {
public OResultSet query(String query, Object... args) {
checkOpenness();
checkAndSendTransaction();
ORemoteQueryResult result = storage.query(this, query, args);
Expand Down Expand Up @@ -1251,4 +1251,39 @@ public int[] getClustersIds(Set<String> filterClusters) {
checkIfActive();
return filterClusters.stream().map((c) -> getClusterIdByName(c)).mapToInt(i -> i).toArray();
}

/** {@inheritDoc} */
@Override
public void truncateCluster(String clusterName) {
command("truncate cluster " + clusterName).close();
}

@Override
public void truncateClass(String name) {
command("truncate class " + name).close();
}

@Override
public long truncateClusterInternal(String name) {
throw new UnsupportedOperationException();
}

@Override
public long truncateClass(String name, boolean polimorfic) {
long count = 0;
if (polimorfic) {
try (OResultSet result = command("truncate class " + name + " polymorphic ")) {
while (result.hasNext()) {
count += (long) result.next().getProperty("count");
}
}
} else {
try (OResultSet result = command("truncate class " + name)) {
while (result.hasNext()) {
count += (long) result.next().getProperty("count");
}
}
}
return count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,10 @@ default void endEsclusiveMetadataChange() {}
default void queryStartUsingViewCluster(int cluster) {}

default void queryStartUsingViewIndex(String index) {}

public void truncateClass(String name);

public long truncateClass(String name, boolean polimorfic);

public long truncateClusterInternal(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,6 @@ public long countClusterElements(final int iClusterId) {
return countClusterElements(iClusterId, false);
}

/** {@inheritDoc} */
@Override
public void truncateCluster(String clusterName) {
command("truncate cluster " + clusterName).close();
}

/** {@inheritDoc} */
public OMetadataDefault getMetadata() {
checkOpenness();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
import com.orientechnologies.orient.core.metadata.security.ORole;
import com.orientechnologies.orient.core.metadata.security.ORule;
import com.orientechnologies.orient.core.metadata.security.OSecurityInternal;
import com.orientechnologies.orient.core.metadata.security.OSecurityShared;
import com.orientechnologies.orient.core.metadata.security.OSecurityUser;
import com.orientechnologies.orient.core.metadata.security.OToken;
import com.orientechnologies.orient.core.metadata.security.OUser;
Expand Down Expand Up @@ -2023,4 +2024,66 @@ public void startEsclusiveMetadataChange() {
public void endEsclusiveMetadataChange() {
((OAbstractPaginatedStorage) storage).endDDL();
}

@Override
public long truncateClass(String name, boolean polimorfic) {
this.checkSecurity(ORule.ResourceGeneric.CLASS, ORole.PERMISSION_UPDATE);
OClass clazz = getClass(name);
if (clazz.isSubClassOf(OSecurityShared.RESTRICTED_CLASSNAME)) {
throw new OSecurityException(
"Class '"
+ getName()
+ "' cannot be truncated because has record level security enabled (extends '"
+ OSecurityShared.RESTRICTED_CLASSNAME
+ "')");
}

int[] clusterIds;
if (polimorfic) {
clusterIds = clazz.getPolymorphicClusterIds();
} else {
clusterIds = clazz.getClusterIds();
}
long count = 0;
for (int id : clusterIds) {
if (id < 0) continue;
final String clusterName = getClusterNameById(id);
if (clusterName == null) continue;
count += truncateClusterInternal(clusterName);
}
return count;
}

@Override
public void truncateClass(String name) {
truncateClass(name, true);
}

@Override
public long truncateClusterInternal(String clusterName) {
checkSecurity(ORule.ResourceGeneric.CLUSTER, ORole.PERMISSION_DELETE, clusterName);
checkForClusterPermissions(clusterName);
int id = getClusterIdByName(clusterName);
if (id == -1) {
throw new ODatabaseException("Cluster with name " + clusterName + " does not exist");
}
final OClass clazz = getMetadata().getSchema().getClassByClusterId(id);
if (clazz != null) {
checkSecurity(ORule.ResourceGeneric.CLASS, ORole.PERMISSION_DELETE, clazz.getName());
}
long count = 0;
final ORecordIteratorCluster<ORecord> iteratorCluster =
new ORecordIteratorCluster<ORecord>(this, id);
while (iteratorCluster.hasNext()) {
final ORecord record = iteratorCluster.next();
record.delete();
count++;
}
return count;
}

@Override
public void truncateCluster(String clusterName) {
truncateClusterInternal(clusterName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1653,4 +1653,19 @@ public String getClusterRecordConflictStrategy(int clusterId) {
public int[] getClustersIds(Set<String> filterClusters) {
return internal.getClustersIds(filterClusters);
}

@Override
public void truncateClass(String name) {
internal.truncateClass(name);
}

@Override
public long truncateClusterInternal(String name) {
return internal.truncateClusterInternal(name);
}

@Override
public long truncateClass(String name, boolean polimorfic) {
return internal.truncateClass(name, polimorfic);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -445,13 +445,7 @@ protected OPropertyEmbedded createPropertyInstance(OGlobalProperty global) {
public OClass truncateCluster(String clusterName) {
getDatabase().checkSecurity(ORule.ResourceGeneric.CLASS, ORole.PERMISSION_DELETE, name);

acquireSchemaReadLock();
try {
final ODatabaseDocumentInternal database = getDatabase();
truncateClusterInternal(clusterName, database);
} finally {
releaseSchemaReadLock();
}
truncateClusterInternal(clusterName, getDatabase());

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,17 @@
import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.exception.ODatabaseException;
import com.orientechnologies.orient.core.exception.OSchemaException;
import com.orientechnologies.orient.core.exception.OSecurityAccessException;
import com.orientechnologies.orient.core.exception.OSecurityException;
import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.index.OIndex;
import com.orientechnologies.orient.core.index.OIndexDefinition;
import com.orientechnologies.orient.core.index.OIndexDefinitionFactory;
import com.orientechnologies.orient.core.index.OIndexException;
import com.orientechnologies.orient.core.index.OIndexManagerAbstract;
import com.orientechnologies.orient.core.iterator.ORecordIteratorCluster;
import com.orientechnologies.orient.core.metadata.schema.clusterselection.OClusterSelectionStrategy;
import com.orientechnologies.orient.core.metadata.security.ORole;
import com.orientechnologies.orient.core.metadata.security.ORule;
import com.orientechnologies.orient.core.metadata.security.OSecurityShared;
import com.orientechnologies.orient.core.record.OElement;
import com.orientechnologies.orient.core.record.ORecord;
import com.orientechnologies.orient.core.record.impl.ODocument;
Expand Down Expand Up @@ -631,16 +627,7 @@ public static OClass addClusters(final OClass cls, final int iClusters) {

protected void truncateClusterInternal(
final String clusterName, final ODatabaseDocumentInternal database) {
database.checkForClusterPermissions(clusterName);

final ORecordIteratorCluster<ORecord> iteratorCluster = database.browseCluster(clusterName);
if (iteratorCluster == null) {
throw new ODatabaseException("Cluster with name " + clusterName + " does not exist");
}
while (iteratorCluster.hasNext()) {
final ORecord record = iteratorCluster.next();
record.delete();
}
database.truncateCluster(clusterName);
}

public Collection<OClass> getSubclasses() {
Expand Down Expand Up @@ -805,38 +792,7 @@ public long count(final boolean isPolymorphic) {
/** Truncates all the clusters the class uses. */
public void truncate() {
ODatabaseDocumentInternal db = getDatabase();
db.checkSecurity(ORule.ResourceGeneric.CLASS, ORole.PERMISSION_UPDATE);

if (isSubClassOf(OSecurityShared.RESTRICTED_CLASSNAME)) {
throw new OSecurityException(
"Class '"
+ getName()
+ "' cannot be truncated because has record level security enabled (extends '"
+ OSecurityShared.RESTRICTED_CLASSNAME
+ "')");
}

acquireSchemaReadLock();
try {

for (int id : clusterIds) {
if (id < 0) continue;
final String clusterName = db.getClusterNameById(id);
if (clusterName == null) continue;
db.checkForClusterPermissions(clusterName);

final ORecordIteratorCluster<ORecord> iteratorCluster = db.browseCluster(clusterName);
if (iteratorCluster == null) {
throw new ODatabaseException("Cluster with name " + clusterName + " does not exist");
}
while (iteratorCluster.hasNext()) {
final ORecord record = iteratorCluster.next();
record.delete();
}
}
} finally {
releaseSchemaReadLock();
}
db.truncateClass(name, false);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
/* JavaCCOptions:MULTI=true,NODE_USES_PARSER=false,VISITOR=true,TRACK_TOKENS=true,NODE_PREFIX=O,NODE_EXTENDS=,NODE_FACTORY=,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
package com.orientechnologies.orient.core.sql.parser;

import com.orientechnologies.common.exception.OException;
import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.db.ODatabaseSession;
import com.orientechnologies.orient.core.db.ODatabaseDocumentInternal;
import com.orientechnologies.orient.core.exception.OCommandExecutionException;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OSchema;
import com.orientechnologies.orient.core.sql.executor.OInternalResultSet;
import com.orientechnologies.orient.core.sql.executor.OResultInternal;
import com.orientechnologies.orient.core.sql.executor.OResultSet;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;

Expand All @@ -31,7 +29,7 @@ public OTruncateClassStatement(OrientSql p, int id) {

@Override
public OResultSet executeDDL(OCommandContext ctx) {
ODatabaseSession db = ctx.getDatabase();
ODatabaseDocumentInternal db = (ODatabaseDocumentInternal) ctx.getDatabase();
OSchema schema = db.getMetadata().getSchema();
OClass clazz = schema.getClass(className.getStringValue());
if (clazz == null) {
Expand Down Expand Up @@ -70,24 +68,21 @@ public OResultSet executeDDL(OCommandContext ctx) {
}
}

try {
clazz.truncate();
OResultInternal result = new OResultInternal();
result.setProperty("operation", "truncate class");
result.setProperty("className", className.getStringValue());
rs.add(result);
if (polymorphic) {
for (OClass subclass : subclasses) {
subclass.truncate();
result = new OResultInternal();
result.setProperty("operation", "truncate class");
result.setProperty("className", className.getStringValue());
rs.add(result);
}
long count = db.truncateClass(clazz.getName(), false);
OResultInternal result = new OResultInternal();
result.setProperty("operation", "truncate class");
result.setProperty("className", className.getStringValue());
result.setProperty("count", count);
rs.add(result);
if (polymorphic) {
for (OClass subclass : subclasses) {
count = db.truncateClass(subclass.getName(), false);
result = new OResultInternal();
result.setProperty("operation", "truncate class");
result.setProperty("className", className.getStringValue());
result.setProperty("count", count);
rs.add(result);
}
} catch (IOException e) {
throw OException.wrapException(
new OCommandExecutionException("Error on executing command"), e);
}

return rs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentAbstract;
import com.orientechnologies.orient.core.exception.ODatabaseException;
import com.orientechnologies.orient.core.iterator.ORecordIteratorCluster;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OSchema;
import com.orientechnologies.orient.core.record.ORecord;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.executor.OInternalResultSet;
import com.orientechnologies.orient.core.sql.executor.OResultInternal;
import com.orientechnologies.orient.core.sql.executor.OResultSet;
Expand Down Expand Up @@ -45,31 +40,14 @@ public OResultSet executeDDL(OCommandContext ctx) {
throw new ODatabaseException("Cluster with name " + clusterName + " does not exist");
}

final OSchema schema = database.getMetadata().getSchema();
final OClass clazz = schema.getClassByClusterId(clusterId);
if (clazz == null) {
final String clusterName = database.getClusterNameById(clusterId);
database.checkForClusterPermissions(clusterName);

final ORecordIteratorCluster<ODocument> iteratorCluster = database.browseCluster(clusterName);
if (iteratorCluster == null) {
throw new ODatabaseException("Cluster with name " + clusterName + " does not exist");
}
while (iteratorCluster.hasNext()) {
final ORecord record = iteratorCluster.next();
record.delete();
}
} else {
String name = database.getClusterNameById(clusterId);
clazz.truncateCluster(name);
}
String name = database.getClusterNameById(clusterId);
long count = database.truncateClusterInternal(name);

OResultInternal result = new OResultInternal();
result.setProperty("operation", "truncate cluster");
if (clusterName != null) {
result.setProperty("clusterName", clusterName.getStringValue());
}
result.setProperty("clusterName", name);
result.setProperty("clusterId", clusterId);
result.setProperty("count", count);

rs.add(result);
return rs;
Expand Down

0 comments on commit cc7bff0

Please sign in to comment.