Class: XxxRename::Data::SceneDatastoreQuery

Inherits:
QueryInterface show all
Includes:
FileUtilities
Defined in:
lib/xxx_rename/data/scene_datastore.rb

Constant Summary

Constants included from FileUtilities

FileUtilities::MAX_FILENAME_LEN

Instance Attribute Summary

Attributes inherited from QueryInterface

#store

Instance Method Summary collapse

Methods included from FileUtilities

#read_file!, #read_yaml, #read_yaml!, #valid_dir?, #valid_file?

Methods inherited from QueryInterface

#destroy!, #generate_lookup_key, #initialize, #upsert

Constructor Details

This class inherits a constructor from XxxRename::Data::QueryInterface

Instance Method Details

#allObject



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/xxx_rename/data/scene_datastore.rb', line 162

def all
  benchmark("all") do
    semaphore.synchronize do
      store.transaction(true) do
        md5_regex = Regexp.new("^[a-f0-9]{32}$", Regexp::IGNORECASE)

        store.roots.select { |x| x.match?(md5_regex) }.map { |key| store[key] }
      end
    end
  end
end

#countObject



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/xxx_rename/data/scene_datastore.rb', line 150

def count
  benchmark("count") do
    semaphore.synchronize do
      store.transaction(true) do
        md5_regex = Regexp.new("^[a-f0-9]{32}$", Regexp::IGNORECASE)

        store.roots.select { |x| x.match?(md5_regex) }.length
      end
    end
  end
end

#create!(scene_data, force: false) ⇒ Object

Parameters:

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/xxx_rename/data/scene_datastore.rb', line 42

def create!(scene_data, force: false)
  benchmark("create!") do
    semaphore.synchronize do
      store.transaction do
        unless force
          existing_record = store.fetch(scene_data.key, nil)
          raise UniqueRecordViolation, existing_record if existing_record
        end

        store[scene_data.key] = scene_data
        create_indexes(scene_data.key, scene_data)
        scene_data.key
      end
    end
  end
end

#destroy(scene_data, *filenames) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/xxx_rename/data/scene_datastore.rb', line 137

def destroy(scene_data, *filenames)
  benchmark("destroy") do
    semaphore.synchronize do
      store.transaction do
        key = scene_data.key
        store.delete(key)
        destroy_indexes(key, scene_data, *filenames)
        key
      end
    end
  end
end

#exists?(key) ⇒ Boolean Also known as: exist?

Returns:

  • (Boolean)


127
128
129
130
131
132
133
# File 'lib/xxx_rename/data/scene_datastore.rb', line 127

def exists?(key)
  semaphore.synchronize do
    store.transaction(true) do
      store.root?(key)
    end
  end
end

#find(id: nil, collection_tag: nil, title: nil, actors: nil) ⇒ Object

Find a scene using one of

  1. collection_tag && id

  2. collection_tag && title

  3. title && actors

Parameters:

  • title (String) (defaults to: nil)
  • actors (Array[String]) (defaults to: nil)
  • collection_tag (String) (defaults to: nil)
  • id (String) (defaults to: nil)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/xxx_rename/data/scene_datastore.rb', line 68

def find(id: nil, collection_tag: nil, title: nil, actors: nil)
  param = { id: id, collection_tag: collection_tag, title: title, actors: actors }.reject { |_k, v| v.nil? }
  benchmark("find #{param}") do
    validate_type_params!(id: id, collection_tag: collection_tag, title: title, actors: actors)
    semaphore.synchronize do
      store.transaction(read_only: true) do
        keys = fetch_keys?(id: id, collection_tag: collection_tag, title: title, actors: actors)
        keys.map do |key|
          store[key]
        end.compact
      end
    end
  end
end

#find_by_abs_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
92
93
# File 'lib/xxx_rename/data/scene_datastore.rb', line 83

def find_by_abs_path?(path)
  benchmark("find_by_abs_path?") do
    store.transaction(read_only: true) do
      index_key = generate_lookup_key(REGISTERED_FILE_PATHS_PREFIX, path)
      key = store.fetch(index_key, nil)
      return if key.nil?

      store[key]
    end
  end
