Skip to content

Commit 85ea076

Browse files
committed
objectbox/properties: simplfied fundamental property names #29
1 parent 72725a8 commit 85ea076

6 files changed

Lines changed: 243 additions & 83 deletions

File tree

example/ollama/llamas.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,15 @@
1515

1616

1717
from objectbox.model import *
18-
from objectbox.model.properties import *
19-
import numpy as np
2018

2119
# Have fresh data for each start
2220
objectbox.Store.remove_db_files("objectbox")
2321

2422
@Entity(id=1, uid=1)
2523
class DocumentEmbedding:
2624
id = Id(id=1, uid=1001)
27-
document = Property(str, id=2, uid=1002)
28-
embedding = Property(np.ndarray, type=PropertyType.floatVector, id=3, uid=1003, index=HnswIndex(
25+
document = String(id=2, uid=1002)
26+
embedding = Float32Vector(id=3, uid=1003, index=HnswIndex(
2927
id=3, uid=10001,
3028
dimensions=1024,
3129
distance_type=VectorDistanceType.COSINE

example/tasks/model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
@Entity(id=1, uid=1)
55
class Task:
66
id = Id(id=1, uid=1001)
7-
text = Property(str, id=2, uid=1002)
7+
text = String(id=2, uid=1002)
88

9-
date_created = Property(int, type=PropertyType.date, id=3, uid=1003)
10-
date_finished = Property(int, type=PropertyType.date, id=4, uid=1004)
9+
date_created = Date(id=3, uid=1003)
10+
date_finished = Date(id=4, uid=1004)
1111

1212

1313
def get_objectbox_model():

example/vectorsearch-cities/model.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
from objectbox.model import *
2-
from objectbox.model.properties import *
3-
import objectbox
4-
import numpy as np
5-
62

73
@Entity(id=1, uid=1)
84
class City:
95
id = Id(id=1, uid=1001)
10-
name = Property(str, id=2, uid=1002)
11-
location = Property(np.ndarray, type=PropertyType.floatVector, id=3, uid=1003, index=HnswIndex(
6+
name = String(id=2, uid=1002)
7+
location = Float32Vector(id=3, uid=1003, index=HnswIndex(
128
id=3, uid=10001,
139
dimensions=2,
1410
distance_type=VectorDistanceType.EUCLIDEAN
1511
))
1612

17-
1813
def get_objectbox_model():
1914
m = Model()
2015
m.entity(City, last_property_id=IdUid(3, 1003))

objectbox/model/__init__.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,39 @@
2020
__all__ = [
2121
'Model',
2222
'Entity',
23-
'Id',
2423
'IdUid',
2524
'Property',
26-
'PropertyType'
25+
'PropertyType',
26+
'Id',
27+
'IdUid',
28+
'Bool',
29+
'String',
30+
'Int8',
31+
'Int16',
32+
'Int32',
33+
'Int64',
34+
'Float32',
35+
'Float64',
36+
'Date',
37+
'DateNano',
38+
'Flex',
39+
'BoolVector',
40+
'Int8Vector',
41+
'Int16Vector',
42+
'CharVector',
43+
'Int32Vector',
44+
'Int64Vector',
45+
'Float32Vector',
46+
'Float64Vector',
47+
'Index',
48+
'HnswIndex',
49+
'VectorDistanceType',
50+
'BoolList',
51+
'Int8List',
52+
'Int16List',
53+
'CharList',
54+
'Int32List',
55+
'Int64List',
56+
'Float32List',
57+
'Float64List',
2758
]

objectbox/model/properties.py

Lines changed: 171 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
# limitations under the License.
1414

1515
from enum import IntEnum
16+
from datetime import datetime
1617
import flatbuffers.number_types
1718
import numpy as np
1819
from dataclasses import dataclass
1920

2021
from objectbox.c import *
2122
from objectbox.condition import PropertyQueryCondition, PropertyQueryConditionOp
2223

23-
2424
class PropertyType(IntEnum):
2525
bool = OBXPropertyType_Bool
2626
byte = OBXPropertyType_Byte
@@ -183,26 +183,49 @@ def _set_flags(self):
183183
if isinstance(self._index, Index): # Generic index
184184
self._flags |= self._index.type
185185

186-
def equals(self, value, case_sensitive: bool = True) -> PropertyQueryCondition:
187-
args = {'value': value, 'case_sensitive': case_sensitive}
186+
def equals(self, value) -> PropertyQueryCondition:
187+
args = {'value': value, 'case_sensitive': False}
188188
return PropertyQueryCondition(self._id, PropertyQueryConditionOp.EQ, args)
189189

190-
def not_equals(self, value, case_sensitive: bool = True) -> PropertyQueryCondition:
191-
args = {'value': value, 'case_sensitive': case_sensitive}
190+
def not_equals(self, value) -> PropertyQueryCondition:
191+
args = {'value': value, 'case_sensitive': False}
192192
return PropertyQueryCondition(self._id, PropertyQueryConditionOp.NOT_EQ, args)
193193

194-
def contains(self, value: str, case_sensitive: bool = True) -> PropertyQueryCondition:
195-
args = {'value': value, 'case_sensitive': case_sensitive}
196-
return PropertyQueryCondition(self._id, PropertyQueryConditionOp.CONTAINS, args)
194+
# ID property (primary key)
195+
class Id(Property):
196+
def __init__(self, id : int = 0, uid : int = 0, py_type: type = int):
197+
super(Id, self).__init__(py_type, id=id, uid=uid)
197198

199+
# Bool property
200+
class Bool(Property):
201+
def __init__(self, id : int = 0, uid : int = 0, **kwargs):
202+
super(Bool, self).__init__(bool, type=PropertyType.bool, id=id, uid=uid, **kwargs)
203+
204+
# String property with starts/ends_with
205+
class String(Property):
206+
def __init__(self, id: int = 0, uid : int = 0, **kwargs):
207+
super(String, self).__init__(str, type=PropertyType.string, id=id, uid=uid, **kwargs)
208+
198209
def starts_with(self, value: str, case_sensitive: bool = True) -> PropertyQueryCondition:
199210
args = {'value': value, 'case_sensitive': case_sensitive}
200211
return PropertyQueryCondition(self._id, PropertyQueryConditionOp.STARTS_WITH, args)
201212

202213
def ends_with(self, value: str, case_sensitive: bool = True) -> PropertyQueryCondition:
203214
args = {'value': value, 'case_sensitive': case_sensitive}
204215
return PropertyQueryCondition(self._id, PropertyQueryConditionOp.ENDS_WITH, args)
216+
217+
def equals(self, value, case_sensitive: bool = True) -> PropertyQueryCondition:
218+
args = {'value': value, 'case_sensitive': case_sensitive}
219+
return PropertyQueryCondition(self._id, PropertyQueryConditionOp.EQ, args)
205220

221+
def not_equals(self, value, case_sensitive: bool = True) -> PropertyQueryCondition:
222+
args = {'value': value, 'case_sensitive': case_sensitive}
223+
return PropertyQueryCondition(self._id, PropertyQueryConditionOp.NOT_EQ, args)
224+
225+
def contains(self, value: str, case_sensitive: bool = True) -> PropertyQueryCondition:
226+
args = {'value': value, 'case_sensitive': case_sensitive}
227+
return PropertyQueryCondition(self._id, PropertyQueryConditionOp.CONTAINS, args)
228+
206229
def greater_than(self, value, case_sensitive: bool = True) -> PropertyQueryCondition:
207230
args = {'value': value, 'case_sensitive': case_sensitive}
208231
return PropertyQueryCondition(self._id, PropertyQueryConditionOp.GT, args)
@@ -218,21 +241,154 @@ def less_than(self, value, case_sensitive: bool = True) -> PropertyQueryConditio
218241
def less_or_equal(self, value, case_sensitive: bool = True) -> PropertyQueryCondition:
219242
args = {'value': value, 'case_sensitive': case_sensitive}
220243
return PropertyQueryCondition(self._id, PropertyQueryConditionOp.LTE, args)
244+
245+
246+
# Numeric Properties
247+
class _NumericProperty(Property):
248+
def __init__(self, py_type : Type, **kwargs):
249+
super(_NumericProperty, self).__init__(py_type, **kwargs)
250+
251+
def equals(self, value) -> PropertyQueryCondition:
252+
args = {'value': value, 'case_sensitive': False}
253+
return PropertyQueryCondition(self._id, PropertyQueryConditionOp.EQ, args)
254+
255+
def not_equals(self, value) -> PropertyQueryCondition:
256+
args = {'value': value, 'case_sensitive': False}
257+
return PropertyQueryCondition(self._id, PropertyQueryConditionOp.NOT_EQ, args)
258+
259+
def greater_than(self, value) -> PropertyQueryCondition:
260+
args = {'value': value, 'case_sensitive': False}
261+
return PropertyQueryCondition(self._id, PropertyQueryConditionOp.GT, args)
262+
263+
def greater_or_equal(self, value) -> PropertyQueryCondition:
264+
args = {'value': value, 'case_sensitive': False}
265+
return PropertyQueryCondition(self._id, PropertyQueryConditionOp.GTE, args)
266+
267+
def less_than(self, value) -> PropertyQueryCondition:
268+
args = {'value': value, 'case_sensitive': False}
269+
return PropertyQueryCondition(self._id, PropertyQueryConditionOp.LT, args)
270+
271+
def less_or_equal(self, value) -> PropertyQueryCondition:
272+
args = {'value': value, 'case_sensitive': False}
273+
return PropertyQueryCondition(self._id, PropertyQueryConditionOp.LTE, args)
221274

222275
def between(self, a, b) -> PropertyQueryCondition:
223276
args = {'a': a, 'b': b}
224277
return PropertyQueryCondition(self._id, PropertyQueryConditionOp.BETWEEN, args)
225278

226-
def nearest_neighbor(self, query_vector, element_count: int) -> PropertyQueryCondition:
227-
args = {'query_vector': query_vector, 'element_count': element_count}
228-
return PropertyQueryCondition(self._id, PropertyQueryConditionOp.NEAREST_NEIGHBOR, args)
229279

280+
# Signed Integer Numeric Properties
281+
class Int8(_NumericProperty):
282+
def __init__(self, id : int = 0, uid : int = 0, **kwargs):
283+
super(Int8, self).__init__(int, type=PropertyType.byte, id=id, uid=uid, **kwargs)
284+
class Int16(_NumericProperty):
285+
def __init__(self, id : int = 0, uid : int = 0, **kwargs):
286+
super(Int16, self).__init__(int, type=PropertyType.short, id=id, uid=uid, **kwargs)
287+
class Int32(_NumericProperty):
288+
def __init__(self, id : int = 0, uid : int = 0, **kwargs):
289+
super(Int32, self).__init__(int, type=PropertyType.int, id=id, uid=uid, **kwargs)
290+
class Int64(_NumericProperty):
291+
def __init__(self, id : int = 0, uid : int = 0, **kwargs):
292+
super(Int64, self).__init__(int, type=PropertyType.long, id=id, uid=uid, **kwargs)
293+
294+
# Floating-Point Numeric Properties
295+
class Float32(_NumericProperty):
296+
def __init__(self, id : int = 0, uid : int = 0, **kwargs):
297+
super(Float32, self).__init__(float, type=PropertyType.float, id=id, uid=uid, **kwargs)
298+
299+
class Float64(_NumericProperty):
300+
def __init__(self, id : int = 0, uid : int = 0, **kwargs):
301+
super(Float64, self).__init__(float, type=PropertyType.double, id=id, uid=uid, **kwargs)
302+
303+
# Date Properties
304+
class Date(_NumericProperty):
305+
def __init__(self, id : int = 0, uid : int = 0, **kwargs):
306+
super(Date, self).__init__(int, type=PropertyType.date, id=id, uid=uid, **kwargs)
307+
308+
class DateNano(_NumericProperty):
309+
def __init__(self, id : int = 0, uid : int = 0, **kwargs):
310+
super(DateNano, self).__init__(int, type=PropertyType.dateNano, id=id, uid=uid, **kwargs)
311+
312+
# Flex Property
313+
class Flex(Property):
314+
def __init__(self, id : int = 0, uid : int = 0, **kwargs):
315+
super(Flex, self).__init__(Generic, type=PropertyType.flex, id=id, uid=uid, **kwargs)
230316
def contains_key_value(self, key: str, value: str, case_sensitive: bool = True) -> PropertyQueryCondition:
231317
args = {'key': key, 'value': value, 'case_sensitive': case_sensitive}
232318
return PropertyQueryCondition(self._id, PropertyQueryConditionOp.CONTAINS_KEY_VALUE, args)
233319

234320

235-
# ID property (primary key)
236-
class Id(Property):
237-
def __init__(self, py_type: type = int, id: int = 0, uid: int = 0):
238-
super(Id, self).__init__(py_type, id=id, uid=uid)
321+
322+
class _VectorProperty(Property):
323+
def __init__(self, py_type : Type, **kwargs):
324+
super(_VectorProperty, self).__init__(py_type, **kwargs)
325+
def nearest_neighbor(self, query_vector, element_count: int) -> PropertyQueryCondition:
326+
args = {'query_vector': query_vector, 'element_count': element_count}
327+
return PropertyQueryCondition(self._id, PropertyQueryConditionOp.NEAREST_NEIGHBOR, args)
328+
329+
class BoolVector(_VectorProperty):
330+
def __init__(self, id: int = 0, uid: int = 0, **kwargs):
331+
super(BoolVector, self).__init__(np.ndarray, type=PropertyType.boolVector, id=id, uid=uid, **kwargs)
332+
class Int8Vector(_VectorProperty):
333+
def __init__(self, id: int = 0, uid: int = 0, **kwargs):
334+
super(Int8Vector, self).__init__(bytes, type=PropertyType.byteVector, id=id, uid=uid, **kwargs)
335+
336+
class Int16Vector(_VectorProperty):
337+
def __init__(self, id: int = 0, uid: int = 0, **kwargs):
338+
super(Int16Vector, self).__init__(np.ndarray, type=PropertyType.shortVector, id=id, uid=uid, **kwargs)
339+
340+
class CharVector(_VectorProperty):
341+
def __init__(self, id: int = 0, uid: int = 0, **kwargs):
342+
super(CharVector, self).__init__(np.ndarray, type=PropertyType.charVector, id=id, uid=uid, **kwargs)
343+
344+
class Int32Vector(_VectorProperty):
345+
def __init__(self, id: int = 0, uid: int = 0, **kwargs):
346+
super(Int32Vector, self).__init__(np.ndarray, type=PropertyType.intVector, id=id, uid=uid, **kwargs)
347+
348+
class Int64Vector(_VectorProperty):
349+
def __init__(self, id: int = 0, uid: int = 0, **kwargs):
350+
super(Int64Vector, self).__init__(np.ndarray, type=PropertyType.longVector, id=id, uid=uid, **kwargs)
351+
352+
class Float32Vector(_VectorProperty):
353+
def __init__(self, id: int = 0, uid: int = 0, **kwargs):
354+
super(Float32Vector, self).__init__(np.ndarray, type=PropertyType.floatVector, id=id, uid=uid, **kwargs)
355+
356+
class Float64Vector(_VectorProperty):
357+
def __init__(self, id: int = 0, uid: int = 0, **kwargs):
358+
super(Float64Vector, self).__init__(np.ndarray, type=PropertyType.doubleVector, id=id, uid=uid, **kwargs)
359+
360+
class _ListProperty(Property):
361+
def __init__(self, **kwargs):
362+
super(_ListProperty, self).__init__(list, **kwargs)
363+
364+
class BoolList(_ListProperty):
365+
def __init__(self, id: int = 0, uid: int = 0, **kwargs):
366+
super(BoolList, self).__init__(type=PropertyType.boolVector, id=id, uid=uid, **kwargs)
367+
368+
class Int8List(_ListProperty):
369+
def __init__(self, id: int = 0, uid: int = 0, **kwargs):
370+
super(Int8List, self).__init__(type=PropertyType.byteVector, id=id, uid=uid, **kwargs)
371+
372+
class Int16List(_ListProperty):
373+
def __init__(self, id: int = 0, uid: int = 0, **kwargs):
374+
super(Int16List, self).__init__(type=PropertyType.shortVector, id=id, uid=uid, **kwargs)
375+
376+
class Int32List(_ListProperty):
377+
def __init__(self, id: int = 0, uid: int = 0, **kwargs):
378+
super(Int32List, self).__init__(type=PropertyType.intVector, id=id, uid=uid, **kwargs)
379+
380+
class Int64List(_ListProperty):
381+
def __init__(self, id: int = 0, uid: int = 0, **kwargs):
382+
super(Int64List, self).__init__(type=PropertyType.longVector, id=id, uid=uid, **kwargs)
383+
384+
class Float32List(_ListProperty):
385+
def __init__(self, id: int = 0, uid: int = 0, **kwargs):
386+
super(Float32List, self).__init__(type=PropertyType.floatVector, id=id, uid=uid, **kwargs)
387+
388+
class Float64List(_ListProperty):
389+
def __init__(self, id: int = 0, uid: int = 0, **kwargs):
390+
super(Float64List, self).__init__(type=PropertyType.doubleVector, id=id, uid=uid, **kwargs)
391+
392+
class CharList(_ListProperty):
393+
def __init__(self, id: int = 0, uid: int = 0, **kwargs):
394+
super(CharList, self).__init__(type=PropertyType.charVector, id=id, uid=uid, **kwargs)

0 commit comments

Comments
 (0)