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,11 @@ 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 == np .ndarray :
89+ if (val == np .array (prop )).all ():
90+ return np .array ([])
91+ elif val == prop :
92+ return prop ._py_type () # default (empty) value for the given type
8393 return val
8494
8595 def get_object_id (self , object ) -> int :
@@ -99,6 +109,14 @@ def marshal(self, object, id: int) -> bytearray:
99109 offsets [prop ._id ] = builder .CreateString (val .encode ('utf-8' ))
100110 elif prop ._ob_type == OBXPropertyType_ByteVector :
101111 offsets [prop ._id ] = builder .CreateByteVector (val )
112+ elif prop ._ob_type == OBXPropertyType_IntVector :
113+ offsets [prop ._id ] = builder .CreateNumpyVector (np .array (val , dtype = np .int32 ))
114+ elif prop ._ob_type == OBXPropertyType_LongVector :
115+ offsets [prop ._id ] = builder .CreateNumpyVector (np .array (val , dtype = np .int64 ))
116+ elif prop ._ob_type == OBXPropertyType_FloatVector :
117+ offsets [prop ._id ] = builder .CreateNumpyVector (np .array (val , dtype = np .float32 ))
118+ elif prop ._ob_type == OBXPropertyType_DoubleVector :
119+ offsets [prop ._id ] = builder .CreateNumpyVector (np .array (val , dtype = np .float64 ))
102120 else :
103121 assert False , "programming error - invalid type OB & FB type combination"
104122
@@ -140,12 +158,20 @@ def unmarshal(self, data: bytes):
140158 # access the FB byte vector information
141159 start = table .Vector (o )
142160 size = table .VectorLen (o )
143-
144161 # slice the vector as a requested type
145162 val = prop ._py_type (table .Bytes [start :start + size ])
163+ elif prop ._ob_type == OBXPropertyType_IntVector :
164+ val = table .GetVectorAsNumpy (flatbuffers .number_types .Int32Flags , o )
165+ elif prop ._ob_type == OBXPropertyType_LongVector :
166+ val = table .GetVectorAsNumpy (flatbuffers .number_types .Int64Flags , o )
167+ elif prop ._ob_type == OBXPropertyType_FloatVector :
168+ val = table .GetVectorAsNumpy (flatbuffers .number_types .Float32Flags , o )
169+ elif prop ._ob_type == OBXPropertyType_DoubleVector :
170+ val = table .GetVectorAsNumpy (flatbuffers .number_types .Float64Flags , o )
146171 else :
147172 val = table .Get (prop ._fb_type , o + table .Pos )
148-
173+ if prop ._py_type == list :
174+ val = val .tolist ()
149175 setattr (obj , prop ._name , val )
150176 return obj
151177
0 commit comments