Skip to content

Commit 61edbe6

Browse files
ivahnenkoAnnagreenrobot
authored andcommitted
Add bool, short and char vectors #14
1 parent f02a8b4 commit 61edbe6

File tree

6 files changed

+58
-14
lines changed

6 files changed

+58
-14
lines changed

objectbox/c.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,10 @@ def c_voidp_as_bytes(voidp, size):
396396
OBXPropertyType_Relation = 11
397397
OBXPropertyType_DateNano = 12
398398
OBXPropertyType_Flex = 13
399+
OBXPropertyType_BoolVector = 22
399400
OBXPropertyType_ByteVector = 23
401+
OBXPropertyType_ShortVector = 24
402+
OBXPropertyType_CharVector = 25
400403
OBXPropertyType_IntVector = 26
401404
OBXPropertyType_LongVector = 27
402405
OBXPropertyType_FloatVector = 28

objectbox/model/entity.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ def fill_properties(self):
7171
if prop._fb_type == flatbuffers.number_types.UOffsetTFlags:
7272
assert prop._ob_type in [
7373
OBXPropertyType_String,
74+
OBXPropertyType_BoolVector,
7475
OBXPropertyType_ByteVector,
76+
OBXPropertyType_ShortVector,
77+
OBXPropertyType_CharVector,
7578
OBXPropertyType_IntVector,
7679
OBXPropertyType_LongVector,
7780
OBXPropertyType_FloatVector,
@@ -117,8 +120,16 @@ def marshal(self, object, id: int) -> bytearray:
117120
val = self.get_value(object, prop)
118121
if prop._ob_type == OBXPropertyType_String:
119122
offsets[prop._id] = builder.CreateString(val.encode('utf-8'))
123+
elif prop._ob_type == OBXPropertyType_BoolVector:
124+
# Using a numpy bool as it seems to be more consistent in terms of size. TBD
125+
# https://numpy.org/doc/stable/reference/arrays.scalars.html#numpy.bool
126+
offsets[prop._id] = builder.CreateNumpyVector(np.array(val, dtype=np.bool_))
120127
elif prop._ob_type == OBXPropertyType_ByteVector:
121128
offsets[prop._id] = builder.CreateByteVector(val)
129+
elif prop._ob_type == OBXPropertyType_ShortVector:
130+
offsets[prop._id] = builder.CreateNumpyVector(np.array(val, dtype=np.int16))
131+
elif prop._ob_type == OBXPropertyType_CharVector:
132+
offsets[prop._id] = builder.CreateNumpyVector(np.array(val, dtype=np.uint16))
122133
elif prop._ob_type == OBXPropertyType_IntVector:
123134
offsets[prop._id] = builder.CreateNumpyVector(np.array(val, dtype=np.int32))
124135
elif prop._ob_type == OBXPropertyType_LongVector:
@@ -176,12 +187,18 @@ def unmarshal(self, data: bytes):
176187
val = prop._py_type() # use default (empty) value if not present in the object
177188
elif prop._ob_type == OBXPropertyType_String:
178189
val = table.String(o + table.Pos).decode('utf-8')
190+
elif prop._ob_type == OBXPropertyType_BoolVector:
191+
val = table.GetVectorAsNumpy(flatbuffers.number_types.BoolFlags, o)
179192
elif prop._ob_type == OBXPropertyType_ByteVector:
180193
# access the FB byte vector information
181194
start = table.Vector(o)
182195
size = table.VectorLen(o)
183196
# slice the vector as a requested type
184197
val = prop._py_type(table.Bytes[start:start+size])
198+
elif prop._ob_type == OBXPropertyType_ShortVector:
199+
val = table.GetVectorAsNumpy(flatbuffers.number_types.Int16Flags, o)
200+
elif prop._ob_type == OBXPropertyType_CharVector:
201+
val = table.GetVectorAsNumpy(flatbuffers.number_types.Int16Flags, o)
185202
elif prop._ob_type == OBXPropertyType_Date and prop._py_type == datetime:
186203
table_val = table.Get(prop._fb_type, o + table.Pos)
187204
val = datetime.fromtimestamp(table_val/1000) if table_val != 0 else datetime.fromtimestamp(0) # default timestamp

objectbox/model/properties.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ class PropertyType(IntEnum):
3333
dateNano = OBXPropertyType_DateNano
3434
flex = OBXPropertyType_Flex
3535
# relation = OBXPropertyType_Relation
36+
boolVector = OBXPropertyType_BoolVector
3637
byteVector = OBXPropertyType_ByteVector
38+
shortVector = OBXPropertyType_ShortVector
39+
charVector = OBXPropertyType_CharVector
3740
intVector = OBXPropertyType_IntVector
3841
longVector = OBXPropertyType_LongVector
3942
floatVector = OBXPropertyType_FloatVector
@@ -55,7 +58,10 @@ class PropertyType(IntEnum):
5558
PropertyType.dateNano: flatbuffers.number_types.Int64Flags,
5659
PropertyType.flex: flatbuffers.number_types.UOffsetTFlags,
5760
# PropertyType.relation: flatbuffers.number_types.Int64Flags,
61+
PropertyType.boolVector: flatbuffers.number_types.UOffsetTFlags,
5862
PropertyType.byteVector: flatbuffers.number_types.UOffsetTFlags,
63+
PropertyType.shortVector: flatbuffers.number_types.UOffsetTFlags,
64+
PropertyType.charVector: flatbuffers.number_types.UOffsetTFlags,
5965
PropertyType.intVector: flatbuffers.number_types.UOffsetTFlags,
6066
PropertyType.longVector: flatbuffers.number_types.UOffsetTFlags,
6167
PropertyType.floatVector: flatbuffers.number_types.UOffsetTFlags,

tests/common.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def autocleanup():
2424
def load_empty_test_objectbox(name: str = "") -> objectbox.ObjectBox:
2525
model = objectbox.Model()
2626
from objectbox.model import IdUid
27-
model.entity(TestEntity, last_property_id=IdUid(21, 1021))
27+
model.entity(TestEntity, last_property_id=IdUid(27, 1027))
2828
model.last_entity_id = IdUid(2, 2)
2929

3030
db_name = test_dir if len(name) == 0 else test_dir + "/" + name
@@ -77,11 +77,17 @@ def assert_equal(actual: TestEntity, expected: TestEntity):
7777
assert_equal_prop(actual.float64, expected.float64, 0)
7878
assert_equal_prop(actual.float32, expected.float32, 0)
7979
assert_equal_prop(actual.bytes, expected.bytes, b'')
80+
assert_equal_prop_vector(actual.bools, expected.bools, np.array([]))
8081
assert_equal_prop_vector(actual.ints, expected.ints, np.array([]))
82+
assert_equal_prop_vector(actual.shorts, expected.shorts, np.array([]))
83+
assert_equal_prop_vector(actual.chars, expected.chars, np.array([]))
8184
assert_equal_prop_vector(actual.longs, expected.longs, np.array([]))
8285
assert_equal_prop_vector(actual.floats, expected.floats, np.array([]))
8386
assert_equal_prop_vector(actual.doubles, expected.doubles, np.array([]))
87+
assert_equal_prop_approx(actual.bools_list, expected.bools_list, [])
8488
assert_equal_prop_approx(actual.ints_list, expected.ints_list, [])
89+
assert_equal_prop_approx(actual.shorts_list, expected.shorts_list, [])
90+
assert_equal_prop_approx(actual.chars_list, expected.chars_list, [])
8591
assert_equal_prop_approx(actual.longs_list, expected.longs_list, [])
8692
assert_equal_prop_approx(actual.floats_list, expected.floats_list, [])
8793
assert_equal_prop_approx(actual.doubles_list, expected.doubles_list, [])

tests/model.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,24 @@ class TestEntity:
1818
int8 = Property(int, type=PropertyType.byte, id=7, uid=1007)
1919
float64 = Property(float, type=PropertyType.double, id=8, uid=1008)
2020
float32 = Property(float, type=PropertyType.float, id=9, uid=1009)
21-
bytes = Property(bytes, id=10, uid=1010, index_type=IndexType.hash64)
22-
ints = Property(np.ndarray, type=PropertyType.intVector, id=11, uid=1011)
23-
longs = Property(np.ndarray, type=PropertyType.longVector, id=12, uid=1012)
24-
floats = Property(np.ndarray, type=PropertyType.floatVector, id=13, uid=1013)
25-
doubles = Property(np.ndarray, type=PropertyType.doubleVector, id=14, uid=1014)
26-
ints_list = Property(list, type=PropertyType.intVector, id=15, uid=1015)
27-
longs_list = Property(list, type=PropertyType.longVector, id=16, uid=1016)
28-
floats_list = Property(list, type=PropertyType.floatVector, id=17, uid=1017)
29-
doubles_list = Property(list, type=PropertyType.doubleVector, id=18, uid=1018)
30-
date = Property(int, type=PropertyType.date, id=19, uid=1019)
31-
date_nano = Property(int, type=PropertyType.dateNano, id=20, uid=1020)
32-
flex = Property(Generic, type=PropertyType.flex, id=21, uid=1021)
21+
bools = Property(np.ndarray, type=PropertyType.boolVector, id=10, uid=1010)
22+
bytes = Property(bytes, id=11, uid=1011)
23+
shorts = Property(np.ndarray, type=PropertyType.shortVector, id=12, uid=1012)
24+
chars = Property(np.ndarray, type=PropertyType.charVector, id=13, uid=1013)
25+
ints = Property(np.ndarray, type=PropertyType.intVector, id=14, uid=1014)
26+
longs = Property(np.ndarray, type=PropertyType.longVector, id=15, uid=1015)
27+
floats = Property(np.ndarray, type=PropertyType.floatVector, id=16, uid=1016)
28+
doubles = Property(np.ndarray, type=PropertyType.doubleVector, id=17, uid=1017)
29+
bools_list = Property(list, type=PropertyType.boolVector, id=18, uid=1018)
30+
shorts_list = Property(list, type=PropertyType.shortVector, id=19, uid=1019)
31+
chars_list = Property(list, type=PropertyType.charVector, id=20, uid=1020)
32+
ints_list = Property(list, type=PropertyType.intVector, id=21, uid=1021)
33+
longs_list = Property(list, type=PropertyType.longVector, id=22, uid=1022)
34+
floats_list = Property(list, type=PropertyType.floatVector, id=23, uid=1023)
35+
doubles_list = Property(list, type=PropertyType.doubleVector, id=24, uid=1024)
36+
date = Property(int, type=PropertyType.date, id=25, uid=1025)
37+
date_nano = Property(int, type=PropertyType.dateNano, id=26, uid=1026)
38+
flex = Property(Generic, type=PropertyType.flex, id=27, uid=1027)
3339
transient = "" # not "Property" so it's not stored
3440

3541
def __init__(self, string: str = ""):

tests/test_box.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,17 @@ def test_box_basics():
3939
object.float64 = 4.2
4040
object.float32 = 1.5
4141
object.bytes = bytes([1, 1, 2, 3, 5])
42+
object.bools = np.array([True, False, True, True, False], dtype=np.bool_)
4243
object.ints = np.array([1, 2, 3, 555, 120, 222], dtype=np.int32)
43-
object.longs = np.array([9182, 8273, 7364, 6455, 55462547], dtype=np.int64)
44+
object.shorts = np.array([7, 8, 9, 12, 13, 22], dtype=np.int16)
45+
object.chars = np.array([311, 426, 852, 927, 1025], dtype=np.uint16)
46+
object.longs = np.array([4299185519, 155462547, 5019238156195], dtype=np.int64)
4447
object.floats = np.array([0.1, 1.2, 2.3, 3.4, 4.5], dtype=np.float32)
4548
object.doubles = np.array([99.99, 88.88, 77.77, 66.66, 55.595425], dtype=np.float64)
49+
object.bools_list = [True, False, True, True, False]
4650
object.ints_list = [91, 82, 73, 64, 55]
51+
object.shorts_list = [8, 2, 7, 3, 6]
52+
object.chars_list = [4, 5, 43, 75, 12]
4753
object.longs_list = [4568, 8714, 1234, 5678, 9012240941]
4854
object.floats_list = [0.11, 1.22, 2.33, 3.44, 4.5595]
4955
object.doubles_list = [99.1999, 88.2888, 77.3777, 66.4666, 55.6597555]

0 commit comments

Comments
 (0)