Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Uses a variable to simplify the use of 'binary.LittleEndian'

  • Loading branch information...
commit d864c741a1eb228f768d2c80269ceaa86175f9e7 1 parent b6f73d0
John Mac authored committed
Showing with 30 additions and 32 deletions.
  1. +12 −13 mongo/bson.go
  2. +18 −19 mongo/message.go
View
25 mongo/bson.go
@@ -16,7 +16,6 @@ import (
"time"
"bytes"
"strconv"
- "encoding/binary"
"container/vector"
)
@@ -90,7 +89,7 @@ func (self *_Number) Number() float64 { return self.value }
func (self *_Number) Bytes() []byte {
bits := math.Float64bits(self.value)
b := []byte{0, 0, 0, 0, 0, 0, 0, 0}
- binary.LittleEndian.PutUint64(b, bits)
+ pack.PutUint64(b, bits)
return b
}
@@ -104,7 +103,7 @@ func (self *_String) String() string { return self.value }
func (self *_String) Bytes() []byte {
b := []byte{0, 0, 0, 0}
l := len(self.value) + 1
- binary.LittleEndian.PutUint32(b, uint32(l))
+ pack.PutUint32(b, uint32(l))
buf := bytes.NewBuffer(b)
buf.WriteString(self.value)
@@ -144,7 +143,7 @@ func (self *_Object) Bytes() []byte {
l := buf.Len() + 4
b := []byte{0, 0, 0, 0}
- binary.LittleEndian.PutUint32(b, uint32(l))
+ pack.PutUint32(b, uint32(l))
return bytes.Add(b, buf.Bytes())
}
@@ -182,7 +181,7 @@ func (self *_Array) Bytes() []byte {
l := buf.Len() + 4
b := []byte{0, 0, 0, 0}
- binary.LittleEndian.PutUint32(b, uint32(l))
+ pack.PutUint32(b, uint32(l))
return bytes.Add(b, buf.Bytes())
}
@@ -219,7 +218,7 @@ func (self *_Date) Date() *time.Time { return self.value }
func (self *_Date) Bytes() []byte {
b := []byte{0, 0, 0, 0, 0, 0, 0, 0}
mtime := self.value.Seconds() * 1000
- binary.LittleEndian.PutUint64(b, uint64(mtime))
+ pack.PutUint64(b, uint64(mtime))
return b
}
@@ -247,7 +246,7 @@ func (self *_Int) Kind() int { return IntKind }
func (self *_Int) Int() int32 { return self.value }
func (self *_Int) Bytes() []byte {
b := []byte{0, 0, 0, 0}
- binary.LittleEndian.PutUint32(b, uint32(self.value))
+ pack.PutUint32(b, uint32(self.value))
return b
}
@@ -260,7 +259,7 @@ func (self *_Long) Kind() int { return LongKind }
func (self *_Long) Long() int64 { return self.value }
func (self *_Long) Bytes() []byte {
b := []byte{0, 0, 0, 0, 0, 0, 0, 0}
- binary.LittleEndian.PutUint64(b, uint64(self.value))
+ pack.PutUint64(b, uint64(self.value))
return b
}
@@ -450,12 +449,12 @@ func Parse(buf *bytes.Buffer, builder Builder) (err os.Error) {
case NumberKind:
lr := io.LimitReader(buf, 8)
bits, _ := ioutil.ReadAll(lr)
- ui64 := binary.LittleEndian.Uint64(bits)
+ ui64 := pack.Uint64(bits)
fl64 := math.Float64frombits(ui64)
b2.Float64(fl64)
case StringKind:
bits, _ := ioutil.ReadAll(io.LimitReader(buf, 4))
- l := binary.LittleEndian.Uint32(bits)
+ l := pack.Uint32(bits)
s, _ := ioutil.ReadAll(io.LimitReader(buf, int64(l-1)))
buf.ReadByte()
b2.String(string(s))
@@ -479,7 +478,7 @@ func Parse(buf *bytes.Buffer, builder Builder) (err os.Error) {
}
case DateKind:
bits, _ := ioutil.ReadAll(io.LimitReader(buf, 8))
- ui64 := binary.LittleEndian.Uint64(bits)
+ ui64 := pack.Uint64(bits)
b2.Date(time.SecondsToUTC(int64(ui64) / 1000))
case RegexKind:
regex := readCString(buf)
@@ -487,11 +486,11 @@ func Parse(buf *bytes.Buffer, builder Builder) (err os.Error) {
b2.Regex(regex, options)
case IntKind:
bits, _ := ioutil.ReadAll(io.LimitReader(buf, 4))
- ui32 := binary.LittleEndian.Uint32(bits)
+ ui32 := pack.Uint32(bits)
b2.Int32(int32(ui32))
case LongKind:
bits, _ := ioutil.ReadAll(io.LimitReader(buf, 8))
- ui64 := binary.LittleEndian.Uint64(bits)
+ ui64 := pack.Uint64(bits)
b2.Int64(int64(ui64))
default:
err = os.NewError(fmt.Sprintf("don't know how to handle kind %v yet", kind))
View
37 mongo/message.go
@@ -12,7 +12,6 @@ package mongo
import (
"bytes"
"container/vector"
- "encoding/binary"
"io"
"io/ioutil"
"os"
@@ -50,10 +49,10 @@ type msgHeader struct {
func header(h msgHeader) []byte {
b := make([]byte, 16)
- binary.LittleEndian.PutUint32(b[0:4], uint32(h.messageLength))
- binary.LittleEndian.PutUint32(b[4:8], uint32(h.requestID))
- binary.LittleEndian.PutUint32(b[8:12], uint32(h.responseTo))
- binary.LittleEndian.PutUint32(b[12:16], uint32(h.opCode))
+ pack.PutUint32(b[0:4], uint32(h.messageLength))
+ pack.PutUint32(b[4:8], uint32(h.requestID))
+ pack.PutUint32(b[8:12], uint32(h.responseTo))
+ pack.PutUint32(b[12:16], uint32(h.opCode))
return b
}
@@ -105,7 +104,7 @@ func (self *opUpdate) Bytes() []byte {
buf.WriteString(self.fullCollectionName)
buf.WriteByte(0)
- binary.LittleEndian.PutUint32(b, uint32(self.flags))
+ pack.PutUint32(b, uint32(self.flags))
buf.Write(b)
buf.Write(self.selector.Bytes())
@@ -171,16 +170,16 @@ func (self *opQuery) Bytes() []byte {
var buf bytes.Buffer
b := make([]byte, 4)
- binary.LittleEndian.PutUint32(b, uint32(self.opts))
+ pack.PutUint32(b, uint32(self.opts))
buf.Write(b)
buf.WriteString(self.fullCollectionName)
buf.WriteByte(0)
- binary.LittleEndian.PutUint32(b, uint32(self.numberToSkip))
+ pack.PutUint32(b, uint32(self.numberToSkip))
buf.Write(b)
- binary.LittleEndian.PutUint32(b, uint32(self.numberToReturn))
+ pack.PutUint32(b, uint32(self.numberToReturn))
buf.Write(b)
buf.Write(self.query.Bytes())
@@ -215,11 +214,11 @@ func (self *opGetMore) Bytes() []byte {
buf.WriteString(self.fullCollectionName)
buf.WriteByte(0)
- binary.LittleEndian.PutUint32(b, uint32(self.numberToReturn))
+ pack.PutUint32(b, uint32(self.numberToReturn))
buf.Write(b)
b = make([]byte, 8)
- binary.LittleEndian.PutUint64(b, uint64(self.cursorID))
+ pack.PutUint64(b, uint64(self.cursorID))
buf.Write(b)
return buf.Bytes()
@@ -280,12 +279,12 @@ func (self *opKillCursors) Bytes() []byte {
b := make([]byte, 4)
buf := bytes.NewBuffer(b) // _ZERO
- binary.LittleEndian.PutUint32(b, uint32(self.numberOfCursorIDs))
+ pack.PutUint32(b, uint32(self.numberOfCursorIDs))
buf.Write(b)
b = make([]byte, 8)
for _, id := range self.cursorIDs {
- binary.LittleEndian.PutUint64(b, uint64(id))
+ pack.PutUint64(b, uint64(id))
buf.Write(b)
}
@@ -318,7 +317,7 @@ type opReply struct {
func (self *Connection) readReply() (*opReply, os.Error) {
size_bits, _ := ioutil.ReadAll(io.LimitReader(self.conn, 4))
- size := binary.LittleEndian.Uint32(size_bits)
+ size := pack.Uint32(size_bits)
rest, _ := ioutil.ReadAll(io.LimitReader(self.conn, int64(size)-4))
reply := parseReply(rest)
return reply, nil
@@ -326,11 +325,11 @@ func (self *Connection) readReply() (*opReply, os.Error) {
func parseReply(b []byte) *opReply {
r := new(opReply)
- r.responseTo = int32(binary.LittleEndian.Uint32(b[4:8]))
- r.responseFlag = int32(binary.LittleEndian.Uint32(b[12:16]))
- r.cursorID = int64(binary.LittleEndian.Uint64(b[16:24]))
- r.startingFrom = int32(binary.LittleEndian.Uint32(b[24:28]))
- r.numberReturned = int32(binary.LittleEndian.Uint32(b[28:32]))
+ 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.docs = new(vector.Vector)
if r.numberReturned > 0 {
Please sign in to comment.
Something went wrong with that request. Please try again.