def item_exists(self, user_id, collection_name, item_id): """Returns a timestamp if an item exists.""" collection_id = self._get_collection_id(user_id, collection_name) query = self._get_query('ITEM_EXISTS', user_id) res = self._do_query_fetchone(query, user_id=user_id, item_id=item_id, collection_id=collection_id) if res is None: return None return bigint2time(res[0])
def get_collection_timestamps(self, user_id): """return the collection names for a given user""" query = 'COLLECTION_STAMPS' query = self._get_query(query, user_id) res = self._do_query_fetchall(query, user_id=user_id) try: return dict([(self._collid2name(user_id, coll_id), bigint2time(stamp)) for coll_id, stamp in res]) finally: self._purge_cache(user_id)
def get_collection_max_timestamp(self, user_id, collection_name): """Returns the max timestamp of a collection.""" query = self._get_query('COLLECTION_MAX_STAMPS', user_id) collection_id = self._get_collection_id(user_id, collection_name) res = self._do_query_fetchone(query, user_id=user_id, collection_id=collection_id) stamp = res[0] if stamp is None: return None return bigint2time(stamp)
def item_exists(self, user_id, collection_name, item_id): """Returns a timestamp if an item exists.""" collection_id = self._get_collection_id(user_id, collection_name) query = self._get_query('ITEM_EXISTS', user_id) res = safe_execute(self._engine, query, user_id=user_id, item_id=item_id, collection_id=collection_id, ttl=_int_now()) res = res.fetchone() if res is None: return None return bigint2time(res[0])
def test_bigint2time(self): self.assertEquals(bigint2time(None), None) # make sure we always get two-digits Decimals # even if the time ms is 0 def check(value): res = bigint2time(time2bigint(round_time(value))) res = str(res) self.assertTrue('.' in res) self.assertEqual(len(str(res).split('.')[-1]), 2) for value in (1297417122.0, 1297417122.1, 97417122.18765): check(value)
def _set_item(self, user_id, collection_name, item_id, **values): """Adds or update an item""" wbo = self._get_wbo_table(user_id) if 'modified' in values: values['modified'] = _roundedbigint(values['modified']) if 'ttl' not in values: values['ttl'] = MAX_TTL else: # ttl is provided in seconds, so we add it # to the current timestamp values['ttl'] += _int_now() modified = self.item_exists(user_id, collection_name, item_id) if 'payload' in values: values['payload_size'] = len(values['payload']) collection_id = self._get_collection_id(user_id, collection_name) if modified is None: # does not exists values['collection'] = collection_id values['id'] = item_id values['username'] = user_id query = insert(wbo).values(**values) else: if 'id' in values: del values['id'] key = and_(wbo.c.id == item_id, wbo.c.username == user_id, wbo.c.collection == collection_id) query = update(wbo).where(key).values(**values) try: self._do_query(query) except IntegrityError: raise StorageConflictError() if 'modified' in values: return bigint2time(values['modified']) return modified
def test_time2bigint(self): now = time.time() two_digits = bigint2time(time2bigint(now)) self.assertAlmostEqual(float(two_digits), now, places=1)
def check(value): res = bigint2time(time2bigint(round_time(value))) res = str(res) self.assertTrue('.' in res) self.assertEqual(len(str(res).split('.')[-1]), 2)