diff --git a/examples/gno.land/p/demo/teritori/worx/gno.mod b/examples/gno.land/p/demo/teritori/worx/gno.mod index 2c58c902520..3875a032eca 100644 --- a/examples/gno.land/p/demo/teritori/worx/gno.mod +++ b/examples/gno.land/p/demo/teritori/worx/gno.mod @@ -1 +1,6 @@ module gno.land/p/demo/teritori/worx + +require ( + gno.land/p/demo/avl v0.0.0-latest + gno.land/p/demo/ufmt v0.0.0-latest +) \ No newline at end of file diff --git a/examples/gno.land/p/demo/teritori/worx/worxs.gno b/examples/gno.land/p/demo/teritori/worx/worxs.gno index 359f3bba311..06ab66eec3d 100644 --- a/examples/gno.land/p/demo/teritori/worx/worxs.gno +++ b/examples/gno.land/p/demo/teritori/worx/worxs.gno @@ -1,13 +1,56 @@ package worx +import( + "gno.land/p/demo/avl" + "gno.land/p/demo/ufmt" +) type WorxKeeper struct { - worxs []*Worx + worxs *avl.Tree } +const dayToSeconds = 1 +func NewWorxKeeper() *WorxKeeper{ + return &WorxKeeper{ + worxs: avl.NewTree(), + } +} func (keeper *WorxKeeper) Store(worx *Worx) { - keeper.worxs = append(keeper.worxs, worx) + key := ufmt.Sprintf("%d", worx.Timestamp) + worxsInterface, exists := keeper.worxs.Get(key) + + if exists { + worxsOnDay := worxsInterface.([]*Worx) + worxsOnDay = append(worxsOnDay, worx) + keeper.worxs.Set(key, worxsOnDay) + return + } + keeper.worxs.Set(key, []*Worx{worx}) } func (keeper *WorxKeeper) Get() []*Worx { - return keeper.worxs + totalWorx := make([]*Worx, 0) + keeper.worxs.Iterate("", "", func(key string, value interface{}) bool { + worxByDay := value.([]*Worx) + totalWorx = append(totalWorx, worxByDay...) + return false + }) + + return totalWorx +} + +func (keeper *WorxKeeper) GetFromDate(date int64) []*Worx { + start := ufmt.Sprintf("%d", date) + totalWorx := make([]*Worx, 0) + keeper.worxs.Iterate(start, "", func(key string, value interface{}) bool { + // here we account for string comparation "95" > "105" because comparing first character 9 > 1 (Comparation of lenght) + // and simply comparing same lenght string 25 > 12 this in order to stop tree for iterating over lower dates leaves + if len(start) > len(key) || start > key { + return true + } + worxByDay := value.([]*Worx) + totalWorx = append(totalWorx, worxByDay...) + return false + }) + + return totalWorx } \ No newline at end of file diff --git a/examples/gno.land/p/demo/teritori/worx/worxs_test.gno b/examples/gno.land/p/demo/teritori/worx/worxs_test.gno new file mode 100644 index 00000000000..ec3efab0e82 --- /dev/null +++ b/examples/gno.land/p/demo/teritori/worx/worxs_test.gno @@ -0,0 +1,63 @@ +package worx + +import ( + "testing" + "gno.land/p/demo/rand" + "gno.land/p/demo/testutils" + "gno.land/p/demo/ufmt" + "std" +) + +func TestAddGet(t *testing.T) { + keeper := NewWorxKeeper() + user1 := testutils.TestAddress("user1") + if len(keeper.Get()) != 0{ + t.Fatalf("Keeper is not empty initialized") + } + + fillRandomWorx(keeper, 10123423, user1) + if len(keeper.Get()) != 1{ + t.Fatalf("Keeper Worx was not added to keeper 1") + } + + fillRandomWorx(keeper, 10123423, user1) + if len(keeper.Get()) != 2{ + t.Fatalf("Keeper Worx was not added to keeper 2") + } +} + +func TestGetFromDate(t *testing.T) { + keeper := NewWorxKeeper() + user1 := testutils.TestAddress("user1") + if len(keeper.Get()) != 0{ + t.Fatalf("Keeper is not empty initialized") + } + + for i:=0; i < 100; i++ { + fillRandomWorx(keeper, int64(i*10), user1) + } + + if len(keeper.Get()) != 100{ + t.Fatalf("Keeper Worx was not totally added") + } + + if len(keeper.GetFromDate(1003)) != 0 { + t.Fatalf("Get From Date Should have found 0 registers") + } + + if len(keeper.GetFromDate(903)) != 9 { + t.Fatalf("Get From Date Should have found 9 registers") + } + +} + +func fillRandomWorx(keeper *WorxKeeper, timestamp int64, address std.Address){ + r := rand.New() + keeper.Store(NewWorx( + r.Intn(25), + "somekey", + address, + r.Intn(100), + timestamp, + )) +} \ No newline at end of file