This commit is contained in:
2025-09-13 16:54:33 +02:00
parent aa44716264
commit 164748601e
3 changed files with 58 additions and 21 deletions

View File

@@ -15,13 +15,12 @@ pub mut:
pub fn decoder_new(data []u8) Decoder {
mut e := Decoder{}
e.data = data
// e.data = data.reverse()
return e
}
pub fn (mut d Decoder) get_string() !string {
n := d.get_u16()!
// THIS IS ALWAYS TRYE BECAUSE u16 is max 64KB
// THIS IS ALWAYS TRUE BECAUSE u16 is max 64KB
// if n > 64 * 1024 { // 64KB limit
// return error('string length ${n} exceeds 64KB limit')
// }

View File

@@ -1,6 +1,7 @@
module db
import freeflowuniverse.herolib.data.ourtime
import freeflowuniverse.herolib.data.encoder
pub fn (mut self DB) set[T](obj_ T) !u32 {
// Get the next ID
@@ -14,15 +15,59 @@ pub fn (mut self DB) set[T](obj_ T) !u32 {
}
obj.updated_at = t
data := obj.dump()!
self.redis.hset(self.db_name[T](), obj.id.str(), data.bytestr())!
// id u32
// name string
// description string
// created_at i64
// updated_at i64
// securitypolicy u32
// tags u32 // when we set/get we always do as []string but this can then be sorted and md5ed this gies the unique id of tags
// comments []u32
mut e := encoder.new()
e.add_u8(1)
e.add_u32(obj.id)
e.add_string(obj.name)
e.add_string(obj.description)
e.add_i64(obj.created_at)
e.add_i64(obj.updated_at)
e.add_u32(obj.securitypolicy)
e.add_u32(obj.tags)
e.add_u16(u16(obj.comments.len))
for comment in obj.comments {
e.add_u32(comment)
}
println('aaaa: ${e.data.len} - ${obj.dump()!.len}')
e.data << obj.dump()!
println('bbbb: ${e.data.len} - ${obj.dump()!.len}')
self.redis.hset(self.db_name[T](), obj.id.str(), e.data.bytestr())!
return obj.id
}
// return the data, cannot return the object as we do not know the type
pub fn (mut self DB) get_data[T](id u32) ![]u8 {
pub fn (mut self DB) get_data[T](id u32) !(T, []u8) {
data := self.redis.hget(self.db_name[T](), id.str())!
return data.bytes()
if data.len == 0 {
return error('herodb:${self.db_name[T]()} not found for ${id}')
}
mut e := encoder.decoder_new(data.bytes())
version := e.get_u8()!
if version != 1 {
panic('wrong version in base load')
}
mut base := T{}
base.id = e.get_u32()!
base.name = e.get_string()!
base.description = e.get_string()!
base.created_at = e.get_i64()!
base.updated_at = e.get_i64()!
base.securitypolicy = e.get_u32()!
base.tags = e.get_u32()!
for _ in 0 .. e.get_u16()! {
base.comments << e.get_u32()!
}
return base, e.data
}
pub fn (mut self DB) exists[T](id u32) !bool {

View File

@@ -31,27 +31,18 @@ pub fn (self Comment) type_name() string {
pub fn (self Comment) dump() ![]u8 {
// Create a new encoder
mut e := encoder.new()
e.add_u8(1)
e.add_u32(self.id)
e.add_string(self.comment)
e.add_u32(self.parent)
e.add_u32(self.author)
return e.data
}
pub fn (mut self DBComments) load(data []u8) !Comment {
fn (mut self DBComments) load(mut o Comment, data []u8) ! {
// Create a new decoder
mut e := encoder.decoder_new(data)
version := e.get_u8()!
if version != 1 {
panic('wrong version in comment load')
}
mut comment := Comment{}
comment.id = e.get_u32()!
comment.comment = e.get_string()!
comment.parent = e.get_u32()!
comment.author = e.get_u32()!
return comment
o.comment = e.get_string()!
o.parent = e.get_u32()!
o.author = e.get_u32()!
}
@[params]
@@ -87,9 +78,11 @@ pub fn (mut self DBComments) exist(id u32) !bool {
}
pub fn (mut self DBComments) get(id u32) !Comment {
return self.load(self.db.get_data[Comment](id)!)!
mut o, data := self.db.get_data[Comment](id)!
self.load(mut o, data)!
return o
}
pub fn (mut self DBComments) list() ![]Comment {
return self.db.list[Comment]()!.map(self.load(self.db.get_data[Comment](it)!)!)
return self.db.list[Comment]()!.map(self.get(it)!)
}