daniancu commented on code in PR #2038: URL: https://github.com/apache/jackrabbit-oak/pull/2038#discussion_r1977781043
########## oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoFullGcNodeBin.java: ########## @@ -0,0 +1,160 @@ +/* + * 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.document.mongo; + +import com.mongodb.BasicDBObject; +import org.apache.jackrabbit.oak.plugins.document.Collection; +import org.apache.jackrabbit.oak.plugins.document.Document; +import org.apache.jackrabbit.oak.plugins.document.DocumentStore; +import org.apache.jackrabbit.oak.plugins.document.FullGcNodeBin; +import org.apache.jackrabbit.oak.plugins.document.NodeDocument; +import org.apache.jackrabbit.oak.plugins.document.UpdateOp; +import org.slf4j.Logger; +import static org.slf4j.LoggerFactory.getLogger; + +import java.time.Instant; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * This class is as a wrapper around DocumentStore that expose two methods used to clean garbage from NODES collection + * public int remove(Map<String, Long> orphanOrDeletedRemovalMap) + * public List<NodeDocument> findAndUpdate(List<UpdateOp> updateOpList) + * When enabled + * Each method saves the document ID or empty properties names (that will be deleted) to a separate _bin collection as a BinDocument then delegates deletion to DocumentStore + * + * When disabled (default) + * Each method delegates directly to DocumentStore + */ +public class MongoFullGcNodeBin implements FullGcNodeBin { + private static final Logger LOG = getLogger(MongoFullGcNodeBin.class); + public static final String GC_COLLECTED_AT = "_gcCollectedAt"; + private final MongoDocumentStore mongoDocumentStore; + private boolean enabled; + + public MongoFullGcNodeBin(MongoDocumentStore ds) { + mongoDocumentStore = ds; + enabled = System.getProperty("oak.document.fullGcBin.enabled", "false").equals("true"); + } + + /** + * Remove orphaned or deleted documents from the NODES collection + * If bin is enabled, the document IDs are saved to the SETTINGS collection with ID prefixed with '/bin/' + * If document ID cannot be saved then the removal of the document fails + * If the bin is disabled, the document IDs are directly removed from the NODES collection + * + * @param orphanOrDeletedRemovalMap the keys of the documents to remove with the corresponding timestamps + * @return the number of documents removed + * @see DocumentStore#remove(Collection, Map) + */ + @Override + public int remove(Map<String, Long> orphanOrDeletedRemovalMap) { + if (orphanOrDeletedRemovalMap.isEmpty() || !addToBin(orphanOrDeletedRemovalMap)) { + return 0; + } + + // use remove() with the modified check to rule + // out any further race-condition where this removal + // races with a un-orphan/re-creation as a result of which + // the node should now not be removed. The modified check + // ensures a node would then not be removed + // (and as a result the removedSize != map.size()) + return mongoDocumentStore.remove(Collection.NODES, orphanOrDeletedRemovalMap); + } + + + /** + * Performs a conditional update + * If the bin is enabled, the removed properties are saved to the SETTINGS collection with ID prefixed with '/bin/' and empty value + * If the document ID and properties cannot be saved then the removal of the property fails + * If bin is disabled, the removed properties are directly removed from the NODES collection + * + * @param updateOpList the update operation List + * @return the list containing old documents + * @see DocumentStore#findAndUpdate(Collection, List) + */ + @Override + public List<NodeDocument> findAndUpdate(List<UpdateOp> updateOpList) { + LOG.info("Updating {} documents", updateOpList.size()); + if (updateOpList.isEmpty() || !addToBin(updateOpList)) { + return Collections.emptyList(); + } + return mongoDocumentStore.findAndUpdate(Collection.NODES, updateOpList); + } + + protected boolean addToBin(Map<String, Long> orphanOrDeletedRemovalMap) { Review Comment: done -- 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: oak-dev-unsubscr...@jackrabbit.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org