Permalink
Browse files

Removed field 'RequestID' in messages types.

  • Loading branch information...
1 parent d652cfe commit d241f2906a9ece542723b89bee795d4db35fa586 John Mac committed with Jun 19, 2010
Showing with 44 additions and 52 deletions.
  1. +38 −15 mongo/collection.go
  2. +5 −7 mongo/cursor.go
  3. +1 −30 mongo/message.go
View
53 mongo/collection.go
@@ -5,8 +5,8 @@
package mongo
import (
+ "bytes"
"os"
- "rand"
)
@@ -59,21 +59,21 @@ func (self *Collection) Drop() os.Error {
}
func (self *Collection) Insert(doc BSON) os.Error {
- im := &opInsert{self.fullName(), doc, rand.Int31()}
- return self.db.Conn.writeMessage(im)
+ im := &opInsert{self.fullName(), doc}
+ return self.db.Conn.writeOp(im)
}
func (self *Collection) Remove(selector BSON) os.Error {
- dm := &opDelete{self.fullName(), selector, rand.Int31()}
- return self.db.Conn.writeMessage(dm)
+ dm := &opDelete{self.fullName(), selector}
+ return self.db.Conn.writeOp(dm)
}
func (self *Collection) Query(query BSON, skip, limit int) (*Cursor, os.Error) {
- req_id := rand.Int31()
+ reqID := getRequestID()
conn := self.db.Conn
- qm := &opQuery{0, self.fullName(), int32(skip), int32(limit), query, req_id}
+ qm := &opQuery{0, self.fullName(), int32(skip), int32(limit), query}
- err := conn.writeMessage(qm)
+ err := conn.writeOpQuery(qm, reqID)
if err != nil {
return nil, err
}
@@ -82,7 +82,7 @@ func (self *Collection) Query(query BSON, skip, limit int) (*Cursor, os.Error) {
if err != nil {
return nil, err
}
- if reply.responseTo != req_id {
+ if reply.responseTo != reqID {
return nil, os.NewError("wrong responseTo code")
}
@@ -119,25 +119,48 @@ func (self *Collection) Count(query BSON) (int64, os.Error) {
}
func (self *Collection) update(um *opUpdate) os.Error {
- um.requestID = rand.Int31()
conn := self.db.Conn
- return conn.writeMessage(um)
+ return conn.writeOp(um)
}
func (self *Collection) Update(selector, document BSON) os.Error {
- return self.update(&opUpdate{self.fullName(), 0, selector, document, 0})
+ return self.update(&opUpdate{self.fullName(), 0, selector, document})
}
func (self *Collection) Upsert(selector, document BSON) os.Error {
- return self.update(&opUpdate{self.fullName(), 1, selector, document, 0})
+ return self.update(&opUpdate{self.fullName(), 1, selector, document})
}
func (self *Collection) UpdateAll(selector, document BSON) os.Error {
- return self.update(&opUpdate{self.fullName(), 2, selector, document, 0})
+ return self.update(&opUpdate{self.fullName(), 2, selector, document})
}
func (self *Collection) UpsertAll(selector, document BSON) os.Error {
- return self.update(&opUpdate{self.fullName(), 3, selector, document, 0})
+ return self.update(&opUpdate{self.fullName(), 3, selector, document})
+}
+
+
+// *** Utility
+// ***
+
+func (self *Connection) writeOp(m message) os.Error {
+ body := m.Bytes()
+ h := header(msgHeader{int32(len(body) + 16), getRequestID(), 0, m.OpCode()})
+
+ msg := bytes.Add(h, body)
+ _, err := self.conn.Write(msg)
+
+ return err
+}
+
+func (self *Connection) writeOpQuery(m message, reqID int32) os.Error {
+ body := m.Bytes()
+ h := header(msgHeader{int32(len(body) + 16), reqID, 0, m.OpCode()})
+
+ msg := bytes.Add(h, body)
+ _, err := self.conn.Write(msg)
+
+ return err
}
View
12 mongo/cursor.go
@@ -5,9 +5,8 @@
package mongo
import (
- "os"
- "rand"
"container/vector"
+ "os"
)
@@ -45,9 +44,9 @@ func (self *Cursor) GetMore() os.Error {
return os.NewError("no cursorID")
}
- gm := &opGetMore{self.collection.fullName(), 0, self.id, rand.Int31()}
+ gm := &opGetMore{self.collection.fullName(), 0, self.id}
conn := self.collection.db.Conn
- err := conn.writeMessage(gm)
+ err := conn.writeOp(gm)
if err != nil {
return err
}
@@ -69,9 +68,8 @@ func (self *Cursor) Close() os.Error {
return nil
}
- req_id := rand.Int31()
- km := &opKillCursors{1, []int64{self.id}, req_id}
+ km := &opKillCursors{1, []int64{self.id}}
conn := self.collection.db.Conn
- return conn.writeMessage(km)
+ return conn.writeOp(km)
}
View
31 mongo/message.go
@@ -33,8 +33,6 @@ const (
_OP_KILL_CURSORS = 2007 // Tell database client is done with a cursor
)
-var last_req int32
-
// *** Standard Message Header
// ***
@@ -63,7 +61,6 @@ func header(h msgHeader) []byte {
type message interface {
Bytes() []byte
- RequestID() int32
OpCode() int32
}
@@ -91,11 +88,10 @@ type opUpdate struct {
fullCollectionName string
flags int32
selector, document BSON
- requestID int32
+
}
func (self *opUpdate) OpCode() int32 { return _OP_UPDATE }
-func (self *opUpdate) RequestID() int32 { return self.requestID }
func (self *opUpdate) Bytes() []byte {
b := make([]byte, 4)
@@ -125,11 +121,9 @@ type opInsert struct {
type opInsert struct {
fullCollectionName string
doc BSON
- requestID int32
}
func (self *opInsert) OpCode() int32 { return _OP_INSERT }
-func (self *opInsert) RequestID() int32 { return self.requestID }
func (self *opInsert) Bytes() []byte {
buf := bytes.NewBuffer(make([]byte, 4)) // _ZERO
@@ -160,11 +154,9 @@ type opQuery struct {
numberToSkip int32
numberToReturn int32
query BSON
- requestID int32
}
func (self *opQuery) OpCode() int32 { return _OP_QUERY }
-func (self *opQuery) RequestID() int32 { return self.requestID }
func (self *opQuery) Bytes() []byte {
var buf bytes.Buffer
@@ -201,11 +193,9 @@ type opGetMore struct {
fullCollectionName string
numberToReturn int32
cursorID int64
- requestID int32
}
func (self *opGetMore) OpCode() int32 { return _OP_GET_MORE }
-func (self *opGetMore) RequestID() int32 { return self.requestID }
func (self *opGetMore) Bytes() []byte {
b := make([]byte, 4)
@@ -237,11 +227,9 @@ type opDelete struct {
type opDelete struct {
fullCollectionName string
selector BSON
- requestID int32
}
func (self *opDelete) OpCode() int32 { return _OP_DELETE }
-func (self *opDelete) RequestID() int32 { return self.requestID }
func (self *opDelete) Bytes() []byte {
b := make([]byte, 4)
@@ -269,11 +257,9 @@ func (self *opDelete) Bytes() []byte {
type opKillCursors struct {
numberOfCursorIDs int32
cursorIDs []int64
- requestID int32
}
func (self *opKillCursors) OpCode() int32 { return _OP_KILL_CURSORS }
-func (self *opKillCursors) RequestID() int32 { return self.requestID }
func (self *opKillCursors) Bytes() []byte {
b := make([]byte, 4)
@@ -348,18 +334,3 @@ func parseReply(b []byte) *opReply {
return r
}
-
-// *** Utility
-// ***
-
-func (self *Connection) writeMessage(m message) os.Error {
- body := m.Bytes()
- h := header(msgHeader{int32(len(body) + 16), m.RequestID(), 0, m.OpCode()})
-
- msg := bytes.Add(h, body)
- _, err := self.conn.Write(msg)
-
- last_req = m.RequestID()
- return err
-}
-

0 comments on commit d241f29

Please sign in to comment.