def encode_timeseries_put(self, tsobj): ''' Returns an Erlang-TTB encoded tuple with the appropriate data and metadata from a TsObject. :param tsobj: a TsObject :type tsobj: TsObject :rtype: term-to-binary encoded object ''' if tsobj.columns: raise NotImplementedError('columns are not used') if tsobj.rows and isinstance(tsobj.rows, list): req_rows = [] for row in tsobj.rows: req_r = [] for cell in row: req_r.append(self.encode_to_ts_cell(cell)) req_rows.append(tuple(req_r)) req = tsputreq_a, tsobj.table.name, [], req_rows mc = MSG_CODE_TS_TTB_MSG rc = MSG_CODE_TS_TTB_MSG return Msg(mc, encode(req), rc) else: raise RiakError("TsObject requires a list of rows")
def test_decode_data_from_get(self): colnames = ["varchar", "sint64", "double", "timestamp", "boolean", "varchar", "varchar"] coltypes = [varchar_a, sint64_a, double_a, timestamp_a, boolean_a, varchar_a, varchar_a] r0 = (bd0, 0, 1.2, unix_time_millis(ts0), True, [], str1, None) r1 = (bd1, 3, 4.5, unix_time_millis(ts1), False, [], str1, None) rows = [r0, r1] # { tsgetresp, { [colnames], [coltypes], [rows] } } data_t = colnames, coltypes, rows rsp_data = tsgetresp_a, data_t rsp_ttb = encode(rsp_data) tsobj = TsObject(None, self.table) c = TtbCodec() c.decode_timeseries(decode(rsp_ttb), tsobj) for i in range(0, 1): dr = rows[i] r = tsobj.rows[i] # encoded self.assertEqual(r[0], dr[0].encode('utf-8')) self.assertEqual(r[1], dr[1]) self.assertEqual(r[2], dr[2]) # NB *not* decoding timestamps # dt = datetime_from_unix_time_millis(dr[3]) self.assertEqual(r[3], dr[3]) if i == 0: self.assertEqual(r[4], True) else: self.assertEqual(r[4], False) self.assertEqual(r[5], None) self.assertEqual(r[6], dr[6].encode('ascii')) self.assertEqual(r[7], None)
def encode_timeseries_query(self, table, query, interpolations=None): q = query if '{table}' in q: q = q.format(table=table.name) tsi = tsinterpolation_a, q, [] req = tsqueryreq_a, tsi, False, udef_a mc = MSG_CODE_TS_TTB_MSG rc = MSG_CODE_TS_TTB_MSG return Msg(mc, encode(req), rc)
def test_encode_data_for_get(self): keylist = [ str_to_bytes('hash1'), str_to_bytes('user2'), unix_time_millis(ts0) ] req = tsgetreq_a, str_to_bytes(table_name), keylist, udef_a req_test = encode(req) test_key = ['hash1', 'user2', ts0] c = TtbCodec() msg = c.encode_timeseries_keyreq(self.table, test_key) self.assertEqual(req_test, msg.data)
def test_encode_data_for_put(self): r0 = (bd0, 0, 1.2, unix_time_millis(ts0), True, []) r1 = (bd1, 3, 4.5, unix_time_millis(ts1), False, []) rows = [r0, r1] req = tsputreq_a, str_to_bytes(table_name), [], rows req_test = encode(req) rows_to_encode = [[bd0, 0, 1.2, ts0, True, None], [bd1, 3, 4.5, ts1, False, None]] tsobj = TsObject(None, self.table, rows_to_encode, None) c = TtbCodec() msg = c.encode_timeseries_put(tsobj) self.assertEqual(req_test, msg.data)
def test_encode_data_for_put(self): r0 = (bd0, 0, 1.2, unix_time_millis(ts0), True, []) r1 = (bd1, 3, 4.5, unix_time_millis(ts1), False, []) rows = [r0, r1] req = tsputreq_a, str_to_bytes(table_name), [], rows req_test = encode(req) rows_to_encode = [ [bd0, 0, 1.2, ts0, True, None], [bd1, 3, 4.5, ts1, False, None] ] tsobj = TsObject(None, self.table, rows_to_encode, None) c = TtbCodec() msg = c.encode_timeseries_put(tsobj) self.assertEqual(req_test, msg.data)
def encode_timeseries_keyreq(self, table, key, is_delete=False): key_vals = None if isinstance(key, list): key_vals = key else: raise ValueError("key must be a list") mc = MSG_CODE_TS_TTB_MSG rc = MSG_CODE_TS_TTB_MSG req_atom = tsgetreq_a if is_delete: req_atom = tsdelreq_a # TODO FUTURE add timeout as last param req = req_atom, table.name, \ [self.encode_to_ts_cell(k) for k in key_vals], udef_a return Msg(mc, encode(req), rc)
def test_decode_data_from_get(self): colnames = [ "varchar", "sint64", "double", "timestamp", "boolean", "varchar", "varchar", "blob" ] coltypes = [ varchar_a, sint64_a, double_a, timestamp_a, boolean_a, varchar_a, varchar_a ] r0 = (bd0, 0, 1.2, unix_time_millis(ts0), True, [], str1, None, None) r1 = (bd1, 3, 4.5, unix_time_millis(ts1), False, [], str1, None, blob0) rows = [r0, r1] # { tsgetresp, { [colnames], [coltypes], [rows] } } data_t = colnames, coltypes, rows rsp_data = tsgetresp_a, data_t rsp_ttb = encode(rsp_data) tsobj = TsObject(None, self.table) c = TtbCodec() c.decode_timeseries(decode(rsp_ttb), tsobj) for i in range(0, 1): dr = rows[i] r = tsobj.rows[i] # encoded self.assertEqual(r[0], dr[0].encode('utf-8')) self.assertEqual(r[1], dr[1]) self.assertEqual(r[2], dr[2]) # NB *not* decoding timestamps # dt = datetime_from_unix_time_millis(dr[3]) self.assertEqual(r[3], dr[3]) if i == 0: self.assertEqual(r[4], True) else: self.assertEqual(r[4], False) self.assertEqual(r[5], None) self.assertEqual(r[6], dr[6].encode('ascii')) self.assertEqual(r[7], None) self.assertEqual(r[8], dr[8])
def testEncode(self): for python, expected_type, erlang in erlang_term_binaries + erlang_term_encode: encoded = encode(python) self.assertEqual(erlang, encoded)