belliottsmith commented on code in PR #1951:
URL: https://github.com/apache/cassandra/pull/1951#discussion_r1014269213


##########
src/java/org/apache/cassandra/service/accord/db/AbstractKeyIndexed.java:
##########
@@ -89,99 +113,126 @@ public boolean equals(Object o)
         if (this == o) return true;
         if (o == null || getClass() != o.getClass()) return false;
         AbstractKeyIndexed<?> that = (AbstractKeyIndexed<?>) o;
-        return serialized.equals(that.serialized);
+        return Arrays.equals(serialized, that.serialized);
     }
 
     @Override
     public int hashCode()
     {
-        return Objects.hash(serialized);
+        return Arrays.hashCode(serialized);
     }
 
     @Override
     public String toString()
     {
-        return getClass().getSimpleName() + serialized.entrySet().stream()
-                         .map(e -> e.getKey() + "=" + 
deserialize(e.getValue()))
-                         .collect(Collectors.joining(", ", "{", "}"));
+        return getClass().getSimpleName() + IntStream.range(0, keys.size())
+                                                     .mapToObj(i -> 
keys.get(i) + "=" + deserialize(serialized[i]))
+                                                     
.collect(Collectors.joining(", ", "{", "}"));
+    }
+
+    protected AbstractKeyIndexed(Keys keys, ByteBuffer[] serialized)
+    {
+        this.keys = keys;
+        this.serialized = serialized;
     }
 
     public AbstractKeyIndexed(List<T> items, Function<T, PartitionKey> 
keyFunction)
     {
-        this.serialized = new TreeMap<>();
+        Key[] keys = new Key[items.size()];
+        for (int i=0, mi=items.size(); i<mi; i++)
+            keys[i] = keyFunction.apply(items.get(i));
+        this.keys = Keys.of(keys);
+        this.serialized = new ByteBuffer[items.size()];
         for (int i=0, mi=items.size(); i<mi; i++)
+            serialized[this.keys.indexOf(keyFunction.apply(items.get(i)))] = 
serialize(items.get(i));
+    }
+
+    protected <V> V slice(KeyRanges ranges, BiFunction<Keys, ByteBuffer[], V> 
constructor)
+    {
+        // TODO: Routables patch permits us to do this more efficiently
+        Keys keys = this.keys.slice(ranges);
+        ByteBuffer[] serialized = new ByteBuffer[keys.size()];
+        int j = 0;
+        for (int i = 0 ; i < keys.size() ; ++i)
         {
-            T item = items.get(i);
-            PartitionKey key = keyFunction.apply(item);
-            // TODO: support multiple reads/writes per key
-            Preconditions.checkArgument(!this.serialized.containsKey(key));
-            this.serialized.put(key, serialize(item));
+            j = this.keys.findNext(keys.get(i), j);
+            serialized[i] = this.serialized[j++];
         }
+        return constructor.apply(keys, serialized);
     }
 
-    public AbstractKeyIndexed(NavigableMap<PartitionKey, ByteBuffer> 
serialized)
+    public <V> V merge(AbstractKeyIndexed<?> that, BiFunction<Keys, 
ByteBuffer[], V> constructor)
     {
-        this.serialized = serialized;
+        // TODO: special method for linear merging keyed and non-keyed lists 
simultaneously
+        Keys keys = this.keys.union(that.keys);
+        ByteBuffer[] serialized = new ByteBuffer[keys.size()];
+        int i = 0, j = 0, o = 0;
+        while (i < this.keys.size() && j < that.keys.size())
+        {
+            int c = this.keys.get(i).compareTo(that.keys.get(j));
+            if (c < 0) serialized[o++] = this.serialized[i++];
+            else if (c > 0) serialized[o++] = that.serialized[j++];
+            else { serialized[o++] = this.serialized[i++]; j++; }
+        }
+        while (i < this.keys.size())
+            serialized[o++] = this.serialized[i++];
+        while (j < that.keys.size())
+            serialized[o++] = that.serialized[j++];
+        return constructor.apply(keys, serialized);
     }
 
     public T getDeserialized(PartitionKey key)
     {
-        ByteBuffer bytes = serialized.get(key);
-        if (bytes == null)
-            return null;
-        return deserialize(bytes);
+        int i = keys.indexOf(key);
+        if (i < 0) return null;
+        return deserialize(serialized[i]);
     }
 
     public long estimatedSizeOnHeap()
     {
         long size = emptySizeOnHeap();
-        for (Map.Entry<PartitionKey, ByteBuffer> entry : serialized.entrySet())
-        {
-            size += entry.getKey().estimatedSizeOnHeap();
-            size += ByteBufferUtil.EMPTY_SIZE_ON_HEAP + 
ByteBufferAccessor.instance.size(entry.getValue());
-        }
+        for (Key key : keys) size += ((PartitionKey) 
key).estimatedSizeOnHeap();
+        for (ByteBuffer buffer : serialized) size += 
ByteBufferUtil.EMPTY_SIZE_ON_HEAP + ByteBufferAccessor.instance.size(buffer);

Review Comment:
   There is, but I only mechanically updated these - I wasn't trying to edit 
the existing code. I've made the change, anyway



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to