Skip to content

Commit

Permalink
Add Statement#idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
Watson1978 committed Nov 24, 2023
1 parent f4bdd29 commit 5b51d9b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ end
statement = Ilios::Cassandra.session.prepare(<<~CQL)
SELECT * FROM ilios.example
CQL
statement.idempotent = true
statement.page_size = 25
result = Ilios::Cassandra.session.execute(statement)
result.each do |row|
Expand Down
18 changes: 18 additions & 0 deletions ext/ilios/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,23 @@ static VALUE statement_page_size(VALUE self, VALUE page_size)
return self;
}

/**
* Sets whether the statement is idempotent. Idempotent statements are able to be
* automatically retried after timeouts/errors and can be speculatively executed.
* The default is +false+.
*
* @param idempotent [Boolean] Whether the statement is idempotent.
* @return [Cassandra::Statement] self.
*/
static VALUE statement_idempotent(VALUE self, VALUE idempotent)
{
CassandraStatement *cassandra_statement;

GET_STATEMENT(self, cassandra_statement);
cass_statement_set_is_idempotent(cassandra_statement->statement, RTEST(idempotent) ? cass_true : cass_false);
return self;
}

static void statement_mark(void *ptr)
{
CassandraStatement *cassandra_statement = (CassandraStatement *)ptr;
Expand Down Expand Up @@ -225,4 +242,5 @@ void Init_statement(void)

rb_define_method(cStatement, "bind", statement_bind, 1);
rb_define_method(cStatement, "page_size=", statement_page_size, 1);
rb_define_method(cStatement, "idempotent=", statement_idempotent, 1);
}
1 change: 1 addition & 0 deletions sig/ilios.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module Ilios
class Statement
def bind: (Hash[Symbol, untyped] | Hash[String, untyped]) -> self
def page_size: (Integer) -> self
def idempotent: (true | false) -> self
end

class Future
Expand Down
5 changes: 5 additions & 0 deletions test/test_statement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ def test_page_size
assert_equal(5, results.to_a.size)
end

def test_idempotent
ret = (@insert_statement.idempotent = true)
assert_kind_of(Ilios::Cassandra::Statement, ret)
end

private

def insert_and_get_results
Expand Down

0 comments on commit 5b51d9b

Please sign in to comment.