Exemplo n.º 1
0
 def testTimeToLiveGets(self):
     ttl = TimeToLive.of_days(8)
     self.assertEqual(ttl.get_value(), 8)
     self.assertEqual(ttl.get_unit(), TimeUnit.DAYS)
     ttl = TimeToLive.of_hours(10)
     self.assertEqual(ttl.get_value(), 10)
     self.assertEqual(ttl.get_unit(), TimeUnit.HOURS)
Exemplo n.º 2
0
 def testQueryStatementUpdateTTL(self):
     hour_in_milliseconds = 60 * 60 * 1000
     self.query_request.set_statement(
         'UPDATE ' + table_name + ' $u SET TTL CASE WHEN ' +
         'remaining_hours($u) < 0 THEN 3 ELSE remaining_hours($u) + 3 END '
         + 'HOURS WHERE fld_sid = 1 AND fld_id = 3')
     result = self.handle.query(self.query_request)
     ttl = TimeToLive.of_hours(3)
     expect_expiration = ttl.to_expiration_time(int(round(time() * 1000)))
     records = result.get_results()
     self.assertEqual(len(records), 1)
     self.assertEqual(records[0], {'NumRowsUpdated': 1})
     self.assertIsNone(result.get_continuation_key())
     self.assertEqual(result.get_read_kb(), 2 + prepare_cost)
     self.assertEqual(result.get_read_units(), 4 + prepare_cost)
     self.assertEqual(result.get_write_kb(), 3)
     self.assertEqual(result.get_write_units(), 3)
     # check the record after update ttl request succeed
     self.get_request.set_key({'fld_sid': 1, 'fld_id': 3})
     result = self.handle.get(self.get_request)
     actual_expiration = result.get_expiration_time()
     actual_expect_diff = actual_expiration - expect_expiration
     self.assertGreater(actual_expiration, 0)
     self.assertLess(actual_expect_diff, hour_in_milliseconds)
     self.assertEqual(result.get_read_kb(), 1)
     self.assertEqual(result.get_read_units(), 2)
     self.assertEqual(result.get_write_kb(), 0)
     self.assertEqual(result.get_write_units(), 0)
Exemplo n.º 3
0
 def testTimeToLiveUnitIsDaysOrHours(self):
     ttl = TimeToLive.of_days(6)
     self.assertTrue(ttl.unit_is_days())
     self.assertFalse(ttl.unit_is_hours())
     ttl = TimeToLive.of_hours(6)
     self.assertFalse(ttl.unit_is_days())
     self.assertTrue(ttl.unit_is_hours())
Exemplo n.º 4
0
 def setUp(self):
     self.set_up()
     self.row = get_row(with_sid=False)
     self.key = {'fld_id': 1}
     self.put_request = PutRequest().set_value(
         self.row).set_table_name(table_name).set_timeout(timeout)
     self.get_request = GetRequest().set_key(
         self.key).set_table_name(table_name)
     self.ttl = TimeToLive.of_hours(24)
     self.hour_in_milliseconds = 60 * 60 * 1000
     self.day_in_milliseconds = 24 * 60 * 60 * 1000
Exemplo n.º 5
0
 def testTimeToLiveToExpirationTime(self):
     ttl = TimeToLive.of_days(0)
     self.assertRaises(IllegalArgumentException, ttl.to_expiration_time,
                       'IllegalReferenceTime')
     self.assertRaises(IllegalArgumentException, ttl.to_expiration_time, 0)
     self.assertRaises(IllegalArgumentException, ttl.to_expiration_time, -1)
     self.assertEqual(ttl.to_expiration_time(int(round(time() * 1000))), 0)
     ttl = TimeToLive.of_hours(16)
     reference_time = int(round(time() * 1000))
     self.assertEqual(ttl.to_expiration_time(reference_time),
                      reference_time + 16 * 60 * 60 * 1000)
     ttl = TimeToLive.of_days(16)
     self.assertEqual(ttl.to_expiration_time(reference_time),
                      reference_time + 16 * 24 * 60 * 60 * 1000)
