Skip to content

Commit

Permalink
basic insert/remove
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm1 committed Aug 1, 2008
1 parent c23ad67 commit abc92de
Showing 1 changed file with 95 additions and 8 deletions.
103 changes: 95 additions & 8 deletions rmongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def connection_completed
end

def receive_data data
# log 'receive_data', data
log 'receive_data', data
@buf << data

# packet size
Expand All @@ -49,7 +49,7 @@ def receive_data data
end

def send_data data
# log 'send_data', data
log 'send_data', data
super
end

Expand All @@ -59,6 +59,7 @@ def unbind

# commands

# to sort: { query : { ... } , orderby : { ... } }
def find obj, &cb
buf = Buffer.new

Expand All @@ -68,10 +69,10 @@ def find obj, &cb
buf.write :int, operation = 2004

# body
buf.write :int, reserved = 0
buf.write :cstring, 'default.test'
buf.write :int, skip = 0
buf.write :int, ret = 0
buf.write :int, reserved = 0
buf.write :cstring, namespace = 'default.test'
buf.write :int, skip = 0
buf.write :int, ret = 0

# bson
buf.write :bson, obj
Expand All @@ -83,6 +84,51 @@ def find obj, &cb
send_data buf.data
}
end

def insert obj
buf = Buffer.new

# header
buf.write :int, id = @id+=1
buf.write :int, response = 0
buf.write :int, operation = 2002

# body
buf.write :int, reserved = 0
buf.write :cstring, namespace = 'default.test'

# bson
buf.write :bson, obj

callback{
send_data [ buf.size + 4 ].pack('i')
send_data buf.data
}
end

def remove obj
buf = Buffer.new

# header
buf.write :int, id = @id+=1
buf.write :int, response = 0
buf.write :int, operation = 2006

# body
buf.write :int, reserved = 0
buf.write :cstring, namespace = 'default.test'
buf.write :int, 0

# bson
buf.write :bson, obj

callback{
send_data [ buf.size + 4 ].pack('i')
send_data buf.data
}
end

# connection

def self.connect opts = {}
opts[:host] ||= '127.0.0.1'
Expand All @@ -104,7 +150,48 @@ def log *args

EM.run{
mongo = Mongo::Client.connect
mongo.find({}) do |results|

mongo.remove({})

mongo.insert({ :n => 1, :_id => '4892ae52771f9ae3002d9cf5' })
mongo.insert({ :n => 1, :_id => '4892ae52771f9ae3002d9cf6' })

mongo.find({:n=>1}) do |results|
pp [:found, results]
puts
EM.stop_event_loop
end
}
}

__END__

["connected"]

["send_data", "*\000\000\000"]

["send_data",
"\001\000\000\000\000\000\000\000\326\a\000\000\000\000\000\000default.test\000\000\000\000\000\005\000\000\000\000"]

["send_data", "B\000\000\000"]

["send_data",
"\002\000\000\000\000\000\000\000\322\a\000\000\000\000\000\000default.test\000!\000\000\000\001n\000\000\000\000\000\000\000\360?\a_id\000\343\232\037wR\256\222H\365\234-\000\000"]

["send_data", "B\000\000\000"]

["send_data",
"\003\000\000\000\000\000\000\000\322\a\000\000\000\000\000\000default.test\000!\000\000\000\001n\000\000\000\000\000\000\000\360?\a_id\000\343\232\037wR\256\222H\366\234-\000\000"]

["send_data", "9\000\000\000"]

["send_data",
"\004\000\000\000\000\000\000\000\324\a\000\000\000\000\000\000default.test\000\000\000\000\000\000\000\000\000\020\000\000\000\001n\000\000\000\000\000\000\000\360?\000"]

["receive_data",
"f\000\000\000\3056\027\245\004\000\000\000\001\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\002\000\000\000!\000\000\000\001n\000\000\000\000\000\000\000\360?\a_id\000\343\232\037wR\256\222H\365\234-\000\000!\000\000\000\001n\000\000\000\000\000\000\000\360?\a_id\000\343\232\037wR\256\222H\366\234-\000\000"]

[:found,
[{:n=>1.0, :_id=>"4892ae52771f9ae3002d9cf5"},
{:n=>1.0, :_id=>"4892ae52771f9ae3002d9cf6"}]]

["disconnected"]

0 comments on commit abc92de

Please sign in to comment.