Browse files

Allow specifying col validators in create_col_fam

Fixes #103
  • Loading branch information...
1 parent 888306d commit fc5869426e85f374edbf064f64d77b7e5b51ff2f @thobbs thobbs committed Jan 23, 2012
Showing with 42 additions and 15 deletions.
  1. +19 −11 pycassa/system_manager.py
  2. +1 −0 tests/test_autopacking.py
  3. +22 −4 tests/test_system_manager.py
View
30 pycassa/system_manager.py
@@ -258,6 +258,7 @@ def _qualify_type_class(self, classname):
def create_column_family(self, keyspace, name, super=False,
comparator_type=None,
subcomparator_type=None,
+ column_validation_classes=None,
key_cache_size=None,
row_cache_size=None,
gc_grace_seconds=None,
@@ -300,6 +301,10 @@ def create_column_family(self, keyspace, name, super=False,
:param str subcomparator_type: Like `comparator_type`, but if the column family
is a super column family, this applies to the type of the subcolumn names
+ :param dict column_validation_classes: keys are column names, values are
+ types. The effect is like calling alter_column for each key as column,
+ with each value as value_type
+
:param key_cache_size: The size of the key cache, either in a percentage of
total keys (0.15, for example) or in an absolute number of
keys (20000, for example).
@@ -357,6 +362,9 @@ def create_column_family(self, keyspace, name, super=False,
:param str comment: A human readable description
+ .. versionadded:: 1.4.0
+ The `column_validation_classes` parameter.
+
"""
self._conn.set_keyspace(keyspace)
@@ -372,6 +380,10 @@ def create_column_family(self, keyspace, name, super=False,
cfdef.default_validation_class = self._qualify_type_class(default_validation_class)
cfdef.key_validation_class = self._qualify_type_class(key_validation_class)
+ if column_validation_classes:
+ for (columnName, value_type) in column_validation_classes.items():
+ cfdef = self._alter_column_cfdef(cfdef, columnName, value_type)
+
cfdef.replicate_on_write = replicate_on_write
cfdef.comment = comment
cfdef.key_alias = key_alias
@@ -415,7 +427,7 @@ def alter_column_family(self, keyspace, column_family,
gc_grace_seconds=None,
read_repair_chance=None,
default_validation_class=None,
- column_validation_classes={},
+ column_validation_classes=None,
min_compaction_threshold=None,
max_compaction_threshold=None,
key_cache_save_period_in_seconds=None,
@@ -435,13 +447,7 @@ def alter_column_family(self, keyspace, column_family,
Parameter meanings are the same as for :meth:`create_column_family`,
but column family attributes which may not be modified are not
- included here. There is an additional optional parameter:
-
- :param dict column_validation_classes: keys are column names, values are
- comparator_type. The effect is like calling alter_column for each key as column,
- with each value as value_type
-
-
+ included here.
"""
self._conn.set_keyspace(keyspace)
@@ -467,8 +473,9 @@ def alter_column_family(self, keyspace, column_family,
if row_cache_provider:
cfdef.row_cache_provider = row_cache_provider
- for (columnName, value_type) in column_validation_classes.items():
- cfdef = self._alter_column_cfdef(cfdef, columnName, value_type)
+ if column_validation_classes:
+ for (columnName, value_type) in column_validation_classes.items():
+ cfdef = self._alter_column_cfdef(cfdef, columnName, value_type)
self._system_update_column_family(cfdef)
@@ -490,6 +497,7 @@ def _alter_column_cfdef(self, cfdef, column, value_type):
value_type = self._qualify_type_class(value_type)
+ cfdef.column_metadata = cfdef.column_metadata or []
matched = False
for c in cfdef.column_metadata:
if c.name == packed_column:
@@ -498,7 +506,7 @@ def _alter_column_cfdef(self, cfdef, column, value_type):
break
if not matched:
cfdef.column_metadata.append(ColumnDef(packed_column, value_type, None, None))
-
+
return cfdef
def alter_column(self, keyspace, column_family, column, value_type):
View
1 tests/test_autopacking.py
@@ -5,6 +5,7 @@
from pycassa.system_manager import *
from pycassa.types import *
from pycassa.index import *
+from pycassa.cassandra.constants import *
from nose.tools import (assert_raises, assert_equal, assert_almost_equal,
assert_true)
View
26 tests/test_system_manager.py
@@ -4,11 +4,14 @@
from pycassa.pool import ConnectionPool
from pycassa.columnfamily import ColumnFamily
-from pycassa.system_manager import *
+from pycassa.system_manager import (SIMPLE_STRATEGY, LONG_TYPE, SystemManager,
+ UTF8_TYPE, TIME_UUID_TYPE, ASCII_TYPE, INT_TYPE)
+
from pycassa.cassandra.ttypes import InvalidRequestException
from pycassa.types import LongType
TEST_KS = 'PycassaTestKeyspace'
+sys = None
def setup_module():
global sys
@@ -43,7 +46,7 @@ def test_system_calls(self):
def test_bad_comparator(self):
sys.create_keyspace('TestKeyspace', SIMPLE_STRATEGY, {'replication_factor': '3'})
- for comparator in [types.LongType, 123]:
+ for comparator in [LongType, 123]:
assert_raises(TypeError, sys.create_column_family,
'TestKeyspace', 'TestBadCF', comparator_type=comparator)
sys.drop_keyspace('TestKeyspace')
@@ -62,6 +65,21 @@ def test_alter_column_non_bytes_type(self):
assert_equal(cf.get('key')[2], 2)
def test_alter_column_super_cf(self):
- sys.create_column_family(TEST_KS, 'SuperCF', super=True, comparator_type=TIME_UUID_TYPE,
- subcomparator_type=UTF8_TYPE)
+ sys.create_column_family(TEST_KS, 'SuperCF', super=True,
+ comparator_type=TIME_UUID_TYPE, subcomparator_type=UTF8_TYPE)
sys.alter_column(TEST_KS, 'SuperCF', 'foobar_col', UTF8_TYPE)
+
+ def test_column_validators(self):
+ validators = {'name': UTF8_TYPE, 'age': LONG_TYPE}
+ sys.create_column_family(TEST_KS, 'ValidatedCF',
+ column_validation_classes=validators)
+ pool = ConnectionPool(TEST_KS)
+ cf = ColumnFamily(pool, 'ValidatedCF')
+ cf.insert('key', {'name': 'John', 'age': 40})
+ self.assertEquals(cf.get('key'), {'name': 'John', 'age': 40})
+
+ validators = {'name': ASCII_TYPE, 'age': INT_TYPE}
+ sys.alter_column_family(TEST_KS, 'ValidatedCF',
+ column_validation_classes=validators)
+ cf.load_schema()
+ self.assertEquals(cf.get('key'), {'name': 'John', 'age': 40})

0 comments on commit fc58694

Please sign in to comment.