Avoid sorting fields for each written object.
This commit is contained in:
parent
0490734b74
commit
5faccc3b4c
@ -2,6 +2,7 @@ package collection
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"sort"
|
||||||
|
|
||||||
"github.com/tidwall/btree"
|
"github.com/tidwall/btree"
|
||||||
"github.com/tidwall/geoindex"
|
"github.com/tidwall/geoindex"
|
||||||
@ -319,6 +320,7 @@ func (c *Collection) FieldArr() []string {
|
|||||||
for field, i := range c.fieldMap {
|
for field, i := range c.fieldMap {
|
||||||
arr[i] = field
|
arr[i] = field
|
||||||
}
|
}
|
||||||
|
sort.Strings(arr)
|
||||||
return arr
|
return arr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,7 +2,6 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"sort"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -23,22 +22,12 @@ type fvt struct {
|
|||||||
value float64
|
value float64
|
||||||
}
|
}
|
||||||
|
|
||||||
type byField []fvt
|
func orderFields(fmap map[string]int, farr []string, fields []float64) []fvt {
|
||||||
|
|
||||||
func (a byField) Len() int {
|
|
||||||
return len(a)
|
|
||||||
}
|
|
||||||
func (a byField) Less(i, j int) bool {
|
|
||||||
return a[i].field < a[j].field
|
|
||||||
}
|
|
||||||
func (a byField) Swap(i, j int) {
|
|
||||||
a[i], a[j] = a[j], a[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
func orderFields(fmap map[string]int, fields []float64) []fvt {
|
|
||||||
var fv fvt
|
var fv fvt
|
||||||
|
var idx int
|
||||||
fvs := make([]fvt, 0, len(fmap))
|
fvs := make([]fvt, 0, len(fmap))
|
||||||
for field, idx := range fmap {
|
for _, field := range farr {
|
||||||
|
idx = fmap[field]
|
||||||
if idx < len(fields) {
|
if idx < len(fields) {
|
||||||
fv.field = field
|
fv.field = field
|
||||||
fv.value = fields[idx]
|
fv.value = fields[idx]
|
||||||
@ -47,7 +36,6 @@ func orderFields(fmap map[string]int, fields []float64) []fvt {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sort.Sort(byField(fvs))
|
|
||||||
return fvs
|
return fvs
|
||||||
}
|
}
|
||||||
func (server *Server) cmdBounds(msg *Message) (resp.Value, error) {
|
func (server *Server) cmdBounds(msg *Message) (resp.Value, error) {
|
||||||
@ -255,7 +243,7 @@ func (server *Server) cmdGet(msg *Message) (resp.Value, error) {
|
|||||||
return NOMessage, errInvalidNumberOfArguments
|
return NOMessage, errInvalidNumberOfArguments
|
||||||
}
|
}
|
||||||
if withfields {
|
if withfields {
|
||||||
fvs := orderFields(col.FieldMap(), fields)
|
fvs := orderFields(col.FieldMap(), col.FieldArr(), fields)
|
||||||
if len(fvs) > 0 {
|
if len(fvs) > 0 {
|
||||||
fvals := make([]resp.Value, 0, len(fvs)*2)
|
fvals := make([]resp.Value, 0, len(fvs)*2)
|
||||||
if msg.OutputType == JSON {
|
if msg.OutputType == JSON {
|
||||||
|
@ -485,7 +485,7 @@ func (sw *scanWriter) writeObject(opts ScanWriterParams) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if sw.hasFieldsOutput() {
|
if sw.hasFieldsOutput() {
|
||||||
fvs := orderFields(sw.fmap, opts.fields)
|
fvs := orderFields(sw.fmap, sw.farr, opts.fields)
|
||||||
if len(fvs) > 0 {
|
if len(fvs) > 0 {
|
||||||
fvals := make([]resp.Value, 0, len(fvs)*2)
|
fvals := make([]resp.Value, 0, len(fvs)*2)
|
||||||
for i, fv := range fvs {
|
for i, fv := range fvs {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user