From 3b6a6c46e37a21a81b11eae8eccb1208a2c9626c Mon Sep 17 00:00:00 2001 From: voodooEntity Date: Thu, 12 Dec 2024 11:45:30 +0100 Subject: [PATCH] reenable gits Remove storage method for testing purposes (experimental). Adding limit for f0o --- gits.go | 2 +- src/query/query.go | 32 ++++++++++++++++++++++++++++++++ src/query/query_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/gits.go b/gits.go index 8ab6d01..cd845fc 100644 --- a/gits.go +++ b/gits.go @@ -124,7 +124,7 @@ func (ii instanceIndex) GetByName(name string) *Gits { return ret } -func (ii instanceIndex) defunc_Remove(name string) { +func (ii instanceIndex) Remove(name string) { instanceMutex.Lock() if _, ok := instances[name]; !ok { fmt.Println("Cant remove non existing instance") diff --git a/src/query/query.go b/src/query/query.go index b34e502..5c8e849 100644 --- a/src/query/query.go +++ b/src/query/query.go @@ -218,6 +218,11 @@ func (self *Query) TraverseIn(depth int) *Query { return self } +func (self *Query) Limit(amount int) *Query { + self.Mode = append(self.Mode, []string{"Limit", strconv.Itoa(amount)}) + return self +} + func Execute(store *storage.Storage, query *Query) transport.Transport { // no type pool = something is very wrong if 0 == len(query.Pool) { @@ -375,6 +380,17 @@ func Execute(store *storage.Storage, query *Query) transport.Transport { ret.Entities = sortResults(ret.Entities, query.Sort.Field, query.Sort.Direction, query.Sort.Mode) } + // finally check if we need to reduce due to given limit + // not the optimal implementation in terms of resource usage + // because if there is no order we could limit earlier in the query + // process but for now we gonne run with this solution ### revisit + limit := getLimitIfExists(*query) + if -1 != limit { + if len(ret.Entities) > limit { + ret.Entities = ret.Entities[:limit] + } + } + // release all the mutex and provide the data mutexh.Release() return ret @@ -579,6 +595,22 @@ func isTraversed(qry Query) (int, int, bool) { return -1, -1, false } +func getLimitIfExists(qry Query) int { + if nil != qry.Mode { + for _, mode := range qry.Mode { + tmpLen := len(mode) + if 0 < tmpLen && "Limit" == mode[0] { + limit, err := strconv.Atoi(mode[1]) + if nil != err { + return -1 + } + return limit + } + } + } + return -1 +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - /** Methods: diff --git a/src/query/query_test.go b/src/query/query_test.go index 8750df4..9a5608e 100644 --- a/src/query/query_test.go +++ b/src/query/query_test.go @@ -893,6 +893,44 @@ func TestRequiredQueryJoinInDepthFail(t *testing.T) { }) } +func TestLimitApplies(t *testing.T) { + for i := 0; i < 100; i++ { + testStorage.MapTransportData(transport.TransportEntity{ + ID: -1, + Value: "Something" + strconv.Itoa(i), + Context: "test", + Type: "Alpha", + }) + } + qry := New().Read("Alpha").Limit(4) + ret := Execute(testStorage, qry) + if 4 != len(ret.Entities) { + t.Error("wrong result format", ret) + } + t.Cleanup(func() { + Cleanup() + }) +} + +func TestLimitButLessDatasets(t *testing.T) { + for i := 0; i < 5; i++ { + testStorage.MapTransportData(transport.TransportEntity{ + ID: -1, + Value: "Something" + strconv.Itoa(i), + Context: "test", + Type: "Alpha", + }) + } + qry := New().Read("Alpha").Limit(10) + ret := Execute(testStorage, qry) + if 5 != len(ret.Entities) { + t.Error("wrong result format", ret) + } + t.Cleanup(func() { + Cleanup() + }) +} + func TestRequiredQueryJoinInDepthSuccess(t *testing.T) { testdata := mapQbStructureMap() testStorage.MapTransportData(testdata)