You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iggy.apache.org by gk...@apache.org on 2025/08/09 20:54:59 UTC

(iggy) branch improved_tpc_partition_ecs created (now ff74233b)

This is an automated email from the ASF dual-hosted git repository.

gkoszyk pushed a change to branch improved_tpc_partition_ecs
in repository https://gitbox.apache.org/repos/asf/iggy.git


      at ff74233b pt1 improved ecs

This branch includes the following new commits:

     new ff74233b pt1 improved ecs

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(iggy) 01/01: pt1 improved ecs

Posted by gk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

gkoszyk pushed a commit to branch improved_tpc_partition_ecs
in repository https://gitbox.apache.org/repos/asf/iggy.git

commit ff74233b151e742ffc615342658049642360aef9
Author: numminex <g....@gmail.com>
AuthorDate: Sat Aug 9 22:54:48 2025 +0200

    pt1 improved ecs
---
 core/server/src/slab/mod.rs        |   1 +
 core/server/src/slab/partitions.rs |  61 ++++++++++++++++++++-
 core/server/src/slab/traits_ext.rs | 106 +++++++++++++++++++++++++++++++++++++
 3 files changed, 166 insertions(+), 2 deletions(-)

diff --git a/core/server/src/slab/mod.rs b/core/server/src/slab/mod.rs
index b03c71ad..006205cc 100644
--- a/core/server/src/slab/mod.rs
+++ b/core/server/src/slab/mod.rs
@@ -2,6 +2,7 @@ pub mod consumer_groups;
 pub mod partitions;
 pub mod streams;
 pub mod topics;
+pub mod traits_ext;
 
 use ahash::AHashMap;
 use slab::Slab;
diff --git a/core/server/src/slab/partitions.rs b/core/server/src/slab/partitions.rs
index bb03a31d..fb0a1af1 100644
--- a/core/server/src/slab/partitions.rs
+++ b/core/server/src/slab/partitions.rs
@@ -1,7 +1,7 @@
-use crate::streaming::{
+use crate::{slab::traits_ext::{Borrow, ComponentsAsRefs, EntityComponentSystem, IntoComponents}, streaming::{
     deduplication::message_deduplicator::MessageDeduplicator, partitions::partition2, segments,
     stats::stats::PartitionStats,
-};
+}};
 use slab::Slab;
 use std::sync::{Arc, atomic::AtomicU64};
 
@@ -17,6 +17,63 @@ pub struct Partitions {
     partition_offsets: Slab<Arc<AtomicU64>>,
 }
 