Exemplo n.º 6
0
    def setUpClass(cls):
        TestBase.set_up_class()
        table_ttl = TimeToLive.of_hours(16)
        create_statement = ('CREATE TABLE ' + table_name +
                            '(fld_sid INTEGER, fld_id INTEGER, \
fld_long LONG, fld_float FLOAT, fld_double DOUBLE, fld_bool BOOLEAN, \
fld_str STRING, fld_bin BINARY, fld_time TIMESTAMP(7), fld_num NUMBER, \
fld_json JSON, fld_arr ARRAY(STRING), fld_map MAP(STRING), \
fld_rec RECORD(fld_id LONG, fld_bool BOOLEAN, fld_str STRING), \
PRIMARY KEY(SHARD(fld_sid), fld_id)) USING TTL ' + str(table_ttl))
        create_request = TableRequest().set_statement(
            create_statement).set_table_limits(TableLimits(5000, 5000, 50))
        cls._result = TestBase.table_request(create_request, State.ACTIVE)
        global row, tb_expect_expiration, hour_in_milliseconds
        row = {
            'fld_sid': 1,
            'fld_id': 1,
            'fld_long': 2147483648,
            'fld_float': 3.1414999961853027,
            'fld_double': 3.1415,
            'fld_bool': True,
            'fld_str': '{"name": u1, "phone": null}',
            'fld_bin': bytearray(pack('>i', 4)),
            'fld_time': datetime.now(),
            'fld_num': Decimal(5),
            'fld_json': {
                'a': '1',
                'b': None,
                'c': '3'
            },
            'fld_arr': ['a', 'b', 'c'],
            'fld_map': {
                'a': '1',
                'b': '2',
                'c': '3'
            },
            'fld_rec': {
                'fld_id': 1,
                'fld_bool': False,
                'fld_str': None
            }
        }
        put_request = PutRequest().set_value(row).set_table_name(table_name)
        cls._handle.put(put_request)
        tb_expect_expiration = table_ttl.to_expiration_time(
            int(round(time() * 1000)))
        hour_in_milliseconds = 60 * 60 * 1000
Exemplo n.º 7
0
    def setUpClass(cls):
        cls.handle = None
        cls.set_up_class()
        table_ttl = TimeToLive.of_hours(16)
        create_statement = ('CREATE TABLE ' + table_name +
                            '(fld_sid INTEGER, fld_id INTEGER, \
fld_long LONG, fld_float FLOAT, fld_double DOUBLE, fld_bool BOOLEAN, \
fld_str STRING, fld_bin BINARY, fld_time TIMESTAMP(7), fld_num NUMBER, \
fld_json JSON, fld_arr ARRAY(STRING), fld_map MAP(STRING), \
fld_rec RECORD(fld_id LONG, fld_bool BOOLEAN, fld_str STRING), \
PRIMARY KEY(SHARD(fld_sid), fld_id)) USING TTL ' + str(table_ttl))
        create_request = TableRequest().set_statement(
            create_statement).set_table_limits(TableLimits(100, 100, 1))
        cls.table_request(create_request)
        global row, tb_expect_expiration, version
        row = get_row()
        put_request = PutRequest().set_value(row).set_table_name(table_name)
        version = cls.handle.put(put_request).get_version()
        tb_expect_expiration = table_ttl.to_expiration_time(
            int(round(time() * 1000)))
Exemplo n.º 8
0
 def testQueryStatementUpdateTTL(self):
     hour_in_milliseconds = 60 * 60 * 1000
     self.query_request.set_statement(
         'UPDATE ' + table_name + ' $u SET TTL CASE WHEN ' +
         'remaining_hours($u) < 0 THEN 3 ELSE remaining_hours($u) + 3 END '
         + 'HOURS WHERE fld_sid = 1 AND fld_id = 3')
     result = self.handle.query(self.query_request)
     ttl = TimeToLive.of_hours(3)
     expect_expiration = ttl.to_expiration_time(int(round(time() * 1000)))
     records = self.check_query_result(result, 1)
     self.assertEqual(records[0], {'NumRowsUpdated': 1})
     # TODO: A difference between old cloud proxy and new cloud proxy, the
     # cost of old proxy is 1 less than that of new proxy.
     # self.check_cost(result, 2 + prepare_cost, 4 + prepare_cost, 6, 6)
     # check the record after update ttl request succeed
     self.get_request.set_key({'fld_sid': 1, 'fld_id': 3})
     result = self.handle.get(self.get_request)
     actual_expiration = result.get_expiration_time()
     actual_expect_diff = actual_expiration - expect_expiration
     self.assertGreater(actual_expiration, 0)
     self.assertLess(actual_expect_diff, hour_in_milliseconds)
     self.check_cost(result, 1, 2, 0, 0)