end

#find_by_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
98
99
100
101
102
103
# File 'lib/xxx_rename/data/scene_datastore.rb', line 95

def find_by_key?(key)
  benchmark("find_by_key? #{key}") do
    semaphore.synchronize do
      store.transaction(read_only: true) do
        store.fetch(key, nil)
      end
    end
  end
end

#metadataObject



174
175
176
177
178
179
180
# File 'lib/xxx_rename/data/scene_datastore.rb', line 174

def 
  semaphore.synchronize do
    store.transaction(true) do
      store.fetch(METADATA_ROOT, {})
    end
  end
end

#register_file(scene_data, filename, old_filename: nil) ⇒ Object

Parameters:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/xxx_rename/data/scene_datastore.rb', line 107

def register_file(scene_data, filename, old_filename: nil)
  validate_file_paths!(filename, old_filename: old_filename)
  benchmark("register_file") do
    semaphore.synchronize do
      store.transaction do
        key = scene_data.key
        new_index_key = generate_lookup_key(REGISTERED_FILE_PATHS_PREFIX, filename)
        store[new_index_key] = key

        if old_filename
          old_index_key = generate_lookup_key(REGISTERED_FILE_PATHS_PREFIX, old_filename)
          store.delete(old_index_key)
        end

        new_index_key
      end
    end
  end
end

#update_metadata(hash) ⇒ Object



182
183
184
185
186
187
188
189
# File 'lib/xxx_rename/data/scene_datastore.rb', line 182

def (hash)
  semaphore.synchronize do
    store.transaction do
      store[METADATA_ROOT] ||= {}
      store[METADATA_ROOT] = store[METADATA_ROOT].merge(hash)
    end
  end
end

#valid?(scene_data, filepath: nil) ⇒ Boolean

Internal method for testing. Is of no use for a user

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

Returns:

  • (Boolean)


195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/xxx_rename/data/scene_datastore.rb', line 195

def valid?(scene_data, filepath: nil)
  validate_file_paths!(filepath, old_filename: nil)

  errors = {
    key: scene_data.key,
    scene_saved: true,
    missing_keys: [],
    conflicting_indexes: {},
    expected_filename_key: nil
  }

  semaphore.synchronize do
    store.transaction(true) do
      key = scene_data.key
      scene = store[key]
      errors[:scene_saved] = false if scene.nil?

      if scene_data.id
        id_index_value = store[generate_lookup_key(scene_data.collection_tag, scene_data.id)]
        errors[:missing_keys] << :id_index if id_index_value.nil?
        errors[:conflicting_indexes][:id_index] = id_index_value if !id_index_value.nil? && id_index_value != key
      end

      # title_index_value = store[generate_lookup_key(scene_data.collection_tag, scene_data.title)]
      # errors[:missing_keys] << :title_index if title_index_value.nil?
      # errors[:conflicting_indexes][:title_index] = title_index_value if !title_index_value.nil? && title_index_value != key

      collection_title_index_value = store[generate_lookup_key(scene_data.collection, scene_data.title)]
      errors[:missing_keys] << :collection_title_index if collection_title_index_value.nil?
      if !collection_title_index_value.nil? && collection_title_index_value != key
        errors[:conflicting_indexes][:collection_title_index] = collection_title_index_value
      end

      # title_actor_index_value = store[generate_lookup_key(scene_data.title, scene_data.actors.sort.join("|"))]
      # errors[:missing_keys] << :title_actors_index if title_actor_index_value.nil? || title_actor_index_value.empty?
      # if title_actor_index_value && !title_actor_index_value.empty? && !title_actor_index_value.include?(key)
      #   errors[:conflicting_indexes][:title_actors_index] = title_actor_index_value
      # end

      if filepath
        filename_value = store[generate_lookup_key(REGISTERED_FILE_PATHS_PREFIX, filepath)]
        unless filename_value
          errors[:missing_keys] << :path
          errors[:expected_filename_key] = sanitize(filepath)
        end
      end

      status = RecordStatus.new(**errors)
      status.valid? ? true : status.errors
    end
  end
end