+pub struct Part {
+    partition: partition2::Partition,
+    bless: (),
+}
+
+impl IntoComponents for Part {
+    type Components = (partition2::Partition, ());
+
+    fn into_components(self) -> Self::Components {
+        (self.partition, self.bless)
+    }
+}
+
+pub struct PartRef<'a> {
+    partition: &'a partition2::Partition,
+    bless: &'a (),
+}
+
+impl<'a> IntoComponents for PartRef<'a> {
+    type Components = (&'a partition2::Partition, &'a ());
+
+    fn into_components(self) -> Self::Components {
+        (self.partition, self.bless)
+    }
+}
+
+pub struct PartRefMut<'a> {
+    partition: &'a mut partition2::Partition,
+    bless: &'a mut (),
+}
+
+impl<'a> IntoComponents for PartRefMut<'a> {
+    type Components = (&'a mut partition2::Partition, &'a mut ()    );
+
+    fn into_components(self) -> Self::Components {
+        (self.partition, self.bless)
+    }
+}
+
+impl EntityComponentSystem<Borrow> for Partitions {
+    type Entity = Part;
+    type EntityRef<'a> = PartRef<'a>;
+
+    fn with_new<T, F>(&self, f: F) -> T
+    where
+        F: FnOnce(Self::EntityRef<'_>) -> T {
+        todo!()
+    }
+
+    async fn with_async_new<T, F>(&self, f: F) -> T
+    where
+        F: FnOnce(Self::EntityRef<'_>) -> T {
+        todo!()
+    }
+}
+
+
 impl Default for Partitions {
     fn default() -> Self {
         Self {
diff --git a/core/server/src/slab/traits_ext.rs b/core/server/src/slab/traits_ext.rs
new file mode 100644
index 00000000..79abce3f
--- /dev/null
+++ b/core/server/src/slab/traits_ext.rs
@@ -0,0 +1,106 @@
+pub trait IntoComponents {
+    type Components;
+    fn into_components(self) -> Self::Components;
+}
+
+pub struct Borrow;
+pub struct RefCell;
+
+mod private {
+    pub trait Sealed {}
+}
+
+pub trait ComponentsAsRefs<RefType>: private::Sealed {
+    type RefMapping<'a>;
+    type RefMutMapping<'a>;
+}
+macro_rules! impl_components_as_refs {
+    ($T:ident) => {
+        impl<$T> private::Sealed for ($T,) {}
+        
+        impl<$T> ComponentsAsRefs<Borrow> for ($T,)
+        where for<'a> $T: 'a
+        {
+            type RefMapping<'a> = (&'a $T,);
+            type RefMutMapping<'a> = (&'a mut $T,);
+        }
+
+        impl<$T> ComponentsAsRefs<RefCell> for ($T,)
+        where for<'a> $T: 'a
+        {
+            type RefMapping<'a> = (std::cell::Ref<'a, $T>,);
+            type RefMutMapping<'a> = (std::cell::RefMut<'a, $T>,);
+        }
+    };
+
+    ($T:ident, $($rest:ident),+) => {
+        impl<$T, $($rest),+> private::Sealed for ($T, $($rest),+) {}
+        
+        impl<$T, $($rest),+> ComponentsAsRefs<Borrow> for ($T, $($rest),+)
+        where
+            for<'a> $T: 'a,
+            $(for<'a> $rest: 'a),+
+        {
+            type RefMapping<'a> = (&'a $T, $(&'a $rest),+);
+            type RefMutMapping<'a> = (&'a mut $T, $(&'a mut $rest),+);
+        }
+
+        impl<$T, $($rest),+> ComponentsAsRefs<RefCell> for ($T, $($rest),+)
+        where
+            for<'a> $T: 'a,
+            $(for<'a> $rest: 'a),+
+        {
+            type RefMapping<'a> = (std::cell::Ref<'a, $T>, $(std::cell::Ref<'a, $rest>),+);
+            type RefMutMapping<'a> = (std::cell::RefMut<'a, $T>, $(std::cell::RefMut<'a, $rest>),+);
+        }
+        impl_components_as_refs!($($rest),+);
+    };
+}
+impl_components_as_refs!(T1, T2, T3, T4, T5, T6, T7, T8);
+
+type ComponentsTuple<'a, T, RefType> =
+    <<T as IntoComponents>::Components as ComponentsAsRefs<RefType>>::RefMapping<'a>;
+type ComponentsTupleMut<'a, T, RefType> =
+    <<T as IntoComponents>::Components as ComponentsAsRefs<RefType>>::RefMutMapping<'a>;
+
+// TODO: extend it with a method `with_by_id`, this one actually will use the `EntityRef`
+// those `with` methods need to work on `impl Iterator<Item = ComponentsAsRefs individual components>`
+pub trait EntityComponentSystem<RefType> {
+    type Entity: IntoComponents;
+    type EntityRef<'a>: IntoComponents<Components = ComponentsTuple<'a, Self::Entity, RefType>>
+    where
+        <Self::Entity as IntoComponents>::Components: ComponentsAsRefs<RefType>,
+        Self: 'a;
+
+    fn with_new<T, F>(&self, f: F) -> T
+    where
+        F: FnOnce(Self::EntityRef<'_>) -> T;
+
+    fn with_async_new<T, F>(&self, f: F) -> impl Future<Output = T>
+    where
+        F: FnOnce(Self::EntityRef<'_>) -> T;
+}
+
+pub trait EntityComponentSystemMut: EntityComponentSystem<Borrow> {
+    type EntityRefMut<'a>: IntoComponents<Components = ComponentsTupleMut<'a, Self::Entity, Borrow>>
+    where
+        <Self::Entity as IntoComponents>::Components: ComponentsAsRefs<Borrow>,
+        Self: 'a;
+
+    fn with_mut_new<T, F>(&mut self, f: F) -> T
+    where
+        F: FnOnce(Self::EntityRefMut<'_>) -> T;
+}
+
+pub trait EntityComponentSystemMutCell: EntityComponentSystem<RefCell> {
+    type EntityRefMut<'a>: IntoComponents<
+        Components = ComponentsTupleMut<'a, Self::Entity, RefCell>,
+    >
+    where
+        <Self::Entity as IntoComponents>::Components: ComponentsAsRefs<RefCell>,
+        Self: 'a;
+
+    fn with_mut_new<T, F>(&mut self, f: F) -> T
+    where
+        F: FnOnce(Self::EntityRefMut<'_>) -> T;
+}