fabriziofortino commented on code in PR #1123: URL: https://github.com/apache/jackrabbit-oak/pull/1123#discussion_r1335595549
########## oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/incrementalstore/MergeIncrementalFlatFileStore.java: ########## @@ -0,0 +1,220 @@ +/* + * 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.index.indexer.document.incrementalstore; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.jackrabbit.oak.commons.Compression; +import org.apache.jackrabbit.oak.index.indexer.document.flatfile.NodeStateHolder; +import org.apache.jackrabbit.oak.index.indexer.document.flatfile.PathElementComparator; +import org.apache.jackrabbit.oak.index.indexer.document.flatfile.SimpleNodeStateHolder; +import org.apache.jackrabbit.oak.index.indexer.document.indexstore.IndexStoreMetadata; +import org.apache.jackrabbit.oak.index.indexer.document.indexstore.IndexStoreMetadataOperatorImpl; +import org.apache.jackrabbit.oak.index.indexer.document.indexstore.IndexStoreUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.Comparator; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.apache.jackrabbit.guava.common.base.Preconditions.checkState; + + +public class MergeIncrementalFlatFileStore implements MergeIncrementalStore { + + private final Logger log = LoggerFactory.getLogger(getClass()); + private final static String MERGE_BASE_AND_INCREMENTAL_FLAT_FILE_STORE = "MergeBaseAndIncrementalFlatFileStore"; + private final File baseFFS; + private final File incrementalFFS; + private final File merged; + private final Compression algorithm; + private final Comparator<NodeStateHolder> comparator; + + public MergeIncrementalFlatFileStore(Set<String> preferredPathElements, File baseFFS, File incrementalFFS, + File merged, Compression algorithm) throws IOException { + this.baseFFS = baseFFS; + this.incrementalFFS = incrementalFFS; + this.merged = merged; + this.algorithm = algorithm; + comparator = (e1, e2) -> new PathElementComparator(preferredPathElements).compare(e1.getPathElements(), e2.getPathElements()); + + if (merged.exists()) { + log.warn("merged file:{} exists, this file will be replaced with new mergedFFS file", merged.getAbsolutePath()); + } else { + Files.createDirectories(merged.getParentFile().toPath()); + } + IndexStoreUtils.validateFlatFileStoreFileName(merged, algorithm); + basicFileValidation(algorithm, baseFFS, incrementalFFS); + } + + @Override + public void doMerge() throws IOException { + log.info("base FFS " + baseFFS.getAbsolutePath()); + log.info("incremental FFS " + incrementalFFS.getAbsolutePath()); + log.info("merged FFS " + merged.getAbsolutePath()); + mergeMetadataFiles(); + mergeIndexStoreFiles(); + } + + @Override + public String getStrategyName() { + return MERGE_BASE_AND_INCREMENTAL_FLAT_FILE_STORE; + } + + private void basicFileValidation(Compression algorithm, File... files) { + for (File file : files) { + checkState(file.isFile(), "File doesn't exist {}", file.getAbsolutePath()); + IndexStoreUtils.validateFlatFileStoreFileName(file, algorithm); + checkState(IndexStoreUtils.getMetadataFile(file, algorithm).exists(), + "metadata file is not present in same directory as indexStore. indexStoreFile:{}, metadataFile should be available at:{}", + file.getAbsolutePath(), IndexStoreUtils.getMetadataFile(file, algorithm)); + } + } + + private void mergeIndexStoreFiles() throws IOException { + Map<String, IncrementalStoreOperand> enumMap = Arrays.stream(IncrementalStoreOperand.values()) + .collect(Collectors.toUnmodifiableMap(IncrementalStoreOperand::toString, k -> IncrementalStoreOperand.valueOf(k.name()))); + + try (BufferedWriter writer = IndexStoreUtils.createWriter(merged, algorithm); + BufferedReader baseFFSBufferedReader = IndexStoreUtils.createReader(baseFFS, algorithm); + BufferedReader incrementalFFSBufferedReader = IndexStoreUtils.createReader(incrementalFFS, algorithm)) { + String baseFFSLine = baseFFSBufferedReader.readLine(); + String incrementalFFSLine = incrementalFFSBufferedReader.readLine(); + + + int compared; + while (baseFFSLine != null || incrementalFFSLine != null) { + if (baseFFSLine != null && incrementalFFSLine != null) { + compared = comparator.compare(new SimpleNodeStateHolder(baseFFSLine), new SimpleNodeStateHolder(incrementalFFSLine)); + if (compared < 0) { // write baseFFSLine in merged file and advance line in baseFFS + baseFFSLine = writeAndAdvance(writer, baseFFSBufferedReader, baseFFSLine); + } else if (compared > 0) { // write incrementalFFSline and advance line in incrementalFFS + String[] incrementalFFSParts = IncrementalFlatFileStoreNodeStateEntryWriter.getParts(incrementalFFSLine); + checkState(IncrementalStoreOperand.ADD.toString().equals(getOperand(incrementalFFSParts))); + incrementalFFSLine = writeAndAdvance(writer, incrementalFFSBufferedReader, + getFFSLineFromIncrementalFFSParts(incrementalFFSParts)); + } else { + String[] incrementalFFSParts = IncrementalFlatFileStoreNodeStateEntryWriter.getParts(incrementalFFSLine); + String operand = getOperand(incrementalFFSParts); + switch (enumMap.get(operand)) { + case MODIFY: + incrementalFFSLine = writeAndAdvance(writer, incrementalFFSBufferedReader, + getFFSLineFromIncrementalFFSParts(incrementalFFSParts)); + break; + case DELETE: + incrementalFFSLine = incrementalFFSBufferedReader.readLine(); + break; + default: + log.error("wrong operand in incremental ffs: operand:{}, line:{}", operand, incrementalFFSLine); + throw new RuntimeException("wrong operand in incremental ffs: operand:" + operand + ", line:" + incrementalFFSLine); + } + baseFFSLine = baseFFSBufferedReader.readLine(); + } + } else { + if (incrementalFFSLine == null) { + baseFFSLine = writeRestOfFFSFileAndAdvance(writer, baseFFSBufferedReader, baseFFSLine); + } else { + incrementalFFSLine = writeRestOfIncrementalFileAndAdvance(writer, incrementalFFSBufferedReader, incrementalFFSLine); + } + } + } + } + } + + private IndexStoreMetadata getIndexStoreMetadataForMergedFile() throws IOException { + File baseFFSMetadataFile = IndexStoreUtils.getMetadataFile(baseFFS, algorithm); + File incrementalMetadataFile = IndexStoreUtils.getMetadataFile(incrementalFFS, algorithm); + + if (baseFFSMetadataFile.exists() && incrementalMetadataFile.exists()) { + IndexStoreMetadata indexStoreMetadata = new IndexStoreMetadataOperatorImpl<IndexStoreMetadata>() + .getIndexStoreMetadata(baseFFSMetadataFile, algorithm, new TypeReference<IndexStoreMetadata>() { Review Comment: the explicit type can be removed ```suggestion .getIndexStoreMetadata(baseFFSMetadataFile, algorithm, new TypeReference<>() { ``` ########## oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/indexstore/IndexStoreMetadataOperatorImpl.java: ########## @@ -0,0 +1,70 @@ +/* + * 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.index.indexer.document.indexstore; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.jackrabbit.oak.commons.Compression; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.IOException; + +public class IndexStoreMetadataOperatorImpl<M> implements IndexStoreMetadataOperator<M> { + private static final Logger log = LoggerFactory.getLogger(IndexStoreMetadataOperatorImpl.class); + + @Override + public File createMetadataFile(M m, File file, Compression algorithm) throws IOException { + File metadataFile; + if (file.isDirectory()) { + metadataFile = new File(file, IndexStoreUtils.getMetadataFileName(algorithm)); + } else { + metadataFile = file; + } + + try (BufferedWriter metadataWriter = IndexStoreUtils.createWriter(metadataFile, algorithm)) { + writeMetadataToFile(metadataWriter, m); + } + log.info("Created metadataFile:{} ", metadataFile.getPath()); + return metadataFile; + } + + private void writeMetadataToFile(BufferedWriter bufferedWriter, M m) throws IOException { + ObjectMapper mapper = new ObjectMapper(); + mapper.writeValue(bufferedWriter, m); + } + + /* + deserialization with generic type doesnot happen automatically. So we explicitly added this 3rd argument to Review Comment: typo ```suggestion deserialization with generic type does not happen automatically. So we explicitly added this 3rd argument to ``` ########## oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/incrementalstore/MergeIncrementalFlatFileStore.java: ########## @@ -0,0 +1,220 @@ +/* + * 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.index.indexer.document.incrementalstore; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.jackrabbit.oak.commons.Compression; +import org.apache.jackrabbit.oak.index.indexer.document.flatfile.NodeStateHolder; +import org.apache.jackrabbit.oak.index.indexer.document.flatfile.PathElementComparator; +import org.apache.jackrabbit.oak.index.indexer.document.flatfile.SimpleNodeStateHolder; +import org.apache.jackrabbit.oak.index.indexer.document.indexstore.IndexStoreMetadata; +import org.apache.jackrabbit.oak.index.indexer.document.indexstore.IndexStoreMetadataOperatorImpl; +import org.apache.jackrabbit.oak.index.indexer.document.indexstore.IndexStoreUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.Comparator; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.apache.jackrabbit.guava.common.base.Preconditions.checkState; + + +public class MergeIncrementalFlatFileStore implements MergeIncrementalStore { + + private final Logger log = LoggerFactory.getLogger(getClass()); + private final static String MERGE_BASE_AND_INCREMENTAL_FLAT_FILE_STORE = "MergeBaseAndIncrementalFlatFileStore"; + private final File baseFFS; + private final File incrementalFFS; + private final File merged; + private final Compression algorithm; + private final Comparator<NodeStateHolder> comparator; + + public MergeIncrementalFlatFileStore(Set<String> preferredPathElements, File baseFFS, File incrementalFFS, + File merged, Compression algorithm) throws IOException { + this.baseFFS = baseFFS; + this.incrementalFFS = incrementalFFS; + this.merged = merged; + this.algorithm = algorithm; + comparator = (e1, e2) -> new PathElementComparator(preferredPathElements).compare(e1.getPathElements(), e2.getPathElements()); + + if (merged.exists()) { + log.warn("merged file:{} exists, this file will be replaced with new mergedFFS file", merged.getAbsolutePath()); + } else { + Files.createDirectories(merged.getParentFile().toPath()); + } + IndexStoreUtils.validateFlatFileStoreFileName(merged, algorithm); + basicFileValidation(algorithm, baseFFS, incrementalFFS); + } + + @Override + public void doMerge() throws IOException { + log.info("base FFS " + baseFFS.getAbsolutePath()); + log.info("incremental FFS " + incrementalFFS.getAbsolutePath()); + log.info("merged FFS " + merged.getAbsolutePath()); + mergeMetadataFiles(); + mergeIndexStoreFiles(); + } + + @Override + public String getStrategyName() { + return MERGE_BASE_AND_INCREMENTAL_FLAT_FILE_STORE; + } + + private void basicFileValidation(Compression algorithm, File... files) { + for (File file : files) { + checkState(file.isFile(), "File doesn't exist {}", file.getAbsolutePath()); + IndexStoreUtils.validateFlatFileStoreFileName(file, algorithm); + checkState(IndexStoreUtils.getMetadataFile(file, algorithm).exists(), + "metadata file is not present in same directory as indexStore. indexStoreFile:{}, metadataFile should be available at:{}", + file.getAbsolutePath(), IndexStoreUtils.getMetadataFile(file, algorithm)); + } + } + + private void mergeIndexStoreFiles() throws IOException { + Map<String, IncrementalStoreOperand> enumMap = Arrays.stream(IncrementalStoreOperand.values()) + .collect(Collectors.toUnmodifiableMap(IncrementalStoreOperand::toString, k -> IncrementalStoreOperand.valueOf(k.name()))); + + try (BufferedWriter writer = IndexStoreUtils.createWriter(merged, algorithm); + BufferedReader baseFFSBufferedReader = IndexStoreUtils.createReader(baseFFS, algorithm); + BufferedReader incrementalFFSBufferedReader = IndexStoreUtils.createReader(incrementalFFS, algorithm)) { + String baseFFSLine = baseFFSBufferedReader.readLine(); + String incrementalFFSLine = incrementalFFSBufferedReader.readLine(); + + + int compared; + while (baseFFSLine != null || incrementalFFSLine != null) { + if (baseFFSLine != null && incrementalFFSLine != null) { + compared = comparator.compare(new SimpleNodeStateHolder(baseFFSLine), new SimpleNodeStateHolder(incrementalFFSLine)); + if (compared < 0) { // write baseFFSLine in merged file and advance line in baseFFS + baseFFSLine = writeAndAdvance(writer, baseFFSBufferedReader, baseFFSLine); + } else if (compared > 0) { // write incrementalFFSline and advance line in incrementalFFS + String[] incrementalFFSParts = IncrementalFlatFileStoreNodeStateEntryWriter.getParts(incrementalFFSLine); + checkState(IncrementalStoreOperand.ADD.toString().equals(getOperand(incrementalFFSParts))); + incrementalFFSLine = writeAndAdvance(writer, incrementalFFSBufferedReader, + getFFSLineFromIncrementalFFSParts(incrementalFFSParts)); + } else { + String[] incrementalFFSParts = IncrementalFlatFileStoreNodeStateEntryWriter.getParts(incrementalFFSLine); + String operand = getOperand(incrementalFFSParts); + switch (enumMap.get(operand)) { + case MODIFY: + incrementalFFSLine = writeAndAdvance(writer, incrementalFFSBufferedReader, + getFFSLineFromIncrementalFFSParts(incrementalFFSParts)); + break; + case DELETE: + incrementalFFSLine = incrementalFFSBufferedReader.readLine(); + break; + default: + log.error("wrong operand in incremental ffs: operand:{}, line:{}", operand, incrementalFFSLine); + throw new RuntimeException("wrong operand in incremental ffs: operand:" + operand + ", line:" + incrementalFFSLine); + } + baseFFSLine = baseFFSBufferedReader.readLine(); + } + } else { + if (incrementalFFSLine == null) { + baseFFSLine = writeRestOfFFSFileAndAdvance(writer, baseFFSBufferedReader, baseFFSLine); + } else { + incrementalFFSLine = writeRestOfIncrementalFileAndAdvance(writer, incrementalFFSBufferedReader, incrementalFFSLine); + } + } + } + } + } + + private IndexStoreMetadata getIndexStoreMetadataForMergedFile() throws IOException { + File baseFFSMetadataFile = IndexStoreUtils.getMetadataFile(baseFFS, algorithm); + File incrementalMetadataFile = IndexStoreUtils.getMetadataFile(incrementalFFS, algorithm); + + if (baseFFSMetadataFile.exists() && incrementalMetadataFile.exists()) { + IndexStoreMetadata indexStoreMetadata = new IndexStoreMetadataOperatorImpl<IndexStoreMetadata>() + .getIndexStoreMetadata(baseFFSMetadataFile, algorithm, new TypeReference<IndexStoreMetadata>() { + }); + IncrementalIndexStoreMetadata incrementalIndexStoreMetadata = new IndexStoreMetadataOperatorImpl<IncrementalIndexStoreMetadata>() + .getIndexStoreMetadata(incrementalMetadataFile, algorithm, new TypeReference<IncrementalIndexStoreMetadata>() { + }); + return mergeIndexStores(indexStoreMetadata, incrementalIndexStoreMetadata); + } else { + throw new RuntimeException("either one or both metadataFiles donot exist at path: " + Review Comment: typo ```suggestion throw new RuntimeException("either one or both metadataFiles do not exist at path: " + ``` ########## oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/incrementalstore/MergeIncrementalFlatFileStore.java: ########## @@ -0,0 +1,220 @@ +/* + * 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.index.indexer.document.incrementalstore; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.jackrabbit.oak.commons.Compression; +import org.apache.jackrabbit.oak.index.indexer.document.flatfile.NodeStateHolder; +import org.apache.jackrabbit.oak.index.indexer.document.flatfile.PathElementComparator; +import org.apache.jackrabbit.oak.index.indexer.document.flatfile.SimpleNodeStateHolder; +import org.apache.jackrabbit.oak.index.indexer.document.indexstore.IndexStoreMetadata; +import org.apache.jackrabbit.oak.index.indexer.document.indexstore.IndexStoreMetadataOperatorImpl; +import org.apache.jackrabbit.oak.index.indexer.document.indexstore.IndexStoreUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.Comparator; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.apache.jackrabbit.guava.common.base.Preconditions.checkState; + + +public class MergeIncrementalFlatFileStore implements MergeIncrementalStore { + + private final Logger log = LoggerFactory.getLogger(getClass()); + private final static String MERGE_BASE_AND_INCREMENTAL_FLAT_FILE_STORE = "MergeBaseAndIncrementalFlatFileStore"; + private final File baseFFS; + private final File incrementalFFS; + private final File merged; + private final Compression algorithm; + private final Comparator<NodeStateHolder> comparator; + + public MergeIncrementalFlatFileStore(Set<String> preferredPathElements, File baseFFS, File incrementalFFS, + File merged, Compression algorithm) throws IOException { + this.baseFFS = baseFFS; + this.incrementalFFS = incrementalFFS; + this.merged = merged; + this.algorithm = algorithm; + comparator = (e1, e2) -> new PathElementComparator(preferredPathElements).compare(e1.getPathElements(), e2.getPathElements()); + + if (merged.exists()) { + log.warn("merged file:{} exists, this file will be replaced with new mergedFFS file", merged.getAbsolutePath()); + } else { + Files.createDirectories(merged.getParentFile().toPath()); + } + IndexStoreUtils.validateFlatFileStoreFileName(merged, algorithm); + basicFileValidation(algorithm, baseFFS, incrementalFFS); + } + + @Override + public void doMerge() throws IOException { + log.info("base FFS " + baseFFS.getAbsolutePath()); + log.info("incremental FFS " + incrementalFFS.getAbsolutePath()); + log.info("merged FFS " + merged.getAbsolutePath()); + mergeMetadataFiles(); + mergeIndexStoreFiles(); + } + + @Override + public String getStrategyName() { + return MERGE_BASE_AND_INCREMENTAL_FLAT_FILE_STORE; + } + + private void basicFileValidation(Compression algorithm, File... files) { + for (File file : files) { + checkState(file.isFile(), "File doesn't exist {}", file.getAbsolutePath()); + IndexStoreUtils.validateFlatFileStoreFileName(file, algorithm); + checkState(IndexStoreUtils.getMetadataFile(file, algorithm).exists(), + "metadata file is not present in same directory as indexStore. indexStoreFile:{}, metadataFile should be available at:{}", + file.getAbsolutePath(), IndexStoreUtils.getMetadataFile(file, algorithm)); + } + } + + private void mergeIndexStoreFiles() throws IOException { + Map<String, IncrementalStoreOperand> enumMap = Arrays.stream(IncrementalStoreOperand.values()) + .collect(Collectors.toUnmodifiableMap(IncrementalStoreOperand::toString, k -> IncrementalStoreOperand.valueOf(k.name()))); + + try (BufferedWriter writer = IndexStoreUtils.createWriter(merged, algorithm); + BufferedReader baseFFSBufferedReader = IndexStoreUtils.createReader(baseFFS, algorithm); + BufferedReader incrementalFFSBufferedReader = IndexStoreUtils.createReader(incrementalFFS, algorithm)) { + String baseFFSLine = baseFFSBufferedReader.readLine(); + String incrementalFFSLine = incrementalFFSBufferedReader.readLine(); + + + int compared; + while (baseFFSLine != null || incrementalFFSLine != null) { + if (baseFFSLine != null && incrementalFFSLine != null) { + compared = comparator.compare(new SimpleNodeStateHolder(baseFFSLine), new SimpleNodeStateHolder(incrementalFFSLine)); + if (compared < 0) { // write baseFFSLine in merged file and advance line in baseFFS + baseFFSLine = writeAndAdvance(writer, baseFFSBufferedReader, baseFFSLine); + } else if (compared > 0) { // write incrementalFFSline and advance line in incrementalFFS + String[] incrementalFFSParts = IncrementalFlatFileStoreNodeStateEntryWriter.getParts(incrementalFFSLine); + checkState(IncrementalStoreOperand.ADD.toString().equals(getOperand(incrementalFFSParts))); + incrementalFFSLine = writeAndAdvance(writer, incrementalFFSBufferedReader, + getFFSLineFromIncrementalFFSParts(incrementalFFSParts)); + } else { + String[] incrementalFFSParts = IncrementalFlatFileStoreNodeStateEntryWriter.getParts(incrementalFFSLine); + String operand = getOperand(incrementalFFSParts); + switch (enumMap.get(operand)) { + case MODIFY: + incrementalFFSLine = writeAndAdvance(writer, incrementalFFSBufferedReader, + getFFSLineFromIncrementalFFSParts(incrementalFFSParts)); + break; + case DELETE: + incrementalFFSLine = incrementalFFSBufferedReader.readLine(); + break; + default: + log.error("wrong operand in incremental ffs: operand:{}, line:{}", operand, incrementalFFSLine); + throw new RuntimeException("wrong operand in incremental ffs: operand:" + operand + ", line:" + incrementalFFSLine); + } + baseFFSLine = baseFFSBufferedReader.readLine(); + } + } else { + if (incrementalFFSLine == null) { + baseFFSLine = writeRestOfFFSFileAndAdvance(writer, baseFFSBufferedReader, baseFFSLine); + } else { + incrementalFFSLine = writeRestOfIncrementalFileAndAdvance(writer, incrementalFFSBufferedReader, incrementalFFSLine); + } + } + } + } + } + + private IndexStoreMetadata getIndexStoreMetadataForMergedFile() throws IOException { + File baseFFSMetadataFile = IndexStoreUtils.getMetadataFile(baseFFS, algorithm); + File incrementalMetadataFile = IndexStoreUtils.getMetadataFile(incrementalFFS, algorithm); + + if (baseFFSMetadataFile.exists() && incrementalMetadataFile.exists()) { + IndexStoreMetadata indexStoreMetadata = new IndexStoreMetadataOperatorImpl<IndexStoreMetadata>() + .getIndexStoreMetadata(baseFFSMetadataFile, algorithm, new TypeReference<IndexStoreMetadata>() { + }); + IncrementalIndexStoreMetadata incrementalIndexStoreMetadata = new IndexStoreMetadataOperatorImpl<IncrementalIndexStoreMetadata>() + .getIndexStoreMetadata(incrementalMetadataFile, algorithm, new TypeReference<IncrementalIndexStoreMetadata>() { Review Comment: the explicit type can be removed ```suggestion .getIndexStoreMetadata(incrementalMetadataFile, algorithm, new TypeReference<>() { ``` ########## oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/incrementalstore/MergeIncrementalFlatFileStore.java: ########## @@ -0,0 +1,220 @@ +/* + * 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.index.indexer.document.incrementalstore; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.jackrabbit.oak.commons.Compression; +import org.apache.jackrabbit.oak.index.indexer.document.flatfile.NodeStateHolder; +import org.apache.jackrabbit.oak.index.indexer.document.flatfile.PathElementComparator; +import org.apache.jackrabbit.oak.index.indexer.document.flatfile.SimpleNodeStateHolder; +import org.apache.jackrabbit.oak.index.indexer.document.indexstore.IndexStoreMetadata; +import org.apache.jackrabbit.oak.index.indexer.document.indexstore.IndexStoreMetadataOperatorImpl; +import org.apache.jackrabbit.oak.index.indexer.document.indexstore.IndexStoreUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.Comparator; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.apache.jackrabbit.guava.common.base.Preconditions.checkState; + + +public class MergeIncrementalFlatFileStore implements MergeIncrementalStore { + + private final Logger log = LoggerFactory.getLogger(getClass()); + private final static String MERGE_BASE_AND_INCREMENTAL_FLAT_FILE_STORE = "MergeBaseAndIncrementalFlatFileStore"; + private final File baseFFS; + private final File incrementalFFS; + private final File merged; + private final Compression algorithm; + private final Comparator<NodeStateHolder> comparator; + + public MergeIncrementalFlatFileStore(Set<String> preferredPathElements, File baseFFS, File incrementalFFS, + File merged, Compression algorithm) throws IOException { + this.baseFFS = baseFFS; + this.incrementalFFS = incrementalFFS; + this.merged = merged; + this.algorithm = algorithm; + comparator = (e1, e2) -> new PathElementComparator(preferredPathElements).compare(e1.getPathElements(), e2.getPathElements()); + + if (merged.exists()) { + log.warn("merged file:{} exists, this file will be replaced with new mergedFFS file", merged.getAbsolutePath()); + } else { + Files.createDirectories(merged.getParentFile().toPath()); + } + IndexStoreUtils.validateFlatFileStoreFileName(merged, algorithm); + basicFileValidation(algorithm, baseFFS, incrementalFFS); + } + + @Override + public void doMerge() throws IOException { + log.info("base FFS " + baseFFS.getAbsolutePath()); + log.info("incremental FFS " + incrementalFFS.getAbsolutePath()); + log.info("merged FFS " + merged.getAbsolutePath()); + mergeMetadataFiles(); + mergeIndexStoreFiles(); + } + + @Override + public String getStrategyName() { + return MERGE_BASE_AND_INCREMENTAL_FLAT_FILE_STORE; + } + + private void basicFileValidation(Compression algorithm, File... files) { + for (File file : files) { + checkState(file.isFile(), "File doesn't exist {}", file.getAbsolutePath()); + IndexStoreUtils.validateFlatFileStoreFileName(file, algorithm); + checkState(IndexStoreUtils.getMetadataFile(file, algorithm).exists(), + "metadata file is not present in same directory as indexStore. indexStoreFile:{}, metadataFile should be available at:{}", + file.getAbsolutePath(), IndexStoreUtils.getMetadataFile(file, algorithm)); + } + } + + private void mergeIndexStoreFiles() throws IOException { + Map<String, IncrementalStoreOperand> enumMap = Arrays.stream(IncrementalStoreOperand.values()) + .collect(Collectors.toUnmodifiableMap(IncrementalStoreOperand::toString, k -> IncrementalStoreOperand.valueOf(k.name()))); + + try (BufferedWriter writer = IndexStoreUtils.createWriter(merged, algorithm); + BufferedReader baseFFSBufferedReader = IndexStoreUtils.createReader(baseFFS, algorithm); + BufferedReader incrementalFFSBufferedReader = IndexStoreUtils.createReader(incrementalFFS, algorithm)) { + String baseFFSLine = baseFFSBufferedReader.readLine(); + String incrementalFFSLine = incrementalFFSBufferedReader.readLine(); + + + int compared; + while (baseFFSLine != null || incrementalFFSLine != null) { + if (baseFFSLine != null && incrementalFFSLine != null) { + compared = comparator.compare(new SimpleNodeStateHolder(baseFFSLine), new SimpleNodeStateHolder(incrementalFFSLine)); + if (compared < 0) { // write baseFFSLine in merged file and advance line in baseFFS + baseFFSLine = writeAndAdvance(writer, baseFFSBufferedReader, baseFFSLine); + } else if (compared > 0) { // write incrementalFFSline and advance line in incrementalFFS + String[] incrementalFFSParts = IncrementalFlatFileStoreNodeStateEntryWriter.getParts(incrementalFFSLine); + checkState(IncrementalStoreOperand.ADD.toString().equals(getOperand(incrementalFFSParts))); + incrementalFFSLine = writeAndAdvance(writer, incrementalFFSBufferedReader, + getFFSLineFromIncrementalFFSParts(incrementalFFSParts)); + } else { + String[] incrementalFFSParts = IncrementalFlatFileStoreNodeStateEntryWriter.getParts(incrementalFFSLine); + String operand = getOperand(incrementalFFSParts); + switch (enumMap.get(operand)) { + case MODIFY: + incrementalFFSLine = writeAndAdvance(writer, incrementalFFSBufferedReader, + getFFSLineFromIncrementalFFSParts(incrementalFFSParts)); + break; + case DELETE: + incrementalFFSLine = incrementalFFSBufferedReader.readLine(); + break; + default: + log.error("wrong operand in incremental ffs: operand:{}, line:{}", operand, incrementalFFSLine); + throw new RuntimeException("wrong operand in incremental ffs: operand:" + operand + ", line:" + incrementalFFSLine); + } + baseFFSLine = baseFFSBufferedReader.readLine(); + } + } else { + if (incrementalFFSLine == null) { + baseFFSLine = writeRestOfFFSFileAndAdvance(writer, baseFFSBufferedReader, baseFFSLine); + } else { + incrementalFFSLine = writeRestOfIncrementalFileAndAdvance(writer, incrementalFFSBufferedReader, incrementalFFSLine); + } + } + } + } + } + + private IndexStoreMetadata getIndexStoreMetadataForMergedFile() throws IOException { + File baseFFSMetadataFile = IndexStoreUtils.getMetadataFile(baseFFS, algorithm); + File incrementalMetadataFile = IndexStoreUtils.getMetadataFile(incrementalFFS, algorithm); + + if (baseFFSMetadataFile.exists() && incrementalMetadataFile.exists()) { + IndexStoreMetadata indexStoreMetadata = new IndexStoreMetadataOperatorImpl<IndexStoreMetadata>() + .getIndexStoreMetadata(baseFFSMetadataFile, algorithm, new TypeReference<IndexStoreMetadata>() { + }); + IncrementalIndexStoreMetadata incrementalIndexStoreMetadata = new IndexStoreMetadataOperatorImpl<IncrementalIndexStoreMetadata>() + .getIndexStoreMetadata(incrementalMetadataFile, algorithm, new TypeReference<IncrementalIndexStoreMetadata>() { + }); + return mergeIndexStores(indexStoreMetadata, incrementalIndexStoreMetadata); + } else { + throw new RuntimeException("either one or both metadataFiles donot exist at path: " + + baseFFSMetadataFile.getAbsolutePath() + ", " + incrementalMetadataFile.getAbsolutePath()); + } + } + + private void mergeMetadataFiles() throws IOException { + try (BufferedWriter writer = IndexStoreUtils.createWriter(IndexStoreUtils.getMetadataFile(merged, algorithm), algorithm)) { + ObjectMapper mapper = new ObjectMapper(); + mapper.writeValue(writer, getIndexStoreMetadataForMergedFile()); + } + } + + /** + * We only merge indexStore and incrementalStore if: + * 1. IndexStore's checkpoint equals incrementalStore's before checkpoint. + * 2. IndexStore's preferredPaths equals incrementalStore's prefferedPaths. Review Comment: typo ```suggestion * 2. IndexStore's preferredPaths equals incrementalStore's preferredPaths. ``` -- 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]