Exemplo n.º 9
0
 def setUp(self):
     TestBase.set_up(self)
     self.row = {
         'fld_id': 1,
         'fld_long': 2147483648,
         'fld_float': 3.1414999961853027,
         'fld_double': 3.1415,
         'fld_bool': True,
         'fld_str': '{"name": u1, "phone": null}',
         'fld_bin': bytearray(pack('>i', 4)),
         'fld_time': datetime.now(),
         'fld_num': Decimal(5),
         'fld_json': {
             'a': '1',
             'b': None,
             'c': '3'
         },
         'fld_arr': ['a', 'b', 'c'],
         'fld_map': {
             'a': '1',
             'b': '2',
             'c': '3'
         },
         'fld_rec': {
             'fld_id': 1,
             'fld_bool': False,
             'fld_str': None
         }
     }
     self.key = {'fld_id': 1}
     self.put_request = PutRequest().set_value(
         self.row).set_table_name(table_name).set_timeout(timeout)
     self.get_request = GetRequest().set_key(
         self.key).set_table_name(table_name)
     self.ttl = TimeToLive.of_hours(24)
     self.hour_in_milliseconds = 60 * 60 * 1000
     self.day_in_milliseconds = 24 * 60 * 60 * 1000
Exemplo n.º 10
0
 def testTimeToLiveToDays(self):
     ttl = TimeToLive.of_hours(26)
     self.assertEqual(ttl.to_days(), 1)
Exemplo n.º 11
0
 def setUp(self):
     self.set_up()
     self.shardkeys = [0, 1]
     self.ids = [0, 1, 2, 3, 4, 5]
     self.rows = list()
     self.new_rows = list()
     self.versions = list()
     self.requests = list()
     self.illegal_requests = list()
     ttl = TimeToLive.of_days(16)
     for sk in self.shardkeys:
         self.rows.append(list())
         self.new_rows.append(list())
         self.versions.append(list())
         for i in self.ids:
             row = get_row()
             row['fld_sid'] = sk
             row['fld_id'] = i
             new_row = deepcopy(row)
             new_row['fld_long'] = 2147483649
             self.rows[sk].append(row)
             self.new_rows[sk].append(new_row)
             put_request = PutRequest().set_value(row).set_table_name(
                 table_name).set_ttl(ttl)
             self.versions[sk].append(
                 self.handle.put(put_request).get_version())
     self.old_expect_expiration = ttl.to_expiration_time(
         int(round(time() * 1000)))
     self.ttl = TimeToLive.of_hours(1)
     self.ops_sk = 0
     illegal_sk = 1
     self.requests.append(PutRequest().set_value(
         self.new_rows[self.ops_sk][0]).set_table_name(table_name).set_ttl(
             self.ttl).set_return_row(True))
     self.requests.append(PutRequest().set_value(self.new_rows[
         self.ops_sk][1]).set_table_name(table_name).set_option(
             PutOption.IF_ABSENT).set_ttl(self.ttl).set_return_row(True))
     self.requests.append(PutRequest().set_value(
         self.new_rows[self.ops_sk][2]).set_use_table_default_ttl(
             True).set_table_name(table_name).set_option(
                 PutOption.IF_PRESENT).set_return_row(True))
     self.requests.append(PutRequest().set_value(
         self.new_rows[self.ops_sk][3]).set_table_name(table_name).set_ttl(
             self.ttl).set_option(PutOption.IF_VERSION).set_match_version(
                 self.versions[self.ops_sk][3]).set_return_row(True))
     self.requests.append(DeleteRequest().set_key({
         'fld_sid': self.ops_sk,
         'fld_id': 4
     }).set_table_name(table_name).set_return_row(True))
     self.requests.append(DeleteRequest().set_key({
         'fld_sid': self.ops_sk,
         'fld_id': 5
     }).set_table_name(table_name).set_return_row(True).set_match_version(
         self.versions[self.ops_sk][0]))
     self.illegal_requests.append(DeleteRequest().set_key({
         'fld_sid': self.ops_sk,
         'fld_id': 0
     }).set_table_name('IllegalUsers'))
     self.illegal_requests.append(DeleteRequest().set_key({
         'fld_sid': illegal_sk,
         'fld_id': 0
     }).set_table_name(table_name))
     self.write_multiple_request = WriteMultipleRequest().set_timeout(
         timeout)
     self.get_request = GetRequest().set_table_name(table_name)
     self.hour_in_milliseconds = 60 * 60 * 1000
     self.day_in_milliseconds = 24 * 60 * 60 * 1000
