Permalink
Browse files

sorting functions related to messages. Using constants to get flags

  • Loading branch information...
1 parent 7cb27ce commit 277e82a42583122391673581997c591da94cbe86 John Mac committed with Jun 20, 2010
Showing with 175 additions and 131 deletions.
  1. +85 −60 mongo/collection.go
  2. +43 −0 mongo/connection.go
  3. +36 −25 mongo/cursor.go
  4. +11 −46 mongo/message.go
View
@@ -10,7 +10,24 @@ import (
)
-var fUpsert, fUpdateAll, fUpsertAll int32 // OP_UPDATE flags
+type indexDesc struct {
+ Name string
+ Ns string
+ Key map[string]int
+}
+
+type Collection struct {
+ db *Database
+ name string
+}
+
+
+// *** Client Request Messages
+// ***
+
+// *** OP_UPDATE
+
+var fUpsert, fUpdateAll, fUpsertAll int32 // flags
// Calculates values of flags
func init() {
@@ -20,19 +37,79 @@ func init() {
setBit32(&fUpsertAll, f_UPSERT, f_MULTI_UPDATE)
}
+func (self *Collection) Update(selector, document BSON) os.Error {
+ return self.update(&opUpdate{self.fullName(), _ZERO, selector, document})
+}
-type indexDesc struct {
- Name string
- Ns string
- Key map[string]int
+func (self *Collection) Upsert(selector, document BSON) os.Error {
+ return self.update(&opUpdate{self.fullName(), fUpsert, selector, document})
}
+func (self *Collection) UpdateAll(selector, document BSON) os.Error {
+ return self.update(&opUpdate{self.fullName(), fUpdateAll, selector, document})
+}
-type Collection struct {
- db *Database
- name string
+func (self *Collection) UpsertAll(selector, document BSON) os.Error {
+ return self.update(&opUpdate{self.fullName(), fUpsertAll, selector, document})
+}
+
+func (self *Collection) update(msg *opUpdate) os.Error {
+ return self.db.Conn.writeOp(msg)
+}
+
+// *** OP_INSERT
+
+func (self *Collection) Insert(doc BSON) os.Error {
+ msg := &opInsert{self.fullName(), doc}
+ return self.db.Conn.writeOp(msg)
+}
+
+// *** OP_QUERY
+
+func (self *Collection) Query(query BSON, skip, limit int32) (*Cursor, os.Error) {
+ conn := self.db.Conn
+ reqID := getRequestID()
+ msg := &opQuery{o_NONE, self.fullName(), skip, limit, query}
+
+ if err := conn.writeOpQuery(msg, reqID); err != nil {
+ return nil, err
+ }
+
+ reply, err := conn.readReply()
+ if err != nil {
+ return nil, err
+ }
+ if reply.responseTo != reqID {
+ return nil, os.NewError("wrong responseTo code")
+ }
+
+ return &Cursor{self, reply.cursorID, 0, reply.documents}, nil
+}
+
+// *** OP_DELETE
+
+var fSingleRemove int32 // flags
+
+// Calculates values of flags
+func init() {
+ setBit32(&fSingleRemove, f_SINGLE_REMOVE)
+}
+
+func (self *Collection) Remove(selector BSON) os.Error {
+ return self.remove(&opDelete{self.fullName(), _ZERO, selector})
}
+func (self *Collection) RemoveFirst(selector BSON) os.Error {
+ return self.remove(&opDelete{self.fullName(), fSingleRemove, selector})
+}
+
+func (self *Collection) remove(msg *opDelete) os.Error {
+ return self.db.Conn.writeOp(msg)
+}
+
+
+//**********
+
func (self *Collection) fullName() string { return self.db.name + "." + self.name }
func (self *Collection) EnsureIndex(name string, index map[string]int) os.Error {
@@ -69,36 +146,6 @@ func (self *Collection) Drop() os.Error {
return err
}
-func (self *Collection) Insert(doc BSON) os.Error {
- im := &opInsert{self.fullName(), doc}
- return self.db.Conn.writeOp(im)
-}
-
-func (self *Collection) Remove(selector BSON) os.Error {
- dm := &opDelete{self.fullName(), selector}
- return self.db.Conn.writeOp(dm)
-}
-
-func (self *Collection) Query(query BSON, skip, limit int) (*Cursor, os.Error) {
- reqID := getRequestID()
- conn := self.db.Conn
- qm := &opQuery{0, self.fullName(), int32(skip), int32(limit), query}
-
- err := conn.writeOpQuery(qm, reqID)
- if err != nil {
- return nil, err
- }
-
- reply, err := conn.readReply()
- if err != nil {
- return nil, err
- }
- if reply.responseTo != reqID {
- return nil, os.NewError("wrong responseTo code")
- }
-
- return &Cursor{self, reply.cursorID, 0, reply.documents}, nil
-}
func (self *Collection) FindAll(query BSON) (*Cursor, os.Error) {
return self.Query(query, 0, 0)
@@ -129,28 +176,6 @@ func (self *Collection) Count(query BSON) (int64, os.Error) {
return int64(reply.Get("n").Number()), nil
}
-func (self *Collection) update(msg *opUpdate) os.Error {
- conn := self.db.Conn
-
- return conn.writeOp(msg)
-}
-
-func (self *Collection) Update(selector, document BSON) os.Error {
- return self.update(&opUpdate{self.fullName(), _ZERO, selector, document})
-}
-
-func (self *Collection) Upsert(selector, document BSON) os.Error {
- return self.update(&opUpdate{self.fullName(), fUpsert, selector, document})
-}
-
-func (self *Collection) UpdateAll(selector, document BSON) os.Error {
- return self.update(&opUpdate{self.fullName(), fUpdateAll, selector, document})
-}
-
-func (self *Collection) UpsertAll(selector, document BSON) os.Error {
- return self.update(&opUpdate{self.fullName(), fUpsertAll, selector, document})
-}
-
// *** Utility
// ***
View
@@ -5,7 +5,11 @@
package mongo
import (
+ "bytes"
+ "container/vector"
"fmt"
+ "io"
+ "io/ioutil"
"net"
"os"
)
@@ -57,3 +61,42 @@ func (self *Connection) GetDB(name string) *Database {
return &Database{self, name}
}
+
+// *** Database Response Message
+// ***
+
+// *** OP_REPLY
+
+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
+}
+
View
@@ -17,37 +17,21 @@ type Cursor struct {
docs *vector.Vector
}
-func (self *Cursor) HasMore() bool {
- if self.pos < self.docs.Len() {
- return true
- }
-
- err := self.GetMore()
- if err != nil {
- return false
- }
- return self.pos < self.docs.Len()
-}
+// *** Client Request Messages
+// ***
-func (self *Cursor) GetNext() (BSON, os.Error) {
- if self.HasMore() {
- doc := self.docs.At(self.pos).(BSON)
- self.pos = self.pos + 1
- return doc, nil
- }
- return nil, os.NewError("cursor failure")
-}
+// *** OP_GET_MORE
func (self *Cursor) GetMore() os.Error {
if self.id == 0 {
return os.NewError("no cursorID")
}
- gm := &opGetMore{self.collection.fullName(), 0, self.id}
conn := self.collection.db.Conn
- err := conn.writeOp(gm)
- if err != nil {
+ msg := &opGetMore{self.collection.fullName(), 0, self.id}
+
+ if err := conn.writeOp(msg); err != nil {
return err
}
@@ -62,14 +46,41 @@ func (self *Cursor) GetMore() os.Error {
return nil
}
+// *** OP_KILL_CURSORS
+
func (self *Cursor) Close() os.Error {
if self.id == 0 {
// not open on server
return nil
}
- km := &opKillCursors{1, []int64{self.id}}
- conn := self.collection.db.Conn
- return conn.writeOp(km)
+ msg := &opKillCursors{1, []int64{self.id}}
+ return self.collection.db.Conn.writeOp(msg)
+}
+
+
+// **************
+
+
+func (self *Cursor) HasMore() bool {
+ if self.pos < self.docs.Len() {
+ return true
+ }
+
+ err := self.GetMore()
+ if err != nil {
+ return false
+ }
+
+ return self.pos < self.docs.Len()
+}
+
+func (self *Cursor) GetNext() (BSON, os.Error) {
+ if self.HasMore() {
+ doc := self.docs.At(self.pos).(BSON)
+ self.pos = self.pos + 1
+ return doc, nil
+ }
+ return nil, os.NewError("cursor failure")
}
Oops, something went wrong.

0 comments on commit 277e82a

Please sign in to comment.