upthewaterspout commented on a change in pull request #6461:
URL: https://github.com/apache/geode/pull/6461#discussion_r632757884



##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/collections/Object2ObjectOpenCustomHashMapWithCursor.java
##########
@@ -0,0 +1,185 @@
+/*
+ * 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.geode.redis.internal.collections;
+
+import static it.unimi.dsi.fastutil.HashCommon.mix;
+
+import java.util.Map;
+
+import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
+import it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap;
+
+/**
+ * An extention of {@link Object2ObjectOpenCustomHashMap} that supports
+ * a method of iteration where each scan operation returns an integer cursor
+ * that allows future scan operations to start from that same point.
+ *
+ * The scan method provides the same guarantees as Redis's HSCAN, and in fact
+ * uses the same algorithm.
+ */
+public class Object2ObjectOpenCustomHashMapWithCursor<K, V>
+    extends Object2ObjectOpenCustomHashMap<K, V> {
+
+
+  public Object2ObjectOpenCustomHashMapWithCursor(int expected, float f,
+      Strategy<? super K> strategy) {
+    super(expected, f, strategy);
+  }
+
+  public Object2ObjectOpenCustomHashMapWithCursor(int expected,
+      Strategy<? super K> strategy) {
+    super(expected, strategy);
+  }
+
+  public Object2ObjectOpenCustomHashMapWithCursor(Strategy<? super K> 
strategy) {
+    super(strategy);
+  }
+
+  public Object2ObjectOpenCustomHashMapWithCursor(Map<? extends K, ? extends 
V> m, float f,
+      Strategy<? super K> strategy) {
+    super(m, f, strategy);
+  }
+
+  public Object2ObjectOpenCustomHashMapWithCursor(Map<? extends K, ? extends 
V> m,
+      Strategy<? super K> strategy) {
+    super(m, strategy);
+  }
+
+  public Object2ObjectOpenCustomHashMapWithCursor(
+      Object2ObjectMap<K, V> m,
+      float f,
+      Strategy<? super K> strategy) {
+    super(m, f, strategy);
+  }
+
+  public Object2ObjectOpenCustomHashMapWithCursor(Object2ObjectMap<K, V> m,
+      Strategy<? super K> strategy) {
+    super(m, strategy);
+  }
+
+  public Object2ObjectOpenCustomHashMapWithCursor(K[] k, V[] v, float f,
+      Strategy<? super K> strategy) {
+    super(k, v, f, strategy);
+  }
+
+  public Object2ObjectOpenCustomHashMapWithCursor(K[] k, V[] v,
+      Strategy<? super K> strategy) {
+    super(k, v, strategy);
+  }
+
+  /**
+   * Scan entries and pass them to the given consumer function, starting at 
the passed in
+   * cursor. This method will scan until at least count entries are returned, 
or the entire
+   * map has been scanned. Once the returned cursor is 0, the entire map is 
scanned.
+   *
+   * This method may emit more than *count* number of elements if there are 
hash collisions.
+   *
+   * @param cursor The cursor to start from. Should be 0 for the initial scan. 
Subsequent calls
+   *        should use the cursor returned by the previous scan call.
+   * @param count The number of elements to scan
+   * @param consumer A function to pass the scanned keys and values to
+   * @param privateData Some data to pass to the function, for example a map 
to collect values in.
+   *        This
+   *        allows the function to be stateless.
+   * @param <D> The type of the data passed to the function/
+   * @return The next cursor to scan from, or 0 if the scan has touched all 
elements.
+   */
+  public <D> int scan(int cursor, int count, EntryConsumer<K, V, D> consumer, 
D privateData) {
+    // Implementation notes
+    //
+    // This stateless scan cursor algorithm is based on the dictScan cursor
+    // implementation from dict.c in redis. Please see the comments in that 
class for the full
+    // details. That iteration algorithm was designed by Pieter Noordhuis.
+    //
+    // There is one wrinkle due to the fact that we are using a different type 
of hashtable here.
+    // The parent class, Object2ObjectOpenHashMap, uses an open addressing 
with a linear
+    // probe. What that means is that when there is a hash collision, instead 
of putting
+    // a linked list of hash entries into a single hash bucket, this 
implementation simply
+    // moves on to the next element to the right in the array and tries to put 
the inserted
+    // object there, continuing until it finds a null slot.
+    //
+    // So in order to use the redis cursor algorithm, our scan needs to probe 
ahead to
+    // subsequent positions to find any hash entries that match the position 
we are scanning.
+    // This is logically equivalent to iterating over the linked list in a 
hashtable bucket
+    // for a redis style closed addressing hashtable.
+    //
+
+    do {
+      // Emit all of the entries at the cursor. This means looking forward in 
the hash
+      // table for any non-null entries that might hash to the current cursor 
and emitting
+      // those as well. This may even wrap around to the front of the 
hashtable.
+      int position = cursor;
+      while (key[position & mask] != null) {
+        if (elementAtHashesTo(position, cursor & mask)) {

Review comment:
       I'm going to merge this and create a new PR with a JMH test for this. 
I'll rename this method in the next PR. Thanks!




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

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to