REQ-3540: 惹名scope的子节点名可以重复

This commit is contained in:
yanglin 2025-03-19 13:45:06 +08:00
parent 4cfe68da04
commit abdde327e5
3 changed files with 23 additions and 18 deletions

View File

@ -12,14 +12,15 @@ import lombok.RequiredArgsConstructor;
public enum DatabaseScope {
// 匿名数据库
ANONYMOUS(DatabaseType.NONE),
ANONYMOUS(DatabaseType.NONE, true),
// 企业数据库
ENT_DATABASE(DatabaseType.CREATED_BY_SYSTEM),
ENT_DATABASE(DatabaseType.CREATED_BY_SYSTEM, false),
// 项目数据库
PROJECT_DATABASE(DatabaseType.CREATED_BY_SYSTEM),
PROJECT_DATABASE(DatabaseType.CREATED_BY_SYSTEM, false),
// 个人数据库: 为单位建的
PERSONAL_FOR_ENT(DatabaseType.CREATED_BY_USER_FOR_ENT);
PERSONAL_FOR_ENT(DatabaseType.CREATED_BY_USER_FOR_ENT, false);
private final DatabaseType databaseType;
private final boolean childrenNameDuplicatable;
}

View File

@ -54,15 +54,13 @@ public class IndexManager {
@BizTransactional
public IndexNode createDatabase(NodeCreate create) {
indexSupport.lockParentAndReleaseOnCommit(create);
indexSupport.ensureChildNameNotUsed(create, IndexNodeType.DATABASE);
maybeEnsureChildNameNotUsed(create, IndexNodeType.DATABASE, true);
return indexSupport.createNode(create, IndexNodeType.DATABASE);
}
@BizTransactional
public IndexNode createDir(NodeCreate create) {
indexSupport.lockParentAndReleaseOnCommit(create);
indexSupport.ensureChildNameNotUsed(create, IndexNodeType.DIRECTORY);
maybeEnsureChildNameNotUsed(create, IndexNodeType.DIRECTORY, true);
return indexSupport.createNode(create, IndexNodeType.DIRECTORY);
}
@ -70,7 +68,7 @@ public class IndexManager {
BizAssertions.assertTrue(format.creatable(), "无法创建: {}", format.readableName());
BizAssertions.assertFalse(TransactionSynchronizationManager.isActualTransactionActive(), "不能在事务中使用");
// check without lock
indexSupport.ensureChildNameNotUsed(create, IndexNodeType.FILE);
maybeEnsureChildNameNotUsed(create, IndexNodeType.FILE, false);
String fullFileName = String.format("%s.%s", create.name(), format.createFileExtension());
String ossFileKey = ossClient.copy(docProps.createFileOssFileKey(format), fullFileName);
return OssFile.create(format, format.createFileExtension(), 0, ossFileKey);
@ -92,10 +90,9 @@ public class IndexManager {
Ref<Boolean> deleteOssFile = Ref.create(false);
try {
return transaction.execute(unused -> {
indexSupport.lockParentAndReleaseOnCommit(create);
try {
// check with lock
indexSupport.ensureChildNameNotUsed(create, IndexNodeType.FILE);
maybeEnsureChildNameNotUsed(create, IndexNodeType.FILE, true);
}
catch (NameUsedException e) {
deleteOssFile.set(true);
@ -118,14 +115,24 @@ public class IndexManager {
}
}
private void maybeEnsureChildNameNotUsed(NodeCreate create, IndexNodeType nodeType, boolean withLock) {
if (create.nodeScope().scope().isChildrenNameDuplicatable())
return;
if (withLock)
indexSupport.lockParentAndReleaseOnCommit(create);
indexSupport.ensureChildNameNotUsed(create, nodeType, create.name());
}
@BizTransactional
public void rename(String code, String newName) {
IndexNode indexNode = getOrThrow(code);
if (indexNode.getName().equals(newName))
return;
docLogDao.log("indexNodeRename", code, "newName", newName);
indexSupport.lockParentAndReleaseOnCommit(indexNode);
indexSupport.ensureChildNameNotUsed(indexNode, indexNode.getNodeType(), newName);
if (!indexNode.scope().isChildrenNameDuplicatable()) {
indexSupport.lockParentAndReleaseOnCommit(indexNode);
indexSupport.ensureChildNameNotUsed(indexNode, indexNode.getNodeType(), newName);
}
indexNodeDao.rename(code, newName);
if (indexNode.isFile())
fileBroadcaster.fireRenameIndexNodeOssFile(code);
@ -207,7 +214,8 @@ public class IndexManager {
indexNodeDao.connectNodes(TreeUtils.collectValues(connectRoot));
IndexNode rootNode = connectRoot.getChildren().get(0).<IndexNode> asValueNode().getValue();
// resolve the name issue
indexSupport.incrNameIfDuplicate(rootNode);
if (!rootNode.scope().isChildrenNameDuplicatable())
indexSupport.incrNameIfDuplicate(rootNode);
// get the coped root with full props
return indexNodeDao.findOrNull(rootNode.getCode());
}

View File

@ -117,10 +117,6 @@ class IndexSupport {
indexNodeDao.rename(rename.getCode(), newName);
}
void ensureChildNameNotUsed(NodeCreate create, IndexNodeType nodeType) {
ensureChildNameNotUsed(create, nodeType, create.name());
}
void ensureChildNameNotUsed(IndexNodeParentScope parentScope, IndexNodeType nodeType, String childName) {
IndexNode child = indexNodeDao.findValidChildByName(parentScope, nodeType, childName);
if (child != null)