Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

f0o wishes #17

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gits.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
32 changes: 32 additions & 0 deletions src/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
38 changes: 38 additions & 0 deletions src/query/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down