vote: Remove old tdb_cassandra.Relation-based vote infrastructure.

This commit is contained in:
Neil Williams
2012-09-12 19:37:59 -07:00
parent 34d93cac28
commit a6e725502e

View File

@@ -26,7 +26,6 @@ import collections
from r2.lib.db.thing import MultiRelation, Relation
from r2.lib.db import tdb_cassandra
from r2.lib.db.tdb_cassandra import TdbException, ASCII_TYPE, UTF8_TYPE
from r2.lib.utils._utils import flatten
from r2.lib.db.sorts import epoch_seconds
from r2.lib.utils import SimpleSillyStub, Storage
@@ -36,7 +35,7 @@ from link import Link, Comment
from pylons import g
from datetime import datetime, timedelta
__all__ = ['Vote', 'CassandraLinkVote', 'CassandraCommentVote', 'score_changes']
__all__ = ['Vote', 'score_changes']
def score_changes(amount, old_amount):
uc = dc = 0
@@ -49,128 +48,12 @@ def score_changes(amount, old_amount):
elif oa < 0 and a > 0: dc = oa; uc = a
return uc, dc
class CassandraVote(tdb_cassandra.Relation):
_use_db = False
_connection_pool = 'main'
_bool_props = ('valid_user', 'valid_thing', 'organic')
_str_props = ('name', # one of '-1', '0', '1'
'notes', 'ip')
_defaults = {'organic': False}
_default_ttls = {'ip': 30*24*60*60}
@classmethod
def _rel(cls, thing1_cls, thing2_cls):
if (thing1_cls, thing2_cls) == (Account, Link):
return CassandraLinkVote
elif (thing1_cls, thing2_cls) == (Account, Comment):
return CassandraCommentVote
raise TdbException("Can't find relation for %r(%r,%r)"
% (cls, thing1_cls, thing2_cls))
@classmethod
def _copy_from(cls, v):
voter = v._thing1
votee = v._thing2
cvc = cls._rel(Account, votee.__class__)
try:
cv = cvc._fast_query(voter, votee)
except tdb_cassandra.NotFound:
cv = cvc(thing1_id = voter._id36, thing2_id = votee._id36)
cv.name = v._name
cv.valid_user, cv.valid_thing = v.valid_user, v.valid_thing
if hasattr(v, 'ip'):
cv.ip = v.ip
if getattr(v, 'organic', False) or hasattr(cv, 'organic'):
cv.organic = getattr(v, 'organic', False)
cv._commit()
class VotesByThing(tdb_cassandra.View):
_use_db = False
_connection_pool = 'main'
_view_of = None
@classmethod
def get_all(cls, *thing_ids):
vbts = cls._byID(thing_ids)
lists = [vbt._values().keys() for vbt in vbts.values()]
vals = flatten(lists)
return cls._view_of._byID(vals).values()
class VotesByLink(VotesByThing):
_use_db = True
_type_prefix = 'VotesByLink'
# _view_of = CassandraLinkVote
@classmethod
def _rowkey(cls, cassandra_link_vote):
return cassandra_link_vote.thing2_id
class VotesByComment(VotesByThing):
_use_db = True
_type_prefix = 'VotesByComment'
# _view_of = CassandraCommentVote
@classmethod
def _rowkey(cls, cassandra_comment_vote):
return cassandra_comment_vote.thing2_id
class CassandraThingVote(CassandraVote):
_use_db = False
_views = []
_read_consistency_level = tdb_cassandra.CL.ONE
def _on_create(self):
wcl = tdb_cassandra.CL.ONE
v_id = {self._id: ''}
for view in self._views:
view._set_values(view._rowkey(self), v_id,
write_consistency_level=wcl)
return CassandraVote._on_create(self)
class CassandraLinkVote(CassandraThingVote):
_use_db = True
_type_prefix = 'LinkVote'
_cf_name = 'LinkVote'
_views = [VotesByLink]
_thing1_cls = Account
_thing2_cls = Link
VotesByLink._view_of = CassandraLinkVote
class CassandraCommentVote(CassandraThingVote):
_use_db = True
_type_prefix = 'CommentVote'
_cf_name = 'CommentVote'
_views = [VotesByComment]
_thing1_cls = Account
_thing2_cls = Comment
VotesByComment._view_of = CassandraCommentVote
class VotesByAccount(tdb_cassandra.DenormalizedRelation):
_use_db = False
_thing1_cls = Account
_read_consistency_level = tdb_cassandra.CL.ONE
# TODO: enable writes when CassandraVote is phased out
_write_last_modified = False
@classmethod
def rel(cls, thing1_cls, thing2_cls):
if (thing1_cls, thing2_cls) == (Account, Link):
@@ -343,7 +226,6 @@ class Vote(MultiRelation('vote',
# now write it out to Cassandra. We'll write it out to both
# this way for a while
CassandraVote._copy_from(v)
VotesByAccount.copy_from(v)
timer.intermediate("cassavotes")
@@ -379,67 +261,3 @@ class Vote(MultiRelation('vote',
for cross, name in votes.iteritems():
ret[cross] = dirs_by_name[name]
return ret
def test():
from r2.models import Link, Account, Comment
from r2.lib.db.tdb_cassandra import thing_cache
assert CassandraVote._rel(Account, Link) == CassandraLinkVote
assert CassandraVote._rel(Account, Comment) == CassandraCommentVote
v1 = CassandraLinkVote('abc', 'def', valid_thing=True, valid_user=False)
v1.testing = 'lala'
v1._commit()
print 'v1', v1, v1._id, v1._t
v2 = CassandraLinkVote._byID('abc_def')
print 'v2', v2, v2._id, v2._t
if v1 != v2:
# this can happen after running the test more than once, it's
# not a big deal
print "Expected %r to be the same as %r" % (v1, v2)
v2.testing = 'lala'
v2._commit()
v1 = None # invalidated this
assert CassandraLinkVote._byID('abc_def') == v2
CassandraLinkVote('abc', 'ghi', name='1')._commit()
try:
print v2.falsy
raise Exception("Got an attribute that doesn't exist?")
except AttributeError:
pass
try:
assert Vote('1', '2') is None
raise Exception("I shouldn't be able to create _use_db==False instances")
except TdbException:
print "You can safely ignore the warning about discarding the uncommitted '1_2'"
except CassandraException:
print "Seriously?"
except Exception, e:
print id(e.__class__), id(TdbException.__class__)
print isinstance(e, TdbException)
print 'Huh?', repr(e)
try:
CassandraLinkVote._byID('bacon')
raise Exception("I shouldn't be able to look up items that don't exist")
except NotFound:
pass
print 'fast_query', CassandraLinkVote._fast_query('abc', ['def'])
assert CassandraLinkVote._fast_query('abc', 'def') == v2
assert CassandraLinkVote._byID('abc_def') == CassandraLinkVote._by_fullname('LinkVote_abc_def')
print 'all', list(CassandraLinkVote._all()), list(VotesByLink._all())
print 'all_by_link', VotesByLink.get_all('abc')
print 'Localcache:', dict(thing_cache.caches[0])