Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Moved parseReply() to message.go. MongoDB uses a port by default.

  • Loading branch information...
commit 004be0db99f329c3954d02cd48a7a2e2b6456cb8 1 parent db31478
John Mac authored committed
Showing with 45 additions and 35 deletions.
  1. +3 −3 mongo/all_test.go
  2. +12 −32 mongo/connection.go
  3. +30 −0 mongo/message.go
6 mongo/all_test.go
View
@@ -23,7 +23,7 @@ func TestStuff(t *testing.T) {
obj, err := BytesToBSON([]byte{92, 0, 0, 0, 1, 115, 101, 99, 111, 110, 100, 0, 0, 0, 0, 0, 0, 0, 0, 64, 3, 102, 105, 102, 116, 104, 0, 23, 0, 0, 0, 2, 118, 0, 2, 0, 0, 0, 101, 0, 2, 102, 0, 2, 0, 0, 0, 105, 0, 0, 3, 102, 111, 117, 114, 116, 104, 0, 5, 0, 0, 0, 0, 2, 116, 104, 105, 114, 100, 0, 6, 0, 0, 0, 116, 104, 114, 101, 101, 0, 16, 102, 105, 114, 115, 116, 0, 1, 0, 0, 0, 0})
assertTrue(err == nil, "failed parsing BSON obj", t)
- conn, err := Connect("127.0.0.1", 27017)
+ conn, err := Connect("127.0.0.1")
assertTrue(err == nil && conn != nil, fmt.Sprintf("failed connecting to mongo: %v", err), t)
db := conn.GetDB("go_driver_tests")
@@ -68,7 +68,7 @@ func TestStuff(t *testing.T) {
func TestOtherStuff(t *testing.T) {
doc, _ := Marshal(map[string]string{"_id": "doc1", "title": "A Mongo document", "content": "Testing, 1. 2. 3."})
- conn, _ := Connect("127.0.0.1", 27017)
+ conn, _ := Connect("127.0.0.1")
collection := conn.GetDB("test").GetCollection("test_collection")
collection.Insert(doc)
@@ -94,7 +94,7 @@ func timeIt(s string, f func(*Collection, *testing.T), coll *Collection, t *test
}
func TestBenchmark(t *testing.T) {
- conn, err := Connect("127.0.0.1", 27017)
+ conn, err := Connect("127.0.0.1")
if err != nil {
t.Error("failed connecting")
}
44 mongo/connection.go
View
@@ -5,8 +5,6 @@
package mongo
import (
- "bytes"
- "container/vector"
"fmt"
"io"
"io/ioutil"
@@ -15,12 +13,21 @@ import (
)
+// Default Socket Port
+const _PORT = 27017
+
+
type Connection struct {
Addr *net.TCPAddr
conn *net.TCPConn
}
-func Connect(host string, port int) (*Connection, os.Error) {
+func Connect(host string) (*Connection, os.Error) {
+ return ConnectAt(host, _PORT)
+}
+
+/* Creates a new connection to a single MongoDB instance at host:port. */
+func ConnectAt(host string, port int) (*Connection, os.Error) {
addr, err := net.ResolveTCPAddr(fmt.Sprintf("%s:%d", host, port))
if err != nil {
return nil, err
@@ -61,42 +68,15 @@ func (self *Connection) GetDB(name string) *Database {
return &Database{self, name}
}
-
-// *** Database Response Message
-// ***
-
// *** OP_REPLY
+/* Gets the message of reply from database. */
func (self *Connection) readReply() (*opReply, os.Error) {
size_bits, _ := ioutil.ReadAll(io.LimitReader(self.conn, 4))
size := pack.Uint32(size_bits)
rest, _ := ioutil.ReadAll(io.LimitReader(self.conn, int64(size)-4))
reply := parseReply(rest)
- return reply, nil
-}
-func parseReply(b []byte) *opReply {
- r := new(opReply)
- r.responseTo = int32(pack.Uint32(b[4:8]))
- r.responseFlag = int32(pack.Uint32(b[12:16]))
- r.cursorID = int64(pack.Uint64(b[16:24]))
- r.startingFrom = int32(pack.Uint32(b[24:28]))
- r.numberReturned = int32(pack.Uint32(b[28:32]))
- r.documents = new(vector.Vector)
-
- if r.numberReturned > 0 {
- buf := bytes.NewBuffer(b[36:len(b)])
- for i := 0; int32(i) < r.numberReturned; i++ {
- var bson BSON
- bb := new(_BSONBuilder)
- bb.ptr = &bson
- bb.Object()
- Parse(buf, bb)
- r.documents.Push(bson)
- ioutil.ReadAll(io.LimitReader(buf, 4))
- }
- }
-
- return r
+ return reply, nil
}
30 mongo/message.go
View
@@ -12,6 +12,8 @@ package mongo
import (
"bytes"
"container/vector"
+ "io"
+ "io/ioutil"
)
@@ -289,3 +291,31 @@ type opReply struct {
documents *vector.Vector // documents
}
+func parseReply(b []byte) *opReply {
+ r := new(opReply)
+
+ r.responseTo = int32(pack.Uint32(b[4:8]))
+ r.responseFlag = int32(pack.Uint32(b[12:16]))
+ r.cursorID = int64(pack.Uint64(b[16:24]))
+ r.startingFrom = int32(pack.Uint32(b[24:28]))
+ r.numberReturned = int32(pack.Uint32(b[28:32]))
+ r.documents = new(vector.Vector)
+
+ if r.numberReturned > 0 {
+ buf := bytes.NewBuffer(b[36:len(b)])
+
+ for i := 0; int32(i) < r.numberReturned; i++ {
+ var bson BSON
+ bb := new(_BSONBuilder)
+
+ bb.ptr = &bson
+ bb.Object()
+ Parse(buf, bb)
+ r.documents.Push(bson)
+ ioutil.ReadAll(io.LimitReader(buf, 4))
+ }
+ }
+
+ return r
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.