-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_types.go
210 lines (183 loc) · 5.1 KB
/
api_types.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
/*
* Copyright 2025 Hypermode Inc.
* Licensed under the terms of the Apache License, Version 2.0
* See the LICENSE file that accompanied this code for further details.
*
* SPDX-FileCopyrightText: 2025 Hypermode Inc. <[email protected]>
* SPDX-License-Identifier: Apache-2.0
*/
package modusdb
import (
"context"
"fmt"
"strings"
"github.com/dgraph-io/dgraph/v24/x"
"github.com/hypermodeinc/modusdb/api/apiutils"
"github.com/hypermodeinc/modusdb/api/querygen"
)
type UniqueField interface {
uint64 | ConstrainedField
}
type ConstrainedField struct {
Key string
Value any
}
type QueryParams struct {
Filter *Filter
Pagination *Pagination
Sorting *Sorting
}
type Filter struct {
Field string
String StringPredicate
Vector VectorPredicate
And *Filter
Or *Filter
Not *Filter
}
type Pagination struct {
Limit int64
Offset int64
After string
}
type Sorting struct {
OrderAscField string
OrderDescField string
OrderDescFirst bool
}
type StringPredicate struct {
Equals string
LessThan string
LessOrEqual string
GreaterThan string
GreaterOrEqual string
AllOfTerms []string
AnyOfTerms []string
AllOfText []string
AnyOfText []string
RegExp string
}
type VectorPredicate struct {
SimilarTo []float32
TopK int64
}
type ModusDbOption func(*modusDbOptions)
type modusDbOptions struct {
ns uint64
}
func WithNamespace(ns uint64) ModusDbOption {
return func(o *modusDbOptions) {
o.ns = ns
}
}
func getDefaultNamespace(engine *Engine, nsId ...uint64) (context.Context, *Namespace, error) {
dbOpts := &modusDbOptions{
ns: engine.db0.ID(),
}
for _, ns := range nsId {
WithNamespace(ns)(dbOpts)
}
d, err := engine.getNamespaceWithLock(dbOpts.ns)
if err != nil {
return nil, nil, err
}
ctx := context.Background()
ctx = x.AttachNamespace(ctx, d.ID())
return ctx, d, nil
}
func filterToQueryFunc(typeName string, f Filter) querygen.QueryFunc {
// Handle logical operators first
if f.And != nil {
return querygen.And(filterToQueryFunc(typeName, *f.And))
}
if f.Or != nil {
return querygen.Or(filterToQueryFunc(typeName, *f.Or))
}
if f.Not != nil {
return querygen.Not(filterToQueryFunc(typeName, *f.Not))
}
// Handle field predicates
if f.String.Equals != "" {
return querygen.BuildEqQuery(apiutils.GetPredicateName(typeName, f.Field), f.String.Equals)
}
if len(f.String.AllOfTerms) != 0 {
return querygen.BuildAllOfTermsQuery(apiutils.GetPredicateName(typeName,
f.Field), strings.Join(f.String.AllOfTerms, " "))
}
if len(f.String.AnyOfTerms) != 0 {
return querygen.BuildAnyOfTermsQuery(apiutils.GetPredicateName(typeName,
f.Field), strings.Join(f.String.AnyOfTerms, " "))
}
if len(f.String.AllOfText) != 0 {
return querygen.BuildAllOfTextQuery(apiutils.GetPredicateName(typeName,
f.Field), strings.Join(f.String.AllOfText, " "))
}
if len(f.String.AnyOfText) != 0 {
return querygen.BuildAnyOfTextQuery(apiutils.GetPredicateName(typeName,
f.Field), strings.Join(f.String.AnyOfText, " "))
}
if f.String.RegExp != "" {
return querygen.BuildRegExpQuery(apiutils.GetPredicateName(typeName,
f.Field), f.String.RegExp)
}
if f.String.LessThan != "" {
return querygen.BuildLtQuery(apiutils.GetPredicateName(typeName,
f.Field), f.String.LessThan)
}
if f.String.LessOrEqual != "" {
return querygen.BuildLeQuery(apiutils.GetPredicateName(typeName,
f.Field), f.String.LessOrEqual)
}
if f.String.GreaterThan != "" {
return querygen.BuildGtQuery(apiutils.GetPredicateName(typeName,
f.Field), f.String.GreaterThan)
}
if f.String.GreaterOrEqual != "" {
return querygen.BuildGeQuery(apiutils.GetPredicateName(typeName,
f.Field), f.String.GreaterOrEqual)
}
if f.Vector.SimilarTo != nil {
return querygen.BuildSimilarToQuery(apiutils.GetPredicateName(typeName,
f.Field), f.Vector.TopK, f.Vector.SimilarTo)
}
// Return empty query if no conditions match
return func() string { return "" }
}
// Helper function to combine multiple filters
func filtersToQueryFunc(typeName string, filter Filter) querygen.QueryFunc {
return filterToQueryFunc(typeName, filter)
}
func paginationToQueryString(p Pagination) string {
paginationStr := ""
if p.Limit > 0 {
paginationStr += ", " + fmt.Sprintf("first: %d", p.Limit)
}
if p.Offset > 0 {
paginationStr += ", " + fmt.Sprintf("offset: %d", p.Offset)
} else if p.After != "" {
paginationStr += ", " + fmt.Sprintf("after: %s", p.After)
}
if paginationStr == "" {
return ""
}
return paginationStr
}
func sortingToQueryString(typeName string, s Sorting) string {
if s.OrderAscField == "" && s.OrderDescField == "" {
return ""
}
var parts []string
first, second := s.OrderDescField, s.OrderAscField
firstOp, secondOp := "orderdesc", "orderasc"
if !s.OrderDescFirst {
first, second = s.OrderAscField, s.OrderDescField
firstOp, secondOp = "orderasc", "orderdesc"
}
if first != "" {
parts = append(parts, fmt.Sprintf("%s: %s", firstOp, apiutils.GetPredicateName(typeName, first)))
}
if second != "" {
parts = append(parts, fmt.Sprintf("%s: %s", secondOp, apiutils.GetPredicateName(typeName, second)))
}
return ", " + strings.Join(parts, ", ")
}