Module: Association::Index

Defined in:
lib/rbbt/association/index.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#source_fieldObject

Returns the value of attribute source_field.



5
6
7
# File 'lib/rbbt/association/index.rb', line 5

def source_field
  @source_field
end

#target_fieldObject

Returns the value of attribute target_field.



5
6
7
# File 'lib/rbbt/association/index.rb', line 5

def target_field
  @target_field
end

#undirectedObject

Returns the value of attribute undirected.



5
6
7
# File 'lib/rbbt/association/index.rb', line 5

def undirected
  @undirected
end

Class Method Details

.setup(repo) ⇒ Object



10
11
12
13
14
# File 'lib/rbbt/association/index.rb', line 10

def self.setup(repo)
  repo.extend Association::Index
  repo.parse_key_field
  repo.unnamed = true
end

Instance Method Details

#match(entity) ⇒ Object



41
42
43
44
# File 'lib/rbbt/association/index.rb', line 41

def match(entity)
  return [] if entity.nil?
  prefix(entity + "~")
end

#matches(entities) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rbbt/association/index.rb', line 46

def matches(entities)
  entities.inject(nil) do |acc,e| 
    m = match(e); 
    if acc.nil? or acc.empty?
      acc = m
    else
      acc.concat m
    end
    acc
  end
end

#parse_key_fieldObject



6
7
8
# File 'lib/rbbt/association/index.rb', line 6

def parse_key_field
  @source_field, @target_field, @undirected = key_field.split("~")
end

#reverseObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rbbt/association/index.rb', line 16

def reverse
  @reverse ||= begin
                 reverse_filename = persistence_path + '.reverse'

                 if File.exists?(reverse_filename)
                   new = Persist.open_tokyocabinet(reverse_filename, false, serializer, TokyoCabinet::BDB)
                 else
                   new = Persist.open_tokyocabinet(reverse_filename, true, serializer, TokyoCabinet::BDB)
                   new.write
                   through do |key, value|
                     new_key = key.split("~").reverse.join("~")
                     new[new_key] = value
                   end
                   annotate(new)
                   new.key_field = key_field.split("~").values_at(1,0,2).compact * "~"
                   new.close
                 end

                 new.unnamed = true

                 Association::Index.setup new
                 new
               end
end

#select_entities(entities) ⇒ Object

{{{ Subset



60
61
62
63
64
65
66
67
68
# File 'lib/rbbt/association/index.rb', line 60

def select_entities(entities)
  source_type = Entity.formats[source_field] 
  target_type = Entity.formats[target_field]

  source_entities = entities[source_field] || entities[Entity.formats[source_field].to_s]  
  target_entities = entities[target_field] || entities[Entity.formats[target_field].to_s]

  [source_entities, target_entities]
end

#subset(source, target) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rbbt/association/index.rb', line 70

def subset(source, target)
  return [] if source.nil? or source.empty? or target.nil? or target.empty?

  matches = source.uniq.inject([]){|acc,e| acc.concat(match(e)) }

  target_matches = {}

  matches.each{|code| 
    s,sep,t = code.partition "~"
    next if (undirected and t > s) 
    target_matches[t] ||= []
    target_matches[t] << code
  }

  target_matches.values_at(*target.uniq).flatten.compact
end

#subset_entities(entities) ⇒ Object



87
88
89
90
# File 'lib/rbbt/association/index.rb', line 87

def subset_entities(entities)
  source, target = select_entities(entities)
  subset source, target
end