Class: ProtoTurf::CachedConfluentSchemaRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/proto_turf/cached_confluent_schema_registry.rb

Instance Method Summary collapse

Constructor Details

#initialize(upstream) ⇒ CachedConfluentSchemaRegistry

Returns a new instance of CachedConfluentSchemaRegistry.

Parameters:



4
5
6
7
8
9
# File 'lib/proto_turf/cached_confluent_schema_registry.rb', line 4

def initialize(upstream)
  @upstream = upstream
  @schemas_by_id = {}
  @ids_by_schema = {}
  @versions_by_subject_and_id = {}
end

Instance Method Details

#fetch(id) ⇒ String

Returns the schema string stored in the registry for the given id.

Parameters:

  • id (Integer)

    the schema ID to fetch

Returns:

  • (String)

    the schema string stored in the registry for the given id



20
21
22
# File 'lib/proto_turf/cached_confluent_schema_registry.rb', line 20

def fetch(id)
  @schemas_by_id[id] ||= @upstream.fetch(id)
end

#fetch_version(id, subject) ⇒ Integer?

Returns the version of the schema for the given subject and id, or nil if not found.

Parameters:

  • id (Integer)

    the schema ID to fetch

  • subject (String)

    the subject to fetch the version for

Returns:

  • (Integer, nil)

    the version of the schema for the given subject and id, or nil if not found



27
28
29
30
31
32
33
# File 'lib/proto_turf/cached_confluent_schema_registry.rb', line 27

def fetch_version(id, subject)
  key = [subject, id]
  return @versions_by_subject_and_id[key] if @versions_by_subject_and_id[key]

  results = @upstream.schema_subject_versions(id)
  @versions_by_subject_and_id[key] = results&.find { |r| r['subject'] == subject }&.dig('version')
end

#register(subject, schema, references: []) ⇒ Object

Parameters:

  • subject (String)

    the subject to register the schema under

  • schema (String)

    the schema text to register

  • references (Array<Hash>) (defaults to: [])

    optional references to other schemas



45
46
47
48
49
# File 'lib/proto_turf/cached_confluent_schema_registry.rb', line 45

def register(subject, schema, references: [])
  key = [subject, schema]

  @ids_by_schema[key] ||= @upstream.register(subject, schema, references: references)
end

#registered?(subject, schema) ⇒ Boolean

Returns true if we know the schema has been registered for that subject.

Parameters:

  • subject (String)

    the subject to check

  • schema (String)

    the schema text to check

Returns:

  • (Boolean)

    true if we know the schema has been registered for that subject.



38
39
40
# File 'lib/proto_turf/cached_confluent_schema_registry.rb', line 38

def registered?(subject, schema)
  @ids_by_schema[[subject, schema]].present?
end