bhabegger commented on code in PR #3198:
URL: https://github.com/apache/jackrabbit-oak/pull/3198#discussion_r4216659460
##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/diff/DiffIndexMerger.java:
##########
@@ -242,8 +248,14 @@ boolean mergeDiff(JsonObject newImageLuceneDefinitions,
JsonObject combined) {
// collect the diff index(es)
HashMap<String, JsonObject> toProcess = new HashMap<>();
- tryExtractDiffIndex(combined, "/oak:index/" + DIFF_INDEX, toProcess);
- tryExtractDiffIndex(combined, "/oak:index/" + DIFF_INDEX_OPTIMIZER,
toProcess);
+ if (DiffIndex.isLegacyMode()) {
+ tryExtractDiffIndexLegacy(combined, "/oak:index/" + DIFF_INDEX,
toProcess);
+ tryExtractDiffIndexLegacy(combined, "/oak:index/" +
DIFF_INDEX_OPTIMIZER, toProcess);
+ } else if (tryExtractDiffIndex(combined, "/oak:index/" + DIFF_INDEX,
toProcess) != null
+ || tryExtractDiffIndex(combined, "/oak:index/" +
DIFF_INDEX_OPTIMIZER, toProcess) != null) {
+ // Abort on extraction errors to avoid treating missing entries as
removed customizations.
+ return false;
Review Comment:
And what happens then ?
##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/diff/DiffIndex.java:
##########
@@ -123,6 +172,93 @@ public static JsonObject collectDiffs(NodeBuilder
indexDefinitions, DiffIndexMer
return diffs;
}
+ /**
+ * Collect the diffs from the diff.index and diff.index.optimizer.
+ *
+ * @param indexDefinitions the node builder for /oak:index
+ * @param merger the merger instance to use for collecting warnings
+ * @return the complete snapshot, or null if absent, unchanged, or invalid
+ */
+ public static JsonObject collectDiffs(NodeBuilder indexDefinitions,
DiffIndexMerger merger) {
+ JsonObject result = new JsonObject(true);
+ List<String> readErrors = new ArrayList<>();
+ boolean wasModified = false;
+ for (String diffIndex : new String[] {
+ DiffIndexMerger.DIFF_INDEX,
+ DiffIndexMerger.DIFF_INDEX_OPTIMIZER }) {
+ String path = "/oak:index/" + diffIndex;
+ JsonObject files = new JsonObject(true);
+ result.getChildren().put(path, files);
+ if (!indexDefinitions.hasChildNode(diffIndex)) {
+ // Override the old repository snapshot when a diff index was
removed.
+ files.getChildren().put("diff", new JsonObject(true));
+ continue;
+ }
+ NodeBuilder diffIndexDefinition =
indexDefinitions.child(diffIndex);
+ List<String> sortedChildren = StreamSupport.stream(
+ diffIndexDefinition.getChildNodeNames().spliterator(),
false)
+ .sorted().collect(Collectors.toList());
+ if (!diffIndexDefinition.hasProperty("info")) {
+ diffIndexDefinition.setProperty("info", "This diff is
automatically merged with other indexes. See
https://oak-indexing.github.io/oakTools/simplified.html");
+ }
+ for (String child : sortedChildren) {
+ String filePath = path + "/" + child;
+ NodeBuilder diffContent =
diffIndexDefinition.getChildNode(child).getChildNode("jcr:content");
Review Comment:
Maybe we could move this into an independent method ? (It would also have
been nice to "see" that this part of the code is the same as the legacy somehow
:) )
##########
oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/diff/DiffIndexLegacyTest.java:
##########
@@ -0,0 +1,272 @@
+/*
+ * 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.jackrabbit.oak.plugins.index.diff;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.lang.reflect.Method;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.jackrabbit.oak.api.Type;
+import org.apache.jackrabbit.oak.commons.json.JsonObject;
+import org.apache.jackrabbit.oak.commons.json.JsopBuilder;
+import org.apache.jackrabbit.oak.plugins.memory.BinaryPropertyState;
+import org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore;
+import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
+import org.apache.jackrabbit.oak.spi.state.NodeStore;
+import org.apache.jackrabbit.oak.spi.toggle.FeatureToggle;
+import org.apache.sling.testing.mock.osgi.MockOsgi;
+import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
+import org.junit.Rule;
+import org.junit.Test;
+
+public class DiffIndexLegacyTest {
+
+ @Rule
+ public final OsgiContext context = new OsgiContext();
+
+ @Test
+ public void featureToggleSelectsLegacyAndNewCollectors() {
+ DiffIndex component = new DiffIndex();
+ MockOsgi.activate(component, context.bundleContext(), Map.of());
+ try {
+ FeatureToggle toggle = context.getService(FeatureToggle.class);
+ assertNotNull(toggle);
+ assertEquals(DiffIndex.LEGACY_DIFF_INDEX_TOGGLE, toggle.getName());
+ assertFalse(DiffIndex.isLegacyMode());
+ assertProcessedFiles(false);
+ toggle.setEnabled(true);
+ assertTrue(DiffIndex.isLegacyMode());
+ assertProcessedFiles(true);
+ toggle.setEnabled(false);
+ assertFalse(DiffIndex.isLegacyMode());
+ assertProcessedFiles(false);
+ } finally {
+ MockOsgi.deactivate(component, context.bundleContext());
+ }
+ assertNull(context.getService(FeatureToggle.class));
+ assertFalse(DiffIndex.isLegacyMode());
+ }
+
+ @Test
+ public void legacyExtractionIgnoresAdditionalJsonFilesAndFileReferences() {
+ DiffIndex component = new DiffIndex();
+ MockOsgi.activate(component, context.bundleContext(), Map.of());
+ try {
+ context.getService(FeatureToggle.class).setEnabled(true);
+ JsonObject definitions = JsonObject.fromJson("""
+ {
+ "/oak:index/diff.index": {
+ "additional.json": {},
+ "diff": {
+ "acme.test": { "jcr:data": ":file:missing.txt"
}
+ }
+ }
+ }
+ """, true);
+ HashMap<String, JsonObject> target = new HashMap<>();
+ assertNull(new DiffIndexMerger().tryExtractDiffIndex(definitions,
"/oak:index/diff.index", target));
+ assertEquals("\":file:missing.txt\"",
target.get("acme.test").getProperties().get("jcr:data"));
+ context.getService(FeatureToggle.class).setEnabled(false);
+ assertNotNull(new DiffIndexMerger().tryExtractDiffIndex(
+ definitions, "/oak:index/diff.index", new HashMap<>()));
+ } finally {
+ MockOsgi.deactivate(component, context.bundleContext());
+ }
+ }
+
+ @Test
+ public void legacyExtractorReadsOnlyDiffJsonAndReportsMalformedFiles() {
+ String path = "/oak:index/diff.index";
+ JsonObject definitions = new JsonObject(true);
+ JsonObject index = new JsonObject(true);
+ JsonObject file = new JsonObject(true);
+ JsonObject content = new JsonObject(true);
+ definitions.getChildren().put(path, index);
+ index.getChildren().put("diff.json", file);
+ file.getChildren().put("jcr:content", content);
+ content.getProperties().put("jcr:data",
JsopBuilder.encode("{\"acme.test\":{\"type\":\"lucene\"}}"));
+ DiffIndexMerger merger = new DiffIndexMerger();
+ HashMap<String, JsonObject> target = new HashMap<>();
+ assertNull(merger.tryExtractDiffIndexLegacy(definitions, path,
target));
+ assertEquals("\"lucene\"",
target.get("acme.test").getProperties().get("type"));
+ file.getChildren().clear();
+ assertEquals("jcr:content child node is missing in diff.json",
+ merger.tryExtractDiffIndexLegacy(definitions, path, new
HashMap<>()));
+ file.getChildren().put("jcr:content", content);
+ content.getProperties().put("jcr:data", JsopBuilder.encode("{broken"));
+ assertTrue(merger.tryExtractDiffIndexLegacy(definitions, path, new
HashMap<>()).startsWith("Illegal Json"));
+ assertEquals(2, merger.getAndClearWarnings().size());
+ index.getChildren().clear();
+ assertNull(merger.tryExtractDiffIndexLegacy(definitions, path, new
HashMap<>()));
+ }
+
+ @Test
+ public void legacyModePreservesCustomerVersionNumbering() {
+ DiffIndex component = new DiffIndex();
+ MockOsgi.activate(component, context.bundleContext(), Map.of());
+ try {
+ JsonObject repository = JsonObject.fromJson("""
+ {
+ "/oak:index/product-2": { "type": "lucene" },
+ "/oak:index/product-1-custom-3": {
+ "type": "lucene", "mergeInfo": "previous
customization"
+ }
+ }
+ """, true);
+ for (boolean legacy : new boolean[] {false, true}) {
+ context.getService(FeatureToggle.class).setEnabled(legacy);
+ JsonObject definitions = JsonObject.fromJson("""
+ {
+ "/oak:index/diff.index": {
+ "diff": { "product": { "additional": true } }
+ }
+ }
+ """, true);
+ new DiffIndexMerger().merge(definitions, repository, null);
+ String expected = "/oak:index/product-2-custom-" + (legacy ?
"4" : "1");
+ assertNotNull(definitions.getChildren().get(expected));
+ }
+ } finally {
+ MockOsgi.deactivate(component, context.bundleContext());
+ }
+ }
+
+ @Test
+ public void legacyCollectorSkipsMissingAndUnchangedData() {
Review Comment:
Maybe add some spacing for the reader to be able to see the intents of the
different tests ?
##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/diff/DiffIndexMerger.java:
##########
@@ -537,7 +657,13 @@ public boolean processMerge(String indexName, JsonObject
indexDiff, JsonObject n
key = prefix + indexName + "-" + productVersion +
"-custom-";
}
if (latestCustomized != null) {
- key += (latestCustomized.getCustomerVersion() + 1);
+ int nextCustomer;
Review Comment:
```suggestion
int nextCustomerVersion;
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]