Skip to content
5 changes: 5 additions & 0 deletions cpp/src/common/record.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ struct TsRecord {
pa.init(512, common::MOD_TSFILE_READER);
}

TsRecord(const std::string &device_name, const int64_t &timestamp)
: device_id_(device_name), timestamp_(timestamp) {
pa.init(512, common::MOD_TSFILE_READER);
}

TsRecord(int64_t timestamp, const std::string &device_name,
int32_t point_count_in_row = 0)
: timestamp_(timestamp), device_id_(device_name), points_() {
Expand Down
4 changes: 1 addition & 3 deletions cpp/src/reader/qds_with_timegenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ struct Node {
class QDSWithTimeGenerator : public ResultSet {
public:
QDSWithTimeGenerator()
: row_record_(nullptr),
result_set_metadata_(nullptr),
: result_set_metadata_(nullptr),
io_reader_(nullptr),
qe_(nullptr),
tree_(nullptr),
Expand All @@ -129,7 +128,6 @@ class QDSWithTimeGenerator : public ResultSet {
int construct_node_tree(Expression *expr, Node *&node);

private:
RowRecord *row_record_;
std::shared_ptr<ResultSetMetadata> result_set_metadata_;
TsFileIOReader *io_reader_;
QueryExpression *qe_;
Expand Down
4 changes: 1 addition & 3 deletions cpp/src/reader/qds_without_timegenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ namespace storage {
class QDSWithoutTimeGenerator : public ResultSet {
public:
QDSWithoutTimeGenerator()
: row_record_(nullptr),
result_set_metadata_(nullptr),
: result_set_metadata_(nullptr),
io_reader_(nullptr),
qe_(nullptr),
ssi_vec_(),
Expand All @@ -54,7 +53,6 @@ class QDSWithoutTimeGenerator : public ResultSet {
int get_next_tsblock(uint32_t index, bool alloc_mem);

private:
RowRecord *row_record_;
std::shared_ptr<ResultSetMetadata> result_set_metadata_;
TsFileIOReader *io_reader_;
QueryExpression *qe_;
Expand Down
70 changes: 68 additions & 2 deletions cpp/src/reader/result_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#define READER_QUERY_DATA_SET_H

#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_map>

Expand Down Expand Up @@ -84,6 +83,8 @@ class ResultSetMetadata {
std::vector<common::TSDataType> column_types_;
};

class ResultSetIterator;

/**
* @brief ResultSet is the query result of the TsfileReader. It provides access
* to the results.
Expand All @@ -96,7 +97,7 @@ class ResultSetMetadata {
* it should be QDSWithTimeGenerator.
* @note If the query uses the table model, the cast should be TableResultSet
*/
class ResultSet {
class ResultSet : std::enable_shared_from_this<ResultSet> {
public:
ResultSet() {}
virtual ~ResultSet() {}
Expand All @@ -122,6 +123,11 @@ class ResultSet {
*/
virtual bool is_null(uint32_t column_index) = 0;

/**
* @brief Simple iterator for ResultSet with smart pointers
*/
virtual ResultSetIterator iterator();

/**
* @brief Get the value of the column by column name
*
Expand Down Expand Up @@ -196,6 +202,7 @@ class ResultSet {
std::unordered_map<std::string, uint32_t, CaseInsensitiveHash,
CaseInsensitiveEqual>
index_lookup_;
RowRecord* row_record_ = nullptr;
common::PageArena pa_;
};

Expand Down Expand Up @@ -231,6 +238,65 @@ inline std::tm ResultSet::get_value(uint32_t column_index) {
return row_record->get_field(column_index)->get_date_value();
}

/**
* @brief Simple iterator for ResultSet with smart pointers
*/
class ResultSetIterator {
public:
explicit ResultSetIterator(ResultSet* result_set)
: result_set_(result_set) {}

/**
* @brief Check if there is a next row available
*/
bool hasNext() {
if (cached_record_ != nullptr) {
return true;
}
if (exhausted_) {
return false;
}

bool has_next = false;
if (result_set_) {
int ret = result_set_->next(has_next);
ASSERT(ret == 0);
if (has_next) {
cached_record_ = result_set_->get_row_record();
} else {
exhausted_ = true;
}
}
return has_next;
}

/**
* @brief Get the next row record
*/
RowRecord* next() {
if (!hasNext()) {
return nullptr;
}
RowRecord* ret = cached_record_;
cached_record_ = nullptr;
return ret;
}

/**
* @brief Get the underlying ResultSet for direct access
*/
ResultSet* getResultSet() const { return result_set_; }

private:
ResultSet* result_set_ = nullptr;
RowRecord* cached_record_ = nullptr;
bool exhausted_ = false;
};

inline ResultSetIterator ResultSet::iterator() {
return ResultSetIterator(this);
}

} // namespace storage

#endif // READER_QUERY_DATA_SET_H
1 change: 0 additions & 1 deletion cpp/src/reader/table_result_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class TableResultSet : public ResultSet {
std::unique_ptr<TsBlockReader> tsblock_reader_;
common::RowIterator* row_iterator_ = nullptr;
common::TsBlock* tsblock_ = nullptr;
RowRecord* row_record_ = nullptr;
std::shared_ptr<ResultSetMetadata> result_set_metadata_;
std::vector<std::unique_ptr<TsBlockReader>> tsblock_readers_;
std::vector<std::string> column_names_;
Expand Down
14 changes: 14 additions & 0 deletions cpp/src/reader/tsfile_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ std::vector<std::shared_ptr<IDeviceID>> TsFileReader::get_all_devices(
return device_ids;
}

std::vector<std::shared_ptr<IDeviceID>> TsFileReader::get_all_device_ids() {
TsFileMeta* tsfile_meta = tsfile_executor_->get_tsfile_meta();
std::vector<std::shared_ptr<IDeviceID>> device_ids;
if (tsfile_meta != nullptr) {
PageArena pa;
pa.init(512, MOD_TSFILE_READER);
for (auto entry : tsfile_meta->table_metadata_index_node_map_) {
auto index_node = entry.second;
get_all_devices(device_ids, index_node, pa);
}
}
return device_ids;
}

int TsFileReader::get_all_devices(
std::vector<std::shared_ptr<IDeviceID>>& device_ids,
std::shared_ptr<MetaIndexNode> index_node, PageArena& pa) {
Expand Down
8 changes: 8 additions & 0 deletions cpp/src/reader/tsfile_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ class TsFileReader {
*/
std::vector<std::shared_ptr<IDeviceID>> get_all_devices(
std::string table_name);

/**
* @brief get all devices in the tsfile
*
* @return std::vector<std::shared_ptr<IDeviceID>> the device id list
*/
std::vector<std::shared_ptr<IDeviceID>> get_all_device_ids();

/**
* @brief get the timeseries schema by the device id and measurement name
*
Expand Down
71 changes: 71 additions & 0 deletions cpp/src/reader/tsfile_tree_reader.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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 "reader/tsfile_tree_reader.h"

namespace storage {

TsFileTreeReader::TsFileTreeReader() {
tsfile_reader_ = std::make_shared<TsFileReader>();
}

TsFileTreeReader::~TsFileTreeReader() = default;

int TsFileTreeReader::open(const std::string &file_path) {
return tsfile_reader_->open(file_path);
}

int TsFileTreeReader::close() { return tsfile_reader_->close(); }

int TsFileTreeReader::query(const std::vector<std::string> &device_ids,
const std::vector<std::string> &measurement_names,
int64_t start_time, int64_t end_time,
ResultSet *&result_set) {
std::vector<std::string> path_list;
for (auto &device_id : device_ids) {
for (auto &measurement : measurement_names) {
path_list.emplace_back(device_id + PATH_SEPARATOR_CHAR +
measurement);
}
}
return tsfile_reader_->query(path_list, start_time, end_time, result_set);
}

void TsFileTreeReader::destroy_query_data_set(ResultSet *qds) {
tsfile_reader_->destroy_query_data_set(qds);
}

std::vector<MeasurementSchema> TsFileTreeReader::get_device_schema(
const std::string &device_id) {
std::vector<MeasurementSchema> schemas;
tsfile_reader_->get_timeseries_schema(
std::make_shared<StringArrayDeviceID>(device_id), schemas);
return schemas;
}

std::vector<std::string> TsFileTreeReader::get_all_device_ids() {
std::vector<std::string> ret_device_ids;
auto device_ids = tsfile_reader_->get_all_device_ids();
for (auto device_id : device_ids) {
ret_device_ids.emplace_back(device_id->get_device_name());
}
return ret_device_ids;
}

} // namespace storage
107 changes: 107 additions & 0 deletions cpp/src/reader/tsfile_tree_reader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* 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.
*/

#ifndef READER_TSFILE_TREE_READER_H
#define READER_TSFILE_TREE_READER_H

#include <memory>
#include <string>
#include <vector>

#include "tsfile_reader.h"

namespace storage {

class TsFileTreeReader {
public:
TsFileTreeReader();
~TsFileTreeReader();

/**
* @brief open the tsfile
*
* @param file_path the path of the tsfile which will be opened
* @return Returns 0 on success, or a non-zero error code on failure.
*/
int open(const std::string &file_path);

/**
* @brief close the tsfile, this method should be called after the
* query is finished
*
* @return Returns 0 on success, or a non-zero error code on failure.
*/
int close();

/**
* @brief Query time series data for specified devices and measurements
* within a time range
*
* @param device_ids List of device identifiers to query
* @param measurement_names List of measurement names to query
* @param start_time Start timestamp of the query range (inclusive)
* @param end_time End timestamp of the query range (inclusive)
* @param[out] result_set Pointer to the ResultSet that will contain query
* results
* @return Returns 0 on success, or a non-zero error code on failure.
* The caller is responsible for destroying the result set using
* destroy_query_data_set()
*/
int query(const std::vector<std::string> &device_ids,
const std::vector<std::string> &measurement_names,
int64_t start_time, int64_t end_time, ResultSet *&result_set);

/**
* @brief Destroy and deallocate the query result set
*
* @param result_set Pointer to the ResultSet to be destroyed
* @note This method should be called after the result set is no longer
* needed to prevent memory leaks
*/
void destroy_query_data_set(ResultSet *result_set);

/**
* @brief Get the measurement schema for a specific device
*
* @param device_id The device identifier
* @return Pointer to the MeasurementSchema for the device, or nullptr if
* not found
* @note The caller should not delete the returned pointer as it's managed
* by the reader
*/
std::vector<MeasurementSchema> get_device_schema(
const std::string &device_id);

/**
* @brief Get all device identifiers in the TsFile
*
* @return Vector containing all device identifiers found in the TsFile
* @note The returned vector will be empty if no devices are found or file
* is not opened
*/
std::vector<std::string> get_all_device_ids();

private:
std::shared_ptr<TsFileReader>
tsfile_reader_; ///< Underlying TsFile reader implementation
};

} // namespace storage

#endif // READER_TSFILE_TREE_READER_H
Loading
Loading