Permalink
Browse files

using constants for op_update's flags

  • Loading branch information...
1 parent 2e44438 commit 7cb27ceeb7d63dd4d7ff2aec31b36f20cbc0564a John Mac committed with Jun 20, 2010
Showing with 70 additions and 30 deletions.
  1. +17 −6 mongo/collection.go
  2. +3 −3 mongo/main.go
  3. +50 −21 mongo/message.go
View
@@ -10,6 +10,17 @@ import (
)
+var fUpsert, fUpdateAll, fUpsertAll int32 // OP_UPDATE flags
+
+// Calculates values of flags
+func init() {
+ //fUpdate := _ZERO
+ setBit32(&fUpsert, f_UPSERT)
+ setBit32(&fUpdateAll, f_MULTI_UPDATE)
+ setBit32(&fUpsertAll, f_UPSERT, f_MULTI_UPDATE)
+}
+
+
type indexDesc struct {
Name string
Ns string
@@ -118,26 +129,26 @@ func (self *Collection) Count(query BSON) (int64, os.Error) {
return int64(reply.Get("n").Number()), nil
}
-func (self *Collection) update(um *opUpdate) os.Error {
+func (self *Collection) update(msg *opUpdate) os.Error {
conn := self.db.Conn
- return conn.writeOp(um)
+ return conn.writeOp(msg)
}
func (self *Collection) Update(selector, document BSON) os.Error {
- return self.update(&opUpdate{self.fullName(), 0, selector, document})
+ return self.update(&opUpdate{self.fullName(), _ZERO, selector, document})
}
func (self *Collection) Upsert(selector, document BSON) os.Error {
- return self.update(&opUpdate{self.fullName(), 1, selector, document})
+ return self.update(&opUpdate{self.fullName(), fUpsert, selector, document})
}
func (self *Collection) UpdateAll(selector, document BSON) os.Error {
- return self.update(&opUpdate{self.fullName(), 2, selector, document})
+ return self.update(&opUpdate{self.fullName(), fUpdateAll, selector, document})
}
func (self *Collection) UpsertAll(selector, document BSON) os.Error {
- return self.update(&opUpdate{self.fullName(), 3, selector, document})
+ return self.update(&opUpdate{self.fullName(), fUpsertAll, selector, document})
}
View
@@ -14,14 +14,14 @@ import (
// Like BSON documents, all data in the mongo wire protocol is little-endian.
var pack = binary.LittleEndian
-var lastRequestID int32
-
var (
- // To zero
+ // Initialized to zero.
_WORD32 = make([]byte, 4)
_WORD64 = make([]byte, 8)
)
+var lastRequestID int32
+
func init() {
// Uses the 'urandom' device to get a seed which will be used by 'rand'.
View
@@ -32,7 +32,7 @@ const (
)
const (
- _ZERO = 0
+ _ZERO = int32(0)
_HEADER_SIZE = 16 // 4 (fields) of int32 (4 bytes)
)
@@ -75,16 +75,24 @@ type message interface {
// *** OP_UPDATE
-/*const (
- Upsert
- MultiUpdate
-)*/
+// flags
+const (
+ // If set, the database will insert the supplied object into the collection
+ // if no matching document is found.
+ f_UPSERT = 0
+
+ // If set, the database will update all matching objects in the collection.
+ // Otherwise only updates first matching doc.
+ f_MULTI_UPDATE = 1
+
+ // 2-31 - Reserved - Must be set to 0.
+)
type opUpdate struct {
//header msgHeader // standard message header
- //_ZERO int32 // 0 - reserved for future use
+ //ZERO int32 // 0 - reserved for future use
fullCollectionName string // "dbname.collectionname"
- flags int32 // bit vector. see below
+ flags int32 // bit vector. See above
selector BSON // the query to select the document
update BSON // specification of the update to perform
}
@@ -93,7 +101,7 @@ func (self *opUpdate) OpCode() int32 { return _OP_UPDATE }
func (self *opUpdate) Bytes() []byte {
w32 := _WORD32
- buf := bytes.NewBuffer(w32) // _ZERO
+ buf := bytes.NewBuffer(w32) // ZERO
buf.WriteString(self.fullCollectionName)
buf.WriteByte(0)
@@ -111,15 +119,15 @@ func (self *opUpdate) Bytes() []byte {
type opInsert struct {
//header msgHeader // standard message header
- //_ZERO int32 // 0 - reserved for future use
+ //ZERO int32 // 0 - reserved for future use
fullCollectionName string // "dbname.collectionname"
documents BSON // one or more documents to insert into the collection
}
func (self *opInsert) OpCode() int32 { return _OP_INSERT }
func (self *opInsert) Bytes() []byte {
- buf := bytes.NewBuffer(_WORD32) // _ZERO
+ buf := bytes.NewBuffer(_WORD32) // ZERO
buf.WriteString(self.fullCollectionName)
buf.WriteByte(0)
@@ -131,14 +139,26 @@ func (self *opInsert) Bytes() []byte {
// *** OP_QUERY
+// opts
+const (
+ _QUERY_None = 0
+ _QUERY_TailableCursor = 2
+ _QUERY_SlaveOK = 4
+ //_QUERY_OplogReplay = 8 // drivers should not implement
+ _QUERY_NoCursorTimeout = 16
+)
+
+// query
+//Possible elements include $query, $orderby, $hint, $explain, and $snapshot
+
type opQuery struct {
- //header msgHeader // standard message header
- opts int32 // query options. See below for details.
+ //header msgHeader // standard message header
+ opts int32 // query options. See above for details.
fullCollectionName string // "dbname.collectionname"
numberToSkip int32 // number of documents to skip
numberToReturn int32 // number of documents to return in the first OP_REPLY batch
- query BSON // query object. See below for details.
- //returnFieldSelector BSON // Optional. Selector indicating the fields to return. See below for details.
+ query BSON // query object. See above for details.
+ //returnFieldSelector BSON // Optional. Selector indicating the fields to return.
}
func (self *opQuery) OpCode() int32 { return _OP_QUERY }
@@ -168,7 +188,7 @@ func (self *opQuery) Bytes() []byte {
type opGetMore struct {
//header msgHeader // standard message header
- //_ZERO int32 // 0 - reserved for future use
+ //ZERO int32 // 0 - reserved for future use
fullCollectionName string // "dbname.collectionname"
numberToReturn int32 // number of documents to return
cursorID int64 // cursorID from the OP_REPLY
@@ -179,7 +199,7 @@ func (self *opGetMore) OpCode() int32 { return _OP_GET_MORE }
func (self *opGetMore) Bytes() []byte {
w32 := _WORD32
w64 := _WORD64
- buf := bytes.NewBuffer(w32) // _ZERO
+ buf := bytes.NewBuffer(w32) // ZERO
buf.WriteString(self.fullCollectionName)
buf.WriteByte(0)
@@ -195,19 +215,28 @@ func (self *opGetMore) Bytes() []byte {
// *** OP_DELETE
+// flags
+const (
+ // If set, the database will remove only the first matching document in the
+ // collection. Otherwise all matching documents will be removed.
+ _DELETE_SingleRemove = 1
+
+ // 1-31 - Reserved - Must be set to 0.
+)
+
type opDelete struct {
//header msgHeader // standard message header
- //_ZERO int32 // 0 - reserved for future use
+ //ZERO int32 // 0 - reserved for future use
fullCollectionName string // "dbname.collectionname"
- //flags int32 // bit vector - see below for details.
+ //flags int32 // bit vector - see above for details.
selector BSON // query object. See below for details.
}
func (self *opDelete) OpCode() int32 { return _OP_DELETE }
func (self *opDelete) Bytes() []byte {
w32 := _WORD32
- buf := bytes.NewBuffer(w32) // _ZERO
+ buf := bytes.NewBuffer(w32) // ZERO
buf.WriteString(self.fullCollectionName)
buf.WriteByte(0)
@@ -223,7 +252,7 @@ func (self *opDelete) Bytes() []byte {
type opKillCursors struct {
//header msgHeader // standard message header
- //_ZERO int32 // 0 - reserved for future use
+ //ZERO int32 // 0 - reserved for future use
numberOfCursorIDs int32 // number of cursorIDs in message
cursorIDs []int64 // sequence of cursorIDs to close
}
@@ -233,7 +262,7 @@ func (self *opKillCursors) OpCode() int32 { return _OP_KILL_CURSORS }
func (self *opKillCursors) Bytes() []byte {
w32 := _WORD32
w64 := _WORD64
- buf := bytes.NewBuffer(w32) // _ZERO
+ buf := bytes.NewBuffer(w32) // ZERO
pack.PutUint32(w32, uint32(self.numberOfCursorIDs))
buf.Write(w32)

0 comments on commit 7cb27ce

Please sign in to comment.