Skip to content

Commit 94fe1fc

Browse files
committed
ResultSet.was_applied property for LWT results.
1 parent a0569f1 commit 94fe1fc

3 files changed

Lines changed: 56 additions & 6 deletions

File tree

cassandra/cluster.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@
7070
HostConnectionPool, HostConnection,
7171
NoConnectionsAvailable)
7272
from cassandra.query import (SimpleStatement, PreparedStatement, BoundStatement,
73-
BatchStatement, bind_params, QueryTrace, Statement,
74-
named_tuple_factory, dict_factory, FETCH_SIZE_UNSET)
73+
BatchStatement, bind_params, QueryTrace,
74+
named_tuple_factory, dict_factory, tuple_factory, FETCH_SIZE_UNSET)
7575

7676

7777
def _is_eventlet_monkey_patched():
@@ -3409,3 +3409,24 @@ def get_all_query_traces(self, max_wait_sec_per=None):
34093409
See :meth:`.ResponseFuture.get_all_query_traces` for details.
34103410
"""
34113411
return self.response_future.get_all_query_traces(max_wait_sec_per)
3412+
3413+
@property
3414+
def was_applied(self):
3415+
"""
3416+
For LWT results, returns whether the transaction was applied.
3417+
3418+
Result is indeterminate if called on a result that was not an LWT request.
3419+
3420+
Only valid when one of tne of the internal row factories is in use.
3421+
"""
3422+
if self.response_future.row_factory not in (named_tuple_factory, dict_factory, tuple_factory):
3423+
raise RuntimeError("Cannot determine LWT result with row factory %s" % (self.response_future.row_factsory,))
3424+
if len(self.current_rows) != 1:
3425+
raise RuntimeError("LWT result should have exactly one row. This has %d." % (len(self.current_rows)))
3426+
3427+
row = self.current_rows[0]
3428+
if isinstance(row, tuple):
3429+
return row[0]
3430+
else:
3431+
return row['[applied]']
3432+

cassandra/cqlengine/query.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@ class MultipleObjectsReturned(QueryException):
6363

6464
def check_applied(result):
6565
"""
66-
check if result contains some column '[applied]' with false value,
67-
if that value is false, it means our light-weight transaction didn't
68-
applied to database.
66+
Raises LWTException if it looks like a failed LWT request.
6967
"""
70-
if result and '[applied]' in result[0] and not result[0]['[applied]']:
68+
try:
69+
applied = result.was_applied
70+
except Exception:
71+
applied = True # result was not LWT form
72+
if not applied:
7173
raise LWTException(result[0])
7274

7375

tests/unit/test_resultset.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
from cassandra.query import named_tuple_factory, dict_factory, tuple_factory
1415

1516
try:
1617
import unittest2 as unittest
@@ -161,3 +162,29 @@ def test_eq(self):
161162
def test_bool(self):
162163
self.assertFalse(ResultSet(Mock(has_more_pages=False), []))
163164
self.assertTrue(ResultSet(Mock(has_more_pages=False), [1]))
165+
166+
def test_was_applied(self):
167+
# unknown row factory raises
168+
with self.assertRaises(RuntimeError):
169+
ResultSet(Mock(), []).was_applied
170+
171+
response_future = Mock(row_factory=named_tuple_factory)
172+
173+
# no row
174+
with self.assertRaises(RuntimeError):
175+
ResultSet(response_future, []).was_applied
176+
177+
# too many rows
178+
with self.assertRaises(RuntimeError):
179+
ResultSet(response_future, [tuple(), tuple()]).was_applied
180+
181+
# various internal row factories
182+
for row_factory in (named_tuple_factory, tuple_factory):
183+
for applied in (True, False):
184+
rs = ResultSet(Mock(row_factory=row_factory), [(applied,)])
185+
self.assertEqual(rs.was_applied, applied)
186+
187+
row_factory = dict_factory
188+
for applied in (True, False):
189+
rs = ResultSet(Mock(row_factory=row_factory), [{'[applied]': applied}])
190+
self.assertEqual(rs.was_applied, applied)

0 commit comments

Comments
 (0)