Class: NewsCrawler::Storage::YAMLStor::MongoStorage

Inherits:
YAMLStorEngine show all
Includes:
Mongo
Defined in:
lib/news_crawler/storage/yaml_stor/mongo_storage.rb

Overview

YAML storage implement using MongoDB

Constant Summary collapse

NAME =
'mongo'

Instance Method Summary collapse

Methods inherited from YAMLStorEngine

get_engines, inherited

Constructor Details

#initialize(*opts) ⇒ MongoStorage

Returns a new instance of MongoStorage.



39
40
41
42
43
44
45
# File 'lib/news_crawler/storage/yaml_stor/mongo_storage.rb', line 39

def initialize(*opts)
  config = (SimpleConfig.for :application)
  client = MongoClient.new(config.mongodb.host, config.mongodb.port)
  db = client[config.mongodb.db_name]
  @coll = db[config.prefix + '_' + config.suffix.yaml]
  # @coll.ensure_index({:key => Mongo::ASCENDING}, {:unique => true})
end

Instance Method Details

#add(module_name, key, value) ⇒ Object

Add entry to yaml collection, overwrite old data

Parameters:

  • module_name (String)
  • key (String)
  • value (Object)

    YAML string



51
52
53
54
55
56
57
58
# File 'lib/news_crawler/storage/yaml_stor/mongo_storage.rb', line 51

def add(module_name, key, value)
  yaml_str = value.to_yaml
  yaml_str.encode!('utf-8', :invalid => :replace, :undef => :replace)
  @coll.update({:key   => key,
                 :m_name => module_name},
               {:$set  => {:value => yaml_str}},
               {:upsert => true})
end

#clearObject



79
80
81
# File 'lib/news_crawler/storage/yaml_stor/mongo_storage.rb', line 79

def clear
  @coll.remove
end

#countObject

Get number of raw data entries



75
76
77
# File 'lib/news_crawler/storage/yaml_stor/mongo_storage.rb', line 75

def count
  @coll.count
end

#get(module_name, key) ⇒ Object?

Find document with correspond key

Parameters:

  • module_name (String)
  • key (String)

Returns:

  • (Object, nil)


64
65
66
67
68
69
70
71
72
# File 'lib/news_crawler/storage/yaml_stor/mongo_storage.rb', line 64

def get(module_name, key)
  result = @coll.find_one({:key => key,
                            :m_name => module_name})
  if (!result.nil?)
    YAML.load(result['value'])
  else
    nil
  end
end