Class: HashIndex
- Inherits:
-
Object
- Object
- HashIndex
- Defined in:
- lib/ObjectModel/Indexes/HashIndex.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#repository ⇒ Object
Returns the value of attribute repository.
-
#storage ⇒ Object
Returns the value of attribute storage.
Instance Method Summary collapse
- #[](hash) ⇒ Object
- #add(entity) ⇒ Object
- #create_index ⇒ Object
- #delete_index ⇒ Object
- #get_entity_id(hash) ⇒ Object
- #include?(hash) ⇒ Boolean
-
#initialize(name, &hasher) ⇒ HashIndex
constructor
A new instance of HashIndex.
- #print_storage(i_name) ⇒ Object
- #update(entity, copy) ⇒ Object
Constructor Details
#initialize(name, &hasher) ⇒ HashIndex
Returns a new instance of HashIndex.
4 5 6 7 |
# File 'lib/ObjectModel/Indexes/HashIndex.rb', line 4 def initialize name, &hasher name.should! :be_a, Symbol @name, @hasher = name, hasher end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
2 3 4 |
# File 'lib/ObjectModel/Indexes/HashIndex.rb', line 2 def name @name end |
#repository ⇒ Object
Returns the value of attribute repository.
2 3 4 |
# File 'lib/ObjectModel/Indexes/HashIndex.rb', line 2 def repository @repository end |
#storage ⇒ Object
Returns the value of attribute storage.
2 3 4 |
# File 'lib/ObjectModel/Indexes/HashIndex.rb', line 2 def storage @storage end |
Instance Method Details
#[](hash) ⇒ Object
14 15 16 17 |
# File 'lib/ObjectModel/Indexes/HashIndex.rb', line 14 def [] hash entity_id = get_entity_id hash return entity_id != nil ? @repository.by_id(entity_id) : nil end |
#add(entity) ⇒ Object
32 33 34 35 36 |
# File 'lib/ObjectModel/Indexes/HashIndex.rb', line 32 def add entity hash = @hasher.call(entity) # hash.should! :be_a, String storage[@name].insert :hash => hash.to_s, :entity_id => entity.entity_id if hash != nil end |
#create_index ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/ObjectModel/Indexes/HashIndex.rb', line 45 def create_index if storage.db.table_exists? @name return true else storage.db.create_table @name do column :hash, :text column :entity_id, :text primary_key :hash # index :hash # index :entity_id end return false end end |
#delete_index ⇒ Object
61 62 63 |
# File 'lib/ObjectModel/Indexes/HashIndex.rb', line 61 def delete_index storage.db.drop_table @name if storage.db.table_exists? @name end |
#get_entity_id(hash) ⇒ Object
19 20 21 22 |
# File 'lib/ObjectModel/Indexes/HashIndex.rb', line 19 def get_entity_id hash row = storage[@name][:hash => hash.to_s] return row ? row[:entity_id] : nil end |
#include?(hash) ⇒ Boolean
9 10 11 12 |
# File 'lib/ObjectModel/Indexes/HashIndex.rb', line 9 def include? hash hash.should! :be_a, String return storage[@name][:hash => hash] != nil end |
#print_storage(i_name) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/ObjectModel/Indexes/HashIndex.rb', line 38 def print_storage i_name return unless i_name == nil or i_name == @name puts "\nHashIndex #{@name}:" storage[@name].print @storage end |
#update(entity, copy) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/ObjectModel/Indexes/HashIndex.rb', line 24 def update entity, copy storage[@name].filter(:entity_id => entity.entity_id).delete unless copy.deleted? hash = @hasher.call(entity) storage[@name].insert :hash => hash.to_s, :entity_id => entity.entity_id if hash != nil end end |