/*
 * 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.rdb;

import java.util.Collections;
import java.util.List;

import org.apache.jackrabbit.oak.plugins.document.Collection;
import org.apache.jackrabbit.oak.plugins.document.MissingLastRevSeeker;
import org.apache.jackrabbit.oak.plugins.document.NodeDocument;
import org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore.QueryCondition;
import org.apache.jackrabbit.oak.stats.Clock;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * RDB specific version of MissingLastRevSeeker.
 */
public class RDBMissingLastRevSeeker extends MissingLastRevSeeker {

    private static final Logger LOG = LoggerFactory.getLogger(RDBMissingLastRevSeeker.class);

    // 1: seek using historical, paging mode
    // 2: use custom single query directly using RDBDocumentStore API
    private static final int MODE;

    private static final int DEFAULTMODE = 2;

    static {
        String propName = "org.apache.jackrabbit.oak.plugins.document.rdb.RDBMissingLastRevSeeker.MODE";
        String value = System.getProperty(propName, "");
        switch (value) {
            case "":
                MODE = DEFAULTMODE;
                break;
            case "1":
                MODE = 1;
                break;
            case "2":
                MODE = 2;
                break;
            default:
                LOG.error("Ignoring unexpected value '" + value + "' for system property " + propName);
                MODE = DEFAULTMODE;
                break;
        }

        if (DEFAULTMODE != MODE) {
            LOG.info("Strategy for " + RDBMissingLastRevSeeker.class.getName() + " set to " + MODE + " (via system property "
                    + propName + ")");
        }
    }

    private final RDBDocumentStore store;

    public RDBMissingLastRevSeeker(RDBDocumentStore store, Clock clock) {
        super(store, clock);
        this.store = store;
    }

    @Override
    @NotNull
    public Iterable<NodeDocument> getCandidates(final long startTime) {
        LOG.debug("Running getCandidates() in mode " + MODE);
        if (MODE == 1) {
            return super.getCandidates(startTime);
        } else {
            List<QueryCondition> conditions = Collections.singletonList(
                    new QueryCondition(NodeDocument.MODIFIED_IN_SECS, ">=", NodeDocument.getModifiedInSecs(startTime)));
            return store.queryAsIterable(Collection.NODES, null, null, RDBDocumentStore.EMPTY_KEY_PATTERN, conditions,
                    Integer.MAX_VALUE, null);
        }
    }
}

@@ -1194,24 +1194,103 @@
 yCondition;%0A
+import org.apache.jackrabbit.oak.plugins.document.util.SystemPropertySupplier;%0A
 import org.a
@@ -1323,16 +1323,16 @@
 .Clock;%0A
-
 import o
@@ -1763,44 +1763,8 @@
 API%0A
-    private static final int MODE;%0A%0A
     
@@ -1814,247 +1814,120 @@
     
-static %7B%0A        String propName = %22org.apache.jackrabbit.oak.plugins.document.rdb.RDBMissingLastRevSeeker.MODE%22;%0A        String value = System.getProperty(propName, %22%22);%0A        switch (value) %7B%0A            case %22%22:%0A                
+private static final int MODE = SystemPropertySupplier.create(RDBMissingLastRevSeeker.class.getName() + %22.
 MODE
- =
+%22,
  DEF
@@ -1926,33 +1926,33 @@
 DE%22, DEFAULTMODE
-;
+)
 %0A               
@@ -1952,321 +1952,109 @@
     
-    break;%0A            case %221%22:%0A                MODE = 1;%0A                break;%0A            case %222%22:%0A                MODE = 2;%0A                break;%0A            default:%0A                LOG.error(%22Ignoring unexpected value '%22 + value + %22' for system property %22 + propName);%0A                MODE = DEFAULTMODE;
+.loggingTo(LOG).validateWith(value -%3E (value == 1 %7C%7C value == 2)).formatSetMessage((name, value) -%3E %7B
 %0A   
@@ -2070,223 +2070,159 @@
     
-b
 re
-ak;%0A        %7D%0A%0A        if (DEFAULTMODE != MODE) %7B%0A            LOG.info(%22Strategy for %22 + RDBMissingLastRevSeeker.class.getName() + %22 set to %22 + MODE + %22 (via system property %22%0A                    + propName + %22)%22
+turn String.format(%22Strategy for %25s set to %25s (via system property %25s)%22, RDBMissingLastRevSeeker.class.getName(),%0A                        name, value
 );%0A 
@@ -2228,23 +2228,29 @@
         
-%7D%0A
     %7D
+).get();
 %0A%0A    pr

use SystemPropertySupplier in RDBMissingLastRevSeeker