Skip to content

Commit

Permalink
Drop single-column insert special case, use batch_mutate
Browse files Browse the repository at this point in the history
  • Loading branch information
thobbs committed May 21, 2012
1 parent 33fdac2 commit 4f8433b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
30 changes: 5 additions & 25 deletions pycassa/columnfamily.py
Expand Up @@ -936,31 +936,11 @@ def insert(self, key, columns, timestamp=None, ttl=None,
timestamp = self.timestamp()

packed_key = self._pack_key(key)

if ((not self.super) and len(columns) == 1) or \
(self.super and len(columns) == 1 and len(columns.values()[0]) == 1):

if self.super:
super_col = columns.keys()[0]
cp = self._column_path(super_col)
columns = columns.values()[0]
else:
cp = self._column_path()

colname = columns.keys()[0]
colval = self._pack_value(columns.values()[0], colname)
colname = self._pack_name(colname, False)
column = Column(colname, colval, timestamp, ttl)

self.pool.execute('insert', packed_key, cp, column,
write_consistency_level or self.write_consistency_level,
allow_retries=self._allow_retries)
else:
mut_list = self._make_mutation_list(columns, timestamp, ttl)
mutations = {packed_key: {self.column_family: mut_list}}
self.pool.execute('batch_mutate', mutations,
write_consistency_level or self.write_consistency_level,
allow_retries=self._allow_retries)
mut_list = self._make_mutation_list(columns, timestamp, ttl)
mutations = {packed_key: {self.column_family: mut_list}}
self.pool.execute('batch_mutate', mutations,
write_consistency_level or self.write_consistency_level,
allow_retries=self._allow_retries)

return timestamp

Expand Down
12 changes: 12 additions & 0 deletions tests/test_columnfamily.py
Expand Up @@ -331,6 +331,18 @@ def test_add(self):
result = counter_cf.get('key')
assert_equal(result, {'col': 2, 'col2': 1})

def test_insert_counters(self):
if not have_counters:
raise SkipTest('Cassandra 0.7 does not support counters')

counter_cf.insert('counter_key', {'col1': 1})
result = counter_cf.get('counter_key')
assert_equal(result['col1'], 1)

counter_cf.insert('counter_key', {'col1': 1, 'col2': 1})
result = counter_cf.get('counter_key')
assert_equal(result, {'col1': 2, 'col2': 1})

def test_remove(self):
key = 'TestColumnFamily.test_remove'
columns = {'1': 'val1', '2': 'val2'}
Expand Down

0 comments on commit 4f8433b

Please sign in to comment.