From 2b9e783f08fa074e91bb9b6a11e6da3ede6770cc Mon Sep 17 00:00:00 2001 From: Tglman Date: Thu, 2 Feb 2023 18:28:39 +0000 Subject: [PATCH] fix: avoid to re-index new cluster when swapping the cluster in views. --- .../core/db/viewmanager/ViewManager.java | 27 ++++++++++--------- .../core/exception/OTransactionException.java | 4 ++- .../core/metadata/schema/OClassImpl.java | 12 +++++++++ .../core/metadata/schema/OViewEmbedded.java | 2 +- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/core/src/main/java/com/orientechnologies/orient/core/db/viewmanager/ViewManager.java b/core/src/main/java/com/orientechnologies/orient/core/db/viewmanager/ViewManager.java index 8c438c3f49f..4092c3f2f32 100644 --- a/core/src/main/java/com/orientechnologies/orient/core/db/viewmanager/ViewManager.java +++ b/core/src/main/java/com/orientechnologies/orient/core/db/viewmanager/ViewManager.java @@ -115,7 +115,8 @@ public boolean registerLiveUpdateFor(ODatabaseSession db, String viewName) { OView view = db.getMetadata().getSchema().getView(viewName); viewsExist = true; boolean registered = false; - if (view.getUpdateStrategy() != null + if (view != null + && view.getUpdateStrategy() != null && view.getUpdateStrategy().equalsIgnoreCase(OViewConfig.UPDATE_STRATEGY_LIVE)) { db.live(view.getQuery(), new ViewUpdateListener(view.getName())); registered = true; @@ -199,7 +200,6 @@ public void cleanUnusedViewClusters(ODatabaseDocument db) { } } for (Integer cluster : clusters) { - System.out.println("dropping cluster " + cluster); db.dropCluster(cluster); viewCluserVisitors.remove(cluster); oldClustersPerViews.remove(cluster); @@ -329,23 +329,22 @@ public void updateViewInternal(OView view, ODatabaseDocumentInternal db) { // backup is running handle rebuild the next run return; } + String viewName = view.getName(); try { synchronized (this) { - if (refreshing.contains(view.getName())) { + if (refreshing.contains(viewName)) { return; } - refreshing.add(view.getName()); + refreshing.add(viewName); } - OLogManager.instance().info(this, "Starting refresh of view '%s'", view.getName()); - lastUpdateTimestampForView.put(view.getName(), System.currentTimeMillis()); + OLogManager.instance().info(this, "Starting refresh of view '%s'", viewName); + lastUpdateTimestampForView.put(viewName, System.currentTimeMillis()); String clusterName = createNextClusterNameFor(view, db); int cluster = db.getClusterIdByName(clusterName); - String viewName = view.getName(); String query = view.getQuery(); String originRidField = view.getOriginRidField(); - List indexes = createNewIndexesForView(view, cluster, db); try { @@ -355,9 +354,10 @@ public void updateViewInternal(OView view, ODatabaseDocumentInternal db) { db.getMetadata().getIndexManagerInternal().dropIndex(db, index.getName()); } db.dropCluster(cluster); + throw e; } - view = db.getMetadata().getSchema().getView(view.getName()); + view = db.getMetadata().getSchema().getView(viewName); if (view == null) { // the view was dropped in the meantime clustersToDrop.add(cluster); @@ -370,7 +370,7 @@ public void updateViewInternal(OView view, ODatabaseDocumentInternal db) { .warn( this, "Replacing for view '%s' clusters '%s' with '%s'", - view.getName(), + viewName, Arrays.stream(oldMetadata.getClusters()) .mapToObj((i) -> i + " => " + db.getClusterNameById(i)) .collect(Collectors.toList()) @@ -378,7 +378,7 @@ public void updateViewInternal(OView view, ODatabaseDocumentInternal db) { cluster + " => " + db.getClusterNameById(cluster)); for (int i : oldMetadata.getClusters()) { clustersToDrop.add(i); - oldClustersPerViews.put(i, view.getName()); + oldClustersPerViews.put(i, viewName); } oldMetadata @@ -389,9 +389,9 @@ public void updateViewInternal(OView view, ODatabaseDocumentInternal db) { }); cleanUnusedViewIndexes(db); cleanUnusedViewClusters(db); - OLogManager.instance().info(this, "Finished refresh of view '%s'", view.getName()); + OLogManager.instance().info(this, "Finished refresh of view '%s'", viewName); } finally { - refreshing.remove(view.getName()); + refreshing.remove(viewName); } } @@ -410,6 +410,7 @@ private void fillView( addItemToView(item, db, originRidField, viewName, clusterName, indexes); if (iterationCount % 100 == 0) { db.commit(); + db.begin(); } } } diff --git a/core/src/main/java/com/orientechnologies/orient/core/exception/OTransactionException.java b/core/src/main/java/com/orientechnologies/orient/core/exception/OTransactionException.java index 6472855eb4f..d087f497236 100755 --- a/core/src/main/java/com/orientechnologies/orient/core/exception/OTransactionException.java +++ b/core/src/main/java/com/orientechnologies/orient/core/exception/OTransactionException.java @@ -19,7 +19,9 @@ */ package com.orientechnologies.orient.core.exception; -public class OTransactionException extends OCoreException { +import com.orientechnologies.common.exception.OHighLevelException; + +public class OTransactionException extends OCoreException implements OHighLevelException { private static final long serialVersionUID = 2347493191705052402L; diff --git a/core/src/main/java/com/orientechnologies/orient/core/metadata/schema/OClassImpl.java b/core/src/main/java/com/orientechnologies/orient/core/metadata/schema/OClassImpl.java index 90d0b3354f3..08ec27117bd 100755 --- a/core/src/main/java/com/orientechnologies/orient/core/metadata/schema/OClassImpl.java +++ b/core/src/main/java/com/orientechnologies/orient/core/metadata/schema/OClassImpl.java @@ -1538,6 +1538,18 @@ protected void addPolymorphicClusterId(int clusterId) { } } + protected void onlyAddPolymorphicClusterId(int clusterId) { + if (Arrays.binarySearch(polymorphicClusterIds, clusterId) >= 0) return; + + polymorphicClusterIds = OArrays.copyOf(polymorphicClusterIds, polymorphicClusterIds.length + 1); + polymorphicClusterIds[polymorphicClusterIds.length - 1] = clusterId; + Arrays.sort(polymorphicClusterIds); + + for (OClassImpl superClass : superClasses) { + superClass.onlyAddPolymorphicClusterId(clusterId); + } + } + protected abstract OProperty addProperty( final String propertyName, final OType type, diff --git a/core/src/main/java/com/orientechnologies/orient/core/metadata/schema/OViewEmbedded.java b/core/src/main/java/com/orientechnologies/orient/core/metadata/schema/OViewEmbedded.java index e8b3901a2ce..d4264a4384f 100644 --- a/core/src/main/java/com/orientechnologies/orient/core/metadata/schema/OViewEmbedded.java +++ b/core/src/main/java/com/orientechnologies/orient/core/metadata/schema/OViewEmbedded.java @@ -139,7 +139,7 @@ protected OClass addClusterIdInternal(ODatabaseDocumentInternal database, final clusterIds[clusterIds.length - 1] = clusterId; Arrays.sort(clusterIds); - addPolymorphicClusterId(clusterId); + onlyAddPolymorphicClusterId(clusterId); if (defaultClusterId == NOT_EXISTENT_CLUSTER_ID) defaultClusterId = clusterId;