def test_encode_data_for_put(self):
        c = PbufCodec()
        tsobj = TsObject(None, self.table, self.rows, None)
        msg = c.encode_timeseries_put(tsobj)
        req = riak.pb.riak_ts_pb2.TsPutReq()
        req.ParseFromString(msg.data)

        # NB: expected, actual
        self.assertEqual(self.table.name, bytes_to_str(req.table))
        self.assertEqual(len(self.rows), len(req.rows))

        r0 = req.rows[0]
        self.assertEqual(bytes_to_str(r0.cells[0].varchar_value),
                         self.rows[0][0])
        self.assertEqual(r0.cells[1].sint64_value, self.rows[0][1])
        self.assertEqual(r0.cells[2].double_value, self.rows[0][2])
        self.assertEqual(r0.cells[3].timestamp_value, self.ts0ms)
        self.assertEqual(r0.cells[4].boolean_value, self.rows[0][4])
        self.assertFalse(r0.cells[5].HasField('varchar_value'))

        r1 = req.rows[1]
        self.assertEqual(bytes_to_str(r1.cells[0].varchar_value),
                         self.rows[1][0])
        self.assertEqual(r1.cells[1].sint64_value, self.rows[1][1])
        self.assertEqual(r1.cells[2].double_value, self.rows[1][2])
        self.assertEqual(r1.cells[3].timestamp_value, self.ts1ms)
        self.assertEqual(r1.cells[4].boolean_value, self.rows[1][4])
        self.assertEqual(r1.cells[5].varchar_value, self.rows[1][5])
 def ts_query(self, table, query, interpolations=None):
     msg_code = riak.pb.messages.MSG_CODE_TS_QUERY_REQ
     codec = self._get_codec(msg_code)
     msg = codec.encode_timeseries_query(table, query, interpolations)
     resp_code, resp = self._request(msg, codec)
     tsobj = TsObject(self._client, table)
     codec.decode_timeseries(resp, tsobj, self._ts_convert_timestamp)
     return tsobj
 def ts_get(self, table, key):
     msg_code = MSG_CODE_TS_TTB_MSG
     codec = self._get_codec(msg_code)
     msg = codec.encode_timeseries_keyreq(table, key)
     resp_code, resp = self._request(msg, codec)
     tsobj = TsObject(self._client, table)
     codec.decode_timeseries(resp, tsobj, self._ts_convert_timestamp)
     return tsobj
Exemple #4
0
    def ts_get(self, table, key):
        req = riak.pb.riak_ts_pb2.TsGetReq()
        self._encode_timeseries_keyreq(table, key, req)

        msg_code, ts_get_resp = self._request(
            riak.pb.messages.MSG_CODE_TS_GET_REQ, req,
            riak.pb.messages.MSG_CODE_TS_GET_RESP)

        tsobj = TsObject(self._client, table, [], None)
        self._decode_timeseries(ts_get_resp, tsobj)
        return tsobj
    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)
Exemple #6
0
    def new(self, rows, columns=None):
        """
        A shortcut for manually instantiating a new
        :class:`~riak.ts_object.TsObject`

        :param rows: An list of lists with timeseries data
        :type rows: list
        :param columns: An list of Column names and types. Optional.
        :type columns: list
        :rtype: :class:`~riak.ts_object.TsObject`
        """
        from riak.ts_object import TsObject

        return TsObject(self._client, self, rows, columns)
Exemple #7
0
    def ts_query(self, table, query, interpolations=None):
        req = riak.pb.riak_ts_pb2.TsQueryReq()

        q = query
        if '{table}' in q:
            q = q.format(table=table.name)

        req.query.base = str_to_bytes(q)

        msg_code, ts_query_resp = self._request(
            riak.pb.messages.MSG_CODE_TS_QUERY_REQ, req,
            riak.pb.messages.MSG_CODE_TS_QUERY_RESP)

        tsobj = TsObject(self._client, table, [], [])
        self._decode_timeseries(ts_query_resp, tsobj)
        return tsobj
    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])