Exemplo n.º 12
0
 def setUp(self):
     TestBase.set_up(self)
     self.shardkeys = [0, 1]
     self.ids = [0, 1, 2, 3, 4, 5]
     self.rows = list()
     self.new_rows = list()
     self.versions = list()
     self.requests = list()
     self.illegal_requests = list()
     ttl = TimeToLive.of_days(16)
     for sk in self.shardkeys:
         self.rows.append(list())
         self.new_rows.append(list())
         self.versions.append(list())
         for i in self.ids:
             row = {'fld_sid': sk, 'fld_id': i, 'fld_long': 2147483648,
                    'fld_float': 3.1414999961853027, 'fld_double': 3.1415,
                    'fld_bool': True,
                    'fld_str': '{"name": u1, "phone": null}',
                    'fld_bin': bytearray(pack('>i', 4)),
                    'fld_time': datetime.now(), 'fld_num': Decimal(5),
                    'fld_json': {'a': '1', 'b': None, 'c': '3'},
                    'fld_arr': ['a', 'b', 'c'],
                    'fld_map': {'a': '1', 'b': '2', 'c': '3'},
                    'fld_rec': {'fld_id': 1, 'fld_bool': False,
                                'fld_str': None}}
             new_row = deepcopy(row)
             new_row.update({'fld_long': 2147483649})
             self.rows[sk].append(row)
             self.new_rows[sk].append(new_row)
             put_request = PutRequest().set_value(row).set_table_name(
                 table_name).set_ttl(ttl)
             self.versions[sk].append(
                 self.handle.put(put_request).get_version())
     self.old_expect_expiration = ttl.to_expiration_time(
         int(round(time() * 1000)))
     self.ttl = TimeToLive.of_hours(1)
     self.ops_sk = 0
     illegal_sk = 1
     self.requests.append(PutRequest().set_value(
         self.new_rows[self.ops_sk][0]).set_table_name(table_name).set_ttl(
         self.ttl).set_return_row(True))
     self.requests.append(PutRequest().set_value(
         self.new_rows[self.ops_sk][1]).set_table_name(
         table_name).set_option(PutOption.IF_ABSENT).set_ttl(
         self.ttl).set_return_row(True))
     self.requests.append(PutRequest().set_value(
         self.new_rows[self.ops_sk][2]).set_use_table_default_ttl(
         True).set_table_name(table_name).set_option(
         PutOption.IF_PRESENT).set_return_row(True))
     self.requests.append(PutRequest().set_value(
         self.new_rows[self.ops_sk][3]).set_table_name(
         table_name).set_option(PutOption.IF_VERSION).set_ttl(
         self.ttl).set_match_version(
         self.versions[self.ops_sk][3]).set_return_row(True))
     self.requests.append(DeleteRequest().set_key(
         {'fld_sid': self.ops_sk, 'fld_id': 4}).set_table_name(
         table_name).set_return_row(True))
     self.requests.append(DeleteRequest().set_key(
         {'fld_sid': self.ops_sk, 'fld_id': 5}).set_table_name(
             table_name).set_match_version(
             self.versions[self.ops_sk][0]).set_return_row(True))
     self.illegal_requests.append(DeleteRequest().set_key(
         {'fld_sid': self.ops_sk, 'fld_id': 0}).set_table_name(
         'IllegalUsers'))
     self.illegal_requests.append(DeleteRequest().set_key(
         {'fld_sid': illegal_sk, 'fld_id': 0}).set_table_name(table_name))
     self.write_multiple_request = WriteMultipleRequest().set_timeout(
         timeout)
     self.get_request = GetRequest().set_table_name(table_name)
     self.hour_in_milliseconds = 60 * 60 * 1000
     self.day_in_milliseconds = 24 * 60 * 60 * 1000