AlgoMaster Logo
AlgoMasterReplay CDC into a Materialized Viewmedium

Replay CDC into a Materialized View

medium

Change Data Capture replays inserts, updates, and deletes from a durable log into a queryable view. Retries are safe only when the same log offset cannot change the view twice.

Design a CdcMaterializedView class:

  • boolean apply(int offset, String operation, int key, int value) applies a previously unseen offset and returns true; repeated offsets return false without changing state.
  • int get(int key) returns the current value or -1 when absent.
  • int size() returns the number of present keys.

operation is "upsert" or "delete". Upsert inserts or replaces. Delete removes the key and ignores value. Even deletion of an absent key consumes its new offset.

Example 1:
Example 2:

Constraints

  • 0 <= offset, key <= 10^9
  • 0 <= value <= 10^9; therefore -1 is reserved for absence.
  • At most 10^4 method calls are made per object.
  • Offsets uniquely identify log records.
  • New offsets are supplied in the intended log application order; retries may repeat an earlier call.
Hints

Loading...
CallReturns
new CdcMaterializedView()null
apply(1, "upsert", 10, 100)true
get(10)100
apply(1, "upsert", 10, 999)false
get(10)100
size()1

The repeated offset 1 is ignored, so it cannot overwrite the value produced by the first application.

Run checks these cases. Submit also runs a larger hidden set.