Class: Gitlab::GithubImport::IssuableFinder
- Inherits:
-
Object
- Object
- Gitlab::GithubImport::IssuableFinder
- Defined in:
- lib/gitlab/github_import/issuable_finder.rb
Overview
IssuableFinder can be used for caching and retrieving database IDs for issuable objects such as issues and pull requests. By caching these IDs we remove the need for running a lot of database queries when importing GitHub projects.
Constant Summary collapse
- CACHE_KEY =
The base cache key to use for storing/retrieving issuable IDs.
'github-import/issuable-finder/%{project}/%{type}/%{iid}'- CACHE_OBJECT_NOT_FOUND =
-1
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#project ⇒ Object
readonly
Returns the value of attribute project.
Instance Method Summary collapse
-
#cache_database_id(database_id) ⇒ Object
Associates the given database ID with the current object.
-
#database_id ⇒ Object
Returns the database ID for the object.
-
#initialize(project, object) ⇒ IssuableFinder
constructor
project - An instance of
Project.
Constructor Details
#initialize(project, object) ⇒ IssuableFinder
project - An instance of Project.
object - The object to look up or set a database ID for.
18 19 20 21 |
# File 'lib/gitlab/github_import/issuable_finder.rb', line 18 def initialize(project, object) @project = project @object = object end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
10 11 12 |
# File 'lib/gitlab/github_import/issuable_finder.rb', line 10 def object @object end |
#project ⇒ Object (readonly)
Returns the value of attribute project.
10 11 12 |
# File 'lib/gitlab/github_import/issuable_finder.rb', line 10 def project @project end |
Instance Method Details
#cache_database_id(database_id) ⇒ Object
Associates the given database ID with the current object.
database_id - The ID of the corresponding database row.
42 43 44 |
# File 'lib/gitlab/github_import/issuable_finder.rb', line 42 def cache_database_id(database_id) Gitlab::Cache::Import::Caching.write(cache_key, database_id, timeout: timeout) end |
#database_id ⇒ Object
Returns the database ID for the object.
This method will return nil if no ID could be found.
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/gitlab/github_import/issuable_finder.rb', line 26 def database_id val = Gitlab::Cache::Import::Caching.read_integer(cache_key, timeout: timeout) return if val == CACHE_OBJECT_NOT_FOUND return val if val.present? object_id = cache_key_type.safe_constantize&.find_by(project_id: project.id, iid: cache_key_iid)&.id || CACHE_OBJECT_NOT_FOUND cache_database_id(object_id) object_id == CACHE_OBJECT_NOT_FOUND ? nil : object_id end |