This commit is contained in:
2026-02-15 21:21:56 -05:00
parent 8be13e14bc
commit d6c98bcf10
10 changed files with 139 additions and 84 deletions

View File

@@ -11,6 +11,7 @@ pub const DB_VERSION: u64 = 0;
#[derive(Clone)]
pub struct Db {
db: Database,
pub account_tokens: DbMap<String, ServerPerms>,
pub msgs: DbMap<MsgId, Msg>,
pub users: DbMap<UserId, User>,
pub usernames: DbMap<String, UserId>,
@@ -23,6 +24,7 @@ pub type ImageId = ImageIdV0;
pub type User = UserV0;
pub type Msg = MsgV0;
pub type ChannelInfo = ChannelV0;
pub type ServerPerms = ServerPermsV0;
impl Db {
pub fn open(path: impl AsRef<Path>) -> Db {
@@ -34,10 +36,11 @@ impl Db {
panic!("non matching db version! (auto update in the future)");
}
} else {
println!("no previous db found, creating new");
println!("no previous db found, creating new version {DB_VERSION}");
info.insert("version", &DB_VERSION);
}
Db {
account_tokens: DbMap::open("account_token", &db),
msgs: DbMap::open("msg", &db),
users: DbMap::open("user", &db),
usernames: DbMap::open("username", &db),

View File

@@ -46,6 +46,14 @@ impl<K: Key<Output = K>, V: Encode + DecodeOwned> DbMap<K, V> {
Some(bitcode::decode(&v).unwrap())
}
pub fn len(&self) -> usize {
self.keyspace.approximate_len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn iter(&self) -> impl Iterator<Item = (K, V)> {
self.db.read_tx().iter(self)
}
@@ -129,6 +137,12 @@ impl WriteTx {
self.0.get(&map.keyspace, k).unwrap().is_some()
}
pub fn remove<K: Key<Output = K>, V: DecodeOwned>(&mut self, map: &DbMap<K, V>, k: K) -> Option<V> {
let k = Slice::new(k.to_bytes().as_ref());
let v = self.0.take(&map.keyspace, k).unwrap()?;
Some(bitcode::decode(&v).unwrap())
}
// TODO: K2 IS NOT A SAFE ABSTRACTION!! need to have KeyLike which has key assoc type
pub fn insert<K: Key<Output = K>, K2: Key<Output = K> + ?Sized, V: Encode>(
&mut self,

View File

@@ -3,12 +3,20 @@ pub type MsgIdV0 = i128;
pub type ChannelIdV0 = u64;
pub type ImageIdV0 = u64;
#[derive(Clone, Copy, bitcode::Encode, bitcode::Decode)]
pub struct ServerPermsV0(u32);
impl ServerPermsV0 {
pub const NONE: Self = Self(0);
pub const ACCOUNT_TOKENS: Self = Self(1 << 0);
}
#[derive(bitcode::Encode, bitcode::Decode)]
pub struct UserV0 {
pub username: String,
pub password_hash: String,
pub pfp: Option<ImageIdV0>,
pub bio: String,
pub server_perms: ServerPermsV0,
}
#[derive(bitcode::Encode, bitcode::Decode)]
@@ -21,3 +29,9 @@ pub struct ChannelV0 {
pub name: String,
pub desc: String,
}
impl ServerPermsV0 {
pub fn contains(&self, other: Self) -> bool {
(self.0 & other.0) == other.0
}
}