Exemple #9
0
    def test_encode_data_for_put(self):
        tsobj = TsObject(None, self.table, self.rows, None)
        ts_put_req = riak.pb.riak_ts_pb2.TsPutReq()
        self.c._encode_timeseries_put(tsobj, ts_put_req)

        # NB: expected, actual
        self.assertEqual(self.table.name, bytes_to_str(ts_put_req.table))
        self.assertEqual(len(self.rows), len(ts_put_req.rows))

        r0 = ts_put_req.rows[0]
        self.assertEqual(bytes_to_str(r0.cells[0].varchar_value),
                         self.rows[0][0])
        self.assertEqual(r0.cells[1].sint64_value, self.rows[0][1])
        self.assertEqual(r0.cells[2].double_value, self.rows[0][2])
        self.assertEqual(r0.cells[3].timestamp_value, self.ts0ms)
        self.assertEqual(r0.cells[4].boolean_value, self.rows[0][4])

        r1 = ts_put_req.rows[1]
        self.assertEqual(bytes_to_str(r1.cells[0].varchar_value),
                         self.rows[1][0])
        self.assertEqual(r1.cells[1].sint64_value, self.rows[1][1])
        self.assertEqual(r1.cells[2].double_value, self.rows[1][2])
        self.assertEqual(r1.cells[3].timestamp_value, self.ts1ms)
        self.assertEqual(r1.cells[4].boolean_value, self.rows[1][4])
    def test_decode_data_from_query(self):
        tqr = riak.pb.riak_ts_pb2.TsQueryResp()

        c0 = tqr.columns.add()
        c0.name = str_to_bytes('col_varchar')
        c0.type = TsColumnType.Value('VARCHAR')
        c1 = tqr.columns.add()
        c1.name = str_to_bytes('col_integer')
        c1.type = TsColumnType.Value('SINT64')
        c2 = tqr.columns.add()
        c2.name = str_to_bytes('col_double')
        c2.type = TsColumnType.Value('DOUBLE')
        c3 = tqr.columns.add()
        c3.name = str_to_bytes('col_timestamp')
        c3.type = TsColumnType.Value('TIMESTAMP')
        c4 = tqr.columns.add()
        c4.name = str_to_bytes('col_boolean')
        c4.type = TsColumnType.Value('BOOLEAN')
        c5 = tqr.columns.add()
        c5.name = str_to_bytes('col_blob')
        c5.type = TsColumnType.Value('BLOB')

        r0 = tqr.rows.add()
        r0c0 = r0.cells.add()
        r0c0.varchar_value = str_to_bytes(self.rows[0][0])
        r0c1 = r0.cells.add()
        r0c1.sint64_value = self.rows[0][1]
        r0c2 = r0.cells.add()
        r0c2.double_value = self.rows[0][2]
        r0c3 = r0.cells.add()
        r0c3.timestamp_value = self.ts0ms
        r0c4 = r0.cells.add()
        r0c4.boolean_value = self.rows[0][4]
        r0.cells.add()

        r1 = tqr.rows.add()
        r1c0 = r1.cells.add()
        r1c0.varchar_value = str_to_bytes(self.rows[1][0])
        r1c1 = r1.cells.add()
        r1c1.sint64_value = self.rows[1][1]
        r1c2 = r1.cells.add()
        r1c2.double_value = self.rows[1][2]
        r1c3 = r1.cells.add()
        r1c3.timestamp_value = self.ts1ms
        r1c4 = r1.cells.add()
        r1c4.boolean_value = self.rows[1][4]
        r1c5 = r1.cells.add()
        r1c5.varchar_value = self.rows[1][5]

        tsobj = TsObject(None, self.table)
        c = PbufCodec()
        c.decode_timeseries(tqr, tsobj, True)

        self.assertEqual(len(tsobj.rows), len(self.rows))
        self.assertEqual(len(tsobj.columns.names), len(tqr.columns))
        self.assertEqual(len(tsobj.columns.types), len(tqr.columns))

        cn, ct = tsobj.columns
        self.assertEqual(cn[0], 'col_varchar')
        self.assertEqual(ct[0], 'varchar')
        self.assertEqual(cn[1], 'col_integer')
        self.assertEqual(ct[1], 'sint64')
        self.assertEqual(cn[2], 'col_double')
        self.assertEqual(ct[2], 'double')
        self.assertEqual(cn[3], 'col_timestamp')
        self.assertEqual(ct[3], 'timestamp')
        self.assertEqual(cn[4], 'col_boolean')
        self.assertEqual(ct[4], 'boolean')
        self.assertEqual(cn[5], 'col_blob')
        self.assertEqual(ct[5], 'blob')

        r0 = tsobj.rows[0]
        self.assertEqual(bytes_to_str(r0[0]), self.rows[0][0])
        self.assertEqual(r0[1], self.rows[0][1])
        self.assertEqual(r0[2], self.rows[0][2])
        self.assertEqual(r0[3], ts0)
        self.assertEqual(r0[4], self.rows[0][4])
        self.assertEqual(r0[5], self.rows[0][5])

        r1 = tsobj.rows[1]
        self.assertEqual(bytes_to_str(r1[0]), self.rows[1][0])
        self.assertEqual(r1[1], self.rows[1][1])
        self.assertEqual(r1[2], self.rows[1][2])
        self.assertEqual(r1[3], ts1)
        self.assertEqual(r1[4], self.rows[1][4])
        self.assertEqual(r1[5], self.rows[1][5])
