-
Notifications
You must be signed in to change notification settings - Fork 693
/
sortby_objects.go
72 lines (63 loc) · 1.96 KB
/
sortby_objects.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2022 SeMI Technologies B.V. All rights reserved.
//
// CONTACT: hello@semi.technology
//
package sorter
import (
"github.com/semi-technologies/weaviate/entities/schema"
"github.com/semi-technologies/weaviate/entities/storobj"
)
type sortByObjects struct {
objects []*storobj.Object
distances []float32
property string
dataType []string
sortBy sortBy
}
func newSortByObjects(objects []*storobj.Object, distances []float32, property, order string, dataType []string) *sortByObjects {
return &sortByObjects{objects, distances, property, dataType, sortBy{newComparator(order)}}
}
func (s *sortByObjects) Len() int {
return len(s.objects)
}
func (s *sortByObjects) Swap(i, j int) {
s.objects[i], s.objects[j] = s.objects[j], s.objects[i]
if len(s.distances) > 0 {
s.distances[i], s.distances[j] = s.distances[j], s.distances[i]
}
}
func (s *sortByObjects) Less(i, j int) bool {
return s.sortBy.compare(s.getProperty(i), s.getProperty(j), s.getDataType())
}
func (s *sortByObjects) getProperty(i int) interface{} {
if s.property == "id" || s.property == "_id" {
// special case for "id" property
return s.objects[i].ID().String()
}
if s.property == "_creationTimeUnix" {
// special case for "_creationTimeUnix" property
return float64(s.objects[i].CreationTimeUnix())
}
if s.property == "_lastUpdateTimeUnix" {
// special case for "_lastUpdateTimeUnix" property
return float64(s.objects[i].LastUpdateTimeUnix())
}
properties := s.objects[i].Properties()
propertiesMap, ok := properties.(map[string]interface{})
if ok {
return propertiesMap[s.property]
}
return nil
}
func (s *sortByObjects) getDataType() schema.DataType {
if len(s.dataType) > 0 {
return schema.DataType(s.dataType[0])
}
return ""
}