alievmirza commented on a change in pull request #118:
URL: https://github.com/apache/ignite-3/pull/118#discussion_r639871684



##########
File path: 
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/command/CommandUtils.java
##########
@@ -0,0 +1,157 @@
+/*
+ * 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.ignite.internal.table.distributed.command;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Collection;
+import java.util.function.Consumer;
+import org.apache.ignite.internal.schema.BinaryRow;
+import org.apache.ignite.internal.schema.ByteBufferRow;
+import org.apache.ignite.lang.IgniteLogger;
+
+/**
+ * This is an utility class for serialization cache tuples. It will be removed 
after another way for serialization is
+ * implemented into the network layer.

Review comment:
       where is todo if this solution is temporary?

##########
File path: 
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/storage/InternalTableImpl.java
##########
@@ -102,12 +158,36 @@ public InternalTableImpl(
 
     /** {@inheritDoc} */
     @Override public @NotNull CompletableFuture<Collection<BinaryRow>> 
insertAll(Collection<BinaryRow> rows) {
-        return null;
+        HashMap<Integer, HashSet<BinaryRow>> setByPartition = new HashMap<>();
+
+        for (BinaryRow keyRow : rows) {
+            setByPartition.computeIfAbsent(keyRow.hash() % partitions, 
HashSet::new)
+                .add(keyRow);
+        }
+
+        CompletableFuture<MultiRowsResponse>[] futures = new 
CompletableFuture[setByPartition.size()];
+
+        int batchNum = 0;
+
+        for (Map.Entry<Integer, HashSet<BinaryRow>> partToRows : 
setByPartition.entrySet()) {
+            futures[batchNum] = partitionMap.get(partToRows.getKey()).run(new 
InsertAllCommand(partToRows.getValue()));
+
+            batchNum++;
+        }
+
+        CompletableFuture<Collection<BinaryRow>> future = 
CompletableFuture.allOf(futures)

Review comment:
       local variable future is rudndant

##########
File path: 
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/raft/PartitionCommandListener.java
##########
@@ -163,4 +308,26 @@ else if (clo.command() instanceof UpsertCommand) {
             return hash;
         }
     }
