Class: Spider::Model::Storage::Document::Mongodb

Inherits:
DocumentStorage show all
Defined in:
lib/spiderfw/model/storage/document/adapters/mongodb.rb

Defined Under Namespace

Modules: MapperExtension

Constant Summary

CONDITION_OPS =
{
    '>' => '$gt', '<' => '$lt', '>=' => '$gte', '<=' => '$lte', 'not' => '$e', '<>' => '$ne'
}

Instance Attribute Summary

Attributes inherited from BaseStorage

#instance_name, #url

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from DocumentStorage

#get_mapper, storage_type

Methods inherited from BaseStorage

#==, base_types, #commit, #commit!, #commit_or_continue, #configure, #connected?, #connection, #connection_attributes, connection_attributes, #connection_pool, connection_pools, #create_sequence, #curr, #do_commit, #do_rollback, #do_start_transaction, #generate_uuid, get_connection, #get_mapper, #in_transaction, #in_transaction?, inherited, #initialize, max_connections, release_connection, remove_connection, #rollback, #rollback!, #rollback_savepoint, #savepoint, #sequence_exists?, #sequence_file_path, #sequence_next, sequence_sync, #start_transaction, storage_type, #supports?, supports?, #supports_transactions?, #transactions_enabled?, #update_sequence, #value_for_condition, #value_for_save

Methods included from Logger

add, close, close_all, datetime_format, datetime_format=, debug, #debug, debug?, #debug?, enquire_loggers, #error, error, error?, #error?, #fatal, fatal, #fatal?, fatal?, info, #info, info?, #info?, log, #log, method_missing, open, reopen, send_to_loggers, unknown, #unknown, #warn, warn, #warn?, warn?

Constructor Details

This class inherits a constructor from Spider::Model::Storage::BaseStorage

Class Method Details

+ (Boolean) connection_alive?(conn)

Returns:

  • (Boolean)


42
43
44
# File 'lib/spiderfw/model/storage/document/adapters/mongodb.rb', line 42

def self.connection_alive?(conn)
    conn.connected?
end

+ (Object) disconnect(conn)



38
39
40
# File 'lib/spiderfw/model/storage/document/adapters/mongodb.rb', line 38

def self.disconnect(conn)
    conn.close
end

+ (Object) new_connection(host = nil, user = nil, passwd = nil, db = nil, port = nil)



32
33
34
35
36
# File 'lib/spiderfw/model/storage/document/adapters/mongodb.rb', line 32

def self.new_connection(host=nil, user=nil, passwd=nil, db=nil, port=nil)
    conn = ::Mongo::Connection.new(host, port)
    conn.authenticate(user, passwd) if user
    return conn
end

+ (Object) parse_url(url)



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/spiderfw/model/storage/document/adapters/mongodb.rb', line 11

def self.parse_url(url)
    # doc:mongodb://<username:password>@<host>:<port>/<database>
    if (url =~ /.+:\/\/(?:(.+):(.+)@)?(.+)?\/(.+)/)
        user = $1
        pass = $2
        location = $3
        db_name = $4
    else
        raise ArgumentError, "Mongodb url '#{url}' is invalid"
    end
    location =~ /(.+)(?::(\d+))/
    host = $1
    port = $2
    return [host, user, pass, db_name, port]
end

Instance Method Details

- (Object) collection(coll)



62
63
64
# File 'lib/spiderfw/model/storage/document/adapters/mongodb.rb', line 62

def collection(coll)
    db.collection(coll)
end

- (Object) connect



46
47
48
49
50
# File 'lib/spiderfw/model/storage/document/adapters/mongodb.rb', line 46

def connect
    conn = super
    curr[:db] = conn.db(@connection_params[3])
    conn
end

- (Object) db



57
58
59
60
# File 'lib/spiderfw/model/storage/document/adapters/mongodb.rb', line 57

def db
    connection
    curr[:db]
end

- (Object) delete(collection, doc)



