1- # Copyright 2019-2021 ObjectBox Ltd. All rights reserved.
1+ # Copyright 2019-2023 ObjectBox Ltd. All rights reserved.
22#
33# Licensed under the Apache License, Version 2.0 (the "License");
44# you may not use this file except in compliance with the License.
1414
1515
1616import flatbuffers
17+ import numpy as np
1718from objectbox .c import *
1819from objectbox .model .properties import Property
1920
@@ -64,8 +65,14 @@ def fill_properties(self):
6465 self .id_property = prop
6566
6667 if prop ._fb_type == flatbuffers .number_types .UOffsetTFlags :
67- assert prop ._ob_type in [OBXPropertyType_String , OBXPropertyType_ByteVector ], \
68- "programming error - invalid type OB & FB type combination"
68+ assert prop ._ob_type in [
69+ OBXPropertyType_String ,
70+ OBXPropertyType_ByteVector ,
71+ OBXPropertyType_IntVector ,
72+ OBXPropertyType_LongVector ,
73+ OBXPropertyType_FloatVector ,
74+ OBXPropertyType_DoubleVector ,
75+ ], "programming error - invalid type OB & FB type combination"
6976 self .offset_properties .append (prop )
7077
7178 # print('Property {}.{}: {} (ob:{} fb:{})'.format(self.name, prop._name, prop._py_type, prop._ob_type, prop._fb_type))
@@ -78,8 +85,14 @@ def fill_properties(self):
7885 def get_value (self , object , prop : Property ):
7986 # in case value is not overwritten on the object, it's the Property object itself (= as defined in the Class)
8087 val = getattr (object , prop ._name )
81- if val == prop :
82- return prop ._py_type () # default (empty) value for the given type
88+ if prop ._py_type == list [int ] or prop ._py_type == list [float ]:
89+ if (val == np .array (prop )).all ():
90+ return prop ._py_type ()
91+ elif prop ._py_type == np .ndarray :
92+ if (val == np .array (prop )).all ():
93+ return np .array ([0 ])
94+ elif val == prop :
95+ return prop ._py_type () # default (empty) value for the given type
8396 return val
8497
8598 def get_object_id (self , object ) -> int :
@@ -99,6 +112,14 @@ def marshal(self, object, id: int) -> bytearray:
99112 offsets [prop ._id ] = builder .CreateString (val .encode ('utf-8' ))
100113 elif prop ._ob_type == OBXPropertyType_ByteVector :
101114 offsets [prop ._id ] = builder .CreateByteVector (val )
115+ elif prop ._ob_type == OBXPropertyType_IntVector :
116+ offsets [prop ._id ] = builder .CreateNumpyVector (np .array (val , dtype = np .int32 ))
117+ elif prop ._ob_type == OBXPropertyType_LongVector :
118+ offsets [prop ._id ] = builder .CreateNumpyVector (np .array (val , dtype = np .int64 ))
119+ elif prop ._ob_type == OBXPropertyType_FloatVector :
120+ offsets [prop ._id ] = builder .CreateNumpyVector (np .array (val , dtype = np .float32 ))
121+ elif prop ._ob_type == OBXPropertyType_DoubleVector :
122+ offsets [prop ._id ] = builder .CreateNumpyVector (np .array (val , dtype = np .float64 ))
102123 else :
103124 assert False , "programming error - invalid type OB & FB type combination"
104125
@@ -143,6 +164,22 @@ def unmarshal(self, data: bytes):
143164
144165 # slice the vector as a requested type
145166 val = prop ._py_type (table .Bytes [start :start + size ])
167+ elif prop ._ob_type == OBXPropertyType_IntVector :
168+ val = table .GetVectorAsNumpy (flatbuffers .number_types .Int32Flags , o )
169+ if prop ._py_type == list [int ]:
170+ val = val .tolist ()
171+ elif prop ._ob_type == OBXPropertyType_LongVector :
172+ val = table .GetVectorAsNumpy (flatbuffers .number_types .Int64Flags , o )
173+ if prop ._py_type == list [int ]:
174+ val = val .tolist ()
175+ elif prop ._ob_type == OBXPropertyType_FloatVector :
176+ val = table .GetVectorAsNumpy (flatbuffers .number_types .Float32Flags , o )
177+ if prop ._py_type == list [float ]:
178+ val = val .tolist ()
179+ elif prop ._ob_type == OBXPropertyType_DoubleVector :
180+ val = table .GetVectorAsNumpy (flatbuffers .number_types .Float64Flags , o )
181+ if prop ._py_type == list [float ]:
182+ val = val .tolist ()
146183 else :
147184 val = table .Get (prop ._fb_type , o + table .Pos )
148185
0 commit comments