keith-turner commented on a change in pull request #969: [WIP] Issue-967
URL: https://github.com/apache/fluo/pull/969#discussion_r152160862
 
 

 ##########
 File path: 
modules/core/src/main/java/org/apache/fluo/core/impl/AsyncReader.java
 ##########
 @@ -0,0 +1,101 @@
+/*
+ * 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.fluo.core.impl;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
+
+import com.google.common.collect.Collections2;
+import org.apache.fluo.api.data.Bytes;
+import org.apache.fluo.api.data.Column;
+import org.apache.fluo.api.data.RowColumn;
+import org.apache.fluo.core.impl.TransactionImpl;
+
+public class AsyncReader {
+  private BlockingQueue<AsyncGet> asyncGetsQueue;
+  private ExecutorService executorService;
+  private TransactionImpl tx;
+
+  public AsyncReader(TransactionImpl tx) {
+    this.tx = tx;
+    asyncGetsQueue = new LinkedBlockingQueue<>();
+    executorService = Executors.newSingleThreadExecutor();
+  }
+
+  public CompletableFuture<Bytes> get(Bytes row, Column column) {
+    return get(row, column, null);
+  }
+
+  public CompletableFuture<Bytes> get(Bytes row, Column column, Bytes 
defaultValue) {
+    AsyncGet curAsyncGet = new AsyncGet(row, column, defaultValue);
+    asyncGetsQueue.add(curAsyncGet);
+
+    executorService.submit(() -> {
+      List<AsyncGet> getsList = new ArrayList<>();
+      asyncGetsQueue.drainTo(getsList);
+
+      try {
+        Collection<RowColumn> rowColumns = Collections2.transform(getsList, ag 
-> ag.rc);
+
+        Map<RowColumn, Bytes> getsMap = tx.get(rowColumns);
+
+        for (AsyncGet asyncGet : getsList) {
+          Bytes result = getsMap.get(asyncGet.rc);
+          asyncGet.res.complete(result == null ? defaultValue : result);
+          getsList.remove(asyncGet);
 
 Review comment:
   The following comments are moot because I don't think things needs to be 
removed based on earlier comment.  Just sharing some knowledge in following 
comments.
   
   This could cause a concurrent modification exception.  The foreach has a 
hidden iterator.  In java, many iterators will throw a concurrent modification 
exception if the collection is modified during iteration.  Using a while loop 
with an explicit iterator and calling remove on the iterator is the best way to 
remove while iterating.  
   
   Also removing items from an arraylist could be an O(N^2) operation.  In the 
situation of removing from the middle of a list a linked list may be better.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to