Skip to content

Commit 8c287c4

Browse files
authored
query result ordering does not create additional goroutine (#221)
Previously, query ordering collected and sorted query results and then used a goprocess goroutine to send those results over a channel. This change eliminates the goroutine and uses a query Iiterator to directly return the sorted results.
1 parent 036ea57 commit 8c287c4

File tree

1 file changed

+22
-27
lines changed

1 file changed

+22
-27
lines changed

query/query_impl.go

+22-27
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package query
22

33
import (
44
"path"
5-
6-
goprocess "github.com/jbenet/goprocess"
75
)
86

97
// NaiveFilter applies a filter to the results.
@@ -84,35 +82,32 @@ func NaiveOrder(qr Results, orders ...Order) Results {
8482
return qr
8583
}
8684

87-
return ResultsWithProcess(qr.Query(), func(worker goprocess.Process, out chan<- Result) {
88-
defer qr.Close()
89-
var entries []Entry
90-
collect:
91-
for {
92-
select {
93-
case <-worker.Closing():
94-
return
95-
case e, ok := <-qr.Next():
96-
if !ok {
97-
break collect
98-
}
99-
if e.Error != nil {
100-
out <- e
101-
continue
102-
}
103-
entries = append(entries, e.Entry)
104-
}
85+
var entries []Entry
86+
var errs []Result
87+
for res := range qr.Next() {
88+
if res.Error != nil {
89+
errs = append(errs, res)
90+
continue
10591
}
92+
entries = append(entries, res.Entry)
93+
}
10694

107-
Sort(orders, entries)
95+
Sort(orders, entries)
10896

109-
for _, e := range entries {
110-
select {
111-
case <-worker.Closing():
112-
return
113-
case out <- Result{Entry: e}:
97+
return ResultsFromIterator(qr.Query(), Iterator{
98+
Next: func() (Result, bool) {
99+
if len(errs) != 0 {
100+
errResult := errs[0]
101+
errs = errs[1:]
102+
return errResult, true
114103
}
115-
}
104+
if len(entries) == 0 {
105+
return Result{}, false
106+
}
107+
next := entries[0]
108+
entries = entries[1:]
109+
return Result{Entry: next}, true
110+
},
116111
})
117112
}
118113

0 commit comments

Comments
 (0)