big-r81 commented on code in PR #4291:
URL: https://github.com/apache/couchdb/pull/4291#discussion_r1129227966


##########
java/nouveau/base/src/main/java/org/apache/couchdb/nouveau/core/Index.java:
##########
@@ -0,0 +1,145 @@
+//
+// Licensed 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.couchdb.nouveau.core;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.couchdb.nouveau.api.DocumentDeleteRequest;
+import org.apache.couchdb.nouveau.api.DocumentUpdateRequest;
+import org.apache.couchdb.nouveau.api.IndexInfo;
+import org.apache.couchdb.nouveau.api.SearchRequest;
+import org.apache.couchdb.nouveau.api.SearchResults;
+
+public abstract class Index<T> implements Closeable {
+
+    private long updateSeq;
+
+    private boolean deleteOnClose = false;
+
+    private int refCount = 1;
+
+    private long lastUsed = now();
+
+    protected Index(final long updateSeq) {
+        this.updateSeq = updateSeq;
+    }
+
+    public final IndexInfo info() throws IOException {
+        updateLastUsed();
+        final int numDocs = doNumDocs();
+        return new IndexInfo(updateSeq, numDocs);
+    }
+
+    protected abstract int doNumDocs() throws IOException;
+
+    public final synchronized void update(final String docId, final 
DocumentUpdateRequest<T> request) throws IOException {
+        assertUpdateSeqIsLower(request.getSeq());
+        updateLastUsed();
+        doUpdate(docId, request);
+        incrementUpdateSeq(request.getSeq());
+    }
+
+    protected abstract void doUpdate(final String docId, final 
DocumentUpdateRequest<T> request) throws IOException;
+
+    public final synchronized void delete(final String docId, final 
DocumentDeleteRequest request) throws IOException {
+        assertUpdateSeqIsLower(request.getSeq());
+        updateLastUsed();
+        doDelete(docId, request);
+        incrementUpdateSeq(request.getSeq());
+    }
+
+    protected abstract void doDelete(final String docId, final 
DocumentDeleteRequest request) throws IOException;
+
+    public final SearchResults<T> search(final SearchRequest request) throws 
IOException {
+        updateLastUsed();
+        return doSearch(request);
+    }
+
+    protected abstract SearchResults<T> doSearch(final SearchRequest request) 
throws IOException;
+
+    public final boolean commit() throws IOException {
+        final long updateSeq;
+        synchronized (this) {
+            updateSeq = this.updateSeq;
+        }
+        return doCommit(updateSeq);
+    }
+
+    protected abstract boolean doCommit(final long updateSeq) throws 
IOException;
+
+    @Override
+    public final void close() throws IOException {
+        decRef();
+    }
+
+    protected abstract void doClose() throws IOException;
+
+    public abstract boolean isOpen();
+
+    public boolean isDeleteOnClose() {
+        return deleteOnClose;
+    }
+
+    public void setDeleteOnClose(final boolean deleteOnClose) {
+        this.deleteOnClose = deleteOnClose;
+    }
+
+    protected final void assertUpdateSeqIsLower(final long updateSeq) throws 
UpdatesOutOfOrderException {
+        if (!(updateSeq > this.updateSeq)) {
+            throw new UpdatesOutOfOrderException();
+        }
+    }
+
+    protected final void incrementUpdateSeq(final long updateSeq) throws 
IOException {
+        assertUpdateSeqIsLower(updateSeq);
+        this.updateSeq = updateSeq;
+    }
+
+    private synchronized void updateLastUsed() {
+        this.lastUsed = now();
+    }
+
+    public final synchronized void incRef() {
+        // zero is forever.
+        if (refCount > 0) {
+            refCount++;
+        }
+    }
+
+    public final synchronized int getRefCount() {
+        return refCount;
+    }
+
+    public final boolean decRef() throws IOException {
+        boolean close;
+        synchronized (this) {
+            close = --refCount == 0;
+        }
+        if (close) {
+            doClose();
+        }
+        return close;
+    }
+
+    public synchronized long secondsSinceLastUse() {
+        return TimeUnit.NANOSECONDS.toMillis(now() - lastUsed);
+    }
+
+    private long now() {
+        return System.nanoTime();
+    }
+
+}

Review Comment:
   nit: missing empty newline



-- 
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]

Reply via email to