M.Almokadem
M.Almokadem

Reputation: 475

Cassandra 3 trigger on delete operation

I'm trying to implement a trigger on Cassandra 3 to catch delete operations on a specific table by implementing

 public Collection<Mutation> augment(Partition partition)

on ITrigger interface but I can't differentiate between update and delete operations.

How can I catch that the operation is a delete operation?

Upvotes: 0

Views: 730

Answers (1)

Ashraful Islam
Ashraful Islam

Reputation: 12830

Here is how you can catch all type of deletion

public Collection<Mutation> augment(Partition partition) {
    if(partition.partitionLevelDeletion().isLive()) {
        UnfilteredRowIterator it = partition.unfilteredIterator();
        while (it.hasNext()) {
            Unfiltered unfiltered = it.next();
            switch (unfiltered.kind()) {
                case ROW:
                    Row row = (Row) unfiltered;
                    
                    if (!row.deletion().isLive()) {
                        // Row deletion
                    }

                    for (Cell cell : row.cells()) {
                        if (cell.isTombstone()) {
                            // Cell deletion
                        } else {
                            // Insert or Update
                        }  
                    }
                    break;
                case RANGE_TOMBSTONE_MARKER:
                    // Range Deletion
                    break;
            }
        }
    } else {
        // Partition Level Deletion
    }
}

Let's say we have the table :

CREATE TABLE kv (
    pk int,
    ck int,
    d int,
    PRIMARY KEY (pk, ck)
);

Here pk is the partition key and ck is the clustering key

Partition Level Deletion :

DELETE from kv WHERE pk = 1;

Range Deletion :

DELETE from kv WHERE pk = 1 AND ck > 10;

Row deletion :

 DELETE from kv WHERE pk = 1 AND ck = 10;

And cell deletion :

DELETE d from kv WHERE pk = 1 AND ck = 10;

Upvotes: 3

Related Questions