Permalink
Browse files

More helper methods, more tests.

  • Loading branch information...
1 parent fd5bcf0 commit 0804c988ba6ffa4e5830b888311b7ef0ea7b0eb9 John Wehr committed Dec 25, 2010
Showing with 76 additions and 17 deletions.
  1. +2 −4 cassorm/dict.py
  2. +21 −1 cassorm/list.py
  3. +15 −9 cassorm/model.py
  4. +5 −0 cassorm/pool.py
  5. +1 −1 cassorm/tests/dict.py
  6. +19 −0 cassorm/tests/list.py
  7. +12 −0 cassorm/tests/model.py
  8. +0 −1 cassorm/types.py
  9. +1 −1 setup.py
View
@@ -1,6 +1,7 @@
from django.conf import settings
import pycassa
from pycassa.system_manager import SystemManager
+from .pool import CASSANDRA_POOL
MANAGER_MAPS = {}
@@ -16,7 +17,7 @@ class CassandraDictManager(pycassa.ColumnFamily):
def __init__(self, cls):
self.cls = cls
super(CassandraDictManager, self).__init__(
- settings.CASSANDRA_POOL,
+ CASSANDRA_POOL,
self.cls.__name__.lower())
@@ -47,7 +48,6 @@ def sync(cls, default_validation_class=None, destructive=False):
default_validation_class=default_validation_class)
sys.close()
-
class CassandraDict(object):
"A dictionary that stores its data persistantly in Cassandra"
@@ -125,7 +125,5 @@ def __contains__(self, key):
def __len__(self):
return self.cf.get_count(self.row_key)
- def __dict__(self):
- return dict(self.items())
View
@@ -4,6 +4,7 @@
import pycassa
from pycassa.util import convert_time_to_uuid, convert_uuid_to_time
from pycassa.system_manager import SystemManager
+from .pool import CASSANDRA_POOL
MANAGER_MAPS = {}
@@ -16,7 +17,7 @@ class CassandraListManager(pycassa.ColumnFamily):
def __init__(self, cls):
self.cls = cls
super(CassandraListManager, self).__init__(
- settings.CASSANDRA_POOL,
+ CASSANDRA_POOL,
self.cls.__name__.lower())
@@ -120,6 +121,25 @@ def remove(self, x):
def delete(self):
self.cf.remove(self.row_key)
+ def remove_older_than(self, x):
+ column_start = convert_time_to_uuid(x)
+ while 1:
+ try:
+ columns = self.cf.get(self.row_key, column_start=column_start, column_count=100, column_reversed=True)
+ except pycassa.NotFoundException, e:
+ return
+ keys = columns.keys()
+ self.cf.remove(self.row_key, columns=keys)
+ key = uuid.UUID("{%s}" % keys[0])
+ column_start = uuid.UUID(int=key.int + 1)
+
+ def __iter__(self):
+ try:
+ row = self.cf.get(self.row_key)
+ except pycassa.NotFoundException, e:
+ return iter([])
+ return iter(row.values())
+
def __len__(self):
return self.cf.get_count(self.row_key)
View
@@ -5,6 +5,7 @@
from pycassa.system_manager import SystemManager
from pycassa import BYTES_TYPE, LONG_TYPE, INT_TYPE, ASCII_TYPE, UTF8_TYPE, TIME_UUID_TYPE, LEXICAL_UUID_TYPE
from .types import DateTime, DateTimeString, Float64, FloatString, Int64, IntString, String
+from .pool import CASSANDRA_POOL
MANAGER_MAPS = {}
@@ -96,18 +97,23 @@ class CassandraModelManager(pycassa.ColumnFamilyMap):
def __init__(self, cls, create=False):
self.cls = cls
- try:
- super(CassandraModelManager, self).__init__(
- self.cls,
- pycassa.ColumnFamily(
- settings.CASSANDRA_POOL,
- self.cls.__name__.lower()))
- except:
- pass
-
+ name = self.cls.__name__.lower()
+ super(CassandraModelManager, self).__init__(
+ self.cls,
+ pycassa.ColumnFamily(
+ CASSANDRA_POOL,
+ name))
+ self.cf = pycassa.ColumnFamily(
+ CASSANDRA_POOL,
+ name)
+
def all(self):
return self.get_range()
+ def difference(self, keys):
+ existing = self.cf.multiget(keys)
+ return list(set(keys) - set(existing.keys()))
+
def __getitem__(self, key):
return self.get(key)
View
@@ -0,0 +1,5 @@
+from django.conf import settings
+import pycassa
+
+CASSANDRA_POOL = pycassa.connect(settings.CASSANDRA_KEYSPACE, settings.CASSANDRA_SERVERS)
+
@@ -12,7 +12,7 @@ class CassandraDictTest(unittest.TestCase):
def setUp(self):
TestDict.sync(destructive=True)
- self.d = TestDict("Test")
+ self.d = TestDict("Test")
def test_get(self):
key_1 = "".join(random_utf8_seq() for i in range(10))
View
@@ -1,5 +1,6 @@
from django.utils import unittest
from ..list import CassandraList
+import time
class TestList(CassandraList):
pass
@@ -13,6 +14,24 @@ def setUp(self):
def tearDown(self):
self.seq.delete()
+ def test_list(self):
+ seq = list(self.seq)
+ self.assertEqual(len(seq), 0)
+ self.seq.append("a")
+ self.seq.append("b")
+ seq = list(self.seq)
+ self.assertEqual(len(seq), 2)
+ self.assertEqual(seq.pop(), "b")
+ self.assertEqual(seq.pop(), "a")
+
+ def test_remove_older_than(self):
+ self.seq.append("a")
+ time.sleep(5)
+ self.seq.append("b")
+ self.seq.remove_older_than(time.time() - 2)
+ self.assertEqual(len(self.seq), 1)
+ self.assertEqual(self.seq.pop(), "b")
+
def test_append(self):
self.seq.append("a")
self.seq.append("b")
@@ -110,3 +110,15 @@ def test_all(self):
self.assertEqual(tm_2.e, value_e)
self.assertEqual(tm_2.f, value_f)
self.assertEqual(tm_2.g, value_g)
+
+ def test_difference(self):
+ value = "".join(random_utf8_seq() for i in range(10))
+ tm_1 = TestModel(a=datetime.now())
+ tm_1.save()
+ tm_2 = TestModel(a=datetime.now())
+ tm_2.save()
+ seq = [tm_1.key, tm_2.key, value]
+ difference = TestModel.objects.difference(seq)
+ self.assertEqual(len(difference), 1)
+ self.assertEqual(difference.pop(), value)
+
View
@@ -8,7 +8,6 @@ def _pycassa_type_init(self, *args, **kwargs):
else:
self.index = False
super(self.__class__, self).__init__(*args, **kwargs)
-
class DateTime(pycassa.DateTime):
__init__ = _pycassa_type_init
View
@@ -4,7 +4,7 @@
import setuptools
from distutils.core import setup
-__version_info__ = (0, 0, 3)
+__version_info__ = (0, 0, 4)
__version__ = '.'.join([str(v) for v in __version_info__])
setup(

0 comments on commit 0804c98

Please sign in to comment.