Skip to content

Commit ff00a4d

Browse files
committed
support for binary dataset write
1 parent 27f6fc8 commit ff00a4d

File tree

1 file changed

+45
-10
lines changed

1 file changed

+45
-10
lines changed

h5json/hdf5db.py

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,13 +2082,16 @@ def getDatasetValuesByUuid(self, obj_uuid, slices=Ellipsis, format="json"):
20822082
raise IOError(errno.EINVAL, msg)
20832083

20842084
if dset is None:
2085-
return None
2085+
msg = "Dataset: " + obj_uuid + " not found"
2086+
self.log.info(msg)
2087+
raise IOError(errno.ENXIO, msg)
2088+
20862089
values = None
20872090
dt = dset.dtype
20882091
typeItem = getTypeItem(dt)
20892092
itemSize = getItemSize(typeItem)
20902093
if itemSize == "H5T_VARIABLE" and format == "binary":
2091-
msg + "Only JSON is supported for for this data type"
2094+
msg = "Only JSON is supported for for this data type"
20922095
self.log.info(msg)
20932096
raise IOError(errno.EINVAL, msg)
20942097

@@ -2116,7 +2119,7 @@ def getDatasetValuesByUuid(self, obj_uuid, slices=Ellipsis, format="json"):
21162119

21172120
if dt.kind == 'O':
21182121
if format != "json":
2119-
msg + "Only JSON is supported for for this data type"
2122+
msg = "Only JSON is supported for for this data type"
21202123
self.log.info(msg)
21212124
raise IOError(errno.EINVAL, msg)
21222125
# numpy object type - could be a vlen string or generic vlen
@@ -2146,7 +2149,7 @@ def getDatasetValuesByUuid(self, obj_uuid, slices=Ellipsis, format="json"):
21462149
# For Python3 fixed string values will be returned as bytes,
21472150
# so finese them into strings
21482151
if format != "json":
2149-
msg + "Only JSON is supported for for this data type"
2152+
msg = "Only JSON is supported for for this data type"
21502153
self.log.info(msg)
21512154
raise IOError(errno.EINVAL, msg)
21522155
values = self.bytesArrayToList(dset[slices])
@@ -2179,6 +2182,7 @@ def getDatasetPointSelectionByUuid(self, obj_uuid, points):
21792182
msg = "Dataset: " + obj_uuid + " not found"
21802183
self.log.info(msg)
21812184
raise IOError(errno.ENXIO, msg)
2185+
21822186
rank = len(dset.shape)
21832187
values = np.zeros(len(points), dtype=dset.dtype)
21842188
try:
@@ -2200,18 +2204,36 @@ def getDatasetPointSelectionByUuid(self, obj_uuid, points):
22002204
setDatasetValuesByUuid - update the given dataset values with supplied data
22012205
and optionally a hyperslab selection (slices)
22022206
"""
2203-
def setDatasetValuesByUuid(self, obj_uuid, data, slices=None):
2207+
def setDatasetValuesByUuid(self, obj_uuid, data, slices=None, format="json"):
22042208
dset = self.getDatasetObjByUuid(obj_uuid)
2205-
2209+
2210+
if format not in ("json", "binary"):
2211+
msg = "only json and binary formats are supported"
2212+
self.log.info(msg)
2213+
raise IOError(errno.EINVAL, msg)
2214+
2215+
if format == "binary" and type(data) is not bytes:
2216+
msg ="data must be of type bytes for binary writing"
2217+
self.log.info(msg)
2218+
raise IOError(errno.EINVAL, msg)
2219+
22062220
if dset is None:
22072221
msg = "Dataset: " + obj_uuid + " not found"
22082222
self.log.info(msg)
22092223
raise IOError(errno.ENXIO, msg)
2224+
2225+
dt = dset.dtype
2226+
typeItem = getTypeItem(dt)
2227+
itemSize = getItemSize(typeItem)
2228+
if itemSize == "H5T_VARIABLE" and format == "binary":
2229+
msg = "Only JSON is supported for for this data type"
2230+
self.log.info(msg)
2231+
raise IOError(errno.EINVAL, msg)
22102232

22112233
# need some special conversion for compound types --
22122234
# each element must be a tuple, but the JSON decoder
22132235
# gives us a list instead.
2214-
if len(dset.dtype) > 1 and type(data) in (list, tuple):
2236+
if format != "binary" and len(dset.dtype) > 1 and type(data) in (list, tuple):
22152237
converted_data = []
22162238
for i in range(len(data)):
22172239
converted_data.append(self.toTuple(data[i]))
@@ -2220,15 +2242,28 @@ def setDatasetValuesByUuid(self, obj_uuid, data, slices=None):
22202242
h5t_check = h5py.check_dtype(ref=dset.dtype)
22212243
if h5t_check in (h5py.Reference, h5py.RegionReference):
22222244
# convert data to data refs
2245+
if format == "binary":
2246+
msg = "Only JSON is supported for for this data type"
2247+
self.log.info(msg)
2248+
raise IOError(errno.EINVAL, msg)
22232249
data = self.listToRef(data)
22242250

22252251
if slices is None:
22262252
# write entire dataset
2227-
dset[()] = data
2253+
if format == "binary":
2254+
if len(data) != dset.size:
2255+
msg = "Expected " + dset.size + " bytes, but got: " + len(data)
2256+
self.log.info(msg)
2257+
raise IOError(errno.EINVAL, msg)
2258+
arr = np.fromstring(data, dtype=dset.dtype)
2259+
arr.reshape(dset.shape)
2260+
dset[()] = arr
2261+
else:
2262+
dset[()] = data
22282263
else:
22292264
if type(slices) != tuple:
2230-
self.log.error(
2231-
"setDatasetValuesByUuid: bad type for dim parameter")
2265+
msg = "setDatasetValuesByUuid: bad type for dim parameter"
2266+
self.log.error(msg)
22322267
return False
22332268
rank = len(dset.shape)
22342269

0 commit comments

Comments
 (0)