Class: Osm2Mongo::Callbacks

Inherits:
Object
  • Object
show all
Defined in:
lib/callbacks.rb

Overview

Container: Responsible for inserting data.

Purpose:

Ability to bulk insert data by limit set at intantiation.

Depends:

  • Container: Handler for MongoDB.

Constant Summary collapse

NODE =
"node"
WAY =
"way"
RELATION =
"relation"
TAG =
"tag"
TEXT =
"#text"
NODEREF =
"nd"
MEMBER =
"member"
NDS =
"nodes"
TGS =
"tags"
MBS =
"members"
DONE =
"done"
KEY =
"k"
VALUE =
"v"
ID =
"id"
MID =
"_id"
LON =
"lon"
LAT =
"lat"
REF =
"ref"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database, collections, qlimit, parser, host, port) ⇒ Callbacks

Returns a new instance of Callbacks


37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/callbacks.rb', line 37

def initialize(database, collections, qlimit, parser, host, port)
    @nodes = DB::Mongohandler.new(database, collections[NODE], qlimit, host, port)
    @nodes.connect()

    @ways = DB::Mongohandler.new(database, collections[WAY], qlimit, host, port)
    @ways.use_connection(@nodes.connection)

    @relations = DB::Mongohandler.new(database, collections[RELATION], qlimit, host, port)
    @relations.use_connection(@nodes.connection)

    parser.add_observer(self)
    @parsed = false
end

Instance Attribute Details

#nodesObject

Returns the value of attribute nodes


35
36
37
# File 'lib/callbacks.rb', line 35

def nodes
  @nodes
end

#parsedObject

Returns the value of attribute parsed


34
35
36
# File 'lib/callbacks.rb', line 34

def parsed
  @parsed
end

#relationsObject

Returns the value of attribute relations


35
36
37
# File 'lib/callbacks.rb', line 35

def relations
  @relations
end

#waysObject

Returns the value of attribute ways


35
36
37
# File 'lib/callbacks.rb', line 35

def ways
  @ways
end

Instance Method Details

#flush_allObject


66
67
68
69
70
71
# File 'lib/callbacks.rb', line 66

def flush_all
    @nodes.flush()
    @ways.flush()
    @relations.flush()
    @parsed = true
end

#node(reader) ⇒ Object


73
74
75
76
77
78
79
# File 'lib/callbacks.rb', line 73

def node(reader)
    attributes = {MID => reader.attribute(ID), LON => reader.attribute(LON), LAT => reader.attribute(LAT)}
    unless reader.empty_element?
        extract_children(reader, attributes)
    end
    @nodes.add(attributes)
end

#relation(reader) ⇒ Object


89
90
91
92
93
94
95
# File 'lib/callbacks.rb', line 89

def relation(reader)
    attributes = {MID => reader.attribute(ID)}
    unless reader.empty_element?
        extract_children(reader, attributes)
    end
    @relations.add(attributes)
end

#update(reader) ⇒ Object


51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/callbacks.rb', line 51

def update(reader)
    case reader.name
        when NODE
            node(reader)
        when WAY
            way(reader)
        when RELATION
            relation(reader)
        when DONE
            flush_all()
        else
            # Skip
    end
end

#way(reader) ⇒ Object


81
82
83
84
85
86
87
# File 'lib/callbacks.rb', line 81

def way(reader)
    attributes = {MID => reader.attribute(ID)}
    unless reader.empty_element?
        extract_children(reader, attributes)
    end
    @ways.add(attributes)
end