isapego commented on code in PR #6352:
URL: https://github.com/apache/ignite-3/pull/6352#discussion_r2251162471


##########
modules/platforms/cpp/ignite/client/transaction/transaction_options.h:
##########
@@ -0,0 +1,48 @@
+/*
+* 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.
+ */
+
+#pragma once
+
+/**
+ * Transaction options.
+ */
+class transaction_options {
+public:
+    transaction_options() = default;
+    ~transaction_options() = default;
+    transaction_options(const transaction_options&) = default;
+    transaction_options(transaction_options &&) = default;
+    transaction_options &operator=(const transaction_options &other) = default;
+    transaction_options &operator=(transaction_options &&other) = default;
+
+    [[nodiscard]] std::int64_t get_timeout_millis() const { return 
m_timeout_millis; }
+
+    transaction_options& set_timeout_millis(std::int64_t timeout_millis) {
+        m_timeout_millis = timeout_millis;
+        return *this;
+    }
+
+    [[nodiscard]] bool is_read_only() const { return m_read_only; }
+
+    transaction_options& set_read_only(bool read_only) {

Review Comment:
   Let's add doxygen comments to every public method.



##########
modules/platforms/cpp/ignite/client/transaction/transactions.h:
##########
@@ -40,15 +41,33 @@ class transactions {
     // Delete
     transactions() = delete;
 
+    /**
+     * Starts a new transaction.
+     *
+     * @param tx_opts Transaction options.
+     * @return A new transaction.
+     */
+    IGNITE_API transaction begin(transaction_options tx_opts) {
+        return sync<transaction>([this, &tx_opts](auto callback) { 
begin_async(std::move(callback), tx_opts); });
+    }
+
     /**
      * Starts a new transaction.
      *
      * @return A new transaction.
      */
     IGNITE_API transaction begin() {
-        return sync<transaction>([this](auto callback) { 
begin_async(std::move(callback)); });
+        return begin({});
     }
 
+    /**
+     * Starts a new transaction asynchronously.
+     *
+     * @param callback Callback to be called with a new transaction or error 
upon completion of asynchronous operation.
+     * @param tx_opts Transaction options.
+     */
+    IGNITE_API void begin_async(ignite_callback<transaction> callback, 
transaction_options tx_opts);

Review Comment:
   Current approach is to put `callback` the last parameter where possible.



##########
modules/platforms/cpp/ignite/client/transaction/transaction_options.h:
##########
@@ -0,0 +1,48 @@
+/*
+* 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.
+ */
+
+#pragma once
+
+/**
+ * Transaction options.
+ */
+class transaction_options {
+public:
+    transaction_options() = default;
+    ~transaction_options() = default;
+    transaction_options(const transaction_options&) = default;
+    transaction_options(transaction_options &&) = default;
+    transaction_options &operator=(const transaction_options &other) = default;
+    transaction_options &operator=(transaction_options &&other) = default;

Review Comment:
   ```suggestion
       transaction_options() = default;
   ```
   It's a good practice to not define anything you don't really have to. You 
only have to define copy and move operations if you define a destructor, but 
there is no need for you to explicitly declare a destructor if it's default 
anyway.



##########
modules/platforms/cpp/tests/client-test/transactions_test.cpp:
##########
@@ -434,3 +434,55 @@ TEST_F(transactions_test, record_view_remove_all_exact) {
     auto values2 = record_view.remove_all(nullptr, {value0});
     ASSERT_TRUE(values2.empty());
 }
+
+TEST_F(transactions_test, tx_successful_when_timeout_does_not_exceed) {
+    auto record_view = 
m_client.get_tables().get_table(TABLE_1)->get_record_binary_view();
+
+    int64_t key = 42;
+    std::string val = "Lorem ipsum";
+
+    auto tx_opts = 
transaction_options().set_timeout_millis(10'000L).set_read_only(false);
+    auto tx = m_client.get_transactions().begin(tx_opts);
+
+    record_view.insert(&tx, get_tuple(key, val));
+
+    tx.commit();
+
+    auto value = record_view.get(nullptr, get_tuple(42));
+
+    ASSERT_TRUE(value.has_value());
+    EXPECT_EQ(key, value->get<int64_t>(KEY_COLUMN));
+    EXPECT_EQ(val, value->get<std::string>(VAL_COLUMN));
+}
+
+
+TEST_F(transactions_test, tx_failed_when_timeout_exceeds) {
+    auto record_view = 
m_client.get_tables().get_table(TABLE_1)->get_record_binary_view();
+
+    auto timeout = std::chrono::seconds{1};
+
+    int64_t key = 42;
+    std::string val = "Lorem ipsum";
+    auto tx_opts = transaction_options()
+        
.set_timeout_millis(std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count())
+        .set_read_only(false);
+
+    auto tx = m_client.get_transactions().begin(tx_opts);
+
+    record_view.insert(&tx, get_tuple(key, val));
+
+    std::this_thread::sleep_for(timeout * 2);

Review Comment:
   Just a note, but I'd did this an other way around since the `duration_cast` 
is very verbose:
   ```suggestion
       auto timeout_ms = 10'000L;
   
       int64_t key = 42;
       std::string val = "Lorem ipsum";
       auto tx_opts = transaction_options()
           .set_timeout_millis(timeout_ms)
           .set_read_only(false);
   
       auto tx = m_client.get_transactions().begin(tx_opts);
   
       record_view.insert(&tx, get_tuple(key, val));
   
       std::this_thread::sleep_for(std::chrono::milliseconds(timeout_ms * 2));
   ```



-- 
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: notifications-unsubscr...@ignite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to