Class: DB::Mongohandler

Inherits:
Dbhandler show all
Defined in:
lib/db/mongohandler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AbstractInterface

included

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

#collectionObject

Returns the value of attribute collection


6
7
8
# File 'lib/db/mongohandler.rb', line 6

def collection
  @collection
end

#connectionObject

Returns the value of attribute connection


6
7
8
# File 'lib/db/mongohandler.rb', line 6

def connection
  @connection
end

#data_arrayObject

Returns the value of attribute data_array


7
8
9
# File 'lib/db/mongohandler.rb', line 7

def data_array
  @data_array
end

#hostObject

Returns the value of attribute host


6
7
8
# File 'lib/db/mongohandler.rb', line 6

def host
  @host
end

#portObject

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

#connectObject

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

#flushObject

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