Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions components/core/src/clp_s/ArchiveReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <string_view>
#include <utility>

#include <nlohmann/json_fwd.hpp>

#include "ArchiveReaderAdaptor.hpp"
#include "DictionaryReader.hpp"
#include "InputConfig.hpp"
Expand Down Expand Up @@ -159,6 +161,16 @@ class ArchiveReader {
return get_header().has_deprecated_timestamp_format();
}

/**
* @param log_event_idx
* @return The file-level metadata associated with the record at `log_event_idx`.
* @throws ArchiveReaderAdaptor::OperationFailed when `log_event_idx` cannot be mapped to any
* metadata.
*/
[[nodiscard]] auto get_metadata_for_log_event(int64_t log_event_idx) -> nlohmann::json const& {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should log event simply take an unsigned type like uint64_t?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can't since log_event_idx is int64_t.

return m_archive_reader_adaptor->get_metadata_for_log_event(log_event_idx);
}

private:
/**
* Initializes a schema reader passed by reference to become a reader for a given schema.
Expand Down
12 changes: 12 additions & 0 deletions components/core/src/clp_s/ArchiveReaderAdaptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ auto ArchiveReaderAdaptor::try_read_range_index(ZstdDecompressor& decompressor,
end_index,
std::move(range_index_entry.at(RangeIndexWriter::cMetadataFieldsName))
);
if (start_index != end_index) {
m_non_empty_range_metadata_map.emplace(end_index, m_range_index.back().fields);
}
Comment on lines +171 to +173
Copy link
Contributor

@Bill-hbrhbr Bill-hbrhbr Mar 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I've always had the question if the range index accepts files with empty ranges, or gaps between adjacent ranges.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we allow ranges where start_index == end_index to represent empty files, and those are excluded from this map since this map is for an API to look up the file-level metadata for a specific log record.

}
return ErrorCodeSuccess;
}
Expand Down Expand Up @@ -332,4 +335,13 @@ void ArchiveReaderAdaptor::checkin_reader_for_section(std::string_view section)

m_current_reader_holder.reset();
}

auto ArchiveReaderAdaptor::get_metadata_for_log_event(int64_t log_event_idx)
-> nlohmann::json const& {
auto const it{m_non_empty_range_metadata_map.upper_bound(log_event_idx)};
if (m_non_empty_range_metadata_map.end() == it || log_event_idx < 0) {
Comment on lines +371 to +372
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logic seems like you don't really need to look up anything if the log even idx is negative, which makes me feel more strongly about using uint64_t

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto per other comment

throw OperationFailed(ErrorCodeBadParam, __FILENAME__, __LINE__);
}
return it->second;
}
} // namespace clp_s
9 changes: 9 additions & 0 deletions components/core/src/clp_s/ArchiveReaderAdaptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define CLP_S_ARCHIVEREADERADAPTOR_HPP

#include <cstddef>
#include <functional>
#include <memory>
#include <optional>
#include <string>
Expand Down Expand Up @@ -89,6 +90,13 @@ class ArchiveReaderAdaptor {

std::vector<RangeIndexEntry> const& get_range_index() const { return m_range_index; }

/**
* @param log_event_idx
* @return The file-level metadata associated with the record at `log_event_idx`.
* @throws OperationFailed when `log_event_idx` cannot be mapped to any metadata.
*/
[[nodiscard]] auto get_metadata_for_log_event(int64_t log_event_idx) -> nlohmann::json const&;

private:
/**
* Tries to read an ArchiveFileInfo packet from the archive metadata.
Expand Down Expand Up @@ -176,6 +184,7 @@ class ArchiveReaderAdaptor {
std::shared_ptr<TimestampDictionaryReader> m_timestamp_dictionary;
std::shared_ptr<clp::ReaderInterface> m_reader;
std::vector<RangeIndexEntry> m_range_index;
std::map<int64_t, nlohmann::json> m_non_empty_range_metadata_map;
};
} // namespace clp_s
#endif // CLP_S_ARCHIVEREADERADAPTOR_HPP
13 changes: 13 additions & 0 deletions components/core/tests/test-clp_s-range_index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "../src/clp/type_utils.hpp"
#include "../src/clp_s/archive_constants.hpp"
#include "../src/clp_s/ArchiveReader.hpp"
#include "../src/clp_s/ArchiveReaderAdaptor.hpp"
#include "../src/clp_s/ArchiveWriter.hpp"
#include "../src/clp_s/InputConfig.hpp"
#include "../src/clp_s/RangeIndexWriter.hpp"
Expand All @@ -33,6 +34,7 @@ constexpr std::string_view cTestRangeIndexInputFile{"test_no_floats_sorted.jsonl
constexpr std::string_view cTestRangeIndexIRInputFile{"test_no_floats_sorted.ir"};
constexpr std::string_view cTestRangeIndexIRMetadataKey{"test_key"};
constexpr std::string_view cTestRangeIndexIRMetadataValue{"test_value"};
constexpr size_t cNumRecordsInInputFile{4};

namespace {
auto get_test_input_path_relative_to_tests_dir() -> std::filesystem::path;
Expand Down Expand Up @@ -138,6 +140,17 @@ void read_and_check_archive_metadata(bool from_ir) {
REQUIRE_NOTHROW(archive_reader.open(archive_path, clp_s::NetworkAuthOption{}));
auto const& range_index = archive_reader.get_range_index();
check_archive_range_index(range_index, from_ir);
for (size_t i{}; i < cNumRecordsInInputFile; ++i) {
REQUIRE(archive_reader.get_metadata_for_log_event(i) == range_index.at(0).fields);
}
REQUIRE_THROWS_AS(
archive_reader.get_metadata_for_log_event(-1),
clp_s::ArchiveReaderAdaptor::OperationFailed
);
REQUIRE_THROWS_AS(
archive_reader.get_metadata_for_log_event(cNumRecordsInInputFile),
clp_s::ArchiveReaderAdaptor::OperationFailed
);
REQUIRE_NOTHROW(archive_reader.close());
}
}
Expand Down
Loading