Skip to content

Commit

Permalink
[CPP]. add readme for cpp and some examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinLeeo committed May 31, 2024
1 parent 5849181 commit 25c4c23
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 124 deletions.
39 changes: 21 additions & 18 deletions cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,44 +33,47 @@ ___________ ___________.__.__

## Introduction

This directory contains the implementation of the tsfile in the C++ version. Currently, the C++ version has implemented the query and write functions of TsFile, and supports time filtering query.

You can find all source code at ./src, some c examples at ./examples, and a bench_mark of TsFile_cpp at ./bench_mark.
Besides you can find a C function wrapper at ./src/cwrapper, and the Python tool depends on this directory.
This directory contains the C++ implementation of TsFile. The C++ version currently supports the query and write functions of TsFile, including time filtering queries.

The source code can be found in the `./src` directory. C/C++ examples are located in the `./examples` directory, and a benchmark for TsFile_cpp can be found in the `./bench_mark` directory. Additionally, a C function wrapper is available in the `./src/cwrapper` directory, which the Python tool relies on.

## How to make contributions

We use clang-format to make C++ codes obey a consistent ruleset defined in ./clang-format. it's like Google style.
We use `clang-format` to ensure that our C++ code adheres to a consistent set of rules defined in `./clang-format`. This is similar to the Google style.

**Feature List**:

- [ ] add some UTs about the readerwritercompression etc.
- [ ] add UTs about cwrapper.
- [ ] supports multiple flushes of data.
- [ ] supports aligned timeseries.
- [ ] supports describe a table in tsfile.
- [ ] supports get all tables' schema/names.
- [ ] support automatic flush.
- [ ] supports out-of-order data writing
- [ ] support TsFile V4. TsFile CPP does not implement support for the table model, therefore there are differences in file output compared to the Java version.
- [ ] Add unit tests for the reader, writer, compression, etc.
- [ ] Add unit tests for the C wrapper.
- [ ] Support multiple data flushes.
- [ ] Support aligned timeseries.
- [ ] Support table description in tsfile.
- [ ] Retrieve all table schemas/names.
- [ ] Implement automatic flush.
- [ ] Support out-of-order data writing.
- [ ] Support TsFile V4. Note: TsFile CPP does not implement support for the table model, therefore there are differences in file output compared to the Java version.

**Bug List**:

- [ ] Flushing without writing after registering a timeseries will cause a core dump.
- [ ] Misalignment in memory may lead to a bus error.

Any bugs report is welcome, you can open an issue with a title starting with [CPP] to describe the bug, like:https://github.com/apache/tsfile/issues/94

We welcome any bug reports. You can open an issue with a title starting with [CPP] to describe the bug, like: https://github.com/apache/tsfile/issues/94

## Build

### requirement
### Requirements

```
```bash
sudo apt-get update
sudo apt-get install -y cmake make g++ clang-format
```

To build tsfile, you can run: `bash build.sh`. If you have Maven tools, you can run: `mvn package -P with-cpp clean verify`. Then, you can find the shared object at `./build`.

Before you submit your code to GitHub, please ensure that the `mvn` compilation is correct.

## Use TsFile

## Use TsFile
You can find examples on how to read and write data in `demo_read.cpp` and `demo_write.cpp` located under `./examples/cpp_examples`. There are also examples under `./examples/c_examples`on how to use a C-style API to read and write data in a C environment
23 changes: 0 additions & 23 deletions cpp/examples/CMakeLists.txt

This file was deleted.

35 changes: 0 additions & 35 deletions cpp/examples/build.sh

This file was deleted.

48 changes: 0 additions & 48 deletions cpp/examples/c_examples/CMakeLists.txt

This file was deleted.

107 changes: 107 additions & 0 deletions cpp/examples/cpp_examples/demo_read.cpp
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.
*/

#include <iostream>
#include <vector>
#include <string>
#include "common/path.h"
#include "reader/filter/filter.h"
#include "reader/expression.h"
#include "reader/tsfile_reader.h"
#include "reader/qds_with_timegenerator.h"
#include "reader/qds_without_timegenerator.h"
#include "common/row_record.h"

std::string field_to_string(storage::Field *value)
{
if (value->type_ == common::TEXT) {
return std::string(value->value_.sval_);
} else {
std::stringstream ss;
switch (value->type_) {
case common::BOOLEAN:
ss << (value->value_.bval_ ? "true" : "false");
break;
case common::INT32:
ss << value->value_.ival_;
break;
case common::INT64:
ss << value->value_.lval_;
break;
case common::FLOAT:
ss << value->value_.fval_;
break;
case common::DOUBLE:
ss << value->value_.dval_;
break;
case common::NULL_TYPE:
ss << "NULL";
break;
default:
ASSERT(false);
break;
}
return ss.str();
}
}

int main (int argc, char **argv)
{
std::cout<<"begin to read tsfile from /tmp/t1" << std::endl;
std::string device_name = "root.db001.dev001";
std::string measurement_name = "m001";
storage::Path p1(device_name, measurement_name);
std::vector<storage::Path> select_list;
select_list.push_back(p1);
storage::QueryExpression *query_expr =
storage::QueryExpression::create(select_list, nullptr);

common::init_config_value();
storage::TsFileReader reader;
int ret = reader.open("/tmp/t1");

std::cout << "begin to query expr" << std::endl;
ASSERT(ret == 0);
storage::QueryDataSet *qds = nullptr;
ret = reader.query(query_expr, qds);


storage::RowRecord *record;
std::cout << "begin to dump data from tsfile ---" << std::endl;
int row_cout = 0;
do {
record = qds->get_next();
if (record) {
std::cout<< "dump QDS : " << record->get_timestamp() << ",";
if (record) {
int size = record->get_fields()->size();
for (int i = 0; i < size; ++i) {
std::cout << field_to_string(record->get_field(i)) << ",";
}
std::cout<< std::endl;
row_cout++;
}
} else {
break;
}
} while (true);

return (0);
}

59 changes: 59 additions & 0 deletions cpp/examples/cpp_examples/demo_write.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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 <iostream>
#include <string>
#include <time.h>
#include "writer/tsfile_writer.h"
#include "common/record.h"
#include "common/db_common.h"

int main (int argc, char **argv)
{
std::cout<<"get in."<<std::endl;
storage::TsFileWriter tsfile_writer;
std::string device_name = "root.db001.dev001";
std::string measurement_name = "m001";
storage::libtsfile_init();
tsfile_writer.open("/tmp/t1",O_CREAT | O_RDWR, 0644);
int ret = tsfile_writer.register_timeseries(device_name, measurement_name, common::INT32, common::PLAIN, common::UNCOMPRESSED);
std::cout<<"register finish" << device_name<< std::endl;

int row_count = 100;
for (int i = 1; i < row_count; ++i) {
storage::DataPoint point(measurement_name, 10000 + i);
storage::TsRecord record(i, device_name,1);
record.points_.push_back(point);
ret = tsfile_writer.write_record(record);
std::cout<< "write point:" << measurement_name << " " << 10000 + i << std::endl;
ASSERT(ret == 0);
}

tsfile_writer.flush();
std::cout<<"finish flush"<<std::endl;
tsfile_writer.close();
std::cout<<"tsfile closed."<<std::endl;
storage::libtsfile_destroy();
std::cout<< "tsfile to destory."<<std::endl;
std::cout<<"finish writing" << std::endl;
std::cout<<"will close our files"<<std::endl;



return ret;
}

0 comments on commit 25c4c23

Please sign in to comment.