Class: OllamaAgent::Runtime::FencingAllocator

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama_agent/runtime/fencing_allocator.rb

Overview

Monotonic fencing integers per scope in runtime.db (UPSERT increment).

Constant Summary collapse

UPSERT_SQL =
"INSERT INTO fencing_tokens (scope, last_token) VALUES (?, 1) " \
"ON CONFLICT(scope) DO UPDATE SET last_token = last_token + 1"

Instance Method Summary collapse

Constructor Details

#initialize(db) ⇒ FencingAllocator

Returns a new instance of FencingAllocator.

Parameters:

  • db (SQLite3::Database)


13
14
15
# File 'lib/ollama_agent/runtime/fencing_allocator.rb', line 13

def initialize(db)
  @db = db
end

Instance Method Details

#allocate(scope:) ⇒ Integer

Returns new token for scope.

Returns:

  • (Integer)

    new token for scope



18
19
20
# File 'lib/ollama_agent/runtime/fencing_allocator.rb', line 18

def allocate(scope:)
  @db.transaction(:immediate) { allocate_joining(scope: scope) }
end

#allocate_joining(scope:) ⇒ Integer

Like #allocate but assumes the caller already holds an open transaction(:immediate) on @db.

Returns:

  • (Integer)


24
25
26
27
# File 'lib/ollama_agent/runtime/fencing_allocator.rb', line 24

def allocate_joining(scope:)
  @db.execute(UPSERT_SQL, [scope])
  @db.get_first_value("SELECT last_token FROM fencing_tokens WHERE scope = ?", [scope]).to_i
end