Skip to content

Commit

Permalink
refactor(logstore): remove Entry::namemspace (#3875)
Browse files Browse the repository at this point in the history
refactor(logstore): remove LogStore::namemspace and related associate types on Entry.

Signed-off-by: Lei, HUANG <[email protected]>
  • Loading branch information
v0y4g3r authored May 8, 2024
1 parent cc8d6b1 commit c07a1ba
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 48 deletions.
5 changes: 0 additions & 5 deletions src/log-store/src/kafka.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ pub struct EntryImpl {

impl Entry for EntryImpl {
type Error = Error;
type Namespace = NamespaceImpl;

fn data(&self) -> &[u8] {
&self.data
Expand All @@ -67,10 +66,6 @@ impl Entry for EntryImpl {
self.id
}

fn namespace(&self) -> Self::Namespace {
self.ns.clone()
}

fn estimated_size(&self) -> usize {
size_of::<Self>() + self.data.capacity() * size_of::<u8>() + self.ns.topic.capacity()
}
Expand Down
5 changes: 0 additions & 5 deletions src/log-store/src/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ impl Namespace for NamespaceImpl {

impl Entry for EntryImpl {
type Error = Error;
type Namespace = NamespaceImpl;

fn data(&self) -> &[u8] {
&[]
Expand All @@ -47,10 +46,6 @@ impl Entry for EntryImpl {
0
}

fn namespace(&self) -> Self::Namespace {
Default::default()
}

fn estimated_size(&self) -> usize {
0
}
Expand Down
18 changes: 3 additions & 15 deletions src/log-store/src/raft_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ impl Namespace for NamespaceImpl {

impl Entry for EntryImpl {
type Error = Error;
type Namespace = NamespaceImpl;

fn data(&self) -> &[u8] {
self.data.as_slice()
Expand All @@ -78,31 +77,20 @@ impl Entry for EntryImpl {
self.id
}

fn namespace(&self) -> Self::Namespace {
NamespaceImpl {
id: self.namespace_id,
..Default::default()
}
}

fn estimated_size(&self) -> usize {
size_of::<Self>() + self.data.capacity() * size_of::<u8>()
self.data.len() + size_of::<u64>() + size_of::<u64>()
}
}

#[cfg(test)]
mod tests {
use std::mem::size_of;

use store_api::logstore::entry::Entry;

use crate::raft_engine::protos::logstore::EntryImpl;

#[test]
fn test_estimated_size() {
let entry = EntryImpl::create(1, 1, Vec::with_capacity(100));
let expected = size_of::<EntryImpl>() + 100;
let got = entry.estimated_size();
assert_eq!(expected, got);
let entry = EntryImpl::create(1, 1, b"hello, world".to_vec());
assert_eq!(28, entry.estimated_size());
}
}
8 changes: 1 addition & 7 deletions src/store-api/src/logstore/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

use common_error::ext::ErrorExt;

use crate::logstore::namespace::Namespace;

/// An entry's id.
/// Different log store implementations may interpret the id to different meanings.
pub type Id = u64;
Expand All @@ -24,7 +22,6 @@ pub type Id = u64;
/// The log store implementation may have larger or smaller data storage unit than an entry.
pub trait Entry: Send + Sync {
type Error: ErrorExt + Send + Sync;
type Namespace: Namespace;

/// Returns the contained data of the entry.
fn data(&self) -> &[u8];
Expand All @@ -33,9 +30,6 @@ pub trait Entry: Send + Sync {
/// Usually the namespace id is identical with the region id.
fn id(&self) -> Id;

/// Returns the namespace of the entry.
fn namespace(&self) -> Self::Namespace;

/// Computes the estimated size in bytes of the entry.
/// Computes the estimated encoded size.
fn estimated_size(&self) -> usize;
}
17 changes: 1 addition & 16 deletions src/store-api/src/logstore/entry_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ pub type SendableEntryStream<'a, I, E> = Pin<Box<dyn Stream<Item = Result<Vec<I>
#[cfg(test)]
mod tests {
use std::any::Any;
use std::mem::size_of;
use std::task::{Context, Poll};

use common_error::ext::StackError;
Expand All @@ -50,15 +49,6 @@ mod tests {
#[snafu(visibility(pub))]
pub struct Error {}

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Namespace {}

impl crate::logstore::Namespace for Namespace {
fn id(&self) -> crate::logstore::namespace::Id {
0
}
}

impl ErrorExt for Error {
fn as_any(&self) -> &dyn Any {
self
Expand All @@ -75,7 +65,6 @@ mod tests {

impl Entry for SimpleEntry {
type Error = Error;
type Namespace = Namespace;

fn data(&self) -> &[u8] {
&self.data
Expand All @@ -85,12 +74,8 @@ mod tests {
0u64
}

fn namespace(&self) -> Self::Namespace {
Namespace {}
}

fn estimated_size(&self) -> usize {
self.data.capacity() * size_of::<u8>()
self.data.len()
}
}

Expand Down

0 comments on commit c07a1ba

Please sign in to comment.