Implementation:4paradigm OpenMLDB Table Impl
| Knowledge Sources | |
|---|---|
| Domains | Database, Storage |
| Last Updated | 2026-10-12 00:00 GMT |
Overview
Concrete tool for storing and indexing time-series rows with multi-segment partitioning and multi-index support provided by the HybridSE ToyDB storage layer.
Description
The Table class (implemented in table_impl.cc) manages in-memory row storage with multiple index definitions. Each index maps composite keys to Segment objects, which internally use SkipList or LinkList for sorted time-series access. The table decodes primary keys and timestamps from encoded rows, hashes keys to select a segment, and stores data blocks with reference counting. It provides various iterator types: index iterators (by primary key), traverse iterators (full scan), and timestamp-seekable iterators.
Usage
Import this implementation when you need:
- In-memory time-series table storage with multiple indexes
- Primary key + timestamp-based row retrieval
- Segment-partitioned concurrent access
- Full table traversal and index-specific iteration
Code Reference
Source Location
- Repository: 4paradigm_OpenMLDB
- File: hybridse/examples/toydb/src/storage/table_impl.cc
- Lines: 1-446
Signature
class Table {
public:
Table(uint32_t id, uint32_t pid, const TableDef& table_def);
~Table();
bool Init();
bool Put(const char* row, uint32_t size);
bool DecodeKeysAndTs(const IndexSt& index, const char* row,
uint32_t size, std::string& key, int64_t* time_ptr);
std::unique_ptr<TableIterator> NewIndexIterator(
const std::string& pk, const uint32_t index);
std::unique_ptr<TableIterator> NewIterator(
const std::string& pk, const std::string& index_name);
std::unique_ptr<TableIterator> NewIterator(
const std::string& pk, const uint64_t ts);
std::unique_ptr<TableIterator> NewIterator(const std::string& pk);
std::unique_ptr<TableIterator> NewTraverseIterator(
const std::string& index_name);
std::unique_ptr<TableIterator> NewTraverseIterator();
};
class TableIterator {
public:
TableIterator(base::Iterator<uint64_t, DataBlock*>* ts_it);
TableIterator(Segment** segments, uint32_t seg_cnt);
void Seek(const uint64_t& time);
void Seek(const std::string& key, uint64_t ts);
bool Valid() const;
void Next();
void NextTs();
void NextTsInPks();
void SeekToFirst();
const base::Slice& GetValue();
const uint64_t& GetKey() const;
const base::Slice GetPK();
bool IsSeekable() const;
};
Import
#include "storage/table_impl.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| id | uint32_t | Yes | Table identifier |
| pid | uint32_t | Yes | Partition identifier |
| table_def | TableDef | Yes | Schema with columns and index definitions |
| row | const char* | Yes | Encoded row data to store |
| size | uint32_t | Yes | Row data size in bytes |
| pk | std::string | Yes | Primary key for index lookup |
| index_name | std::string | No | Specific index name (defaults to first) |
Outputs
| Name | Type | Description |
|---|---|---|
| Put returns | bool | True if row stored successfully |
| NewIterator returns | unique_ptr<TableIterator> | Iterator over rows by PK and timestamp |
| NewTraverseIterator returns | unique_ptr<TableIterator> | Full scan iterator across all segments |
| GetValue returns | base::Slice | Row data at current iterator position |
| GetKey returns | uint64_t& | Timestamp at current iterator position |
Usage Examples
Creating and Populating a Table
#include "storage/table_impl.h"
#include "codec/fe_row_codec.h"
// 1. Define table schema
hybridse::type::TableDef def;
auto* col = def.add_columns();
col->set_name("pk_col");
col->set_type(hybridse::type::kVarchar);
col = def.add_columns();
col->set_name("ts_col");
col->set_type(hybridse::type::kInt64);
col = def.add_columns();
col->set_name("value_col");
col->set_type(hybridse::type::kVarchar);
auto* index = def.add_indexes();
index->set_name("index1");
index->add_first_keys("pk_col");
index->set_second_key("ts_col");
// 2. Create and init table
hybridse::storage::Table table(1, 1, def);
table.Init();
// 3. Build and insert rows
hybridse::codec::RowBuilder builder(def.columns());
uint32_t row_size = builder.CalTotalLength(4);
std::string row(row_size, '\0');
builder.SetBuffer(reinterpret_cast<int8_t*>(&row[0]), row_size);
builder.AppendString("key1", 4);
builder.AppendInt64(1000);
builder.AppendString("val1", 4);
table.Put(row.c_str(), row.length());
// 4. Iterate by primary key
auto iter = table.NewIterator("key1");
iter->SeekToFirst();
while (iter->Valid()) {
// Process iter->GetValue()
iter->Next();
}