Class: DB::Mongohandler
Instance Attribute Summary collapse
-
#collection ⇒ Object
Returns the value of attribute collection.
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#data_array ⇒ Object
Returns the value of attribute data_array.
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
Instance Method Summary collapse
-
#add(data) ⇒ Object
Insert data into Data Array.
-
#bulk_insert(data) ⇒ Object
Insert at once many data.
-
#connect ⇒ Object
Connecting to Mongo database.
-
#flush ⇒ Object
Insert remaining data in Array and close Database connection.
-
#initialize(database_name, collection_name, bulk_limit, host, port) ⇒ Mongohandler
constructor
Constructor.
-
#insert(data) ⇒ Object
Insert data.
-
#use_connection(connection) ⇒ Object
Use Existing Connection ==== Attributes *
connection
Existing connection.
Methods included from AbstractInterface
Constructor Details
#initialize(database_name, collection_name, bulk_limit, host, port) ⇒ Mongohandler
Constructor.
Attributes
-
database_name
database name -
collection_name
collection name -
bulk_limit
Array limit for bulk insert. -
host
host name (default: localhost) -
port
port number (default: 27017)
17 18 19 20 21 22 23 24 |
# File 'lib/db/mongohandler.rb', line 17 def initialize(database_name, collection_name, bulk_limit, host, port) @host = host @port = port @dbname = database_name @collname = collection_name @bulk_limit = bulk_limit @data_array = Array.new end |
Instance Attribute Details
#collection ⇒ Object
Returns the value of attribute collection.
6 7 8 |
# File 'lib/db/mongohandler.rb', line 6 def collection @collection end |
#connection ⇒ Object
Returns the value of attribute connection.
6 7 8 |
# File 'lib/db/mongohandler.rb', line 6 def connection @connection end |
#data_array ⇒ Object
Returns the value of attribute data_array.
7 8 9 |
# File 'lib/db/mongohandler.rb', line 7 def data_array @data_array end |
#host ⇒ Object
Returns the value of attribute host.
6 7 8 |
# File 'lib/db/mongohandler.rb', line 6 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
6 7 8 |
# File 'lib/db/mongohandler.rb', line 6 def port @port end |
Instance Method Details
#add(data) ⇒ Object
Insert data into Data Array.
Purpose:
When limit reached DBHandler::bulk_insert will be called on data array.
Attributes
-
data
Hash or Array
82 83 84 85 86 87 88 89 90 |
# File 'lib/db/mongohandler.rb', line 82 def add(data) if (@data_array.size >= @bulk_limit) bulk_insert(@data_array) @data_array.clear @data_array.push(data) else @data_array.push(data) end end |
#bulk_insert(data) ⇒ Object
Insert at once many data.
Attributes
-
data
Array of Hashes
bulk_insert([=> value,=> another_value])
65 66 67 68 69 70 71 |
# File 'lib/db/mongohandler.rb', line 65 def bulk_insert(data) begin @collection.insert(data) rescue Exception => e puts e end end |
#connect ⇒ Object
Connecting to Mongo database.
39 40 41 42 |
# File 'lib/db/mongohandler.rb', line 39 def connect @connection = Mongo::Connection.new(@host, @port, :pool_size => 5) @collection = (@connection[@dbname])[@collname] end |
#flush ⇒ Object
Insert remaining data in Array and close Database connection.
95 96 97 98 99 100 101 102 |
# File 'lib/db/mongohandler.rb', line 95 def flush # Push remaining data to database bulk_insert(@data_array) # Clear Hash Array @data_array.clear # Close Database connection. @connection.close() end |
#insert(data) ⇒ Object
Insert data.
Attributes
-
data
Hash or Array
Examples
insert(=> value)
51 52 53 54 55 56 57 |
# File 'lib/db/mongohandler.rb', line 51 def insert(data) begin @collection.insert(data) rescue Exception => e puts e end end |
#use_connection(connection) ⇒ Object
Use Existing Connection
Attributes
-
connection
Existing connection
31 32 33 34 |
# File 'lib/db/mongohandler.rb', line 31 def use_connection(connection) @connection = connection @collection = (@connection[@dbname])[@collname] end |