pulsedb/collective/
types.rs1use serde::{Deserialize, Serialize};
7
8use crate::types::{CollectiveId, Timestamp};
9
10#[derive(Clone, Debug, Serialize, Deserialize)]
28pub struct Collective {
29 pub id: CollectiveId,
31
32 pub name: String,
34
35 pub owner_id: Option<String>,
40
41 pub embedding_dimension: u16,
46
47 pub created_at: Timestamp,
49
50 pub updated_at: Timestamp,
52}
53
54impl Collective {
55 pub fn new(name: impl Into<String>, embedding_dimension: u16) -> Self {
60 let now = Timestamp::now();
61 Self {
62 id: CollectiveId::new(),
63 name: name.into(),
64 owner_id: None,
65 embedding_dimension,
66 created_at: now,
67 updated_at: now,
68 }
69 }
70
71 pub fn with_owner(
73 name: impl Into<String>,
74 owner_id: impl Into<String>,
75 embedding_dimension: u16,
76 ) -> Self {
77 let mut collective = Self::new(name, embedding_dimension);
78 collective.owner_id = Some(owner_id.into());
79 collective
80 }
81}
82
83#[derive(Clone, Debug)]
88pub struct CollectiveStats {
89 pub experience_count: u64,
91 pub storage_bytes: u64,
93 pub oldest_experience: Option<Timestamp>,
95 pub newest_experience: Option<Timestamp>,
97}
98
99#[cfg(test)]
100mod tests {
101 use super::*;
102
103 #[test]
104 fn test_collective_new() {
105 let collective = Collective::new("test-project", 384);
106 assert_eq!(collective.name, "test-project");
107 assert_eq!(collective.embedding_dimension, 384);
108 assert!(collective.owner_id.is_none());
109 assert!(collective.created_at == collective.updated_at);
110 }
111
112 #[test]
113 fn test_collective_with_owner() {
114 let collective = Collective::with_owner("test-project", "user-1", 768);
115 assert_eq!(collective.name, "test-project");
116 assert_eq!(collective.owner_id.as_deref(), Some("user-1"));
117 assert_eq!(collective.embedding_dimension, 768);
118 }
119
120 #[test]
121 fn test_collective_bincode_roundtrip() {
122 let collective = Collective::new("roundtrip-test", 384);
123 let bytes = bincode::serialize(&collective).unwrap();
124 let restored: Collective = bincode::deserialize(&bytes).unwrap();
125
126 assert_eq!(collective.id, restored.id);
127 assert_eq!(collective.name, restored.name);
128 assert_eq!(collective.owner_id, restored.owner_id);
129 assert_eq!(collective.embedding_dimension, restored.embedding_dimension);
130 assert_eq!(collective.created_at, restored.created_at);
131 assert_eq!(collective.updated_at, restored.updated_at);
132 }
133
134 #[test]
135 fn test_collective_bincode_roundtrip_with_owner() {
136 let collective = Collective::with_owner("owned-project", "tenant-42", 768);
137 let bytes = bincode::serialize(&collective).unwrap();
138 let restored: Collective = bincode::deserialize(&bytes).unwrap();
139
140 assert_eq!(collective.id, restored.id);
141 assert_eq!(collective.owner_id, restored.owner_id);
142 }
143}