rishabhdaim commented on code in PR #1021: URL: https://github.com/apache/jackrabbit-oak/pull/1021#discussion_r1283516621
########## oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/SimpleFlatFileUtil.java: ########## @@ -0,0 +1,102 @@ +/* + * 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.flatfile; + +import static org.apache.jackrabbit.guava.common.collect.ImmutableList.copyOf; +import static org.apache.jackrabbit.oak.commons.PathUtils.elements; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.IOException; +import java.io.Writer; +import java.nio.file.Files; +import java.util.stream.StreamSupport; + +import org.apache.jackrabbit.oak.index.indexer.document.NodeStateEntry; +import org.apache.jackrabbit.oak.plugins.document.DocumentNodeState; +import org.apache.jackrabbit.oak.spi.state.NodeState; +import org.apache.jackrabbit.oak.spi.state.NodeStateUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This util class can be used to export a tree (eg entire repository) to a flat + * file, without index dependency/involvement. + */ +public class SimpleFlatFileUtil { + + private static final Logger log = LoggerFactory.getLogger(SimpleFlatFileUtil.class); + + private static final String LINE_SEPARATOR = System.getProperty("line.separator"); + + private final Writer writer; + private final NodeStateEntryWriter entryWriter; + private long totalLines = 0; + + private SimpleFlatFileUtil(Writer writer) { + // blobStore is only used for deserialization - so pass null here: + this.entryWriter = new NodeStateEntryWriter(null, true); + this.writer = writer; + } + + public static void createFlatFileFor(NodeState ns, File f) throws IOException { + log.info("createFlatFileFor : writing to {}", f.getCanonicalPath()); + try (BufferedWriter bw = Files.newBufferedWriter(f.toPath())) { + createFlatFileFor(ns, bw); + } + } + + public static void createFlatFileFor(NodeState ns, Writer writer) throws IOException { + SimpleFlatFileUtil h = new SimpleFlatFileUtil(writer); + h.addEntryAndTraverseChildren(ns); + log.info("createFlatFileFor : done. wrote {} lines in total.", h.totalLines); + } + + private void addEntryAndTraverseChildren(NodeState ns) throws IOException { + addEntry(ns); + StreamSupport.stream(ns.getChildNodeEntries().spliterator(), false) + .sorted((o1, o2) -> o1.getName().compareTo(o2.getName())).forEach(e -> { Review Comment: ```suggestion .sorted(Comparator.comparing(ChildNodeEntry::getName)).forEach(e -> { ``` -- 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]
