zjw1111 commented on code in PR #199:
URL: https://github.com/apache/paimon-cpp/pull/199#discussion_r3801593881


##########
src/paimon/core/realtime/realtime_context_impl.cpp:
##########
@@ -0,0 +1,355 @@
+/*
+ * 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.
+ */
+
+#include "paimon/core/realtime/realtime_context_impl.h"
+
+#include <algorithm>
+#include <chrono>
+#include <condition_variable>
+#include <deque>
+#include <limits>
+#include <map>
+#include <mutex>
+#include <optional>
+#include <string>
+#include <system_error>
+#include <thread>
+#include <tuple>
+#include <utility>
+#include <vector>
+
+#include "arrow/c/helpers.h"
+#include "paimon/arrow/abi.h"
+#include "paimon/common/utils/uuid.h"
+#include "paimon/macros.h"
+#include "paimon/realtime/realtime_store.h"
+#include "paimon/status.h"
+
+namespace paimon {
+
+RealtimeContextImpl::RealtimeContextImpl(const 
std::shared_ptr<RealtimeStoreFactory>& factory)
+    : factory_(factory) {}
+
+RealtimeContextImpl::~RealtimeContextImpl() {
+    {
+        std::lock_guard<std::mutex> lock(read_views_mutex_);
+        stopping_ = true;
+    }
+    read_views_cv_.notify_all();
+    if (read_view_cleanup_thread_.joinable()) {
+        read_view_cleanup_thread_.join();
+    }
+}
+
+Result<std::shared_ptr<RealtimeContextImpl>> RealtimeContextImpl::Create(
+    const std::shared_ptr<RealtimeStoreFactory>& factory) {
+    if (!factory) {
+        return Status::Invalid("real-time store factory is null");
+    }
+    std::shared_ptr<RealtimeContextImpl> context(new 
RealtimeContextImpl(factory));
+    PAIMON_RETURN_NOT_OK(context->Start());
+    return context;
+}
+
+Status RealtimeContextImpl::Start() {
+    try {
+        read_view_cleanup_thread_ = std::thread([this]() { CleanupReadViews(); 
});
+    } catch (const std::system_error& e) {
+        return Status::UnknownError(
+            std::string("failed to start real-time read-view cleanup thread: 
") + e.what());
+    }
+    return Status::OK();
+}
+
+Result<RealtimeStoreState> RealtimeContextImpl::GetOrCreateRealtimeStore(
+    const std::map<std::string, std::string>& partition, int32_t bucket,
+    std::unique_ptr<ArrowSchema> write_schema, const std::map<std::string, 
std::string>& options,
+    const std::shared_ptr<MemoryPool>& memory_pool) {
+    std::lock_guard<std::mutex> progress_lock(progress_mutex_);
+    std::lock_guard<std::mutex> registry_lock(mutex_);
+    const RealtimePartitionBucket key(partition, bucket);
+    int64_t initial_offset = 0;
+    auto offset_iter = committed_offsets_.find(key);
+    if (offset_iter != committed_offsets_.end()) {
+        if (offset_iter->second == std::numeric_limits<int64_t>::max()) {
+            if (write_schema) {
+                ArrowSchemaRelease(write_schema.get());
+            }
+            return Status::Invalid("real-time offset has reached INT64_MAX");
+        }
+        initial_offset = offset_iter->second;
+    }
+    auto iter = stores_.find(key);
+    if (iter != stores_.end()) {
+        if (write_schema) {
+            ArrowSchemaRelease(write_schema.get());
+        }
+        PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<RealtimeReadView> read_view,
+                               iter->second->AcquireReadView());
+        if (!read_view) {
+            return Status::Invalid("real-time store returned a null read 
view");
+        }
+        const std::optional<OffsetRange> memory_range = 
read_view->GetOffsetRange();
+        if (memory_range) {
+            if (memory_range->end == std::numeric_limits<int64_t>::max()) {
+                return Status::Invalid("real-time offset has reached 
INT64_MAX");
+            }
+            // A reused store may retain sealed-but-uncommitted rows beyond 
the committed
+            // offset. Continue after all retained rows instead of restarting 
across a seal.
+            if (memory_range->end > initial_offset) {
+                initial_offset = memory_range->end;
+            }
+        }
+        return RealtimeStoreState{iter->second, initial_offset};
+    }
+    Result<std::shared_ptr<RealtimeStore>> store_result =
+        factory_->Create(std::move(write_schema), options, memory_pool);
+    PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<RealtimeStore> store, 
std::move(store_result));

Review Comment:
   direct `PAIMON_ASSIGN_OR_RAISE`?



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

Reply via email to