Skip to content
Browse files

Fixed building of arraies.

  • Loading branch information...
1 parent d28e9e7 commit 13b98c55db7e9719ab7892f20320b16c1a12d226 John Mac committed with Jul 9, 2010
Showing with 20 additions and 22 deletions.
  1. +7 −7 mongo/bson.go
  2. +4 −4 mongo/main.go
  3. +9 −11 mongo/message.go
View
14 mongo/bson.go
@@ -88,7 +88,7 @@ func (self *_Number) Kind() int { return NumberKind }
func (self *_Number) Number() float64 { return self.value }
func (self *_Number) Bytes() []byte {
bits := math.Float64bits(self.value)
- w64 := _WORD64
+ w64 := make([]byte, _WORD64)
pack.PutUint64(w64, bits)
return w64
}
@@ -101,7 +101,7 @@ type _String struct {
func (self *_String) Kind() int { return StringKind }
func (self *_String) String() string { return self.value }
func (self *_String) Bytes() []byte {
- w32 := _WORD32
+ w32 := make([]byte, _WORD32)
l := len(self.value) + 1
pack.PutUint32(w32, uint32(l))
@@ -142,7 +142,7 @@ func (self *_Object) Bytes() []byte {
buf.WriteByte(0)
l := buf.Len() + 4
- w32 := _WORD32
+ w32 := make([]byte, _WORD32)
pack.PutUint32(w32, uint32(l))
return bytes.Add(w32, buf.Bytes())
}
@@ -180,7 +180,7 @@ func (self *_Array) Bytes() []byte {
buf.WriteByte(0)
l := buf.Len() + 4
- w32 := _WORD32
+ w32 := make([]byte, _WORD32)
pack.PutUint32(w32, uint32(l))
return bytes.Add(w32, buf.Bytes())
}
@@ -216,7 +216,7 @@ type _Date struct {
func (self *_Date) Kind() int { return DateKind }
func (self *_Date) Date() *time.Time { return self.value }
func (self *_Date) Bytes() []byte {
- w64 := _WORD64
+ w64 := make([]byte, _WORD64)
mtime := self.value.Seconds() * 1000
pack.PutUint64(w64, uint64(mtime))
return w64
@@ -245,7 +245,7 @@ type _Int struct {
func (self *_Int) Kind() int { return IntKind }
func (self *_Int) Int() int32 { return self.value }
func (self *_Int) Bytes() []byte {
- w32 := _WORD32
+ w32 := make([]byte, _WORD32)
pack.PutUint32(w32, uint32(self.value))
return w32
}
@@ -258,7 +258,7 @@ type _Long struct {
func (self *_Long) Kind() int { return LongKind }
func (self *_Long) Long() int64 { return self.value }
func (self *_Long) Bytes() []byte {
- w64 := _WORD64
+ w64 := make([]byte, _WORD64)
pack.PutUint64(w64, uint64(self.value))
return w64
}
View
8 mongo/main.go
@@ -15,18 +15,18 @@ import (
// Like BSON documents, all data in the mongo wire protocol is little-endian.
var pack = binary.LittleEndian
+// Words size in bytes.
var (
- // Initialized to zero.
- _WORD32 = make([]byte, 4)
- _WORD64 = make([]byte, 8)
+ _WORD32 = 4
+ _WORD64 = 8
)
var lastRequestID int32
func init() {
// Uses the 'urandom' device to get a seed which will be used by 'rand'.
- randombytes := make([]byte, 8)
+ randombytes := make([]byte, _WORD64)
if err := crand.ReadUrandom(randombytes); err != nil {
panic(err)
}
View
20 mongo/message.go
@@ -35,8 +35,6 @@ const (
_HEADER_SIZE = 16 // 4 (fields) of int32 (4 bytes)
)
-var _HEADER_WORD = make([]byte, _HEADER_SIZE)
-
// === Standard Message Header
// ===
@@ -49,7 +47,7 @@ type msgHeader struct {
}
func header(h msgHeader) []byte {
- w := _HEADER_WORD
+ w := make([]byte, _HEADER_SIZE)
pack.PutUint32(w[0:4], uint32(h.messageLength))
pack.PutUint32(w[4:8], uint32(h.requestID))
@@ -99,7 +97,7 @@ type opUpdate struct {
func (self *opUpdate) OpCode() int32 { return _OP_UPDATE }
func (self *opUpdate) Bytes() []byte {
- w32 := _WORD32
+ w32 := make([]byte, _WORD32)
buf := bytes.NewBuffer(w32) // ZERO
buf.WriteString(self.fullCollectionName)
@@ -126,7 +124,7 @@ type opInsert struct {
func (self *opInsert) OpCode() int32 { return _OP_INSERT }
func (self *opInsert) Bytes() []byte {
- buf := bytes.NewBuffer(_WORD32) // ZERO
+ buf := bytes.NewBuffer(make([]byte, _WORD32)) // ZERO
buf.WriteString(self.fullCollectionName)
buf.WriteByte(0)
@@ -164,7 +162,7 @@ func (self *opQuery) OpCode() int32 { return _OP_QUERY }
func (self *opQuery) Bytes() []byte {
var buf bytes.Buffer
- w32 := _WORD32
+ w32 := make([]byte, _WORD32)
pack.PutUint32(w32, uint32(self.opts))
buf.Write(w32)
@@ -196,8 +194,8 @@ type opGetMore struct {
func (self *opGetMore) OpCode() int32 { return _OP_GET_MORE }
func (self *opGetMore) Bytes() []byte {
- w32 := _WORD32
- w64 := _WORD64
+ w32 := make([]byte, _WORD32)
+ w64 := make([]byte, _WORD64)
buf := bytes.NewBuffer(w32) // ZERO
buf.WriteString(self.fullCollectionName)
@@ -234,7 +232,7 @@ type opDelete struct {
func (self *opDelete) OpCode() int32 { return _OP_DELETE }
func (self *opDelete) Bytes() []byte {
- w32 := _WORD32
+ w32 := make([]byte, _WORD32)
buf := bytes.NewBuffer(w32) // ZERO
buf.WriteString(self.fullCollectionName)
@@ -260,8 +258,8 @@ type opKillCursors struct {
func (self *opKillCursors) OpCode() int32 { return _OP_KILL_CURSORS }
func (self *opKillCursors) Bytes() []byte {
- w32 := _WORD32
- w64 := _WORD64
+ w32 := make([]byte, _WORD32)
+ w64 := make([]byte, _WORD64)
buf := bytes.NewBuffer(w32) // ZERO
pack.PutUint32(w32, uint32(self.numberOfCursorIDs))

0 comments on commit 13b98c5

Please sign in to comment.
Something went wrong with that request. Please try again.