Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Using a same value (self) in all methods since it's more consistent.

  • Loading branch information...
commit d858c9c5c0162a4539f8e4eab2484ef20e1f31a1 1 parent 50096ab
John Mac authored committed
View
2  mongo/all_test.go
@@ -1,4 +1,4 @@
-// Copyright 2009,2010, the 'gomongo' Authors. All rights reserved.
+// Copyright 2009,2010 The 'gomongo' Authors. All rights reserved.
// Use of this source code is governed by the New BSD License
// that can be found in the LICENSE file.
View
103 mongo/bson-struct.go
@@ -1,8 +1,9 @@
// Based on the Go json package.
-// Original Copyright 2009 The Go Authors. All rights reserved.
-// Modifications Copyright 2009 Michael Stephens.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE and LICENSE.GO files.
+
+// Copyright 2009 The Go Authors. All rights reserved.
+// Copyright 2009,2010 The 'gomongo' Authors. All rights reserved.
+// Use of this source code is governed by the New BSD License
+// that can be found in the LICENSE and LICENSE.GO files.
package mongo
@@ -70,22 +71,22 @@ func setint(v reflect.Value, i int64) {
}
}
-// If updating b.val is not enough to update the original,
-// copy a changed b.val out to the original.
-func (b *structBuilder) Flush() {
- if b == nil {
+// If updating self.val is not enough to update the original,
+// copy a changed self.val out to the original.
+func (self *structBuilder) Flush() {
+ if self == nil {
return
}
- if b.map_ != nil {
- b.map_.SetElem(b.key, b.val)
+ if self.map_ != nil {
+ self.map_.SetElem(self.key, self.val)
}
}
-func (b *structBuilder) Int64(i int64) {
- if b == nil {
+func (self *structBuilder) Int64(i int64) {
+ if self == nil {
return
}
- v := b.val
+ v := self.val
if isfloat(v) {
setfloat(v, float64(i))
} else {
@@ -93,20 +94,20 @@ func (b *structBuilder) Int64(i int64) {
}
}
-func (b *structBuilder) Date(t *time.Time) {
- if b == nil {
+func (self *structBuilder) Date(t *time.Time) {
+ if self == nil {
return
}
- if v, ok := b.val.(*reflect.PtrValue); ok {
+ if v, ok := self.val.(*reflect.PtrValue); ok {
v.PointTo(reflect.Indirect(reflect.NewValue(t)))
}
}
-func (b *structBuilder) Int32(i int32) {
- if b == nil {
+func (self *structBuilder) Int32(i int32) {
+ if self == nil {
return
}
- v := b.val
+ v := self.val
if isfloat(v) {
setfloat(v, float64(i))
} else {
@@ -114,11 +115,11 @@ func (b *structBuilder) Int32(i int32) {
}
}
-func (b *structBuilder) Float64(f float64) {
- if b == nil {
+func (self *structBuilder) Float64(f float64) {
+ if self == nil {
return
}
- v := b.val
+ v := self.val
if isfloat(v) {
setfloat(v, f)
} else {
@@ -126,41 +127,41 @@ func (b *structBuilder) Float64(f float64) {
}
}
-func (b *structBuilder) Null() {}
+func (self *structBuilder) Null() {}
-func (b *structBuilder) String(s string) {
- if b == nil {
+func (self *structBuilder) String(s string) {
+ if self == nil {
return
}
- if v, ok := b.val.(*reflect.StringValue); ok {
+ if v, ok := self.val.(*reflect.StringValue); ok {
v.Set(s)
}
}
-func (b *structBuilder) Regex(regex, options string) {
+func (self *structBuilder) Regex(regex, options string) {
// Ignore options for now...
- if b == nil {
+ if self == nil {
return
}
- if v, ok := b.val.(*reflect.StringValue); ok {
+ if v, ok := self.val.(*reflect.StringValue); ok {
v.Set(regex)
}
}
-func (b *structBuilder) Bool(tf bool) {
- if b == nil {
+func (self *structBuilder) Bool(tf bool) {
+ if self == nil {
return
}
- if v, ok := b.val.(*reflect.BoolValue); ok {
+ if v, ok := self.val.(*reflect.BoolValue); ok {
v.Set(tf)
}
}
-func (b *structBuilder) OID(oid []byte) {
- if b == nil {
+func (self *structBuilder) OID(oid []byte) {
+ if self == nil {
return
}
- if v, ok := b.val.(*reflect.SliceValue); ok {
+ if v, ok := self.val.(*reflect.SliceValue); ok {
if v.Cap() < 12 {
nv := reflect.MakeSlice(v.Type().(*reflect.SliceType), 12, 12)
v.Set(nv)
@@ -171,22 +172,22 @@ func (b *structBuilder) OID(oid []byte) {
}
}
-func (b *structBuilder) Array() {
- if b == nil {
+func (self *structBuilder) Array() {
+ if self == nil {
return
}
- if v, ok := b.val.(*reflect.SliceValue); ok {
+ if v, ok := self.val.(*reflect.SliceValue); ok {
if v.IsNil() {
v.Set(reflect.MakeSlice(v.Type().(*reflect.SliceType), 0, 8))
}
}
}
-func (b *structBuilder) Elem(i int) Builder {
- if b == nil || i < 0 {
+func (self *structBuilder) Elem(i int) Builder {
+ if self == nil || i < 0 {
return nobuilder
}
- switch v := b.val.(type) {
+ switch v := self.val.(type) {
case *reflect.ArrayValue:
if i < v.Len() {
return &structBuilder{val: v.Elem(i)}
@@ -214,28 +215,28 @@ func (b *structBuilder) Elem(i int) Builder {
return nobuilder
}
-func (b *structBuilder) Object() {
- if b == nil {
+func (self *structBuilder) Object() {
+ if self == nil {
return
}
- if v, ok := b.val.(*reflect.PtrValue); ok && v.IsNil() {
+ if v, ok := self.val.(*reflect.PtrValue); ok && v.IsNil() {
if v.IsNil() {
v.PointTo(reflect.MakeZero(v.Type().(*reflect.PtrType).Elem()))
- b.Flush()
+ self.Flush()
}
- b.map_ = nil
- b.val = v.Elem()
+ self.map_ = nil
+ self.val = v.Elem()
}
- if v, ok := b.val.(*reflect.MapValue); ok && v.IsNil() {
+ if v, ok := self.val.(*reflect.MapValue); ok && v.IsNil() {
v.Set(reflect.MakeMap(v.Type().(*reflect.MapType)))
}
}
-func (b *structBuilder) Key(k string) Builder {
- if b == nil {
+func (self *structBuilder) Key(k string) Builder {
+ if self == nil {
return nobuilder
}
- switch v := reflect.Indirect(b.val).(type) {
+ switch v := reflect.Indirect(self.val).(type) {
case *reflect.StructValue:
t := v.Type().(*reflect.StructType)
// Case-insensitive field lookup.
View
171 mongo/bson.go
@@ -1,8 +1,9 @@
// Based on the Go json package.
-// Original Copyright 2009 The Go Authors. All rights reserved.
-// Modifications Copyright 2009 Michael Stephens.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE and LICENSE.GO files.
+
+// Copyright 2009 The Go Authors. All rights reserved.
+// Copyright 2009,2010 The 'gomongo' Authors. All rights reserved.
+// Use of this source code is governed by the New BSD License
+// that can be found in the LICENSE and LICENSE.GO files.
package mongo
@@ -84,10 +85,10 @@ type _Number struct {
_Null
}
-func (n *_Number) Kind() int { return NumberKind }
-func (n *_Number) Number() float64 { return n.value }
-func (n *_Number) Bytes() []byte {
- bits := math.Float64bits(n.value)
+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)
b := []byte{0, 0, 0, 0, 0, 0, 0, 0}
binary.LittleEndian.PutUint64(b, bits)
return b
@@ -98,15 +99,15 @@ type _String struct {
_Null
}
-func (s *_String) Kind() int { return StringKind }
-func (s *_String) String() string { return s.value }
-func (s *_String) Bytes() []byte {
+func (self *_String) Kind() int { return StringKind }
+func (self *_String) String() string { return self.value }
+func (self *_String) Bytes() []byte {
b := []byte{0, 0, 0, 0}
- l := len(s.value) + 1
+ l := len(self.value) + 1
binary.LittleEndian.PutUint32(b, uint32(l))
buf := bytes.NewBuffer(b)
- buf.WriteString(s.value)
+ buf.WriteString(self.value)
buf.WriteByte(0)
return buf.Bytes()
@@ -117,23 +118,23 @@ type _Object struct {
_Null
}
-func (o *_Object) Kind() int { return ObjectKind }
-func (o *_Object) Get(s string) BSON {
- if o.value == nil {
+func (self *_Object) Kind() int { return ObjectKind }
+func (self *_Object) Get(s string) BSON {
+ if self.value == nil {
return Null
}
- b, ok := o.value[s]
+ b, ok := self.value[s]
if !ok {
return Null
}
return b
}
-func (o *_Object) Len() int { return len(o.value) }
-func (o *_Object) Bytes() []byte {
+func (self *_Object) Len() int { return len(self.value) }
+func (self *_Object) Bytes() []byte {
buf := bytes.NewBuffer([]byte{})
- for k, v := range o.value {
+ for k, v := range self.value {
buf.WriteByte(byte(v.Kind()))
buf.WriteString(k)
buf.WriteByte(0)
@@ -154,24 +155,24 @@ type _Array struct {
_Null
}
-func (a *_Array) Kind() int { return ArrayKind }
-func (a *_Array) Elem(i int) BSON {
- if a.value == nil {
+func (self *_Array) Kind() int { return ArrayKind }
+func (self *_Array) Elem(i int) BSON {
+ if self.value == nil {
return Null
}
- if a.Len() < i {
+ if self.Len() < i {
return Null
}
- return a.value.At(i).(BSON)
+ return self.value.At(i).(BSON)
}
-func (a *_Array) Len() int { return a.value.Len() }
-func (a *_Array) Bytes() []byte {
+func (self *_Array) Len() int { return self.value.Len() }
+func (self *_Array) Bytes() []byte {
buf := bytes.NewBuffer([]byte{})
- for i := 0; i < a.value.Len(); i++ {
- v := a.value.At(i).(BSON)
+ for i := 0; i < self.value.Len(); i++ {
+ v := self.value.At(i).(BSON)
buf.WriteByte(byte(v.Kind()))
buf.WriteString(strconv.Itoa(i))
buf.WriteByte(0)
@@ -190,19 +191,19 @@ type _OID struct {
_Null
}
-func (o *_OID) Kind() int { return OIDKind }
-func (o *_OID) OID() []byte { return o.value }
-func (o *_OID) Bytes() []byte { return o.value }
+func (self *_OID) Kind() int { return OIDKind }
+func (self *_OID) OID() []byte { return self.value }
+func (self *_OID) Bytes() []byte { return self.value }
type _Boolean struct {
value bool
_Null
}
-func (b *_Boolean) Kind() int { return BooleanKind }
-func (b *_Boolean) Bool() bool { return b.value }
-func (b *_Boolean) Bytes() []byte {
- if b.value {
+func (self *_Boolean) Kind() int { return BooleanKind }
+func (self *_Boolean) Bool() bool { return self.value }
+func (self *_Boolean) Bytes() []byte {
+ if self.value {
return []byte{1}
}
return []byte{0}
@@ -213,11 +214,11 @@ type _Date struct {
_Null
}
-func (d *_Date) Kind() int { return DateKind }
-func (d *_Date) Date() *time.Time { return d.value }
-func (d *_Date) Bytes() []byte {
+func (self *_Date) Kind() int { return DateKind }
+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 := d.value.Seconds() * 1000
+ mtime := self.value.Seconds() * 1000
binary.LittleEndian.PutUint64(b, uint64(mtime))
return b
}
@@ -227,12 +228,12 @@ type _Regex struct {
_Null
}
-func (r *_Regex) Kind() int { return RegexKind }
-func (r *_Regex) Regex() (string, string) { return r.regex, r.options }
-func (r *_Regex) Bytes() []byte {
- buf := bytes.NewBufferString(r.regex)
+func (self *_Regex) Kind() int { return RegexKind }
+func (self *_Regex) Regex() (string, string) { return self.regex, self.options }
+func (self *_Regex) Bytes() []byte {
+ buf := bytes.NewBufferString(self.regex)
buf.WriteByte(0)
- buf.WriteString(r.options)
+ buf.WriteString(self.options)
buf.WriteByte(0)
return buf.Bytes()
}
@@ -242,11 +243,11 @@ type _Int struct {
_Null
}
-func (i *_Int) Kind() int { return IntKind }
-func (i *_Int) Int() int32 { return i.value }
-func (i *_Int) Bytes() []byte {
+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(i.value))
+ binary.LittleEndian.PutUint32(b, uint32(self.value))
return b
}
@@ -255,11 +256,11 @@ type _Long struct {
_Null
}
-func (l *_Long) Kind() int { return LongKind }
-func (l *_Long) Long() int64 { return l.value }
-func (l *_Long) Bytes() []byte {
+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(l.value))
+ binary.LittleEndian.PutUint64(b, uint64(self.value))
return b
}
@@ -325,7 +326,7 @@ type Builder interface {
Float64(f float64)
String(s string)
Bool(b bool)
- Date(d *time.Time)
+ Date(self *time.Time)
OID(o []byte)
Regex(regex, options string)
Null()
@@ -350,47 +351,47 @@ type _BSONBuilder struct {
key string
}
-func (bb *_BSONBuilder) Put(b BSON) {
+func (self *_BSONBuilder) Put(b BSON) {
switch {
- case bb.ptr != nil:
- *bb.ptr = b
- case bb.arr != nil:
- bb.arr.Set(bb.elem, b)
- case bb.obj != nil:
- bb.obj[bb.key] = b
+ case self.ptr != nil:
+ *self.ptr = b
+ case self.arr != nil:
+ self.arr.Set(self.elem, b)
+ case self.obj != nil:
+ self.obj[self.key] = b
}
}
-func (bb *_BSONBuilder) Get() BSON {
+func (self *_BSONBuilder) Get() BSON {
switch {
- case bb.ptr != nil:
- return *bb.ptr
- case bb.arr != nil:
- return bb.arr.At(bb.elem).(BSON)
- case bb.obj != nil:
- return bb.obj[bb.key]
+ case self.ptr != nil:
+ return *self.ptr
+ case self.arr != nil:
+ return self.arr.At(self.elem).(BSON)
+ case self.obj != nil:
+ return self.obj[self.key]
}
return nil
}
-func (bb *_BSONBuilder) Float64(f float64) { bb.Put(&_Number{f, _Null{}}) }
-func (bb *_BSONBuilder) String(s string) { bb.Put(&_String{s, _Null{}}) }
-func (bb *_BSONBuilder) Object() { bb.Put(&_Object{make(map[string]BSON), _Null{}}) }
-func (bb *_BSONBuilder) Array() { bb.Put(&_Array{new(vector.Vector), _Null{}}) }
-func (bb *_BSONBuilder) Bool(b bool) { bb.Put(&_Boolean{b, _Null{}}) }
-func (bb *_BSONBuilder) Date(t *time.Time) { bb.Put(&_Date{t, _Null{}}) }
-func (bb *_BSONBuilder) Null() { bb.Put(Null) }
-func (bb *_BSONBuilder) Regex(regex, options string) {
- bb.Put(&_Regex{regex, options, _Null{}})
+func (self *_BSONBuilder) Float64(f float64) { self.Put(&_Number{f, _Null{}}) }
+func (self *_BSONBuilder) String(s string) { self.Put(&_String{s, _Null{}}) }
+func (self *_BSONBuilder) Object() { self.Put(&_Object{make(map[string]BSON), _Null{}}) }
+func (self *_BSONBuilder) Array() { self.Put(&_Array{new(vector.Vector), _Null{}}) }
+func (self *_BSONBuilder) Bool(b bool) { self.Put(&_Boolean{b, _Null{}}) }
+func (self *_BSONBuilder) Date(t *time.Time) { self.Put(&_Date{t, _Null{}}) }
+func (self *_BSONBuilder) Null() { self.Put(Null) }
+func (self *_BSONBuilder) Regex(regex, options string) {
+ self.Put(&_Regex{regex, options, _Null{}})
}
-func (bb *_BSONBuilder) Int32(i int32) { bb.Put(&_Int{i, _Null{}}) }
-func (bb *_BSONBuilder) Int64(i int64) { bb.Put(&_Long{i, _Null{}}) }
-func (bb *_BSONBuilder) OID(o []byte) { bb.Put(&_OID{o, _Null{}}) }
+func (self *_BSONBuilder) Int32(i int32) { self.Put(&_Int{i, _Null{}}) }
+func (self *_BSONBuilder) Int64(i int64) { self.Put(&_Long{i, _Null{}}) }
+func (self *_BSONBuilder) OID(o []byte) { self.Put(&_OID{o, _Null{}}) }
-func (bb *_BSONBuilder) Key(key string) Builder {
+func (self *_BSONBuilder) Key(key string) Builder {
bb2 := new(_BSONBuilder)
- switch obj := bb.Get().(type) {
+ switch obj := self.Get().(type) {
case *_Object:
bb2.obj = obj.value
bb2.key = key
@@ -406,9 +407,9 @@ func (bb *_BSONBuilder) Key(key string) Builder {
return bb2
}
-func (bb *_BSONBuilder) Elem(i int) Builder {
+func (self *_BSONBuilder) Elem(i int) Builder {
bb2 := new(_BSONBuilder)
- bb2.arr = bb.Get().(*_Array).value
+ bb2.arr = self.Get().(*_Array).value
bb2.elem = i
for i >= bb2.arr.Len() {
bb2.arr.Push(Null)
@@ -416,7 +417,7 @@ func (bb *_BSONBuilder) Elem(i int) Builder {
return bb2
}
-func (bb *_BSONBuilder) Flush() {}
+func (self *_BSONBuilder) Flush() {}
func BytesToBSON(b []byte) (BSON, os.Error) {
var bson BSON
View
2  mongo/bson_test.go
@@ -1,4 +1,4 @@
-// Copyright 2009,2010, the 'gomongo' Authors. All rights reserved.
+// Copyright 2009,2010 The 'gomongo' Authors. All rights reserved.
// Use of this source code is governed by the New BSD License
// that can be found in the LICENSE file.
View
82 mongo/collection.go
@@ -1,4 +1,4 @@
-// Copyright 2009,2010, the 'gomongo' Authors. All rights reserved.
+// Copyright 2009,2010 The 'gomongo' Authors. All rights reserved.
// Use of this source code is governed by the New BSD License
// that can be found in the LICENSE file.
@@ -22,15 +22,11 @@ type Collection struct {
name string
}
-func (db *Database) GetCollection(name string) *Collection {
- return &Collection{db, name}
-}
-
-func (c *Collection) fullName() string { return c.db.name + "." + c.name }
+func (self *Collection) fullName() string { return self.db.name + "." + self.name }
-func (c *Collection) EnsureIndex(name string, index map[string]int) os.Error {
- coll := c.db.GetCollection("system.indexes")
- id := &indexDesc{name, c.fullName(), index}
+func (self *Collection) EnsureIndex(name string, index map[string]int) os.Error {
+ coll := self.db.GetCollection("system.indexes")
+ id := &indexDesc{name, self.fullName(), index}
desc, err := Marshal(id)
if err != nil {
return err
@@ -38,44 +34,44 @@ func (c *Collection) EnsureIndex(name string, index map[string]int) os.Error {
return coll.Insert(desc)
}
-func (c *Collection) DropIndexes() os.Error { return c.DropIndex("*") }
+func (self *Collection) DropIndexes() os.Error { return self.DropIndex("*") }
-func (c *Collection) DropIndex(name string) os.Error {
- cmdm := map[string]string{"deleteIndexes": c.fullName(), "index": name}
+func (self *Collection) DropIndex(name string) os.Error {
+ cmdm := map[string]string{"deleteIndexes": self.fullName(), "index": name}
cmd, err := Marshal(cmdm)
if err != nil {
return err
}
- _, err = c.db.Command(cmd)
+ _, err = self.db.Command(cmd)
return err
}
-func (c *Collection) Drop() os.Error {
- cmdm := map[string]string{"drop": c.fullName()}
+func (self *Collection) Drop() os.Error {
+ cmdm := map[string]string{"drop": self.fullName()}
cmd, err := Marshal(cmdm)
if err != nil {
return err
}
- _, err = c.db.Command(cmd)
+ _, err = self.db.Command(cmd)
return err
}
-func (c *Collection) Insert(doc BSON) os.Error {
- im := &insertMsg{c.fullName(), doc, rand.Int31()}
- return c.db.Conn.writeMessage(im)
+func (self *Collection) Insert(doc BSON) os.Error {
+ im := &insertMsg{self.fullName(), doc, rand.Int31()}
+ return self.db.Conn.writeMessage(im)
}
-func (c *Collection) Remove(selector BSON) os.Error {
- dm := &deleteMsg{c.fullName(), selector, rand.Int31()}
- return c.db.Conn.writeMessage(dm)
+func (self *Collection) Remove(selector BSON) os.Error {
+ dm := &deleteMsg{self.fullName(), selector, rand.Int31()}
+ return self.db.Conn.writeMessage(dm)
}
-func (coll *Collection) Query(query BSON, skip, limit int) (*Cursor, os.Error) {
+func (self *Collection) Query(query BSON, skip, limit int) (*Cursor, os.Error) {
req_id := rand.Int31()
- conn := coll.db.Conn
- qm := &queryMsg{0, coll.fullName(), int32(skip), int32(limit), query, req_id}
+ conn := self.db.Conn
+ qm := &queryMsg{0, self.fullName(), int32(skip), int32(limit), query, req_id}
err := conn.writeMessage(qm)
if err != nil {
@@ -90,31 +86,31 @@ func (coll *Collection) Query(query BSON, skip, limit int) (*Cursor, os.Error) {
return nil, os.NewError("wrong responseTo code")
}
- return &Cursor{coll, reply.cursorID, 0, reply.docs}, nil
+ return &Cursor{self, reply.cursorID, 0, reply.docs}, nil
}
-func (coll *Collection) FindAll(query BSON) (*Cursor, os.Error) {
- return coll.Query(query, 0, 0)
+func (self *Collection) FindAll(query BSON) (*Cursor, os.Error) {
+ return self.Query(query, 0, 0)
}
-func (coll *Collection) FindOne(query BSON) (BSON, os.Error) {
- cursor, err := coll.Query(query, 0, 1)
+func (self *Collection) FindOne(query BSON) (BSON, os.Error) {
+ cursor, err := self.Query(query, 0, 1)
if err != nil {
return nil, err
}
return cursor.GetNext()
}
-func (coll *Collection) Count(query BSON) (int64, os.Error) {
+func (self *Collection) Count(query BSON) (int64, os.Error) {
cmd := &_Object{
map[string]BSON{
- "count": &_String{coll.name, _Null{}},
+ "count": &_String{self.name, _Null{}},
"query": query,
},
_Null{},
}
- reply, err := coll.db.Command(cmd)
+ reply, err := self.db.Command(cmd)
if err != nil {
return -1, err
}
@@ -122,25 +118,25 @@ func (coll *Collection) Count(query BSON) (int64, os.Error) {
return int64(reply.Get("n").Number()), nil
}
-func (coll *Collection) update(um *updateMsg) os.Error {
+func (self *Collection) update(um *updateMsg) os.Error {
um.requestID = rand.Int31()
- conn := coll.db.Conn
+ conn := self.db.Conn
return conn.writeMessage(um)
}
-func (coll *Collection) Update(selector, document BSON) os.Error {
- return coll.update(&updateMsg{coll.fullName(), 0, selector, document, 0})
+func (self *Collection) Update(selector, document BSON) os.Error {
+ return self.update(&updateMsg{self.fullName(), 0, selector, document, 0})
}
-func (coll *Collection) Upsert(selector, document BSON) os.Error {
- return coll.update(&updateMsg{coll.fullName(), 1, selector, document, 0})
+func (self *Collection) Upsert(selector, document BSON) os.Error {
+ return self.update(&updateMsg{self.fullName(), 1, selector, document, 0})
}
-func (coll *Collection) UpdateAll(selector, document BSON) os.Error {
- return coll.update(&updateMsg{coll.fullName(), 2, selector, document, 0})
+func (self *Collection) UpdateAll(selector, document BSON) os.Error {
+ return self.update(&updateMsg{self.fullName(), 2, selector, document, 0})
}
-func (coll *Collection) UpsertAll(selector, document BSON) os.Error {
- return coll.update(&updateMsg{coll.fullName(), 3, selector, document, 0})
+func (self *Collection) UpsertAll(selector, document BSON) os.Error {
+ return self.update(&updateMsg{self.fullName(), 3, selector, document, 0})
}
View
12 mongo/connection.go
@@ -1,4 +1,4 @@
-// Copyright 2009,2010, the 'gomongo' Authors. All rights reserved.
+// Copyright 2009,2010 The 'gomongo' Authors. All rights reserved.
// Use of this source code is governed by the New BSD License
// that can be found in the LICENSE file.
@@ -54,14 +54,18 @@ func (self *Connection) Disconnect() os.Error {
return nil
}
-func (c *Connection) writeMessage(m message) os.Error {
+func (self *Connection) writeMessage(m message) os.Error {
body := m.Bytes()
- hb := header(msgHeader{int32(len(body)+16), m.RequestID(), 0, m.OpCode()})
+ hb := header(msgHeader{int32(len(body) + 16), m.RequestID(), 0, m.OpCode()})
msg := bytes.Add(hb, body)
- _, err := c.conn.Write(msg)
+ _, err := self.conn.Write(msg)
last_req = m.RequestID()
return err
}
+func (self *Connection) GetDB(name string) *Database {
+ return &Database{self, name}
+}
+
View
38 mongo/cursor.go
@@ -1,4 +1,4 @@
-// Copyright 2009,2010, the 'gomongo' Authors. All rights reserved.
+// Copyright 2009,2010 The 'gomongo' Authors. All rights reserved.
// Use of this source code is governed by the New BSD License
// that can be found in the LICENSE file.
@@ -18,35 +18,35 @@ type Cursor struct {
docs *vector.Vector
}
-func (c *Cursor) HasMore() bool {
- if c.pos < c.docs.Len() {
+func (self *Cursor) HasMore() bool {
+ if self.pos < self.docs.Len() {
return true
}
- err := c.GetMore()
+ err := self.GetMore()
if err != nil {
return false
}
- return c.pos < c.docs.Len()
+ return self.pos < self.docs.Len()
}
-func (c *Cursor) GetNext() (BSON, os.Error) {
- if c.HasMore() {
- doc := c.docs.At(c.pos).(BSON)
- c.pos = c.pos + 1
+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")
}
-func (c *Cursor) GetMore() os.Error {
- if c.id == 0 {
+func (self *Cursor) GetMore() os.Error {
+ if self.id == 0 {
return os.NewError("no cursorID")
}
- gm := &getMoreMsg{c.collection.fullName(), 0, c.id, rand.Int31()}
- conn := c.collection.db.Conn
+ gm := &getMoreMsg{self.collection.fullName(), 0, self.id, rand.Int31()}
+ conn := self.collection.db.Conn
err := conn.writeMessage(gm)
if err != nil {
return err
@@ -57,21 +57,21 @@ func (c *Cursor) GetMore() os.Error {
return err
}
- c.pos = 0
- c.docs = reply.docs
+ self.pos = 0
+ self.docs = reply.docs
return nil
}
-func (c *Cursor) Close() os.Error {
- if c.id == 0 {
+func (self *Cursor) Close() os.Error {
+ if self.id == 0 {
// not open on server
return nil
}
req_id := rand.Int31()
- km := &killMsg{1, []int64{c.id}, req_id}
- conn := c.collection.db.Conn
+ km := &killMsg{1, []int64{self.id}, req_id}
+ conn := self.collection.db.Conn
return conn.writeMessage(km)
}
View
20 mongo/database.go
@@ -1,4 +1,4 @@
-// Copyright 2009,2010, the 'gomongo' Authors. All rights reserved.
+// Copyright 2009,2010 The 'gomongo' Authors. All rights reserved.
// Use of this source code is governed by the New BSD License
// that can be found in the LICENSE file.
@@ -15,21 +15,21 @@ type Database struct {
name string
}
-func (c *Connection) GetDB(name string) *Database {
- return &Database{c, name}
+func (self *Database) GetCollection(name string) *Collection {
+ return &Collection{self, name}
}
-func (db *Database) Drop() os.Error {
+func (self *Database) Drop() os.Error {
cmd, err := Marshal(map[string]int{"dropDatabase": 1})
if err != nil {
return err
}
- _, err = db.Command(cmd)
+ _, err = self.Command(cmd)
return err
}
-func (db *Database) Repair(preserveClonedFilesOnFailure, backupOriginalFiles bool) os.Error {
+func (self *Database) Repair(preserveClonedFilesOnFailure, backupOriginalFiles bool) os.Error {
cmd := &_Object{
map[string]BSON{
"repairDatabase": &_Number{1, _Null{}},
@@ -39,16 +39,16 @@ func (db *Database) Repair(preserveClonedFilesOnFailure, backupOriginalFiles boo
_Null{},
}
- _, err := db.Command(cmd)
+ _, err := self.Command(cmd)
return err
}
-func (db *Database) Command(cmd BSON) (BSON, os.Error) {
- coll := db.GetCollection("$cmd")
+func (self *Database) Command(cmd BSON) (BSON, os.Error) {
+ coll := self.GetCollection("$cmd")
return coll.FindOne(cmd)
}
-func (db *Database) GetCollectionNames() *vector.StringVector {
+func (self *Database) GetCollectionNames() *vector.StringVector {
return new(vector.StringVector)
}
View
80 mongo/message.go
@@ -1,4 +1,4 @@
-// Copyright 2009,2010, the 'gomongo' Authors. All rights reserved.
+// Copyright 2009,2010 The 'gomongo' Authors. All rights reserved.
// Use of this source code is governed by the New BSD License
// that can be found in the LICENSE file.
@@ -76,16 +76,16 @@ type deleteMsg struct {
requestID int32
}
-func (d *deleteMsg) OpCode() int32 { return _OP_DELETE }
-func (d *deleteMsg) RequestID() int32 { return d.requestID }
+func (self *deleteMsg) OpCode() int32 { return _OP_DELETE }
+func (self *deleteMsg) RequestID() int32 { return self.requestID }
-func (d *deleteMsg) Bytes() []byte {
+func (self *deleteMsg) Bytes() []byte {
zero := make([]byte, 4)
buf := bytes.NewBuffer(zero)
- buf.WriteString(d.fullCollectionName)
+ buf.WriteString(self.fullCollectionName)
buf.WriteByte(0)
buf.Write(zero)
- buf.Write(d.selector.Bytes())
+ buf.Write(self.selector.Bytes())
return buf.Bytes()
}
@@ -99,20 +99,20 @@ type getMoreMsg struct {
requestID int32
}
-func (g *getMoreMsg) OpCode() int32 { return _OP_GET_MORE }
-func (g *getMoreMsg) RequestID() int32 { return g.requestID }
+func (self *getMoreMsg) OpCode() int32 { return _OP_GET_MORE }
+func (self *getMoreMsg) RequestID() int32 { return self.requestID }
-func (g *getMoreMsg) Bytes() []byte {
+func (self *getMoreMsg) Bytes() []byte {
buf := bytes.NewBuffer(make([]byte, 4))
- buf.WriteString(g.fullCollectionName)
+ buf.WriteString(self.fullCollectionName)
buf.WriteByte(0)
b := make([]byte, 4)
- binary.LittleEndian.PutUint32(b, uint32(g.numberToReturn))
+ binary.LittleEndian.PutUint32(b, uint32(self.numberToReturn))
buf.Write(b)
b = make([]byte, 8)
- binary.LittleEndian.PutUint64(b, uint64(g.cursorID))
+ binary.LittleEndian.PutUint64(b, uint64(self.cursorID))
buf.Write(b)
return buf.Bytes()
@@ -126,14 +126,14 @@ type insertMsg struct {
requestID int32
}
-func (i *insertMsg) OpCode() int32 { return _OP_INSERT }
-func (i *insertMsg) RequestID() int32 { return i.requestID }
+func (self *insertMsg) OpCode() int32 { return _OP_INSERT }
+func (self *insertMsg) RequestID() int32 { return self.requestID }
-func (i *insertMsg) Bytes() []byte {
+func (self *insertMsg) Bytes() []byte {
buf := bytes.NewBuffer(make([]byte, 4))
- buf.WriteString(i.fullCollectionName)
+ buf.WriteString(self.fullCollectionName)
buf.WriteByte(0)
- buf.Write(i.doc.Bytes())
+ buf.Write(self.doc.Bytes())
return buf.Bytes()
}
@@ -145,18 +145,18 @@ type killMsg struct {
requestID int32
}
-func (k *killMsg) OpCode() int32 { return _OP_KILL_CURSORS }
-func (k *killMsg) RequestID() int32 { return k.requestID }
+func (self *killMsg) OpCode() int32 { return _OP_KILL_CURSORS }
+func (self *killMsg) RequestID() int32 { return self.requestID }
-func (k *killMsg) Bytes() []byte {
+func (self *killMsg) Bytes() []byte {
buf := bytes.NewBuffer(make([]byte, 4))
b := make([]byte, 4)
- binary.LittleEndian.PutUint32(b, uint32(k.numberOfCursorIDs))
+ binary.LittleEndian.PutUint32(b, uint32(self.numberOfCursorIDs))
buf.Write(b)
b = make([]byte, 8)
- for _, id := range k.cursorIDs {
+ for _, id := range self.cursorIDs {
binary.LittleEndian.PutUint64(b, uint64(id))
buf.Write(b)
}
@@ -175,24 +175,24 @@ type queryMsg struct {
requestID int32
}
-func (q *queryMsg) OpCode() int32 { return _OP_QUERY }
-func (q *queryMsg) RequestID() int32 { return q.requestID }
+func (self *queryMsg) OpCode() int32 { return _OP_QUERY }
+func (self *queryMsg) RequestID() int32 { return self.requestID }
-func (q *queryMsg) Bytes() []byte {
+func (self *queryMsg) Bytes() []byte {
b := make([]byte, 4)
- binary.LittleEndian.PutUint32(b, uint32(q.opts))
+ binary.LittleEndian.PutUint32(b, uint32(self.opts))
buf := bytes.NewBuffer(b)
- buf.WriteString(q.fullCollectionName)
+ buf.WriteString(self.fullCollectionName)
buf.WriteByte(0)
- binary.LittleEndian.PutUint32(b, uint32(q.numberToSkip))
+ binary.LittleEndian.PutUint32(b, uint32(self.numberToSkip))
buf.Write(b)
- binary.LittleEndian.PutUint32(b, uint32(q.numberToReturn))
+ binary.LittleEndian.PutUint32(b, uint32(self.numberToReturn))
buf.Write(b)
- buf.Write(q.query.Bytes())
+ buf.Write(self.query.Bytes())
return buf.Bytes()
}
@@ -205,20 +205,20 @@ type updateMsg struct {
requestID int32
}
-func (u *updateMsg) OpCode() int32 { return _OP_UPDATE }
-func (u *updateMsg) RequestID() int32 { return u.requestID }
+func (self *updateMsg) OpCode() int32 { return _OP_UPDATE }
+func (self *updateMsg) RequestID() int32 { return self.requestID }
-func (u *updateMsg) Bytes() []byte {
+func (self *updateMsg) Bytes() []byte {
buf := bytes.NewBuffer(make([]byte, 4))
- buf.WriteString(u.fullCollectionName)
+ buf.WriteString(self.fullCollectionName)
buf.WriteByte(0)
b := make([]byte, 4)
- binary.LittleEndian.PutUint32(b, uint32(u.flags))
+ binary.LittleEndian.PutUint32(b, uint32(self.flags))
buf.Write(b)
- buf.Write(u.selector.Bytes())
- buf.Write(u.document.Bytes())
+ buf.Write(self.selector.Bytes())
+ buf.Write(self.document.Bytes())
return buf.Bytes()
}
@@ -236,10 +236,10 @@ type replyMsg struct {
docs *vector.Vector
}
-func (c *Connection) readReply() (*replyMsg, os.Error) {
- size_bits, _ := ioutil.ReadAll(io.LimitReader(c.conn, 4))
+func (self *Connection) readReply() (*replyMsg, os.Error) {
+ size_bits, _ := ioutil.ReadAll(io.LimitReader(self.conn, 4))
size := binary.LittleEndian.Uint32(size_bits)
- rest, _ := ioutil.ReadAll(io.LimitReader(c.conn, int64(size)-4))
+ rest, _ := ioutil.ReadAll(io.LimitReader(self.conn, int64(size)-4))
reply := parseReply(rest)
return reply, nil
}
Please sign in to comment.
Something went wrong with that request. Please try again.