86
87
88
89
90
91
92
# File 'lib/spiderfw/model/storage/document/adapters/mongodb.rb', line 86

def delete(collection, doc)
    begin
        collection(collection).remove({"_id" => doc["_id"]}, doc)
    ensure
        release
    end
end

- (Object) drop_db!



155
156
157
158
159
160
161
162
# File 'lib/spiderfw/model/storage/document/adapters/mongodb.rb', line 155

def drop_db!
    conn = connection
    begin
        conn.drop_database(@db_name)
    ensure
        release
    end
end

- (Object) find(collection, condition, request, options)



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/spiderfw/model/storage/document/adapters/mongodb.rb', line 124

def find(collection, condition, request, options)
    options[:fields] = request
    Spider.logger.debug("Mongodb find #{collection}, #{condition.inspect}")
    begin
        res = collection(collection).find(condition, options).to_a
    ensure
        release
    end
    res = res.map{ |row| keys_to_symbols(row) }
    res.extend(StorageResult)
    res.total_rows = res.length # FIXME
    res
end

- (Object) generate_pk



164
165
166
# File 'lib/spiderfw/model/storage/document/adapters/mongodb.rb', line 164

def generate_pk
    ::BSON::ObjectId.new
end

- (Object) insert(collection, doc)



66
67
68
69
70
71
72
73
74
# File 'lib/spiderfw/model/storage/document/adapters/mongodb.rb', line 66

def insert(collection, doc)
    Spider.logger.debug("Mongodb insert #{collection}:")
    Spider.logger.debug(doc)
    begin
        collection(collection).insert(doc)
    ensure
        release
    end
end

- (Object) keys_to_symbols(h)



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/spiderfw/model/storage/document/adapters/mongodb.rb', line 138

def keys_to_symbols(h)
    sh = {}
    h.each do |k, v|
        v = if v.is_a?(Hash)
            keys_to_symbols(v)
        elsif v.is_a?(Array)
            v.map{ |row| keys_to_symbols(row) }
        else
            v
        end
        #name = k == "_id" ? :id : k.to_sym
        name = k.to_sym
        sh[name] = v
    end
    sh
end

- (Object) parse_url(url)



27
28
29
30
# File 'lib/spiderfw/model/storage/document/adapters/mongodb.rb', line 27

def parse_url(url)
    @host, @user, @pass, @db_name, @port = self.class.parse_url(url)
    @connection_params = [@host, @user, @pass, @db_name, @port]
end

- (Object) prepare_value(type, value)



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/spiderfw/model/storage/document/adapters/mongodb.rb', line 94

def prepare_value(type, value)
    return value if value.nil?
    case type.name
    when 'Time'
        value = value.utc
    when 'DateTime'
        value = value.to_local_time.utc.to_time
    when 'Date'
        value = value.to_gm_time.utc
    when 'BigDecimal'
        # FIXME: should multiply on value.attributes[:scale] and store an integer, converting it back on load
        value = value.to_f
    end
    value 
end

- (Object) release



52
53
54
55
# File 'lib/spiderfw/model/storage/document/adapters/mongodb.rb', line 52

def release
    curr[:db] = nil
    super
end

- (Object) update(collection, selector, vals)



76
77
78
79
80
81
82
83
84
# File 'lib/spiderfw/model/storage/document/adapters/mongodb.rb', line 76

def update(collection, selector, vals)
    Spider.logger.debug("Mongodb update #{collection}, #{selector.inspect}:")
    Spider.logger.debug(vals)
    begin
        collection(collection).update(selector, {'$set' => vals})
    ensure
        release
    end
end

- (Object) value_to_mapper(type, value)



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/spiderfw/model/storage/document/adapters/mongodb.rb', line 110

def value_to_mapper(type, value)
    if value.class == Time
        if type.name == 'Date'
            value = value.to_date
        elsif type.name == 'DateTime'
            value = value.to_datetime
        end
        return value
    elsif type.name == 'Fixnum'
        return value.to_i
    end
    return super
end