realAaronWu commented on code in PR #588:
URL: https://github.com/apache/paimon-cpp/pull/588#discussion_r4667234223
##########
src/paimon/core/snapshot.h:
##########
@@ -242,8 +242,8 @@ class Snapshot : public Jsonizable<Snapshot> {
void FromJson(const rapidjson::Value& obj) noexcept(false) override;
- static Result<Snapshot> FromPath(const std::shared_ptr<FileSystem>& fs,
- const std::string& path);
+ static Result<Snapshot> FromPath(const std::shared_ptr<FileSystem>& fs,
const std::string& path,
+ MemoryPool* pool = nullptr);
Review Comment:
Removed the new default arguments. The existing `Snapshot::FromPath(fs,
path)` and manager constructors keep their original behavior. The pooled
overload requires a `MemoryPool&`, and the data-scan factory requires an
explicit pool.
##########
src/paimon/core/snapshot.cpp:
##########
@@ -323,11 +332,42 @@ void Snapshot::FromJson(const rapidjson::Value& obj)
noexcept(false) {
RapidJsonUtil::DeserializeKeyValue<std::optional<int64_t>>(obj,
FIELD_NEXT_ROW_ID);
}
-Result<Snapshot> Snapshot::FromPath(const std::shared_ptr<FileSystem>& fs,
- const std::string& path) {
- std::string json_str;
- PAIMON_RETURN_NOT_OK(fs->ReadFile(path, &json_str));
- PAIMON_ASSIGN_OR_RAISE(Snapshot snapshot,
Snapshot::FromJsonString(json_str));
+Result<Snapshot> Snapshot::FromPath(const std::shared_ptr<FileSystem>& fs,
const std::string& path,
+ MemoryPool* pool) {
+ if (pool == nullptr || pool == GetDefaultPool().get()) {
+ std::string json_str;
+ PAIMON_RETURN_NOT_OK(fs->ReadFile(path, &json_str));
+ return Snapshot::FromJsonString(json_str);
+ }
+
+ Bytes json;
+ {
+ PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<InputStream> input,
fs->Open(path));
+ if (input == nullptr) {
+ return Status::Invalid("input stream is not suppose to be a null
pointer, path: ",
+ path);
+ }
+ ScopeGuard close([&input]() { (void)input->Close(); });
+ PAIMON_ASSIGN_OR_RAISE(int64_t length, input->Length());
+ PAIMON_RETURN_NOT_OK(ValidateValueNonNegative(length, "read length"));
+ const uint64_t allocation_size = static_cast<uint64_t>(length) + 1;
+ PAIMON_RETURN_NOT_OK(ValidateValueInRange<size_t>(allocation_size,
"snapshot buffer size"));
+ try {
+ json = Bytes(static_cast<size_t>(allocation_size), pool);
+ } catch (const std::bad_alloc&) {
+ return Status::OutOfMemory("cannot allocate ", allocation_size, "
bytes for snapshot ",
+ path);
Review Comment:
`MemoryPool::Malloc()` throws `std::bad_alloc` when it refuses the request.
This catch wraps only the snapshot buffer allocation so `CreatePlan()` can
return `OutOfMemory` before reading the payload. Reads and parsing keep their
existing error handling.
##########
src/paimon/core/snapshot.cpp:
##########
@@ -323,11 +332,42 @@ void Snapshot::FromJson(const rapidjson::Value& obj)
noexcept(false) {
RapidJsonUtil::DeserializeKeyValue<std::optional<int64_t>>(obj,
FIELD_NEXT_ROW_ID);
}
-Result<Snapshot> Snapshot::FromPath(const std::shared_ptr<FileSystem>& fs,
- const std::string& path) {
- std::string json_str;
- PAIMON_RETURN_NOT_OK(fs->ReadFile(path, &json_str));
- PAIMON_ASSIGN_OR_RAISE(Snapshot snapshot,
Snapshot::FromJsonString(json_str));
+Result<Snapshot> Snapshot::FromPath(const std::shared_ptr<FileSystem>& fs,
const std::string& path,
+ MemoryPool* pool) {
+ if (pool == nullptr || pool == GetDefaultPool().get()) {
+ std::string json_str;
+ PAIMON_RETURN_NOT_OK(fs->ReadFile(path, &json_str));
+ return Snapshot::FromJsonString(json_str);
+ }
+
+ Bytes json;
+ {
+ PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<InputStream> input,
fs->Open(path));
+ if (input == nullptr) {
+ return Status::Invalid("input stream is not suppose to be a null
pointer, path: ",
+ path);
+ }
+ ScopeGuard close([&input]() { (void)input->Close(); });
+ PAIMON_ASSIGN_OR_RAISE(int64_t length, input->Length());
+ PAIMON_RETURN_NOT_OK(ValidateValueNonNegative(length, "read length"));
+ const uint64_t allocation_size = static_cast<uint64_t>(length) + 1;
+ PAIMON_RETURN_NOT_OK(ValidateValueInRange<size_t>(allocation_size,
"snapshot buffer size"));
+ try {
+ json = Bytes(static_cast<size_t>(allocation_size), pool);
+ } catch (const std::bad_alloc&) {
+ return Status::OutOfMemory("cannot allocate ", allocation_size, "
bytes for snapshot ",
+ path);
+ }
+ PAIMON_ASSIGN_OR_RAISE(int64_t read_length, input->Read(json.data(),
length));
+ if (read_length != length) {
+ return Status::IOError(fmt::format("path {}, expect read len {},
actual read len {}",
+ path, length, read_length));
+ }
+ json.data()[length] = '\0';
+ }
+ Snapshot snapshot;
Review Comment:
`FromJsonView()` now parses exactly the file length, with no extra byte or
manual terminator. It uses a bounded `MemoryStream` because this RapidJSON
version’s `Parse(data, size)` consumes a leading UTF-8 BOM, which the original
parser rejects. The embedded-NUL check remains.
--
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]