Skip to content
This repository has been archived by the owner on Aug 4, 2024. It is now read-only.

Commit

Permalink
pref: optimize the structure of write buf to avoid frequent copying o…
Browse files Browse the repository at this point in the history
…f keys
  • Loading branch information
KKould committed Sep 7, 2023
1 parent 316b853 commit 660cbcf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kip_db"
version = "0.1.2-alpha.14"
version = "0.1.2-alpha.15"
edition = "2021"
authors = ["Kould <[email protected]>"]
description = "轻量级、异步 基于LSM Leveled Compaction K-V数据库"
Expand Down
23 changes: 11 additions & 12 deletions src/kernel/lsm/mvcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ use std::sync::Arc;
use tokio::sync::mpsc::error::TrySendError;
use tokio::sync::mpsc::Sender;

type MapIter<'a> =
Map<skiplist::skipmap::Iter<'a, Bytes, KeyValue>, fn((&Bytes, &KeyValue)) -> KeyValue>;
type MapIter<'a> = Map<
skiplist::skipmap::Iter<'a, Bytes, Option<Bytes>>,
fn((&Bytes, &Option<Bytes>)) -> KeyValue,
>;

unsafe impl Send for BufPtr {}
unsafe impl Sync for BufPtr {}
Expand All @@ -32,7 +34,7 @@ pub struct Transaction {
pub(crate) compactor_tx: Sender<CompactTask>,

pub(crate) version: Arc<Version>,
pub(crate) writer_buf: SkipMap<Bytes, KeyValue>,
pub(crate) writer_buf: SkipMap<Bytes, Option<Bytes>>,
pub(crate) seq_id: i64,
}

Expand All @@ -42,7 +44,7 @@ impl Transaction {
/// 此处不需要等待压缩,因为在Transaction存活时不会触发Compaction
#[inline]
pub fn get(&self, key: &[u8]) -> Result<Option<Bytes>> {
if let Some((_, value)) = self.writer_buf.get(key) {
if let Some(value) = self.writer_buf.get(key) {
return Ok(value.clone());
}

Expand All @@ -58,18 +60,15 @@ impl Transaction {
}

#[inline]
pub fn set(&mut self, key: &[u8], value: Bytes) {
let bytes = Bytes::copy_from_slice(key);

let _ignore = self.writer_buf.insert(bytes.clone(), (bytes, Some(value)));
pub fn set(&mut self, key: Bytes, value: Bytes) {
let _ignore = self.writer_buf.insert(key, Some(value));
}

#[inline]
pub fn remove(&mut self, key: &[u8]) -> Result<()> {
let _ = self.get(key)?.ok_or(KernelError::KeyNotFound)?;

let bytes = Bytes::copy_from_slice(key);
let _ignore = self.writer_buf.insert(bytes.clone(), (bytes, None));
let _ignore = self.writer_buf.insert(bytes, None);

Ok(())
}
Expand All @@ -80,7 +79,7 @@ impl Transaction {
let batch_data = self
.writer_buf
.iter()
.map(|(_, item)| item.clone())
.map(|(key, value)| (key.clone(), value.clone()))
.collect_vec();

if mem_table.insert_batch_data(batch_data, Sequence::create())? {
Expand Down Expand Up @@ -111,7 +110,7 @@ impl Transaction {
min.map(Bytes::copy_from_slice).as_ref(),
max.map(Bytes::copy_from_slice).as_ref(),
)
.map(|(_, value)| (value.clone()))
.map(|(key, value)| (key.clone(), value.clone()))
}

fn mem_table(&self) -> &MemTable {
Expand Down

0 comments on commit 660cbcf

Please sign in to comment.