Skip to content

Commit c1ab83b

Browse files
rwhoggZhou Wang
andauthored
fix(zlib): Accomodate different Zlib compression levels (#852)
Different Zlib compression levels produce different compression markers. Co-authored-by: Zhou Wang <[email protected]> Co-authored-by: Zhou Wang <[email protected]>
1 parent d2a3366 commit c1ab83b

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

google/cloud/ndb/model.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,14 @@ class Person(Model):
334334
_MEANING_PREDEFINED_ENTITY_USER = 20
335335
_MEANING_COMPRESSED = 22
336336

337-
# As produced by zlib. Indicates compressed byte sequence using DEFLATE at
338-
# default compression level, with a 32K window size.
339-
# From https://github.com/madler/zlib/blob/master/doc/rfc1950.txt
340-
_ZLIB_COMPRESSION_MARKER = b"x\x9c"
337+
_ZLIB_COMPRESSION_MARKERS = (
338+
# As produced by zlib. Indicates compressed byte sequence using DEFLATE at
339+
# default compression level, with a 32K window size.
340+
# From https://github.com/madler/zlib/blob/master/doc/rfc1950.txt
341+
b"x\x9c",
342+
# Other compression levels produce the following marker.
343+
b"x^",
344+
)
341345

342346
_MAX_STRING_LENGTH = 1500
343347
Key = key_module.Key
@@ -2619,7 +2623,7 @@ def _from_base_type(self, value):
26192623
return
26202624

26212625
if self._compressed and not isinstance(value, _CompressedValue):
2622-
if not value.startswith(_ZLIB_COMPRESSION_MARKER):
2626+
if not value.startswith(_ZLIB_COMPRESSION_MARKERS):
26232627
return value
26242628
value = _CompressedValue(value)
26252629

@@ -2645,13 +2649,13 @@ def _to_datastore(self, entity, data, prefix="", repeated=False):
26452649
if self._repeated:
26462650
compressed_value = []
26472651
for rval in value:
2648-
if rval and not rval.startswith(_ZLIB_COMPRESSION_MARKER):
2652+
if rval and not rval.startswith(_ZLIB_COMPRESSION_MARKERS):
26492653
rval = zlib.compress(rval)
26502654
compressed_value.append(rval)
26512655
value = compressed_value
26522656
data[key] = value
26532657
if not self._repeated:
2654-
if value and not value.startswith(_ZLIB_COMPRESSION_MARKER):
2658+
if value and not value.startswith(_ZLIB_COMPRESSION_MARKERS):
26552659
value = zlib.compress(value)
26562660
data[key] = value
26572661

0 commit comments

Comments
 (0)