-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
John Wehr
committed
Dec 21, 2010
1 parent
771425c
commit 1969585
Showing
16 changed files
with
1,095 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.pyc | ||
*.DS_Store* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
John Wehr <[email protected]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Everything else: | ||
http://www.opensource.org/licenses/mit-license.php | ||
|
||
Copyright (c) 2011 John Wehr | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this | ||
software and associated documentation files (the "Software"), to deal in the Software | ||
without restriction, including without limitation the rights to use, copy, modify, merge, | ||
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons | ||
to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or | ||
substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | ||
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE | ||
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
DEALINGS IN THE SOFTWARE. |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from .list import CassandraList | ||
from .model import CassandraModel | ||
from .dict import CassandraDict | ||
from .types import DateTime, DateTimeString, Float64, FloatString, Int64, IntString, String | ||
from .tests import CassandraListTest, CassandraModelTest, CassandraDictTest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
from django.conf import settings | ||
import pycassa | ||
from pycassa.system_manager import SystemManager | ||
|
||
|
||
MANAGER_MAPS = {} | ||
|
||
|
||
class CassandraKeyError(KeyError): | ||
pass | ||
|
||
|
||
class CassandraDictManager(pycassa.ColumnFamily): | ||
"""Manager for CassandraDict, provides schema creation.""" | ||
|
||
def __init__(self, cls): | ||
self.cls = cls | ||
super(CassandraDictManager, self).__init__( | ||
settings.CASSANDRA_POOL, | ||
self.cls.__name__.lower()) | ||
|
||
|
||
class CassandraMetaDict(type): | ||
"""Metaclass for CassandraDict, provides access to CassandraDictManager""" | ||
|
||
@property | ||
def objects(cls): | ||
"""CassandraListManager Singletons""" | ||
if cls.__name__ not in MANAGER_MAPS: | ||
MANAGER_MAPS[id(cls)] = CassandraDictManager(cls) | ||
return MANAGER_MAPS[id(cls)] | ||
|
||
def sync(cls, default_validation_class=None, destructive=False): | ||
"""Create the Cassandra List schema.""" | ||
if default_validation_class is None: | ||
default_validation_class = cls.default_validation_class | ||
sys = SystemManager(settings.CASSANDRA_SERVERS[0]) | ||
if destructive: | ||
try: | ||
sys.drop_column_family(settings.CASSANDRA_KEYSPACE, cls.__name__.lower()) | ||
except: | ||
pass | ||
sys.create_column_family( | ||
settings.CASSANDRA_KEYSPACE, | ||
cls.__name__.lower(), | ||
comparator_type=pycassa.UTF8_TYPE, | ||
default_validation_class=default_validation_class) | ||
sys.close() | ||
|
||
|
||
class CassandraDict(object): | ||
"A dictionary that stores its data persistantly in Cassandra" | ||
|
||
default_validation_class = pycassa.UTF8_TYPE | ||
|
||
__metaclass__ = CassandraMetaDict | ||
|
||
def __init__(self, row_key): | ||
self.row_key = row_key | ||
self.cf = self.__class__.objects | ||
|
||
def get(self, key, default=None): | ||
try: | ||
return self.cf.get(self.row_key, columns=[key]).values()[0] | ||
except pycassa.NotFoundException, e: | ||
if default is None: | ||
raise CassandraKeyError(key) | ||
else: | ||
return default | ||
|
||
def __getitem__(self, key): | ||
try: | ||
return self.cf.get(self.row_key, columns=[key]).values()[0] | ||
except pycassa.NotFoundException, e: | ||
raise CassandraKeyError(key) | ||
|
||
def __setitem__(self, key, value): | ||
return self.cf.insert(self.row_key, {key:value}) | ||
|
||
def __delitem__(self, key): | ||
return self.cf.remove(self.row_key, columns=[key]) | ||
|
||
def update(self, d): | ||
return self.cf.insert(self.row_key, d) | ||
|
||
def keys(self): | ||
try: | ||
return self.cf.get(self.row_key).keys() | ||
except pycassa.NotFoundException, e: | ||
return [] | ||
|
||
def values(self): | ||
try: | ||
return self.cf.get(self.row_key).values() | ||
except pycassa.NotFoundException, e: | ||
return [] | ||
|
||
def items(self): | ||
try: | ||
return self.cf.get(self.row_key) | ||
except pycassa.NotFoundException, e: | ||
return [] | ||
|
||
def iterkeys(self): | ||
return self.keys() | ||
|
||
def itervalues(self): | ||
return self.values() | ||
|
||
def iteritems(self): | ||
return self.items() | ||
|
||
def has_key(self, key): | ||
return key in dict(self.items()) | ||
|
||
def __contains__(self, key): | ||
return self.has_key(key) | ||
|
||
def __len__(self): | ||
return self.cf.get_count(self.row_key) | ||
|
||
def __del__(self): | ||
self.cf.remove(self.row_key) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import time | ||
import uuid | ||
from django.conf import settings | ||
import pycassa | ||
from pycassa.util import convert_time_to_uuid, convert_uuid_to_time | ||
from pycassa.system_manager import SystemManager | ||
from .exceptions import CassandraIndexError | ||
|
||
MANAGER_MAPS = {} | ||
|
||
class CassandraIndexError(IndexError): | ||
pass | ||
|
||
class CassandraListManager(pycassa.ColumnFamily): | ||
"""Manager for CassandraList.""" | ||
|
||
def __init__(self, cls): | ||
self.cls = cls | ||
super(CassandraListManager, self).__init__( | ||
settings.CASSANDRA_POOL, | ||
self.cls.__name__.lower()) | ||
|
||
|
||
class CassandraMetaList(type): | ||
"""Metaclass for CassandraList, provides schema creation, | ||
access to CassandraListManager""" | ||
|
||
@property | ||
def objects(cls): | ||
"""CassandraListManager Singletons""" | ||
if cls.__name__ not in MANAGER_MAPS: | ||
MANAGER_MAPS[id(cls)] = CassandraListManager(cls) | ||
return MANAGER_MAPS[id(cls)] | ||
|
||
def sync(cls, default_validation_class=None, destructive=False): | ||
"""Create the Cassandra List schema.""" | ||
if default_validation_class is None: | ||
default_validation_class = cls.default_validation_class | ||
sys = SystemManager(settings.CASSANDRA_SERVERS[0]) | ||
if destructive: | ||
try: | ||
sys.drop_column_family(settings.CASSANDRA_KEYSPACE, cls.__name__.lower()) | ||
except: | ||
pass | ||
sys.create_column_family( | ||
settings.CASSANDRA_KEYSPACE, | ||
cls.__name__.lower(), | ||
comparator_type=pycassa.TIME_UUID_TYPE, | ||
default_validation_class=default_validation_class) | ||
sys.close() | ||
|
||
|
||
class CassandraList(object): | ||
|
||
default_validation_class = pycassa.UTF8_TYPE | ||
|
||
__metaclass__ = CassandraMetaList | ||
|
||
def __init__(self, row_key): | ||
self.row_key = row_key | ||
self.cf = self.__class__.objects | ||
|
||
def append(self, x): | ||
self.cf.insert(self.row_key, {time.time():x}) | ||
|
||
def extend(self, seq): | ||
rows = {} | ||
start = uuid.UUID("{%s}" % convert_time_to_uuid(time.time())) | ||
i = 0 | ||
for x in seq: | ||
rows[uuid.UUID(int=start.int + i)] = unicode(x) | ||
i += 10 | ||
self.cf.insert(self.row_key, rows) | ||
|
||
def insert(self, i, x): | ||
try: | ||
seq = self.cf.get(self.row_key, column_count=i + 1) | ||
except pycassa.NotFoundException, e: | ||
self.append(x) | ||
return | ||
if i >= len(seq): | ||
self.append(x) | ||
return | ||
old_key = seq.keys().pop() | ||
old_key_time = convert_uuid_to_time(old_key) | ||
low, high = convert_time_to_uuid(old_key_time, randomize=True), \ | ||
convert_time_to_uuid(old_key_time, randomize=True) | ||
if low > high: | ||
high, low = low, high | ||
while high > old_key: | ||
low, high = convert_time_to_uuid(old_key_time, randomize=True), \ | ||
convert_time_to_uuid(old_key_time, randomize=True) | ||
if low > high: | ||
high, low = low, high | ||
old_value = self.cf.get(self.row_key, columns=[old_key]).values().pop() | ||
self.cf.insert(self.row_key, {high:old_value}) | ||
self.cf.insert(self.row_key, {low:x}) | ||
self.cf.remove(self.row_key, columns=[old_key]) | ||
|
||
def pop(self): | ||
try: | ||
item = self.cf.get(self.row_key, column_count=1, column_reversed=True) | ||
except pycassa.NotFoundException, e: | ||
raise CassandraIndexError("pop from empty list") | ||
self.cf.remove(self.row_key, columns=item.keys()) | ||
return item.values()[0] | ||
|
||
def remove(self, x): | ||
column_start = "" | ||
while 1: | ||
try: | ||
columns = self.cf.get(self.row_key, column_start=column_start, column_count=100) | ||
except pycassa.NotFoundException, e: | ||
return | ||
for key in columns: | ||
if columns[key] == x: | ||
self.cf.remove(self.row_key, columns=[key]) | ||
key = uuid.UUID("{%s}" % key) | ||
column_start = uuid.UUID(int=key.int + 1) | ||
|
||
def delete(self): | ||
self.cf.remove(self.row_key) | ||
|
||
def __len__(self): | ||
return self.cf.get_count(self.row_key) | ||
|
||
def __str__(self): | ||
try: | ||
return str([x[1] for x in self.cf.get(self.row_key).items()]) | ||
except pycassa.NotFoundException, e: | ||
return str([]) | ||
|
||
def __getitem__(self, val): | ||
if isinstance(val, slice): | ||
if (val.step is None or val.step > 0) and val.stop is not None: | ||
seq = [x[1] for x in self.cf.get(self.row_key, column_count=val.stop).items()] | ||
return seq[val.start:val.stop:val.step] | ||
seq = [x[1] for x in self.cf.get(self.row_key).items()] | ||
return seq[val.start:val.stop:val.step] | ||
elif isinstance(val, int): | ||
try: | ||
return self[val:val + 1].pop() | ||
except Exception, e: | ||
raise CassandraIndexError("List index out of range.") | ||
else: | ||
return super(CassandraList, self).__getitem__(val) |
Oops, something went wrong.