+
+    /**
+     * @param row Row.
+     * @return Extracted key.
+     */
+    @NotNull private boolean equalValues(@NotNull BinaryRow row, @NotNull 
BinaryRow row2) {

Review comment:
       you don't need @NotNull for return values as far as the return value is 
primitive.

##########
File path: 
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/raft/PartitionCommandListener.java
##########
@@ -90,38 +123,150 @@ else if (clo.command() instanceof ReplaceCommand) {
                     clo.success(false);
             }
             else if (clo.command() instanceof UpsertCommand) {
-                storage.put(
-                    extractAndWrapKey(((UpsertCommand)clo.command()).getRow()),
-                    ((UpsertCommand)clo.command()).getRow()
-                );
+                BinaryRow row = ((UpsertCommand)clo.command()).getRow();
+
+                assert row.hasValue() : "Upsert command should have a value.";
+
+                storage.put(extractAndWrapKey(row), row);
 
                 clo.success(null);
             }
-            else
-                assert false : "Command was not found [cmd=" + clo.command() + 
']';
-        }
-    }
+            else if (clo.command() instanceof InsertAllCommand) {
+                Set<BinaryRow> rows = 
((InsertAllCommand)clo.command()).getRows();
 
-    /**
-     * @param row Row.
-     * @return Extracted key.
-     */
-    @NotNull private boolean equalValues(@NotNull BinaryRow row, @NotNull 
BinaryRow row2) {
-        if (row.hasValue() ^ row2.hasValue())
-            return false;
+                assert rows != null && !rows.isEmpty();
 
-        return row.valueSlice().compareTo(row2.valueSlice()) == 0;
-    }
+                final Set<BinaryRow> res = rows.stream()
+                    .map(k -> storage.putIfAbsent(extractAndWrapKey(k), k) == 
null ? null : k)
+                    .filter(Objects::nonNull)
+                    .filter(BinaryRow::hasValue)
+                    .collect(Collectors.toSet());
 
-    /**
-     * @param row Row.
-     * @return Extracted key.
-     */
-    @NotNull private KeyWrapper extractAndWrapKey(@NotNull BinaryRow row) {
-        final byte[] bytes = new byte[row.keySlice().capacity()];
-        row.keySlice().get(bytes);
+                clo.success(new MultiRowsResponse(res));
+            }
+            else if (clo.command() instanceof UpsertAllCommand) {
+                Set<BinaryRow> rows = 
((UpsertAllCommand)clo.command()).getRows();
 
-        return new KeyWrapper(bytes, row.hash());
+                assert rows != null && !rows.isEmpty();
+
+                rows.stream()

Review comment:
       stream is redundant

##########
File path: 
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/raft/PartitionCommandListener.java
##########
@@ -90,38 +123,150 @@ else if (clo.command() instanceof ReplaceCommand) {
                     clo.success(false);

Review comment:
       ```
                   if ((current == null && !expected.hasValue()) ||
                       equalValues(current, expected)) {
                       storage.put(key, cmd.getRow());
                       clo.success(true);
                   }
   ```
   there is possible NPE because `current` might be null when you pass it to 
`equalValues`

##########
File path: 
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/raft/PartitionCommandListener.java
##########
@@ -57,10 +89,11 @@
             CommandClosure<WriteCommand> clo = iterator.next();
 
             if (clo.command() instanceof InsertCommand) {
-                BinaryRow previous = storage.putIfAbsent(
-                    extractAndWrapKey(((InsertCommand)clo.command()).getRow()),
-                    ((InsertCommand)clo.command()).getRow()
-                );
+                BinaryRow row = ((InsertCommand)clo.command()).getRow();
+
+                assert row.hasValue() : "Insert command should have a value.";
+
+                BinaryRow previous = 
storage.putIfAbsent(extractAndWrapKey(row), row);

Review comment:
       I didn't get the idea of `storage` in `PartitionCommandListener`. When 
this storage will be purged? Do we store all partitions data in this storage? 
Seems that this approach may lead to OOM. If this solution is temporary, where 
is todo with future changes? 

##########
File path: 
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/raft/PartitionCommandListener.java
##########
@@ -163,4 +308,26 @@ else if (clo.command() instanceof UpsertCommand) {
             return hash;
         }
     }
+
+    /**
+     * @param row Row.

Review comment:
       Please add describing javadoc, what contract for comparing is.

##########
File path: 
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/raft/PartitionCommandListener.java
##########
@@ -90,38 +123,150 @@ else if (clo.command() instanceof ReplaceCommand) {
                     clo.success(false);
             }
             else if (clo.command() instanceof UpsertCommand) {
-                storage.put(
-                    extractAndWrapKey(((UpsertCommand)clo.command()).getRow()),
-                    ((UpsertCommand)clo.command()).getRow()
-                );
+                BinaryRow row = ((UpsertCommand)clo.command()).getRow();
+
+                assert row.hasValue() : "Upsert command should have a value.";
+
+                storage.put(extractAndWrapKey(row), row);
 
                 clo.success(null);
             }
-            else
-                assert false : "Command was not found [cmd=" + clo.command() + 
']';
-        }
-    }
+            else if (clo.command() instanceof InsertAllCommand) {
+                Set<BinaryRow> rows = 
((InsertAllCommand)clo.command()).getRows();
 
-    /**
-     * @param row Row.
-     * @return Extracted key.
-     */
-    @NotNull private boolean equalValues(@NotNull BinaryRow row, @NotNull 
BinaryRow row2) {
-        if (row.hasValue() ^ row2.hasValue())
-            return false;
+                assert rows != null && !rows.isEmpty();
 
-        return row.valueSlice().compareTo(row2.valueSlice()) == 0;
-    }
+                final Set<BinaryRow> res = rows.stream()
+                    .map(k -> storage.putIfAbsent(extractAndWrapKey(k), k) == 
null ? null : k)
+                    .filter(Objects::nonNull)
+                    .filter(BinaryRow::hasValue)
+                    .collect(Collectors.toSet());
 
-    /**
-     * @param row Row.
-     * @return Extracted key.
-     */
-    @NotNull private KeyWrapper extractAndWrapKey(@NotNull BinaryRow row) {
-        final byte[] bytes = new byte[row.keySlice().capacity()];
-        row.keySlice().get(bytes);
+                clo.success(new MultiRowsResponse(res));
+            }
+            else if (clo.command() instanceof UpsertAllCommand) {
+                Set<BinaryRow> rows = 
((UpsertAllCommand)clo.command()).getRows();
 
-        return new KeyWrapper(bytes, row.hash());
+                assert rows != null && !rows.isEmpty();
+
+                rows.stream()
+                    .forEach(k -> storage.put(extractAndWrapKey(k), k));
+
+                clo.success(null);
+            }
+            else if (clo.command() instanceof DeleteAllCommand) {
+                Set<BinaryRow> rows = 
((DeleteAllCommand)clo.command()).getRows();
+
+                assert rows != null && !rows.isEmpty();
+
+                final Set<BinaryRow> res = rows.stream()
+                    .map(k -> {
+                        if (k.hasValue())
+                            return null;
+                        else {
+                            BinaryRow r = storage.remove(extractAndWrapKey(k));
+
+                            if (r == null)

Review comment:
       you can just return r instead of if check
   

##########
File path: 
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/command/UpsertAllCommand.java
##########
@@ -0,0 +1,64 @@
+/*
+ * 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.ignite.internal.table.distributed.command;
+
+import java.util.HashSet;
+import java.util.Set;
+import org.apache.ignite.internal.schema.BinaryRow;
+import org.apache.ignite.raft.client.WriteCommand;
+
+/**
+ * The command puts a batch rows.
+ */
+public class UpsertAllCommand implements WriteCommand {
+    /** Rows. */
+    private transient Set<BinaryRow> rows;
+
+    /*
+     * Row bytes.
+     * It is a temporary solution, before network have not implement correct 
serialization BinaryRow.
+     * TODO: Remove the field after.

Review comment:
       need a ticket for that and in every place with todo




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