Skip to content

Commit

Permalink
feat: implement a basic columnar memory table (#1164)
Browse files Browse the repository at this point in the history
## Rationale
Part of `Implementing a column-based memtable` #1093 

## Detailed Changes
Include #1006 #1081 

## Test Plan
Pass all tests and integration-test.

---------

Co-authored-by: tanruixiang <tanruixiang0104@gmail.com>
  • Loading branch information
chunshao90 and tanruixiang authored Aug 28, 2023
1 parent 06e985f commit ee54c5c
Show file tree
Hide file tree
Showing 39 changed files with 3,201 additions and 1,386 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion analytic_engine/src/instance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ impl SpaceStore {
let spaces = self.spaces.read().unwrap().list_all_spaces();
spaces.into_iter().max_by_key(|t| t.memtable_memory_usage())
}

/// The memory space used by all tables in the space.
#[inline]
fn total_memory_usage_space(&self) -> usize {
let spaces = self.spaces.read().unwrap().list_all_spaces();
spaces.into_iter().map(|t| t.memtable_memory_usage()).sum()
}
}

/// Table engine instance
Expand Down Expand Up @@ -268,7 +275,7 @@ impl Instance {
#[inline]
fn should_flush_instance(&self) -> bool {
self.db_write_buffer_size > 0
&& self.mem_usage_collector.total_memory_allocated() >= self.db_write_buffer_size
&& self.space_store.total_memory_usage_space() >= self.db_write_buffer_size
}

#[inline]
Expand Down
1 change: 1 addition & 0 deletions analytic_engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//! Analytic table engine implementations

#![feature(option_get_or_insert_default)]

mod compaction;
mod context;
mod engine;
Expand Down
51 changes: 51 additions & 0 deletions analytic_engine/src/memtable/columnar/factory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2023 The CeresDB Authors
//
// Licensed 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.

//! Columnar memtable factory

use std::{
collections::HashMap,
sync::{
atomic::{AtomicU64, AtomicUsize},
Arc, RwLock,
},
};

use crate::memtable::{
columnar::ColumnarMemTable,
factory::{Factory, Options, Result},
MemTableRef,
};

/// Factory to create memtable
#[derive(Debug)]
pub struct ColumnarMemTableFactory;

impl Factory for ColumnarMemTableFactory {
fn create_memtable(&self, opts: Options) -> Result<MemTableRef> {
let memtable = Arc::new(ColumnarMemTable {
memtable: Arc::new(RwLock::new(HashMap::with_capacity(
opts.schema.num_columns(),
))),
schema: opts.schema.clone(),
last_sequence: AtomicU64::new(opts.creation_sequence),
row_num: AtomicUsize::new(0),
opts,
memtable_size: AtomicUsize::new(0),
metrics: Default::default(),
});

Ok(memtable)
}
}
Loading

0 comments on commit ee54c5c

Please sign in to comment.