Exemple #11
0
    def test_decode_data_from_query(self):
        tqr = riak.pb.riak_ts_pb2.TsQueryResp()

        c0 = tqr.columns.add()
        c0.name = str_to_bytes('col_varchar')
        c0.type = TsColumnType.Value('VARCHAR')
        c1 = tqr.columns.add()
        c1.name = str_to_bytes('col_integer')
        c1.type = TsColumnType.Value('SINT64')
        c2 = tqr.columns.add()
        c2.name = str_to_bytes('col_double')
        c2.type = TsColumnType.Value('DOUBLE')
        c3 = tqr.columns.add()
        c3.name = str_to_bytes('col_timestamp')
        c3.type = TsColumnType.Value('TIMESTAMP')
        c4 = tqr.columns.add()
        c4.name = str_to_bytes('col_boolean')
        c4.type = TsColumnType.Value('BOOLEAN')

        r0 = tqr.rows.add()
        r0c0 = r0.cells.add()
        r0c0.varchar_value = str_to_bytes(self.rows[0][0])
        r0c1 = r0.cells.add()
        r0c1.sint64_value = self.rows[0][1]
        r0c2 = r0.cells.add()
        r0c2.double_value = self.rows[0][2]
        r0c3 = r0.cells.add()
        r0c3.timestamp_value = self.ts0ms
        r0c4 = r0.cells.add()
        r0c4.boolean_value = self.rows[0][4]

        r1 = tqr.rows.add()
        r1c0 = r1.cells.add()
        r1c0.varchar_value = str_to_bytes(self.rows[1][0])
        r1c1 = r1.cells.add()
        r1c1.sint64_value = self.rows[1][1]
        r1c2 = r1.cells.add()
        r1c2.double_value = self.rows[1][2]
        r1c3 = r1.cells.add()
        r1c3.timestamp_value = self.ts1ms
        r1c4 = r1.cells.add()
        r1c4.boolean_value = self.rows[1][4]

        tsobj = TsObject(None, self.table, [], [])
        c = RiakPbcCodec()
        c._decode_timeseries(tqr, tsobj)

        self.assertEqual(len(self.rows), len(tsobj.rows))
        self.assertEqual(len(tqr.columns), len(tsobj.columns))

        c = tsobj.columns
        self.assertEqual(c[0][0], 'col_varchar')
        self.assertEqual(c[0][1], TsColumnType.Value('VARCHAR'))
        self.assertEqual(c[1][0], 'col_integer')
        self.assertEqual(c[1][1], TsColumnType.Value('SINT64'))
        self.assertEqual(c[2][0], 'col_double')
        self.assertEqual(c[2][1], TsColumnType.Value('DOUBLE'))
        self.assertEqual(c[3][0], 'col_timestamp')
        self.assertEqual(c[3][1], TsColumnType.Value('TIMESTAMP'))
        self.assertEqual(c[4][0], 'col_boolean')
        self.assertEqual(c[4][1], TsColumnType.Value('BOOLEAN'))

        r0 = tsobj.rows[0]
        self.assertEqual(r0[0], self.rows[0][0])
        self.assertEqual(r0[1], self.rows[0][1])
        self.assertEqual(r0[2], self.rows[0][2])
        self.assertEqual(r0[3], ts0)
        self.assertEqual(r0[4], self.rows[0][4])

        r1 = tsobj.rows[1]
        self.assertEqual(r1[0], self.rows[1][0])
        self.assertEqual(r1[1], self.rows[1][1])
        self.assertEqual(r1[2], self.rows[1][2])
        self.assertEqual(r1[3], ts1)
        self.assertEqual(r1[4], self.rows[1][4])