Class: GitModel::Index
- Inherits:
-
Object
- Object
- GitModel::Index
- Defined in:
- lib/gitmodel/index.rb
Instance Method Summary collapse
- #attr_index(attr) ⇒ Object
- #filename ⇒ Object
- #generate!(branch) ⇒ Object
- #generated?(branch = GitModel.default_branch) ⇒ Boolean
-
#initialize(model_class) ⇒ Index
constructor
A new instance of Index.
- #load(branch = GitModel.default_branch) ⇒ Object
- #save(options = {}) ⇒ Object
Constructor Details
#initialize(model_class) ⇒ Index
Returns a new instance of Index.
3 4 5 |
# File 'lib/gitmodel/index.rb', line 3 def initialize(model_class) @model_class = model_class end |
Instance Method Details
#attr_index(attr) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/gitmodel/index.rb', line 20 def attr_index(attr) self.load unless @indexes unless @indexes # this is just so that we can stub self.load in tests nil else @indexes[attr.to_s] || {} end end |
#filename ⇒ Object
29 30 31 |
# File 'lib/gitmodel/index.rb', line 29 def filename File.join(@model_class.db_subdir, '_indexes.json') end |
#generate!(branch) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/gitmodel/index.rb', line 7 def generate!(branch) GitModel.logger.debug "Generating indexes for #{@model_class}" # TODO it sucks to load every instance here, optimize later @indexes = {} @model_class.find_all(:branch => branch).each do |o| o.attributes.each do |attr, value| @indexes[attr] ||= {} @indexes[attr][value] ||= SortedSet.new @indexes[attr][value] << o.id end end end |
#generated?(branch = GitModel.default_branch) ⇒ Boolean
33 34 35 |
# File 'lib/gitmodel/index.rb', line 33 def generated?(branch = GitModel.default_branch) (GitModel.current_tree(branch) / filename) ? true : false end |
#load(branch = GitModel.default_branch) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/gitmodel/index.rb', line 56 def load(branch = GitModel.default_branch) @indexes = GitModel.cache(branch, "#{@model_class.db_subdir}-index-load") do unless generated?(branch) GitModel.logger.debug "No index generated for #{@model_class}, on branch #{branch}, not loading." else GitModel.logger.debug "Loading indexes for #{@model_class}..." indexes = {} blob = GitModel.current_tree(branch) / filename data = Yajl::Parser.parse(blob.data) data.each do |attr_and_values| attr = attr_and_values[0] values = {} attr_and_values[1].each do |value_and_ids| value = value_and_ids[0] ids = SortedSet.new(value_and_ids[1]) values[value] = ids end indexes[attr] = values end end indexes end end |
#save(options = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/gitmodel/index.rb', line 37 def save( = {}) GitModel.logger.debug "Saving indexes for #{@model_class}..." transaction = .delete(:transaction) || GitModel::Transaction.new() branch = transaction.branch || .delete(:branch) || GitModel.default_branch result = transaction.execute do |t| # convert to array because JSON hash keys must be strings data = [] @indexes.each do |attr,values| values_and_ids = [] values.each do |value, ids| values_and_ids << [value, ids.to_a] end data << [attr,values_and_ids] end data = Yajl::Encoder.encode(data, nil, :pretty => true) t.index.add(filename, data) end end |