Class: VectorMCP::TokenStore

Inherits:
Object
  • Object
show all
Defined in:
lib/vector_mcp/token_store.rb

Overview

Thread-safe bidirectional store mapping arbitrary string values to opaque tokens and back. The store has no knowledge of domain semantics: callers supply the prefix, and the store guarantees that the same (value, prefix) pair always yields the same token within its lifetime.

Constant Summary collapse

TOKEN_PATTERN =

Regexp describing the token format emitted by #tokenize.

/\A[A-Z]+_[0-9A-F]{8}\z/

Instance Method Summary collapse

Constructor Details

#initializeTokenStore

Returns a new instance of TokenStore.



15
16
17
18
19
# File 'lib/vector_mcp/token_store.rb', line 15

def initialize
  @forward = Concurrent::Hash.new
  @reverse = Concurrent::Hash.new
  @mutex = Mutex.new
end

Instance Method Details

#clearvoid

This method returns an undefined value.

Remove all mappings. Intended for test teardown.



64
65
66
67
68
69
# File 'lib/vector_mcp/token_store.rb', line 64

def clear
  @mutex.synchronize do
    @forward.clear
    @reverse.clear
  end
end

#resolve(token) ⇒ String?

Resolve a token back to its original value.

Parameters:

  • token (String)

    a token previously returned by #tokenize.

Returns:

  • (String, nil)

    the original value, or nil if unknown.



49
50
51
# File 'lib/vector_mcp/token_store.rb', line 49

def resolve(token)
  @reverse[token]
end

#token?(string) ⇒ Boolean

Predicate: does string look like a token issued by this class? This check is purely structural and does not consult the store.

Parameters:

  • string (Object)

    the value to test.

Returns:

  • (Boolean)


58
59
60
# File 'lib/vector_mcp/token_store.rb', line 58

def token?(string)
  string.is_a?(String) && TOKEN_PATTERN.match?(string)
end

#tokenize(value, prefix:) ⇒ String

Return an opaque token for value. Calling this repeatedly with the same value and prefix returns the same token.

Parameters:

  • value (String)

    the value to tokenize.

  • prefix (String)

    the token prefix (uppercase recommended).

Returns:

  • (String)

    a token of the form “PREFIX_XXXXXXXX”.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/vector_mcp/token_store.rb', line 27

def tokenize(value, prefix:)
  key = [prefix, value]
  existing = @forward[key]
  return existing if existing

  @mutex.synchronize do
    existing = @forward[key]
    return existing if existing

    token = generate_token(prefix)
    # Populate the reverse map first so any thread that observes the
    # token in @forward can always resolve it.
    @reverse[token] = value
    @forward[key] = token
    token
  end
end