Class: OpenBEL::Nanopub::NanopubFacets

Inherits:
Object
  • Object
show all
Includes:
Mongo, Helpers::UUIDGenerator, FacetAPI, FacetFilter
Defined in:
lib/openbel/api/nanopub/mongo_facet.rb

Constant Summary

Constants included from FacetFilter

FacetFilter::EMPTY, FacetFilter::NANOPUB_PARTS

Instance Method Summary collapse

Methods included from FacetFilter

#make_filter, #map_citation_facets, #map_experiment_context_facets, #map_metadata_facets, #map_nanopub_facets

Methods included from FacetAPI

#create_facets, #find_facets_by_filters, #remove_facets_by_filters

Constructor Details

#initialize(options = {}) ⇒ NanopubFacets

Returns a new instance of NanopubFacets.



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

def initialize(options = {})
  host = options[:host]
  port = options[:port]
  db   = options[:database]
  @db  = MongoClient.new(
    host,
    port,
    :op_timeout => nil,
    :pool_size  => 30
  ).db(db)

  # Authenticate user if provided.
  username = options[:username]
  password = options[:password]
  if username && password
    auth_db = options[:authentication_database] || db
    @db.authenticate(username, password, nil, auth_db)
  end

  @nanopub             = @db[:nanopub]
  @nanopub_facet_cache = @db[:nanopub_facet_cache]

  # ensure all indexes are created and maintained
  ensure_all_indexes
end

Instance Method Details

#delete_all_facetsObject



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/openbel/api/nanopub/mongo_facet.rb', line 99

def delete_all_facets
  @nanopub_facet_cache.find(
    {},
    :fields => {:_id => 0, :cache_collection => 1}
  ).each do |doc|
    cache_collection = doc['cache_collection']
    @db[cache_collection].drop()
  end

  # remove all entries in nanopub_facet_cache
  @nanopub_facet_cache.remove({})
end

#delete_facets(facets) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/openbel/api/nanopub/mongo_facet.rb', line 81

def delete_facets(facets)
  # Add zero-filter to facets; clears the default search
  facets = facets.to_a
  facets.unshift([])

  # Drop facet cache collections
  @nanopub_facet_cache.find(
    {:filters => {:$in => facets}},
    :fields => {:_id => 0, :cache_collection => 1}
  ).each do |doc|
    cache_collection = doc['cache_collection']
    @db[cache_collection].drop()
  end

  # remove filter match entries in nanopub_facet_cache
  @nanopub_facet_cache.remove({:filters => {:$in => facets}})
end

#ensure_all_indexesObject



112
113
114
115
116
117
118
119
# File 'lib/openbel/api/nanopub/mongo_facet.rb', line 112

def ensure_all_indexes
  @nanopub_facet_cache.ensure_index([
      [:"filters.category",   Mongo::ASCENDING],
      [:"filters.name",       Mongo::ASCENDING]
    ],
    :background => true
  )
end

#find_facets(query, filters, facet_value_limit = -1)) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
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/openbel/api/nanopub/mongo_facet.rb', line 42

def find_facets(query, filters, facet_value_limit = -1)
  sorted_filters = sort_filters(filters)
  cache_collection = facet_cache_collection(sorted_filters)

  if no_collection?(cache_collection)
    cache_collection = "nanopub_facet_cache_#{generate_uuid}"
    create_aggr      = create_aggregation(cache_collection, query)
    @nanopub.aggregate(create_aggr[:pipeline], create_aggr[:options])
    @nanopub_facet_cache.insert({
      :filters          => sorted_filters,
      :cache_collection => cache_collection
    })
  end

  # set up field projection based on value limit
  field_projection = {
    :_id      => 0,
    :category => 1,
    :name     => 1,
    :values   => 1
  }
  if facet_value_limit > 0
    field_projection[:values] = {:$slice => facet_value_limit}
  end

  # cursor facets and apply "filter"
  @db[cache_collection].find({}, :fields => field_projection).map { |facet_doc|
    category, name     = facet_doc.values_at('category', 'name')
    facet_doc['values'].each do |value|
      value[:filter] = MultiJson.dump({
        :category => category,
        :name     => name,
        :value    => value['value']
      })
    end
    facet_doc
  }
end