I have a list of unordered events, and my task is to keep the first and last occurrences for them.
I have the following column family in Kassandra:
CREATE TABLE events (
event_name TEXT,
first_occurrence BIGINT,
last_occurrence BIGINT,
PRIMARY KEY (event_name)
);
So, if I have an event with the name "some_event" and an appearance with 123456, then I want to do something that in terms of MySQL will look like this:
INSERT INTO events (event_name, first_occurence, last_occurence)
VALUES ('some_event', 123456, 123456)
ON DUPLICATE KEY UPDATE
first_occurrence = LEAST(first_occurrence, 12345),
last_occurrence = GREATEST(last_occurrence, 123456)
I was going to use lightweight transactions in Kassandra to accomplish this, for example:
INSERT INTO events(event_name, first_occurrence, last_occurrence) VALUES ('some_event', 12345, 12345) IF NOT EXISTS;
UPDATE events SET first_occurrence = 123456 WHERE event_name='some_event' IF first_occurrence > 123456;
UPDATE events SET last_occurrence = 123456 WHERE event_name='some_event' IF last_occurrence < 123456;
But, as it turned out, CQL3 does not allow <and> in an IF statement with a lightweight transaction.
So my question is: what is the pattern for doing such conditional updates?