From d403f84b6c0ed399eb5b26307fd968027f71da83 Mon Sep 17 00:00:00 2001 From: timurgordon Date: Tue, 21 Jan 2025 02:37:05 +0000 Subject: [PATCH] fix osis compilation --- lib/baobab/osis/factory.v | 6 ------ lib/baobab/osis/indexer.v | 31 +++++++++++++++++++++++++++---- lib/baobab/osis/model.v | 8 +------- lib/baobab/osis/osis.v | 12 ++++-------- lib/baobab/osis/storer_generic.v | 2 +- lib/data/ourdb/db.v | 3 ++- 6 files changed, 35 insertions(+), 27 deletions(-) diff --git a/lib/baobab/osis/factory.v b/lib/baobab/osis/factory.v index 25377a3a..3d97a54d 100644 --- a/lib/baobab/osis/factory.v +++ b/lib/baobab/osis/factory.v @@ -1,11 +1,5 @@ module osis -import os -import db.sqlite -import db.pg -import freeflowuniverse.herolib.data.dbfs -import freeflowuniverse.herolib.data.encoderhero - pub fn new(config OSISConfig) !OSIS { return OSIS{ indexer: new_indexer()! diff --git a/lib/baobab/osis/indexer.v b/lib/baobab/osis/indexer.v index 5cc342e8..0bfdef98 100644 --- a/lib/baobab/osis/indexer.v +++ b/lib/baobab/osis/indexer.v @@ -2,10 +2,8 @@ module osis import json import db.sqlite -import db.pg import freeflowuniverse.herolib.core.texttools import freeflowuniverse.herolib.core.pathlib -import orm pub struct Indexer { db sqlite.DB @@ -27,10 +25,18 @@ pub fn reset(path string) ! { db_file.delete()! } +pub fn (mut i Indexer) new_generic[T](id u32, object T) !u32 { + return i.new(get_table[T](), id, get_indices[T](object))! +} + // new creates a new root object entry in the root_objects table, // and the table belonging to the type of root object with columns for index fields -pub fn (mut backend Indexer) new(object RootObject) !u32 { - panic('implement') +pub fn (mut i Indexer) new(table string, id u32, indices map[string]string) !u32 { + insert_query := 'INSERT into ${table} (${indices.keys().join(',')}) values (${indices.values().join(',')})' + i.db.exec(insert_query) or { + return error('Error inserting object ${id} into table ${table}\n${err}') + } + return 0 } // save the session to redis & mem @@ -90,4 +96,21 @@ fn (mut backend Indexer) table_exists(table_name string) !bool { // get_table_name returns the name of the table belonging to a root struct fn get_table_name(object RootObject) string { panic('implement') +} + +// get_table_name returns the name of the table belonging to a root struct +fn get_table[T]() string { + return typeof[T]() +} + +// returns the lists of the indices of a root objects db table, and corresponding values +pub fn get_indices[T](object T) map[string]string { + mut indices := map[string]string + $for field in T.fields { + if field.attrs.contains('index') { + value := object.$(field.name) + indices[field.name] = '${value}' + } + } + return indices } \ No newline at end of file diff --git a/lib/baobab/osis/model.v b/lib/baobab/osis/model.v index b93b4d6d..f59847dc 100644 --- a/lib/baobab/osis/model.v +++ b/lib/baobab/osis/model.v @@ -1,11 +1,5 @@ module osis -import os -import db.sqlite -import db.pg -import freeflowuniverse.herolib.data.dbfs -import freeflowuniverse.herolib.data.encoderhero - pub struct OSIS { pub mut: indexer Indexer // storing indeces @@ -19,4 +13,4 @@ pub: name string secret string reset bool -} +} \ No newline at end of file diff --git a/lib/baobab/osis/osis.v b/lib/baobab/osis/osis.v index 5e6d2e3c..dd340cbe 100644 --- a/lib/baobab/osis/osis.v +++ b/lib/baobab/osis/osis.v @@ -1,10 +1,6 @@ module osis import os -import db.sqlite -import db.pg -import freeflowuniverse.herolib.data.dbfs -import freeflowuniverse.herolib.data.encoderhero pub fn (mut o OSIS) generic_new[T](obj T) !u32 { id := o.indexer.generic_new[T](obj)! @@ -13,8 +9,8 @@ pub fn (mut o OSIS) generic_new[T](obj T) !u32 { } pub fn (mut o OSIS) new[T](obj T) !u32 { - id := o.indexer.generic_new[T](obj)! - o.storer.generic_new[T](obj)! + id := o.storer.new_generic[T](obj)! + o.indexer.new_generic[T](id, obj)! return id } @@ -22,7 +18,7 @@ pub fn (mut o OSIS) generic_get[T](id u32) !T { return o.storer.generic_get[T](id)! } -pub fn (mut o OSIS) get[T](id int) !T { +pub fn (mut o OSIS) get[T](id u32) !T { return o.storer.generic_get[T](u32(id))! } @@ -36,7 +32,7 @@ pub fn (mut o OSIS) generic_delete[T](id u32) ! { o.storer.generic_delete[T](id)! } -pub fn (mut o OSIS) delete(id int) ! { +pub fn (mut o OSIS) delete(id u32) ! { o.storer.delete(u32(id))! } diff --git a/lib/baobab/osis/storer_generic.v b/lib/baobab/osis/storer_generic.v index babe1c15..fe987812 100644 --- a/lib/baobab/osis/storer_generic.v +++ b/lib/baobab/osis/storer_generic.v @@ -4,7 +4,7 @@ import json // new creates a new root object entry in the root_objects table, // and the table belonging to the type of root object with columns for index fields -pub fn (mut storer Storer) generic_new[T](obj T) !u32 { +pub fn (mut storer Storer) new_generic[T](obj T) !u32 { data := json.encode(obj).bytes() return storer.db.set(data: data) } diff --git a/lib/data/ourdb/db.v b/lib/data/ourdb/db.v index 721abc73..74225eda 100644 --- a/lib/data/ourdb/db.v +++ b/lib/data/ourdb/db.v @@ -18,7 +18,8 @@ import os // and maintains a linked list of previous values for history tracking // Returns the ID used (either x if specified, or auto-incremented if x=0) @[params] -struct OurDBSetArgs { +pub struct OurDBSetArgs { +pub: id ?u32 data []u8 @[required] }