-
Notifications
You must be signed in to change notification settings - Fork 0
/
order_by.go
227 lines (196 loc) · 5.03 KB
/
order_by.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright 2023 The Blocky Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package expr
import (
"encoding/gob"
"sync"
)
func init() {
gob.Register(new(OrderByExpr))
gob.Register(new(OrderByFieldExpr))
}
var (
orderExprPool = &sync.Pool{
New: func() any {
return &OrderByExpr{
Fields: make([]*OrderByFieldExpr, 0, 10),
isAcquired: true,
}
},
}
orderFieldExprPool = &sync.Pool{
New: func() any {
return &OrderByFieldExpr{
isAcquired: true,
}
},
}
)
// AcquireOrderByExpr acquires an OrderByExpr from the pool.
func AcquireOrderByExpr() *OrderByExpr {
return orderExprPool.Get().(*OrderByExpr)
}
// Compile-time check to verify that OrderByExpr implements Expr interface.
var _ Expr = (*OrderByExpr)(nil)
// OrderByExpr is an expression that selects a field to be used for ordering
type OrderByExpr struct {
// Fields is a list of fields to be used for ordering
Fields []*OrderByFieldExpr
isAcquired bool
}
// Merge merges the other OrderByExpr into the current one.
// If the field is already present, it is skipped and not added.
// The other OrderByExpr is not modified and is safe to use or Free after the merge.
func (o *OrderByExpr) Merge(other *OrderByExpr) {
// Verify if no duplicated fields are present.
ln := len(o.Fields)
for _, field := range other.Fields {
foundIndex := -1
for i := 0; i < ln-1; i++ {
f := o.Fields[i]
if f.Field.Equals(field.Field) {
foundIndex = i
break
}
}
if foundIndex == -1 {
// Clone the field and add it to the list.
o.Fields = append(o.Fields, field.Clone().(*OrderByFieldExpr))
}
}
}
// Free puts the OrderByExpr back to the pool.
func (o *OrderByExpr) Free() {
if o == nil {
return
}
for _, sub := range o.Fields {
if sub != nil {
sub.Free()
}
}
if !o.isAcquired {
return
}
o.Fields = o.Fields[:0]
orderExprPool.Put(o)
}
// Complexity returns the complexity of the expression
func (o *OrderByExpr) Complexity() int64 {
complexity := int64(1)
for _, expr := range o.Fields {
complexity += expr.Field.Complexity()
}
return complexity
}
// Equals returns true if the given expression is equal to the current one.
func (o *OrderByExpr) Equals(order Expr) bool {
if o == nil || order == nil {
return false
}
other, ok := order.(*OrderByExpr)
if !ok {
return false
}
if len(o.Fields) != len(other.Fields) {
return false
}
for i := range o.Fields {
if !o.Fields[i].Equals(other.Fields[i]) {
return false
}
}
return true
}
// Clone returns a copy of the OrderByExpr.
func (o *OrderByExpr) Clone() Expr {
if o == nil {
return nil
}
clone := AcquireOrderByExpr()
for _, field := range o.Fields {
clone.Fields = append(clone.Fields, field.Clone().(*OrderByFieldExpr))
}
return clone
}
// AcquireOrderByFieldExpr acquires an OrderByFieldExpr from the pool.
func AcquireOrderByFieldExpr() *OrderByFieldExpr {
return orderFieldExprPool.Get().(*OrderByFieldExpr)
}
// Compile-time check to verify that OrderByFieldExpr implements Expr interface.
var _ Expr = (*OrderByFieldExpr)(nil)
// OrderByFieldExpr is an expression that selects a field to be used for ordering
type OrderByFieldExpr struct {
// Field is the field to be used for ordering
// It can be a traversal selector.
Field *FieldSelectorExpr
// Order is the order of the order by expression
Order Order
isAcquired bool
}
// Clone returns a copy of the OrderByFieldExpr.
func (o *OrderByFieldExpr) Clone() Expr {
if o == nil {
return nil
}
clone := AcquireOrderByFieldExpr()
clone.Field = o.Field.Clone().(*FieldSelectorExpr)
clone.Order = o.Order
return clone
}
// Equals returns true if the given expression is equal to the current one.
func (o *OrderByFieldExpr) Equals(other Expr) bool {
if o == nil || other == nil {
return false
}
oe, ok := other.(*OrderByFieldExpr)
if !ok {
return false
}
return o.Field.Equals(oe.Field) && o.Order == oe.Order
}
// Free puts the OrderByFieldExpr back to the pool.
func (o *OrderByFieldExpr) Free() {
if o == nil {
return
}
o.Field.Free()
if !o.isAcquired {
return
}
orderFieldExprPool.Put(o)
}
// Complexity returns the complexity of the expression
func (o *OrderByFieldExpr) Complexity() int64 {
return o.Field.Complexity()
}
// Order is an enum for the order of the order by expression
type Order int
const (
// ASC determines the order to be ascending.
// This is the default order.
ASC Order = iota
// DESC determines the order to be descending.
DESC
)
func (o Order) String() string {
switch o {
case ASC:
return "ASC"
case DESC:
return "DESC"
default:
return "UNKNOWN"
}
}