Skip to content

Commit

Permalink
using constants for op_update's flags
Browse files Browse the repository at this point in the history
  • Loading branch information
John Mac authored and mikejs committed Jun 23, 2010
1 parent 2e44438 commit 7cb27ce
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 30 deletions.
23 changes: 17 additions & 6 deletions mongo/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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})
}


Expand Down
6 changes: 3 additions & 3 deletions mongo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand Down
71 changes: 50 additions & 21 deletions mongo/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const (
)

const (
_ZERO = 0
_ZERO = int32(0)
_HEADER_SIZE = 16 // 4 (fields) of int32 (4 bytes)
)

Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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 }
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
}
Expand All @@ -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)
Expand Down

0 comments on commit 7cb27ce

Please sign in to comment.