MCPcopy Create free account
hub / github.com/apple/foundationdb / _encode

Function _encode

bindings/python/fdb/tuple.py:311–370  ·  view source on GitHub ↗
(value, nested=False)

Source from the content-addressed store, hash-verified

309
310
311def _encode(value, nested=False):
312 # returns [code][data] (code != 0xFF)
313 # encoded values are self-terminating
314 # sorting need to work too!
315 if value == None: # ==, not is, because some fdb.impl.Value are equal to None
316 if nested:
317 return b''.join([six.int2byte(NULL_CODE), six.int2byte(0xff)]), -1
318 else:
319 return b''.join([six.int2byte(NULL_CODE)]), -1
320 elif isinstance(value, bytes): # also gets non-None fdb.impl.Value
321 return six.int2byte(BYTES_CODE) + value.replace(b'\x00', b'\x00\xFF') + b'\x00', -1
322 elif isinstance(value, six.text_type):
323 return six.int2byte(STRING_CODE) + value.encode('utf-8').replace(b'\x00', b'\x00\xFF') + b'\x00', -1
324 elif isinstance(value, six.integer_types) and (not isinstance(value, bool) or (hasattr(fdb, '_version') and fdb._version < 500)):
325 if value == 0:
326 return b''.join([six.int2byte(INT_ZERO_CODE)]), -1
327 elif value > 0:
328 if value >= _size_limits[-1]:
329 length = (_bit_length(value) + 7) // 8
330 data = [six.int2byte(POS_INT_END), six.int2byte(length)]
331 for i in _range(length - 1, -1, -1):
332 data.append(six.int2byte((value >> (8 * i)) & 0xff))
333 return b''.join(data), -1
334
335 n = bisect_left(_size_limits, value)
336 return six.int2byte(INT_ZERO_CODE + n) + struct.pack(">Q", value)[-n:], -1
337 else:
338 if -value >= _size_limits[-1]:
339 length = (_bit_length(value) + 7) // 8
340 value += (1 << (length * 8)) - 1
341 data = [six.int2byte(NEG_INT_START), six.int2byte(length ^ 0xff)]
342 for i in _range(length - 1, -1, -1):
343 data.append(six.int2byte((value >> (8 * i)) & 0xff))
344 return b''.join(data), -1
345
346 n = bisect_left(_size_limits, -value)
347 maxv = _size_limits[n]
348 return six.int2byte(INT_ZERO_CODE - n) + struct.pack(">Q", maxv + value)[-n:], -1
349 elif isinstance(value, ctypes.c_float) or isinstance(value, SingleFloat):
350 return six.int2byte(FLOAT_CODE) + _float_adjust(struct.pack(">f", value.value), True), -1
351 elif isinstance(value, ctypes.c_double):
352 return six.int2byte(DOUBLE_CODE) + _float_adjust(struct.pack(">d", value.value), True), -1
353 elif isinstance(value, float):
354 return six.int2byte(DOUBLE_CODE) + _float_adjust(struct.pack(">d", value), True), -1
355 elif isinstance(value, uuid.UUID):
356 return six.int2byte(UUID_CODE) + value.bytes, -1
357 elif isinstance(value, bool):
358 if value:
359 return b''.join([six.int2byte(TRUE_CODE)]), -1
360 else:
361 return b''.join([six.int2byte(FALSE_CODE)]), -1
362 elif isinstance(value, Versionstamp):
363 version_pos = -1 if value.is_complete() else 1
364 return six.int2byte(VERSIONSTAMP_CODE) + value.to_bytes(), version_pos
365 elif isinstance(value, tuple) or isinstance(value, list):
366 child_bytes, version_pos = _reduce_children(map(lambda x: _encode(x, True), value))
367 new_version_pos = -1 if version_pos < 0 else version_pos + 1
368 return b''.join([six.int2byte(NESTED_CODE)] + child_bytes + [six.int2byte(0x00)]), new_version_pos

Callers

nothing calls this directly

Calls 13

_bit_lengthFunction · 0.85
_float_adjustFunction · 0.85
_reduce_childrenFunction · 0.85
mapFunction · 0.85
strFunction · 0.85
typeEnum · 0.85
is_completeMethod · 0.80
to_bytesMethod · 0.80
joinMethod · 0.45
replaceMethod · 0.45
encodeMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected