Class: ZeroRuby::LmidStore

Inherits:
Object
  • Object
show all
Defined in:
lib/zero_ruby/lmid_store.rb

Overview

Abstract base class for LMID (Last Mutation ID) storage backends. Implementations must provide thread-safe access to client mutation IDs.

This follows the same atomic increment pattern as Zero’s TypeScript implementation.

Examples:

Custom store implementation

class RedisLmidStore < ZeroRuby::LmidStore
  def fetch_and_increment(client_group_id, client_id)
    # Atomically increment and return the new last mutation ID
  end

  def transaction
    # Redis MULTI/EXEC
  end
end

See Also:

Direct Known Subclasses

ZeroRuby::LmidStores::ActiveRecordStore

Instance Method Summary collapse

Instance Method Details

#delete_mutation_results(args) ⇒ Object

Delete mutation results, called by _zero_cleanupResults to remove acknowledged results.

Parameters:

  • args (Hash)

    Cleanup arguments from the _zero_cleanupResults mutation. For single/legacy format: { “clientGroupID” => String, “clientID” => String, “upToMutationID” => Integer } For bulk format: { “type” => “bulk”, “clientGroupID” => String, “clientIDs” => Array<String> }

Raises:

  • (NotImplementedError)


59
60
61
# File 'lib/zero_ruby/lmid_store.rb', line 59

def delete_mutation_results(args)
  raise NotImplementedError, "#{self.class}#delete_mutation_results must be implemented"
end

#fetch_and_increment(client_group_id, client_id) ⇒ Integer

Atomically increment and return the last mutation ID for a client. Creates the record with ID 1 if it doesn’t exist.

This must be atomic to prevent race conditions - the increment and return should happen in a single operation.

Parameters:

  • client_group_id (String)

    The client group ID

  • client_id (String)

    The client ID

Returns:

  • (Integer)

    The new last mutation ID (post-increment)

Raises:

  • (NotImplementedError)


30
31
32
# File 'lib/zero_ruby/lmid_store.rb', line 30

def fetch_and_increment(client_group_id, client_id)
  raise NotImplementedError, "#{self.class}#fetch_and_increment must be implemented"
end

#transaction { ... } ⇒ Object

Execute a block within a transaction. The transaction should support rollback on error.

Yields:

  • The block to execute within the transaction

Returns:

  • The result of the block

Raises:

  • (NotImplementedError)


39
40
41
# File 'lib/zero_ruby/lmid_store.rb', line 39

def transaction(&block)
  raise NotImplementedError, "#{self.class}#transaction must be implemented"
end

#write_mutation_result(client_group_id, client_id, mutation_id, result) ⇒ Object

Persist a mutation result so clients can read it via replication. Used to surface error results back to the client.

Parameters:

  • client_group_id (String)

    The client group ID

  • client_id (String)

    The client ID

  • mutation_id (Integer)

    The mutation ID

  • result (Hash, String)

    The mutation result to persist

Raises:

  • (NotImplementedError)


50
51
52
# File 'lib/zero_ruby/lmid_store.rb', line 50

def write_mutation_result(client_group_id, client_id, mutation_id, result)
  raise NotImplementedError, "#{self.class}#write_mutation_result must be implemented"
end