From 7f3f84494ed15feeff6069c8394556b6c3e7c16e Mon Sep 17 00:00:00 2001 From: Neil Williams Date: Thu, 24 May 2012 19:46:35 -0700 Subject: [PATCH] Add model for new Last Modified system. This will replace the current stuff in thing_utils.py. The idea is that by storing the various last modified timestamps for a thing in a single row together, we can save Cassandra round trips and seeks per request. --- r2/r2/models/last_modified.py | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 r2/r2/models/last_modified.py diff --git a/r2/r2/models/last_modified.py b/r2/r2/models/last_modified.py new file mode 100644 index 000000000..63a622661 --- /dev/null +++ b/r2/r2/models/last_modified.py @@ -0,0 +1,37 @@ +import datetime + +from pylons import g +from pycassa.system_manager import ASCII_TYPE, DATE_TYPE + +from r2.lib.db import tdb_cassandra +from r2.lib.utils import tup + + +class LastModified(tdb_cassandra.View): + _use_db = True + _value_type = "date" + _connection_pool = "main" + _read_consistency_level = tdb_cassandra.CL.ONE + _extra_schema_creation_args = dict(key_validation_class=ASCII_TYPE, + default_validation_class=DATE_TYPE) + + @classmethod + def touch(cls, fullname, names): + names = tup(names) + now = datetime.datetime.now(g.tz) + values = dict.fromkeys(names, now) + cls._set_values(fullname, values) + return now + + @classmethod + def get(cls, fullname, name, touch_if_not_set=False): + try: + obj = cls._byID(fullname) + except tdb_cassandra.NotFound: + if touch_if_not_set: + time = cls.touch(fullname, name) + return time + else: + return None + + return getattr(obj, name, None)