Browse files

Source files formatted using gofmt

  • Loading branch information...
1 parent bc69048 commit 373125daa6c5467e2c5ecdc5795754005d15aad1 John Mac committed with Jun 17, 2010
Showing with 690 additions and 685 deletions.
  1. +58 −57 mongo/bson-struct.go
  2. +246 −245 mongo/bson.go
  3. +50 −49 mongo/bson_test.go
  4. +220 −219 mongo/mongo.go
  5. +116 −115 mongo/mongo_test.go
View
115 mongo/bson-struct.go
@@ -7,21 +7,21 @@
package mongo
import (
- "reflect";
- "strings";
- "fmt";
- "os";
- "bytes";
- "time";
- "container/vector";
+ "reflect"
+ "strings"
+ "fmt"
+ "os"
+ "bytes"
+ "time"
+ "container/vector"
)
type structBuilder struct {
- val reflect.Value;
+ val reflect.Value
// if map_ != nil, write val to map_[key] on each change
- map_ *reflect.MapValue;
- key reflect.Value;
+ map_ *reflect.MapValue
+ key reflect.Value
}
var nobuilder *structBuilder
@@ -31,7 +31,7 @@ func isfloat(v reflect.Value) bool {
case *reflect.FloatValue, *reflect.Float32Value, *reflect.Float64Value:
return true
}
- return false;
+ return false
}
func setfloat(v reflect.Value, f float64) {
@@ -85,7 +85,7 @@ func (b *structBuilder) Int64(i int64) {
if b == nil {
return
}
- v := b.val;
+ v := b.val
if isfloat(v) {
setfloat(v, float64(i))
} else {
@@ -106,7 +106,7 @@ func (b *structBuilder) Int32(i int32) {
if b == nil {
return
}
- v := b.val;
+ v := b.val
if isfloat(v) {
setfloat(v, float64(i))
} else {
@@ -118,15 +118,15 @@ func (b *structBuilder) Float64(f float64) {
if b == nil {
return
}
- v := b.val;
+ v := b.val
if isfloat(v) {
setfloat(v, f)
} else {
setint(v, int64(f))
}
}
-func (b *structBuilder) Null() {}
+func (b *structBuilder) Null() {}
func (b *structBuilder) String(s string) {
if b == nil {
@@ -162,8 +162,8 @@ func (b *structBuilder) OID(oid []byte) {
}
if v, ok := b.val.(*reflect.SliceValue); ok {
if v.Cap() < 12 {
- nv := reflect.MakeSlice(v.Type().(*reflect.SliceType), 12, 12);
- v.Set(nv);
+ nv := reflect.MakeSlice(v.Type().(*reflect.SliceType), 12, 12)
+ v.Set(nv)
}
for i := 0; i < 12; i++ {
v.Elem(i).(*reflect.Uint8Value).Set(oid[i])
@@ -193,16 +193,16 @@ func (b *structBuilder) Elem(i int) Builder {
}
case *reflect.SliceValue:
if i > v.Cap() {
- n := v.Cap();
+ n := v.Cap()
if n < 8 {
n = 8
}
for n <= i {
n *= 2
}
- nv := reflect.MakeSlice(v.Type().(*reflect.SliceType), v.Len(), n);
- reflect.ArrayCopy(nv, v);
- v.Set(nv);
+ nv := reflect.MakeSlice(v.Type().(*reflect.SliceType), v.Len(), n)
+ reflect.ArrayCopy(nv, v)
+ v.Set(nv)
}
if v.Len() <= i && i < v.Cap() {
v.SetLen(i + 1)
@@ -211,7 +211,7 @@ func (b *structBuilder) Elem(i int) Builder {
return &structBuilder{val: v.Elem(i)}
}
}
- return nobuilder;
+ return nobuilder
}
func (b *structBuilder) Object() {
@@ -220,11 +220,11 @@ func (b *structBuilder) Object() {
}
if v, ok := b.val.(*reflect.PtrValue); ok && v.IsNil() {
if v.IsNil() {
- v.PointTo(reflect.MakeZero(v.Type().(*reflect.PtrType).Elem()));
- b.Flush();
+ v.PointTo(reflect.MakeZero(v.Type().(*reflect.PtrType).Elem()))
+ b.Flush()
}
- b.map_ = nil;
- b.val = v.Elem();
+ b.map_ = nil
+ b.val = v.Elem()
}
if v, ok := b.val.(*reflect.MapValue); ok && v.IsNil() {
v.Set(reflect.MakeMap(v.Type().(*reflect.MapType)))
@@ -237,34 +237,34 @@ func (b *structBuilder) Key(k string) Builder {
}
switch v := reflect.Indirect(b.val).(type) {
case *reflect.StructValue:
- t := v.Type().(*reflect.StructType);
+ t := v.Type().(*reflect.StructType)
// Case-insensitive field lookup.
- k = strings.ToLower(k);
+ k = strings.ToLower(k)
for i := 0; i < t.NumField(); i++ {
if strings.ToLower(t.Field(i).Name) == k {
return &structBuilder{val: v.Field(i)}
}
}
case *reflect.MapValue:
- t := v.Type().(*reflect.MapType);
+ t := v.Type().(*reflect.MapType)
if t.Key() != reflect.Typeof(k) {
break
}
- key := reflect.NewValue(k);
- elem := v.Elem(key);
+ key := reflect.NewValue(k)
+ elem := v.Elem(key)
if elem == nil {
- v.SetElem(key, reflect.MakeZero(t.Elem()));
- elem = v.Elem(key);
+ v.SetElem(key, reflect.MakeZero(t.Elem()))
+ elem = v.Elem(key)
}
- return &structBuilder{val: elem, map_: v, key: key};
+ return &structBuilder{val: elem, map_: v, key: key}
}
- return nobuilder;
+ return nobuilder
}
func Unmarshal(b []byte, val interface{}) (err os.Error) {
- sb := &structBuilder{val: reflect.NewValue(val)};
- err = Parse(bytes.NewBuffer(b[4:len(b)]), sb);
- return;
+ sb := &structBuilder{val: reflect.NewValue(val)}
+ err = Parse(bytes.NewBuffer(b[4:len(b)]), sb)
+ return
}
func Marshal(val interface{}) (BSON, os.Error) {
@@ -289,7 +289,7 @@ func Marshal(val interface{}) (BSON, os.Error) {
return &_Date{v, _Null{}}, nil
}
- var value reflect.Value;
+ var value reflect.Value
switch nv := reflect.NewValue(val).(type) {
case *reflect.PtrValue:
value = nv.Elem()
@@ -299,47 +299,48 @@ func Marshal(val interface{}) (BSON, os.Error) {
switch fv := value.(type) {
case *reflect.StructValue:
- o := &_Object{map[string]BSON{}, _Null{}};
- t := fv.Type().(*reflect.StructType);
+ o := &_Object{map[string]BSON{}, _Null{}}
+ t := fv.Type().(*reflect.StructType)
for i := 0; i < t.NumField(); i++ {
- key := strings.ToLower(t.Field(i).Name);
- el, err := Marshal(fv.Field(i).Interface());
+ key := strings.ToLower(t.Field(i).Name)
+ el, err := Marshal(fv.Field(i).Interface())
if err != nil {
return nil, err
}
- o.value[key] = el;
+ o.value[key] = el
}
- return o, nil;
+ return o, nil
case *reflect.MapValue:
- o := &_Object{map[string]BSON{}, _Null{}};
- mt := fv.Type().(*reflect.MapType);
+ o := &_Object{map[string]BSON{}, _Null{}}
+ mt := fv.Type().(*reflect.MapType)
if mt.Key() != reflect.Typeof("") {
return nil, os.NewError("can't marshall maps with non-string key types")
}
- keys := fv.Keys();
+ keys := fv.Keys()
for _, k := range keys {
- sk := k.(*reflect.StringValue).Get();
- el, err := Marshal(fv.Elem(k).Interface());
+ sk := k.(*reflect.StringValue).Get()
+ el, err := Marshal(fv.Elem(k).Interface())
if err != nil {
return nil, err
}
- o.value[sk] = el;
+ o.value[sk] = el
}
- return o, nil;
+ return o, nil
case *reflect.SliceValue:
- a := &_Array{new(vector.Vector), _Null{}};
+ a := &_Array{new(vector.Vector), _Null{}}
for i := 0; i < fv.Len(); i++ {
- el, err := Marshal(fv.Elem(i).Interface());
+ el, err := Marshal(fv.Elem(i).Interface())
if err != nil {
return nil, err
}
- a.value.Push(el);
+ a.value.Push(el)
}
- return a, nil;
+ return a, nil
default:
return nil, os.NewError(fmt.Sprintf("don't know how to marshal %v\n", value.Type()))
}
- return nil, nil;
+ return nil, nil
}
+
View
491 mongo/bson.go
@@ -7,154 +7,154 @@
package mongo
import (
- "os";
- "io";
- "io/ioutil";
- "fmt";
- "math";
- "time";
- "bytes";
- "strconv";
- "encoding/binary";
- "container/vector";
+ "os"
+ "io"
+ "io/ioutil"
+ "fmt"
+ "math"
+ "time"
+ "bytes"
+ "strconv"
+ "encoding/binary"
+ "container/vector"
)
const (
- EOOKind = iota;
- NumberKind;
- StringKind;
- ObjectKind;
- ArrayKind;
- BinaryKind;
- UndefinedKind; // deprecated
- OIDKind;
- BooleanKind;
- DateKind;
- NullKind;
- RegexKind;
- RefKind; // deprecated
- CodeKind;
- SymbolKind;
- CodeWithScope;
- IntKind;
- TimestampKind;
- LongKind;
- MinKeyKind;
- MaxKeyKind;
+ EOOKind = iota
+ NumberKind
+ StringKind
+ ObjectKind
+ ArrayKind
+ BinaryKind
+ UndefinedKind // deprecated
+ OIDKind
+ BooleanKind
+ DateKind
+ NullKind
+ RegexKind
+ RefKind // deprecated
+ CodeKind
+ SymbolKind
+ CodeWithScope
+ IntKind
+ TimestampKind
+ LongKind
+ MinKeyKind
+ MaxKeyKind
)
type BSON interface {
- Kind() int;
- Number() float64;
- String() string;
- OID() []byte;
- Bool() bool;
- Date() *time.Time;
- Regex() (string, string);
- Int() int32;
- Long() int64;
+ Kind() int
+ Number() float64
+ String() string
+ OID() []byte
+ Bool() bool
+ Date() *time.Time
+ Regex() (string, string)
+ Int() int32
+ Long() int64
- Get(s string) BSON;
- Elem(i int) BSON;
- Len() int;
+ Get(s string) BSON
+ Elem(i int) BSON
+ Len() int
- Bytes() []byte;
+ Bytes() []byte
}
type _Null struct{}
var Null BSON = &_Null{}
-func (*_Null) Kind() int { return NullKind }
-func (*_Null) Number() float64 { return 0 }
-func (*_Null) String() string { return "null" }
-func (*_Null) OID() []byte { return nil }
-func (*_Null) Bool() bool { return false }
-func (*_Null) Date() *time.Time { return nil }
-func (*_Null) Regex() (string, string) { return "", "" }
-func (*_Null) Int() int32 { return 0 }
-func (*_Null) Long() int64 { return 0 }
-func (*_Null) Get(string) BSON { return Null }
-func (*_Null) Elem(int) BSON { return Null }
-func (*_Null) Len() int { return 0 }
-func (*_Null) Bytes() []byte { return []byte{0} }
+func (*_Null) Kind() int { return NullKind }
+func (*_Null) Number() float64 { return 0 }
+func (*_Null) String() string { return "null" }
+func (*_Null) OID() []byte { return nil }
+func (*_Null) Bool() bool { return false }
+func (*_Null) Date() *time.Time { return nil }
+func (*_Null) Regex() (string, string) { return "", "" }
+func (*_Null) Int() int32 { return 0 }
+func (*_Null) Long() int64 { return 0 }
+func (*_Null) Get(string) BSON { return Null }
+func (*_Null) Elem(int) BSON { return Null }
+func (*_Null) Len() int { return 0 }
+func (*_Null) Bytes() []byte { return []byte{0} }
type _Number struct {
- value float64;
- _Null;
+ value float64
+ _Null
}
-func (n *_Number) Kind() int { return NumberKind }
-func (n *_Number) Number() float64 { return n.value }
+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);
- b := []byte{0, 0, 0, 0, 0, 0, 0, 0};
- binary.LittleEndian.PutUint64(b, bits);
- return b;
+ bits := math.Float64bits(n.value)
+ b := []byte{0, 0, 0, 0, 0, 0, 0, 0}
+ binary.LittleEndian.PutUint64(b, bits)
+ return b
}
type _String struct {
- value string;
- _Null;
+ value string
+ _Null
}
-func (s *_String) Kind() int { return StringKind }
-func (s *_String) String() string { return s.value }
+func (s *_String) Kind() int { return StringKind }
+func (s *_String) String() string { return s.value }
func (s *_String) Bytes() []byte {
- b := []byte{0, 0, 0, 0};
- l := len(s.value) + 1;
- binary.LittleEndian.PutUint32(b, uint32(l));
+ b := []byte{0, 0, 0, 0}
+ l := len(s.value) + 1
+ binary.LittleEndian.PutUint32(b, uint32(l))
- buf := bytes.NewBuffer(b);
- buf.WriteString(s.value);
- buf.WriteByte(0);
+ buf := bytes.NewBuffer(b)
+ buf.WriteString(s.value)
+ buf.WriteByte(0)
- return buf.Bytes();
+ return buf.Bytes()
}
type _Object struct {
- value map[string]BSON;
- _Null;
+ value map[string]BSON
+ _Null
}
-func (o *_Object) Kind() int { return ObjectKind }
+func (o *_Object) Kind() int { return ObjectKind }
func (o *_Object) Get(s string) BSON {
if o.value == nil {
return Null
}
- b, ok := o.value[s];
+ b, ok := o.value[s]
if !ok {
return Null
}
- return b;
+ return b
}
-func (o *_Object) Len() int { return len(o.value) }
+func (o *_Object) Len() int { return len(o.value) }
func (o *_Object) Bytes() []byte {
- buf := bytes.NewBuffer([]byte{});
+ buf := bytes.NewBuffer([]byte{})
for k, v := range o.value {
- buf.WriteByte(byte(v.Kind()));
- buf.WriteString(k);
- buf.WriteByte(0);
- buf.Write(v.Bytes());
+ buf.WriteByte(byte(v.Kind()))
+ buf.WriteString(k)
+ buf.WriteByte(0)
+ buf.Write(v.Bytes())
}
- buf.WriteByte(0);
+ buf.WriteByte(0)
- l := buf.Len() + 4;
- b := []byte{0, 0, 0, 0};
- binary.LittleEndian.PutUint32(b, uint32(l));
- return bytes.Add(b, buf.Bytes());
+ l := buf.Len() + 4
+ b := []byte{0, 0, 0, 0}
+ binary.LittleEndian.PutUint32(b, uint32(l))
+ return bytes.Add(b, buf.Bytes())
}
var EmptyObject BSON = &_Object{map[string]BSON{}, _Null{}}
type _Array struct {
- value *vector.Vector;
- _Null;
+ value *vector.Vector
+ _Null
}
-func (a *_Array) Kind() int { return ArrayKind }
+func (a *_Array) Kind() int { return ArrayKind }
func (a *_Array) Elem(i int) BSON {
if a.value == nil {
return Null
@@ -164,103 +164,103 @@ func (a *_Array) Elem(i int) BSON {
return Null
}
- return a.value.At(i).(BSON);
+ return a.value.At(i).(BSON)
}
-func (a *_Array) Len() int { return a.value.Len() }
+func (a *_Array) Len() int { return a.value.Len() }
func (a *_Array) Bytes() []byte {
- buf := bytes.NewBuffer([]byte{});
+ buf := bytes.NewBuffer([]byte{})
for i := 0; i < a.value.Len(); i++ {
- v := a.value.At(i).(BSON);
- buf.WriteByte(byte(v.Kind()));
- buf.WriteString(strconv.Itoa(i));
- buf.WriteByte(0);
- buf.Write(v.Bytes());
+ v := a.value.At(i).(BSON)
+ buf.WriteByte(byte(v.Kind()))
+ buf.WriteString(strconv.Itoa(i))
+ buf.WriteByte(0)
+ buf.Write(v.Bytes())
}
- buf.WriteByte(0);
+ buf.WriteByte(0)
- l := buf.Len() + 4;
- b := []byte{0, 0, 0, 0};
- binary.LittleEndian.PutUint32(b, uint32(l));
- return bytes.Add(b, buf.Bytes());
+ l := buf.Len() + 4
+ b := []byte{0, 0, 0, 0}
+ binary.LittleEndian.PutUint32(b, uint32(l))
+ return bytes.Add(b, buf.Bytes())
}
type _OID struct {
- value []byte;
- _Null;
+ value []byte
+ _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 (o *_OID) Kind() int { return OIDKind }
+func (o *_OID) OID() []byte { return o.value }
+func (o *_OID) Bytes() []byte { return o.value }
type _Boolean struct {
- value bool;
- _Null;
+ value bool
+ _Null
}
-func (b *_Boolean) Kind() int { return BooleanKind }
-func (b *_Boolean) Bool() bool { return b.value }
+func (b *_Boolean) Kind() int { return BooleanKind }
+func (b *_Boolean) Bool() bool { return b.value }
func (b *_Boolean) Bytes() []byte {
if b.value {
return []byte{1}
}
- return []byte{0};
+ return []byte{0}
}
type _Date struct {
- value *time.Time;
- _Null;
+ value *time.Time
+ _Null
}
-func (d *_Date) Kind() int { return DateKind }
-func (d *_Date) Date() *time.Time { return d.value }
+func (d *_Date) Kind() int { return DateKind }
+func (d *_Date) Date() *time.Time { return d.value }
func (d *_Date) Bytes() []byte {
- b := []byte{0, 0, 0, 0, 0, 0, 0, 0};
- mtime := d.value.Seconds() * 1000;
- binary.LittleEndian.PutUint64(b, uint64(mtime));
- return b;
+ b := []byte{0, 0, 0, 0, 0, 0, 0, 0}
+ mtime := d.value.Seconds() * 1000
+ binary.LittleEndian.PutUint64(b, uint64(mtime))
+ return b
}
type _Regex struct {
- regex, options string;
- _Null;
+ regex, options string
+ _Null
}
-func (r *_Regex) Kind() int { return RegexKind }
-func (r *_Regex) Regex() (string, string) { return r.regex, r.options }
+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);
- buf.WriteByte(0);
- buf.WriteString(r.options);
- buf.WriteByte(0);
- return buf.Bytes();
+ buf := bytes.NewBufferString(r.regex)
+ buf.WriteByte(0)
+ buf.WriteString(r.options)
+ buf.WriteByte(0)
+ return buf.Bytes()
}
type _Int struct {
- value int32;
- _Null;
+ value int32
+ _Null
}
-func (i *_Int) Kind() int { return IntKind }
-func (i *_Int) Int() int32 { return i.value }
+func (i *_Int) Kind() int { return IntKind }
+func (i *_Int) Int() int32 { return i.value }
func (i *_Int) Bytes() []byte {
- b := []byte{0, 0, 0, 0};
- binary.LittleEndian.PutUint32(b, uint32(i.value));
- return b;
+ b := []byte{0, 0, 0, 0}
+ binary.LittleEndian.PutUint32(b, uint32(i.value))
+ return b
}
type _Long struct {
- value int64;
- _Null;
+ value int64
+ _Null
}
-func (l *_Long) Kind() int { return LongKind }
-func (l *_Long) Long() int64 { return l.value }
+func (l *_Long) Kind() int { return LongKind }
+func (l *_Long) Long() int64 { return l.value }
func (l *_Long) Bytes() []byte {
- b := []byte{0, 0, 0, 0, 0, 0, 0, 0};
- binary.LittleEndian.PutUint64(b, uint64(l.value));
- return b;
+ b := []byte{0, 0, 0, 0, 0, 0, 0, 0}
+ binary.LittleEndian.PutUint64(b, uint64(l.value))
+ return b
}
func Equal(a, b BSON) bool {
@@ -279,7 +279,7 @@ func Equal(a, b BSON) bool {
case StringKind:
return a.String() == b.String()
case ObjectKind:
- obj := a.(*_Object).value;
+ obj := a.(*_Object).value
if len(obj) != len(b.(*_Object).value) {
return false
}
@@ -288,7 +288,7 @@ func Equal(a, b BSON) bool {
return false
}
}
- return true;
+ return true
case ArrayKind:
if a.Len() != b.Len() {
return false
@@ -298,56 +298,56 @@ func Equal(a, b BSON) bool {
return false
}
}
- return true;
+ return true
case OIDKind:
return bytes.Equal(a.OID(), b.OID())
case BooleanKind:
return a.Bool() == b.Bool()
case DateKind:
return a.Date() == b.Date()
case RegexKind:
- ar, ao := a.Regex();
- br, bo := b.Regex();
- return ar == br && ao == bo;
+ ar, ao := a.Regex()
+ br, bo := b.Regex()
+ return ar == br && ao == bo
case IntKind:
return a.Int() == b.Int()
case LongKind:
return a.Long() == b.Long()
}
- return true;
+ return true
}
type Builder interface {
// Set value
- Int64(i int64);
- Int32(i int32);
- Float64(f float64);
- String(s string);
- Bool(b bool);
- Date(d *time.Time);
- OID(o []byte);
- Regex(regex, options string);
- Null();
- Object();
- Array();
+ Int64(i int64)
+ Int32(i int32)
+ Float64(f float64)
+ String(s string)
+ Bool(b bool)
+ Date(d *time.Time)
+ OID(o []byte)
+ Regex(regex, options string)
+ Null()
+ Object()
+ Array()
// Create sub-Builders
- Key(s string) Builder;
- Elem(i int) Builder;
+ Key(s string) Builder
+ Elem(i int) Builder
// Flush changes to parent Builder if necessary.
- Flush();
+ Flush()
}
type _BSONBuilder struct {
- ptr *BSON;
+ ptr *BSON
- arr *vector.Vector;
- elem int;
+ arr *vector.Vector
+ elem int
- obj map[string]BSON;
- key string;
+ obj map[string]BSON
+ key string
}
func (bb *_BSONBuilder) Put(b BSON) {
@@ -370,134 +370,135 @@ func (bb *_BSONBuilder) Get() BSON {
case bb.obj != nil:
return bb.obj[bb.key]
}
- return nil;
+ 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) 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 (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 (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 (bb *_BSONBuilder) Key(key string) Builder {
- bb2 := new(_BSONBuilder);
+ bb2 := new(_BSONBuilder)
switch obj := bb.Get().(type) {
case *_Object:
- bb2.obj = obj.value;
- bb2.key = key;
- bb2.obj[key] = Null;
+ bb2.obj = obj.value
+ bb2.key = key
+ bb2.obj[key] = Null
case *_Array:
- bb2.arr = obj.value;
- elem, _ := strconv.Atoi(key);
- bb2.elem = elem;
+ bb2.arr = obj.value
+ elem, _ := strconv.Atoi(key)
+ bb2.elem = elem
for elem >= bb2.arr.Len() {
bb2.arr.Push(Null)
}
}
- return bb2;
+ return bb2
}
func (bb *_BSONBuilder) Elem(i int) Builder {
- bb2 := new(_BSONBuilder);
- bb2.arr = bb.Get().(*_Array).value;
- bb2.elem = i;
+ bb2 := new(_BSONBuilder)
+ bb2.arr = bb.Get().(*_Array).value
+ bb2.elem = i
for i >= bb2.arr.Len() {
bb2.arr.Push(Null)
}
- return bb2;
+ return bb2
}
-func (bb *_BSONBuilder) Flush() {}
+func (bb *_BSONBuilder) Flush() {}
func BytesToBSON(b []byte) (BSON, os.Error) {
- var bson BSON;
- bb := new(_BSONBuilder);
- bb.ptr = &bson;
- bb.Object();
- err := Parse(bytes.NewBuffer(b[4:len(b)]), bb);
- return bson, err;
+ var bson BSON
+ bb := new(_BSONBuilder)
+ bb.ptr = &bson
+ bb.Object()
+ err := Parse(bytes.NewBuffer(b[4:len(b)]), bb)
+ return bson, err
}
func readCString(buf *bytes.Buffer) string {
- out := bytes.NewBuffer([]byte{});
- var c byte;
+ out := bytes.NewBuffer([]byte{})
+ var c byte
for c, _ = buf.ReadByte(); c != 0; c, _ = buf.ReadByte() {
out.WriteByte(c)
}
- return out.String();
+ return out.String()
}
func Parse(buf *bytes.Buffer, builder Builder) (err os.Error) {
- kind, _ := buf.ReadByte();
- err = nil;
+ kind, _ := buf.ReadByte()
+ err = nil
for kind != EOOKind {
- name := readCString(buf);
- b2 := builder.Key(name);
+ name := readCString(buf)
+ b2 := builder.Key(name)
switch kind {
case NumberKind:
- lr := io.LimitReader(buf, 8);
- bits, _ := ioutil.ReadAll(lr);
- ui64 := binary.LittleEndian.Uint64(bits);
- fl64 := math.Float64frombits(ui64);
- b2.Float64(fl64);
+ lr := io.LimitReader(buf, 8)
+ bits, _ := ioutil.ReadAll(lr)
+ ui64 := binary.LittleEndian.Uint64(bits)
+ fl64 := math.Float64frombits(ui64)
+ b2.Float64(fl64)
case StringKind:
- bits, _ := ioutil.ReadAll(io.LimitReader(buf, 4));
- l := binary.LittleEndian.Uint32(bits);
- s, _ := ioutil.ReadAll(io.LimitReader(buf, int64(l-1)));
- buf.ReadByte();
- b2.String(string(s));
+ bits, _ := ioutil.ReadAll(io.LimitReader(buf, 4))
+ l := binary.LittleEndian.Uint32(bits)
+ s, _ := ioutil.ReadAll(io.LimitReader(buf, int64(l-1)))
+ buf.ReadByte()
+ b2.String(string(s))
case ObjectKind:
- b2.Object();
- ioutil.ReadAll(io.LimitReader(buf, 4));
- err = Parse(buf, b2);
+ b2.Object()
+ ioutil.ReadAll(io.LimitReader(buf, 4))
+ err = Parse(buf, b2)
case ArrayKind:
- b2.Array();
- ioutil.ReadAll(io.LimitReader(buf, 4));
- err = Parse(buf, b2);
+ b2.Array()
+ ioutil.ReadAll(io.LimitReader(buf, 4))
+ err = Parse(buf, b2)
case OIDKind:
- oid, _ := ioutil.ReadAll(io.LimitReader(buf, 12));
- b2.OID(oid);
+ oid, _ := ioutil.ReadAll(io.LimitReader(buf, 12))
+ b2.OID(oid)
case BooleanKind:
- b, _ := buf.ReadByte();
+ b, _ := buf.ReadByte()
if b == 1 {
b2.Bool(true)
} else {
b2.Bool(false)
}
case DateKind:
- bits, _ := ioutil.ReadAll(io.LimitReader(buf, 8));
- ui64 := binary.LittleEndian.Uint64(bits);
- b2.Date(time.SecondsToUTC(int64(ui64) / 1000));
+ bits, _ := ioutil.ReadAll(io.LimitReader(buf, 8))
+ ui64 := binary.LittleEndian.Uint64(bits)
+ b2.Date(time.SecondsToUTC(int64(ui64) / 1000))
case RegexKind:
- regex := readCString(buf);
- options := readCString(buf);
- b2.Regex(regex, options);
+ regex := readCString(buf)
+ options := readCString(buf)
+ b2.Regex(regex, options)
case IntKind:
- bits, _ := ioutil.ReadAll(io.LimitReader(buf, 4));
- ui32 := binary.LittleEndian.Uint32(bits);
- b2.Int32(int32(ui32));
+ bits, _ := ioutil.ReadAll(io.LimitReader(buf, 4))
+ ui32 := binary.LittleEndian.Uint32(bits)
+ b2.Int32(int32(ui32))
case LongKind:
- bits, _ := ioutil.ReadAll(io.LimitReader(buf, 8));
- ui64 := binary.LittleEndian.Uint64(bits);
- b2.Int64(int64(ui64));
+ bits, _ := ioutil.ReadAll(io.LimitReader(buf, 8))
+ ui64 := binary.LittleEndian.Uint64(bits)
+ b2.Int64(int64(ui64))
default:
err = os.NewError(fmt.Sprintf("don't know how to handle kind %v yet", kind))
}
- kind, _ = buf.ReadByte();
+ kind, _ = buf.ReadByte()
}
- return;
+ return
}
+
View
99 mongo/bson_test.go
@@ -5,10 +5,10 @@
package mongo_test
import (
- "testing";
- "mongo";
- "fmt";
- "time";
+ "testing"
+ "mongo"
+ "fmt"
+ "time"
)
func assertTrue(tf bool, msg string, t *testing.T) {
@@ -20,66 +20,67 @@ func assertTrue(tf bool, msg string, t *testing.T) {
type EmptyStruct struct{}
type OtherStruct struct {
- F, V string;
+ F, V string
}
type ExampleStruct struct {
- First int32;
- Second float64;
- Third string;
- Fourth EmptyStruct;
- Fifth OtherStruct;
+ First int32
+ Second float64
+ Third string
+ Fourth EmptyStruct
+ Fifth OtherStruct
}
var b []byte = []byte{92, 0, 0, 0, 1, 115, 101, 99, 111, 110, 100, 0, 0, 0, 0, 0, 0, 0, 0, 64, 3, 102, 105, 102, 116, 104, 0, 23, 0, 0, 0, 2, 118, 0, 2, 0, 0, 0, 101, 0, 2, 102, 0, 2, 0, 0, 0, 105, 0, 0, 3, 102, 111, 117, 114, 116, 104, 0, 5, 0, 0, 0, 0, 2, 116, 104, 105, 114, 100, 0, 6, 0, 0, 0, 116, 104, 114, 101, 101, 0, 16, 102, 105, 114, 115, 116, 0, 1, 0, 0, 0, 0}
func TestSerializeAndDeserialize(t *testing.T) {
- obj, err := mongo.BytesToBSON(b);
- assertTrue(err == nil, fmt.Sprintf("failed parsing %v", b), t);
- obj2, _ := mongo.BytesToBSON(obj.Bytes());
- assertTrue(mongo.Equal(obj, obj2), fmt.Sprintf("obj != obj2 for %v", b), t);
-
- assertTrue(obj.Get("first").Int() == 1, "obj['first'] != 1", t);
- assertTrue(obj.Get("second").Number() == 2, "obj['second'] != 2.0", t);
- assertTrue(obj.Get("third").String() == "three", "obj['third'] != 'three'", t);
- assertTrue(obj.Get("fifth").Get("f").String() == "i", "obj['fifth']['f'] != 'i'", t);
- assertTrue(obj.Get("fifth").Get("v").String() == "e", "obj['fifth']['v'] != 'e'", t);
+ obj, err := mongo.BytesToBSON(b)
+ assertTrue(err == nil, fmt.Sprintf("failed parsing %v", b), t)
+ obj2, _ := mongo.BytesToBSON(obj.Bytes())
+ assertTrue(mongo.Equal(obj, obj2), fmt.Sprintf("obj != obj2 for %v", b), t)
+
+ assertTrue(obj.Get("first").Int() == 1, "obj['first'] != 1", t)
+ assertTrue(obj.Get("second").Number() == 2, "obj['second'] != 2.0", t)
+ assertTrue(obj.Get("third").String() == "three", "obj['third'] != 'three'", t)
+ assertTrue(obj.Get("fifth").Get("f").String() == "i", "obj['fifth']['f'] != 'i'", t)
+ assertTrue(obj.Get("fifth").Get("v").String() == "e", "obj['fifth']['v'] != 'e'", t)
}
func TestUnmarshal(t *testing.T) {
- var es ExampleStruct;
- mongo.Unmarshal(b, &es);
- assertTrue(es.First == 1, "unmarshal int", t);
- assertTrue(es.Second == 2, "unmarshal float64", t);
- assertTrue(es.Third == "three", "unmarshal string", t);
- assertTrue(es.Fifth.F == "i" && es.Fifth.V == "e", "unmarshal struct", t);
+ var es ExampleStruct
+ mongo.Unmarshal(b, &es)
+ assertTrue(es.First == 1, "unmarshal int", t)
+ assertTrue(es.Second == 2, "unmarshal float64", t)
+ assertTrue(es.Third == "three", "unmarshal string", t)
+ assertTrue(es.Fifth.F == "i" && es.Fifth.V == "e", "unmarshal struct", t)
}
type ExampleStruct2 struct {
- Date *time.Time;
+ Date *time.Time
}
func TestMarshal(t *testing.T) {
- var es1 ExampleStruct;
- mongo.Unmarshal(b, &es1);
- bs1, _ := mongo.Marshal(&es1);
- bs2, _ := mongo.BytesToBSON(b);
- assertTrue(mongo.Equal(bs1, bs2), "unmarshal->marshal", t);
-
- m := map[string]string{"f": "i", "v": "e"};
- bs3, _ := mongo.Marshal(&m);
- assertTrue(mongo.Equal(bs3, bs2.Get("fifth")), "marshal map", t);
-
- arr, _ := mongo.Marshal([]int{1, 2, 3});
- assertTrue(arr.Elem(0).Long() == 1, "array marshal (0)", t);
- assertTrue(arr.Elem(1).Long() == 2, "array marshal (1)", t);
- assertTrue(arr.Elem(2).Long() == 3, "array marshal (2)", t);
-
- d := time.UTC();
- es2 := &ExampleStruct2{d};
- bs2, _ = mongo.Marshal(es2);
- assertTrue(bs2.Get("date").Date().Seconds() == d.Seconds(), "date marshal", t);
- es2 = new(ExampleStruct2);
- mongo.Unmarshal(bs2.Bytes(), es2);
- assertTrue(es2.Date.Seconds() == d.Seconds(), "date unmarshal", t);
+ var es1 ExampleStruct
+ mongo.Unmarshal(b, &es1)
+ bs1, _ := mongo.Marshal(&es1)
+ bs2, _ := mongo.BytesToBSON(b)
+ assertTrue(mongo.Equal(bs1, bs2), "unmarshal->marshal", t)
+
+ m := map[string]string{"f": "i", "v": "e"}
+ bs3, _ := mongo.Marshal(&m)
+ assertTrue(mongo.Equal(bs3, bs2.Get("fifth")), "marshal map", t)
+
+ arr, _ := mongo.Marshal([]int{1, 2, 3})
+ assertTrue(arr.Elem(0).Long() == 1, "array marshal (0)", t)
+ assertTrue(arr.Elem(1).Long() == 2, "array marshal (1)", t)
+ assertTrue(arr.Elem(2).Long() == 3, "array marshal (2)", t)
+
+ d := time.UTC()
+ es2 := &ExampleStruct2{d}
+ bs2, _ = mongo.Marshal(es2)
+ assertTrue(bs2.Get("date").Date().Seconds() == d.Seconds(), "date marshal", t)
+ es2 = new(ExampleStruct2)
+ mongo.Unmarshal(bs2.Bytes(), es2)
+ assertTrue(es2.Date.Seconds() == d.Seconds(), "date unmarshal", t)
}
+
View
439 mongo/mongo.go
@@ -5,167 +5,167 @@
package mongo
import (
- "os";
- "io";
- "io/ioutil";
- "net";
- "fmt";
- "rand";
- "bytes";
- "encoding/binary";
- "container/vector";
+ "os"
+ "io"
+ "io/ioutil"
+ "net"
+ "fmt"
+ "rand"
+ "bytes"
+ "encoding/binary"
+ "container/vector"
)
var last_req int32
const (
- _OP_REPLY = 1;
- _OP_MSG = 1000;
- _OP_UPDATE = 2001;
- _OP_INSERT = 2002;
- _OP_GET_BY_OID = 2003;
- _OP_QUERY = 2004;
- _OP_GET_MORE = 2005;
- _OP_DELETE = 2006;
- _OP_KILL_CURSORS = 2007;
+ _OP_REPLY = 1
+ _OP_MSG = 1000
+ _OP_UPDATE = 2001
+ _OP_INSERT = 2002
+ _OP_GET_BY_OID = 2003
+ _OP_QUERY = 2004
+ _OP_GET_MORE = 2005
+ _OP_DELETE = 2006
+ _OP_KILL_CURSORS = 2007
)
type message interface {
- Bytes() []byte;
- RequestID() int32;
- OpCode() int32;
+ Bytes() []byte
+ RequestID() int32
+ OpCode() int32
}
type Connection struct {
- host string;
- port int;
- conn *net.TCPConn;
+ host string
+ port int
+ conn *net.TCPConn
}
func Connect(host string, port int) (*Connection, os.Error) {
- laddr, _ := net.ResolveTCPAddr("localhost");
- addr, _ := net.ResolveTCPAddr(fmt.Sprintf("%s:%d", host, port));
- conn, err := net.DialTCP("tcp", laddr, addr);
+ laddr, _ := net.ResolveTCPAddr("localhost")
+ addr, _ := net.ResolveTCPAddr(fmt.Sprintf("%s:%d", host, port))
+ conn, err := net.DialTCP("tcp", laddr, addr)
if err != nil {
return nil, err
}
- return &Connection{host, port, conn}, nil;
+ return &Connection{host, port, conn}, nil
}
func header(length, reqID, respTo, opCode int32) []byte {
- b := make([]byte, 16);
- binary.LittleEndian.PutUint32(b[0:4], uint32(length));
- binary.LittleEndian.PutUint32(b[4:8], uint32(reqID));
- binary.LittleEndian.PutUint32(b[8:12], uint32(respTo));
- binary.LittleEndian.PutUint32(b[12:16], uint32(opCode));
- return b;
+ b := make([]byte, 16)
+ binary.LittleEndian.PutUint32(b[0:4], uint32(length))
+ binary.LittleEndian.PutUint32(b[4:8], uint32(reqID))
+ binary.LittleEndian.PutUint32(b[8:12], uint32(respTo))
+ binary.LittleEndian.PutUint32(b[12:16], uint32(opCode))
+ return b
}
func (c *Connection) writeMessage(m message) os.Error {
- body := m.Bytes();
- hb := header(int32(len(body)+16), m.RequestID(), 0, m.OpCode());
- msg := bytes.Add(hb, body);
+ body := m.Bytes()
+ hb := header(int32(len(body)+16), m.RequestID(), 0, m.OpCode())
+ msg := bytes.Add(hb, body)
- _, err := c.conn.Write(msg);
+ _, err := c.conn.Write(msg)
- last_req = m.RequestID();
- return err;
+ last_req = m.RequestID()
+ return err
}
func (c *Connection) readReply() (*replyMsg, os.Error) {
- size_bits, _ := ioutil.ReadAll(io.LimitReader(c.conn, 4));
- size := binary.LittleEndian.Uint32(size_bits);
- rest, _ := ioutil.ReadAll(io.LimitReader(c.conn, int64(size)-4));
- reply := parseReply(rest);
- return reply, nil;
+ size_bits, _ := ioutil.ReadAll(io.LimitReader(c.conn, 4))
+ size := binary.LittleEndian.Uint32(size_bits)
+ rest, _ := ioutil.ReadAll(io.LimitReader(c.conn, int64(size)-4))
+ reply := parseReply(rest)
+ return reply, nil
}
type Database struct {
- conn *Connection;
- name string;
+ conn *Connection
+ name string
}
func (c *Connection) GetDB(name string) *Database {
return &Database{c, name}
}
func (db *Database) Drop() os.Error {
- cmd, err := Marshal(map[string]int{"dropDatabase": 1});
+ cmd, err := Marshal(map[string]int{"dropDatabase": 1})
if err != nil {
return err
}
- _, err = db.Command(cmd);
- return err;
+ _, err = db.Command(cmd)
+ return err
}
func (db *Database) Repair(preserveClonedFilesOnFailure, backupOriginalFiles bool) os.Error {
- cmd := &_Object{map[string]BSON{"repairDatabase": &_Number{1, _Null{}}, "preserveClonedFilesOnFailure": &_Boolean{preserveClonedFilesOnFailure, _Null{}}, "backupOriginalFiles": &_Boolean{backupOriginalFiles, _Null{}}}, _Null{}};
- _, err := db.Command(cmd);
- return err;
+ cmd := &_Object{map[string]BSON{"repairDatabase": &_Number{1, _Null{}}, "preserveClonedFilesOnFailure": &_Boolean{preserveClonedFilesOnFailure, _Null{}}, "backupOriginalFiles": &_Boolean{backupOriginalFiles, _Null{}}}, _Null{}}
+ _, err := db.Command(cmd)
+ return err
}
type Collection struct {
- db *Database;
- name string;
+ db *Database
+ name string
}
func (db *Database) GetCollection(name string) *Collection {
return &Collection{db, name}
}
type Cursor struct {
- collection *Collection;
- id int64;
- pos int;
- docs *vector.Vector;
+ collection *Collection
+ id int64
+ pos int
+ docs *vector.Vector
}
func (c *Cursor) HasMore() bool {
if c.pos < c.docs.Len() {
return true
}
- err := c.GetMore();
+ err := c.GetMore()
if err != nil {
return false
}
- return c.pos < c.docs.Len();
+ return c.pos < c.docs.Len()
}
func (c *Cursor) GetNext() (BSON, os.Error) {
if c.HasMore() {
- doc := c.docs.At(c.pos).(BSON);
- c.pos = c.pos + 1;
- return doc, nil;
+ doc := c.docs.At(c.pos).(BSON)
+ c.pos = c.pos + 1
+ return doc, nil
}
- return nil, os.NewError("cursor failure");
+ return nil, os.NewError("cursor failure")
}
func (c *Cursor) GetMore() os.Error {
if c.id == 0 {
return os.NewError("no cursorID")
}
- gm := &getMoreMsg{c.collection.fullName(), 0, c.id, rand.Int31()};
- conn := c.collection.db.conn;
- err := conn.writeMessage(gm);
+ gm := &getMoreMsg{c.collection.fullName(), 0, c.id, rand.Int31()}
+ conn := c.collection.db.conn
+ err := conn.writeMessage(gm)
if err != nil {
return err
}
- reply, err := conn.readReply();
+ reply, err := conn.readReply()
if err != nil {
return err
}
- c.pos = 0;
- c.docs = reply.docs;
+ c.pos = 0
+ c.docs = reply.docs
- return nil;
+ return nil
}
func (c *Cursor) Close() os.Error {
@@ -174,111 +174,111 @@ func (c *Cursor) Close() os.Error {
return nil
}
- req_id := rand.Int31();
- km := &killMsg{1, []int64{c.id}, req_id};
- conn := c.collection.db.conn;
- return conn.writeMessage(km);
+ req_id := rand.Int31()
+ km := &killMsg{1, []int64{c.id}, req_id}
+ conn := c.collection.db.conn
+ return conn.writeMessage(km)
}
-func (c *Collection) fullName() string { return c.db.name + "." + c.name }
+func (c *Collection) fullName() string { return c.db.name + "." + c.name }
type indexDesc struct {
- Name string;
- Ns string;
- Key map[string]int;
+ Name string
+ Ns string
+ Key map[string]int
}
func (c *Collection) EnsureIndex(name string, index map[string]int) os.Error {
- coll := c.db.GetCollection("system.indexes");
- id := &indexDesc{name, c.fullName(), index};
- desc, err := Marshal(id);
+ coll := c.db.GetCollection("system.indexes")
+ id := &indexDesc{name, c.fullName(), index}
+ desc, err := Marshal(id)
if err != nil {
return err
}
- return coll.Insert(desc);
+ return coll.Insert(desc)
}
-func (c *Collection) DropIndexes() os.Error { return c.DropIndex("*") }
+func (c *Collection) DropIndexes() os.Error { return c.DropIndex("*") }
func (c *Collection) DropIndex(name string) os.Error {
- cmdm := map[string]string{"deleteIndexes": c.fullName(), "index": name};
- cmd, err := Marshal(cmdm);
+ cmdm := map[string]string{"deleteIndexes": c.fullName(), "index": name}
+ cmd, err := Marshal(cmdm)
if err != nil {
return err
}
- _, err = c.db.Command(cmd);
- return err;
+ _, err = c.db.Command(cmd)
+ return err
}
func (c *Collection) Drop() os.Error {
- cmdm := map[string]string{"drop": c.fullName()};
- cmd, err := Marshal(cmdm);
+ cmdm := map[string]string{"drop": c.fullName()}
+ cmd, err := Marshal(cmdm)
if err != nil {
return err
}
- _, err = c.db.Command(cmd);
- return err;
+ _, err = c.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);
+ im := &insertMsg{c.fullName(), doc, rand.Int31()}
+ return c.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);
+ dm := &deleteMsg{c.fullName(), selector, rand.Int31()}
+ return c.db.conn.writeMessage(dm)
}
func (coll *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};
+ req_id := rand.Int31()
+ conn := coll.db.conn
+ qm := &queryMsg{0, coll.fullName(), int32(skip), int32(limit), query, req_id}
- err := conn.writeMessage(qm);
+ err := conn.writeMessage(qm)
if err != nil {
return nil, err
}
- reply, err := conn.readReply();
+ reply, err := conn.readReply()
if err != nil {
return nil, err
}
if reply.responseTo != req_id {
return nil, os.NewError("wrong responseTo code")
}
- return &Cursor{coll, reply.cursorID, 0, reply.docs}, nil;
+ return &Cursor{coll, reply.cursorID, 0, reply.docs}, nil
}
func (coll *Collection) FindAll(query BSON) (*Cursor, os.Error) {
return coll.Query(query, 0, 0)
}
func (coll *Collection) FindOne(query BSON) (BSON, os.Error) {
- cursor, err := coll.Query(query, 0, 1);
+ cursor, err := coll.Query(query, 0, 1)
if err != nil {
return nil, err
}
- return cursor.GetNext();
+ return cursor.GetNext()
}
func (coll *Collection) Count(query BSON) (int64, os.Error) {
- cmd := &_Object{map[string]BSON{"count": &_String{coll.name, _Null{}}, "query": query}, _Null{}};
- reply, err := coll.db.Command(cmd);
+ cmd := &_Object{map[string]BSON{"count": &_String{coll.name, _Null{}}, "query": query}, _Null{}}
+ reply, err := coll.db.Command(cmd)
if err != nil {
return -1, err
}
- return int64(reply.Get("n").Number()), nil;
+ return int64(reply.Get("n").Number()), nil
}
func (coll *Collection) update(um *updateMsg) os.Error {
- um.requestID = rand.Int31();
- conn := coll.db.conn;
- return conn.writeMessage(um);
+ um.requestID = rand.Int31()
+ conn := coll.db.conn
+ return conn.writeMessage(um)
}
func (coll *Collection) Update(selector, document BSON) os.Error {
@@ -298,181 +298,182 @@ func (coll *Collection) UpsertAll(selector, document BSON) os.Error {
}
func (db *Database) Command(cmd BSON) (BSON, os.Error) {
- coll := db.GetCollection("$cmd");
- return coll.FindOne(cmd);
+ coll := db.GetCollection("$cmd")
+ return coll.FindOne(cmd)
}
type queryMsg struct {
- opts int32;
- fullCollectionName string;
- numberToSkip int32;
- numberToReturn int32;
- query BSON;
- requestID int32;
+ opts int32
+ fullCollectionName string
+ numberToSkip int32
+ numberToReturn int32
+ query BSON
+ requestID int32
}
-func (q *queryMsg) OpCode() int32 { return _OP_QUERY }
-func (q *queryMsg) RequestID() int32 { return q.requestID }
+func (q *queryMsg) OpCode() int32 { return _OP_QUERY }
+func (q *queryMsg) RequestID() int32 { return q.requestID }
func (q *queryMsg) Bytes() []byte {
- b := make([]byte, 4);
- binary.LittleEndian.PutUint32(b, uint32(q.opts));
+ b := make([]byte, 4)
+ binary.LittleEndian.PutUint32(b, uint32(q.opts))
- buf := bytes.NewBuffer(b);
- buf.WriteString(q.fullCollectionName);
- buf.WriteByte(0);
+ buf := bytes.NewBuffer(b)
+ buf.WriteString(q.fullCollectionName)
+ buf.WriteByte(0)
- binary.LittleEndian.PutUint32(b, uint32(q.numberToSkip));
- buf.Write(b);
+ binary.LittleEndian.PutUint32(b, uint32(q.numberToSkip))
+ buf.Write(b)
- binary.LittleEndian.PutUint32(b, uint32(q.numberToReturn));
- buf.Write(b);
+ binary.LittleEndian.PutUint32(b, uint32(q.numberToReturn))
+ buf.Write(b)
- buf.Write(q.query.Bytes());
- return buf.Bytes();
+ buf.Write(q.query.Bytes())
+ return buf.Bytes()
}
type insertMsg struct {
- fullCollectionName string;
- doc BSON;
- requestID int32;
+ fullCollectionName string
+ doc BSON
+ requestID int32
}
-func (i *insertMsg) OpCode() int32 { return _OP_INSERT }
-func (i *insertMsg) RequestID() int32 { return i.requestID }
+func (i *insertMsg) OpCode() int32 { return _OP_INSERT }
+func (i *insertMsg) RequestID() int32 { return i.requestID }
func (i *insertMsg) Bytes() []byte {
- buf := bytes.NewBuffer(make([]byte, 4));
- buf.WriteString(i.fullCollectionName);
- buf.WriteByte(0);
- buf.Write(i.doc.Bytes());
- return buf.Bytes();
+ buf := bytes.NewBuffer(make([]byte, 4))
+ buf.WriteString(i.fullCollectionName)
+ buf.WriteByte(0)
+ buf.Write(i.doc.Bytes())
+ return buf.Bytes()
}
type deleteMsg struct {
- fullCollectionName string;
- selector BSON;
- requestID int32;
+ fullCollectionName string
+ selector BSON
+ requestID int32
}
-func (d *deleteMsg) OpCode() int32 { return _OP_DELETE }
-func (d *deleteMsg) RequestID() int32 { return d.requestID }
+func (d *deleteMsg) OpCode() int32 { return _OP_DELETE }
+func (d *deleteMsg) RequestID() int32 { return d.requestID }
func (d *deleteMsg) Bytes() []byte {
- zero := make([]byte, 4);
- buf := bytes.NewBuffer(zero);
- buf.WriteString(d.fullCollectionName);
- buf.WriteByte(0);
- buf.Write(zero);
- buf.Write(d.selector.Bytes());
- return buf.Bytes();
+ zero := make([]byte, 4)
+ buf := bytes.NewBuffer(zero)
+ buf.WriteString(d.fullCollectionName)
+ buf.WriteByte(0)
+ buf.Write(zero)
+ buf.Write(d.selector.Bytes())
+ return buf.Bytes()
}
type getMoreMsg struct {
- fullCollectionName string;
- numberToReturn int32;
- cursorID int64;
- requestID int32;
+ fullCollectionName string
+ numberToReturn int32
+ cursorID int64
+ requestID int32
}
-func (g *getMoreMsg) OpCode() int32 { return _OP_GET_MORE }
-func (g *getMoreMsg) RequestID() int32 { return g.requestID }
+func (g *getMoreMsg) OpCode() int32 { return _OP_GET_MORE }
+func (g *getMoreMsg) RequestID() int32 { return g.requestID }
func (g *getMoreMsg) Bytes() []byte {
- buf := bytes.NewBuffer(make([]byte, 4));
- buf.WriteString(g.fullCollectionName);
- buf.WriteByte(0);
+ buf := bytes.NewBuffer(make([]byte, 4))
+ buf.WriteString(g.fullCollectionName)
+ buf.WriteByte(0)
- b := make([]byte, 4);
- binary.LittleEndian.PutUint32(b, uint32(g.numberToReturn));
- buf.Write(b);
+ b := make([]byte, 4)
+ binary.LittleEndian.PutUint32(b, uint32(g.numberToReturn))
+ buf.Write(b)
- b = make([]byte, 8);
- binary.LittleEndian.PutUint64(b, uint64(g.cursorID));
- buf.Write(b);
+ b = make([]byte, 8)
+ binary.LittleEndian.PutUint64(b, uint64(g.cursorID))
+ buf.Write(b)
- return buf.Bytes();
+ return buf.Bytes()
}
func (db *Database) GetCollectionNames() *vector.StringVector {
return new(vector.StringVector)
}
type replyMsg struct {
- responseTo int32;
- responseFlag int32;
- cursorID int64;
- startingFrom int32;
- numberReturned int32;
- docs *vector.Vector;
+ responseTo int32
+ responseFlag int32
+ cursorID int64
+ startingFrom int32
+ numberReturned int32
+ docs *vector.Vector
}
func parseReply(b []byte) *replyMsg {
- r := new(replyMsg);
- 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 := new(replyMsg)
+ 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.docs = new(vector.Vector)
if r.numberReturned > 0 {
- buf := bytes.NewBuffer(b[36:len(b)]);
+ buf := bytes.NewBuffer(b[36:len(b)])
for i := 0; int32(i) < r.numberReturned; i++ {
- var bson BSON;
- bb := new(_BSONBuilder);
- bb.ptr = &bson;
- bb.Object();
- Parse(buf, bb);
- r.docs.Push(bson);
- ioutil.ReadAll(io.LimitReader(buf, 4));
+ var bson BSON
+ bb := new(_BSONBuilder)
+ bb.ptr = &bson
+ bb.Object()
+ Parse(buf, bb)
+ r.docs.Push(bson)
+ ioutil.ReadAll(io.LimitReader(buf, 4))
}
}
- return r;
+ return r
}
type updateMsg struct {
- fullCollectionName string;
- flags int32;
- selector, document BSON;
- requestID int32;
+ fullCollectionName string
+ flags int32
+ selector, document BSON
+ requestID int32
}
-func (u *updateMsg) OpCode() int32 { return _OP_UPDATE }
-func (u *updateMsg) RequestID() int32 { return u.requestID }
+func (u *updateMsg) OpCode() int32 { return _OP_UPDATE }
+func (u *updateMsg) RequestID() int32 { return u.requestID }
func (u *updateMsg) Bytes() []byte {
- buf := bytes.NewBuffer(make([]byte, 4));
- buf.WriteString(u.fullCollectionName);
- buf.WriteByte(0);
+ buf := bytes.NewBuffer(make([]byte, 4))
+ buf.WriteString(u.fullCollectionName)
+ buf.WriteByte(0)
- b := make([]byte, 4);
- binary.LittleEndian.PutUint32(b, uint32(u.flags));
- buf.Write(b);
+ b := make([]byte, 4)
+ binary.LittleEndian.PutUint32(b, uint32(u.flags))
+ buf.Write(b)
- buf.Write(u.selector.Bytes());
- buf.Write(u.document.Bytes());
+ buf.Write(u.selector.Bytes())
+ buf.Write(u.document.Bytes())
- return buf.Bytes();
+ return buf.Bytes()
}
type killMsg struct {
- numberOfCursorIDs int32;
- cursorIDs []int64;
- requestID int32;
+ numberOfCursorIDs int32
+ cursorIDs []int64
+ requestID int32
}
-func (k *killMsg) OpCode() int32 { return _OP_KILL_CURSORS }
-func (k *killMsg) RequestID() int32 { return k.requestID }
+func (k *killMsg) OpCode() int32 { return _OP_KILL_CURSORS }
+func (k *killMsg) RequestID() int32 { return k.requestID }
func (k *killMsg) Bytes() []byte {
- buf := bytes.NewBuffer(make([]byte, 4));
+ buf := bytes.NewBuffer(make([]byte, 4))
- b := make([]byte, 4);
- binary.LittleEndian.PutUint32(b, uint32(k.numberOfCursorIDs));
- buf.Write(b);
+ b := make([]byte, 4)
+ binary.LittleEndian.PutUint32(b, uint32(k.numberOfCursorIDs))
+ buf.Write(b)
- b = make([]byte, 8);
+ b = make([]byte, 8)
for _, id := range k.cursorIDs {
- binary.LittleEndian.PutUint64(b, uint64(id));
- buf.Write(b);
+ binary.LittleEndian.PutUint64(b, uint64(id))
+ buf.Write(b)
}
- return buf.Bytes();
+ return buf.Bytes()
}
+
View
231 mongo/mongo_test.go
@@ -5,234 +5,234 @@
package mongo_test
import (
- "mongo";
- "testing";
- "fmt";
- "time";
+ "mongo"
+ "testing"
+ "fmt"
+ "time"
)
type KeyStruct struct {
- First int32;
+ First int32
}
type IndexCmd struct {
- Name, Ns string;
- Key *KeyStruct;
+ Name, Ns string
+ Key *KeyStruct
}
func TestStuff(t *testing.T) {
- obj, err := mongo.BytesToBSON([]byte{92, 0, 0, 0, 1, 115, 101, 99, 111, 110, 100, 0, 0, 0, 0, 0, 0, 0, 0, 64, 3, 102, 105, 102, 116, 104, 0, 23, 0, 0, 0, 2, 118, 0, 2, 0, 0, 0, 101, 0, 2, 102, 0, 2, 0, 0, 0, 105, 0, 0, 3, 102, 111, 117, 114, 116, 104, 0, 5, 0, 0, 0, 0, 2, 116, 104, 105, 114, 100, 0, 6, 0, 0, 0, 116, 104, 114, 101, 101, 0, 16, 102, 105, 114, 115, 116, 0, 1, 0, 0, 0, 0});
- assertTrue(err == nil, "failed parsing BSON obj", t);
+ obj, err := mongo.BytesToBSON([]byte{92, 0, 0, 0, 1, 115, 101, 99, 111, 110, 100, 0, 0, 0, 0, 0, 0, 0, 0, 64, 3, 102, 105, 102, 116, 104, 0, 23, 0, 0, 0, 2, 118, 0, 2, 0, 0, 0, 101, 0, 2, 102, 0, 2, 0, 0, 0, 105, 0, 0, 3, 102, 111, 117, 114, 116, 104, 0, 5, 0, 0, 0, 0, 2, 116, 104, 105, 114, 100, 0, 6, 0, 0, 0, 116, 104, 114, 101, 101, 0, 16, 102, 105, 114, 115, 116, 0, 1, 0, 0, 0, 0})
+ assertTrue(err == nil, "failed parsing BSON obj", t)
- conn, err := mongo.Connect("127.0.0.1", 27017);
- assertTrue(err == nil && conn != nil, fmt.Sprintf("failed connecting to mongo: %v", err), t);
+ conn, err := mongo.Connect("127.0.0.1", 27017)
+ assertTrue(err == nil && conn != nil, fmt.Sprintf("failed connecting to mongo: %v", err), t)
- db := conn.GetDB("go_driver_tests");
- coll := db.GetCollection("coll");
- coll.Drop();
+ db := conn.GetDB("go_driver_tests")
+ coll := db.GetCollection("coll")
+ coll.Drop()
- coll.Insert(obj);
+ coll.Insert(obj)
- q, _ := mongo.Marshal(map[string]string{});
- ret, err := coll.FindAll(q);
- assertTrue(err == nil && ret != nil, "query succeeded", t);
+ q, _ := mongo.Marshal(map[string]string{})
+ ret, err := coll.FindAll(q)
+ assertTrue(err == nil && ret != nil, "query succeeded", t)
- doc, _ := ret.GetNext();
- assertTrue(doc.Kind() == mongo.ObjectKind, "query returned document", t);
- assertTrue(doc.Get("first").Int() == 1, "returned doc has proper 'first' element", t);
- assertTrue(doc.Get("second").Number() == 2, "returned doc has proper 'second' element", t);
- assertTrue(doc.Get("third").String() == "three", "returned doc has proper 'third' element", t);
- assertTrue(doc.Get("fourth").Kind() == mongo.ObjectKind, "returned doc has proper 'fourth' element", t);
- assertTrue(doc.Get("fifth").Get("f").String() == "i" && doc.Get("fifth").Get("v").String() == "e", "returned doc has proper 'fifth' element", t);
+ doc, _ := ret.GetNext()
+ assertTrue(doc.Kind() == mongo.ObjectKind, "query returned document", t)
+ assertTrue(doc.Get("first").Int() == 1, "returned doc has proper 'first' element", t)
+ assertTrue(doc.Get("second").Number() == 2, "returned doc has proper 'second' element", t)
+ assertTrue(doc.Get("third").String() == "three", "returned doc has proper 'third' element", t)
+ assertTrue(doc.Get("fourth").Kind() == mongo.ObjectKind, "returned doc has proper 'fourth' element", t)
+ assertTrue(doc.Get("fifth").Get("f").String() == "i" && doc.Get("fifth").Get("v").String() == "e", "returned doc has proper 'fifth' element", t)
- count, err := coll.Count(q);
- assertTrue(count == 1, "count", t);
+ count, err := coll.Count(q)
+ assertTrue(count == 1, "count", t)
- newDoc, _ := mongo.Marshal(map[string]string{"first": "one", "second": "two", "third": "three"});
- coll.Update(q, newDoc);
- doc, _ = coll.FindOne(q);
- assertTrue(doc.Get("first").String() == "one", "update", t);
+ newDoc, _ := mongo.Marshal(map[string]string{"first": "one", "second": "two", "third": "three"})
+ coll.Update(q, newDoc)
+ doc, _ = coll.FindOne(q)
+ assertTrue(doc.Get("first").String() == "one", "update", t)
- rem, _ := mongo.Marshal(map[string]string{"third": "three"});
- coll.Remove(rem);
- doc, err = coll.FindOne(rem);
- assertTrue(err != nil, "remove", t);
+ rem, _ := mongo.Marshal(map[string]string{"third": "three"})
+ coll.Remove(rem)
+ doc, err = coll.FindOne(rem)
+ assertTrue(err != nil, "remove", t)
- coll.Drop();
+ coll.Drop()
- statusCmd, _ := mongo.Marshal(map[string]float64{"serverStatus": 1});
- status, _ := db.Command(statusCmd);
- assertTrue(status.Get("uptime").Number() != 0, "valid serverStatus", t);
+ statusCmd, _ := mongo.Marshal(map[string]float64{"serverStatus": 1})
+ status, _ := db.Command(statusCmd)
+ assertTrue(status.Get("uptime").Number() != 0, "valid serverStatus", t)
- db.Drop();
+ db.Drop()
}
func TestOtherStuff(t *testing.T) {
- doc, _ := mongo.Marshal(map[string]string{"_id": "doc1", "title": "A Mongo document", "content": "Testing, 1. 2. 3."});
- conn, _ := mongo.Connect("127.0.0.1", 27017);
- collection := conn.GetDB("test").GetCollection("test_collection");
- collection.Insert(doc);
+ doc, _ := mongo.Marshal(map[string]string{"_id": "doc1", "title": "A Mongo document", "content": "Testing, 1. 2. 3."})
+ conn, _ := mongo.Connect("127.0.0.1", 27017)
+ collection := conn.GetDB("test").GetCollection("test_collection")
+ collection.Insert(doc)
- query, _ := mongo.Marshal(map[string]string{"_id": "doc1"});
- got, _ := collection.FindOne(query);
- assertTrue(mongo.Equal(doc, got), "equal", t);
+ query, _ := mongo.Marshal(map[string]string{"_id": "doc1"})
+ got, _ := collection.FindOne(query)
+ assertTrue(mongo.Equal(doc, got), "equal", t)
}
const (
- PER_TRIAL = 1000;
- BATCH_SIZE = 100;
+ PER_TRIAL = 1000
+ BATCH_SIZE = 100
)
func timeIt(s string, f func(*mongo.Collection, *testing.T), coll *mongo.Collection, t *testing.T) {
- start := time.Nanoseconds();
- f(coll, t);
- end := time.Nanoseconds();
- diff := end - start;
- ops := (PER_TRIAL / float64(diff)) * 1000000000.0;
- secs := float64(diff) / 1000000000.0;
- t.Logf("%v: %v secs, %v OPS", s, secs, ops);
+ start := time.Nanoseconds()
+ f(coll, t)
+ end := time.Nanoseconds()
+ diff := end - start
+ ops := (PER_TRIAL / float64(diff)) * 1000000000.0
+ secs := float64(diff) / 1000000000.0
+ t.Logf("%v: %v secs, %v OPS", s, secs, ops)
}
func TestBenchmark(t *testing.T) {
- conn, err := mongo.Connect("127.0.0.1", 27017);
+ conn, err := mongo.Connect("127.0.0.1", 27017)
if err != nil {
t.Error("failed connecting")
}
- db := conn.GetDB("perf_test");
- db.Drop();
- db.GetCollection("creation").Insert(mongo.EmptyObject);
- db.GetCollection("creation").Count(mongo.EmptyObject);
+ db := conn.GetDB("perf_test")
+ db.Drop()
+ db.GetCollection("creation").Insert(mongo.EmptyObject)
+ db.GetCollection("creation").Count(mongo.EmptyObject)
- timeIt("single.small Insert", singleInsertSmall, db.GetCollection("single.small"), t);
- timeIt("single.medium Insert", singleInsertMedium, db.GetCollection("single.medium"), t);
- timeIt("single.large Insert", singleInsertLarge, db.GetCollection("single.large"), t);
- timeIt("single.small FindOne", findOneSmall, db.GetCollection("single.small"), t);
- timeIt("single.medium FindOne", findOneMedium, db.GetCollection("single.medium"), t);
- timeIt("single.large FindOne", findOne, db.GetCollection("single.large"), t);
+ timeIt("single.small Insert", singleInsertSmall, db.GetCollection("single.small"), t)
+ timeIt("single.medium Insert", singleInsertMedium, db.GetCollection("single.medium"), t)
+ timeIt("single.large Insert", singleInsertLarge, db.GetCollection("single.large"), t)
+ timeIt("single.small FindOne", findOneSmall, db.GetCollection("single.small"), t)
+ timeIt("single.medium FindOne", findOneMedium, db.GetCollection("single.medium"), t)
+ timeIt("single.large FindOne", findOne, db.GetCollection("single.large"), t)
- t.Log("---");
- db.GetCollection("single.small.indexed").EnsureIndex("my_index1", map[string]int{"x": 1});
- timeIt("single.small.indexed Insert", singleInsertSmall, db.GetCollection("single.small.indexed"), t);
+ t.Log("---")
+ db.GetCollection("single.small.indexed").EnsureIndex("my_index1", map[string]int{"x": 1})
+ timeIt("single.small.indexed Insert", singleInsertSmall, db.GetCollection("single.small.indexed"), t)
- db.GetCollection("single.medium.indexed").EnsureIndex("my_index2", map[string]int{"x": 1});
- timeIt("single.medium.indexed Insert", singleInsertMedium, db.GetCollection("single.medium.indexed"), t);
+ db.GetCollection("single.medium.indexed").EnsureIndex("my_index2", map[string]int{"x": 1})
+ timeIt("single.medium.indexed Insert", singleInsertMedium, db.GetCollection("single.medium.indexed"), t)
- db.GetCollection("single.large.indexed").EnsureIndex("my_index3", map[string]int{"x": 1});
- timeIt("single.large.indexed Insert", singleInsertLarge, db.GetCollection("single.large.indexed"), t);
+ db.GetCollection("single.large.indexed").EnsureIndex("my_index3", map[string]int{"x": 1})
+ timeIt("single.large.indexed Insert", singleInsertLarge, db.GetCollection("single.large.indexed"), t)
- timeIt("single.small.indexed FindOne", findOneSmall, db.GetCollection("single.small.indexed"), t);
- timeIt("single.medium.indexed FindOne", findOneMedium, db.GetCollection("single.medium.indexed"), t);
- timeIt("single.large.indexed FindOne", findOne, db.GetCollection("single.large.indexed"), t);
+ timeIt("single.small.indexed FindOne", findOneSmall, db.GetCollection("single.small.indexed"), t)
+ timeIt("single.medium.indexed FindOne", findOneMedium, db.GetCollection("single.medium.indexed"), t)
+ timeIt("single.large.indexed FindOne", findOne, db.GetCollection("single.large.indexed"), t)
}
type smallStruct struct {
- X int;
+ X int
}
func singleInsertSmall(coll *mongo.Collection, t *testing.T) {
- ss := &smallStruct{0};
+ ss := &smallStruct{0}
for i := 0; i < PER_TRIAL; i++ {
- ss.X = i;
- obj, err := mongo.Marshal(ss);
+ ss.X = i
+ obj, err := mongo.Marshal(ss)
if err != nil {
t.Errorf("singleInsertSmall Marshal: %v\n", err)
}
- err = coll.Insert(obj);
+ err = coll.Insert(obj)
if err != nil {
t.Errorf("singleInsertSmall Insert: %v\n", err)
}
}
}
func findOneSmall(coll *mongo.Collection, t *testing.T) {
- ss := &smallStruct{PER_TRIAL / 2};
- obj, err := mongo.Marshal(ss);
+ ss := &smallStruct{PER_TRIAL / 2}
+ obj, err := mongo.Marshal(ss)
if err != nil {
t.Errorf("findOneSmall Marshal: %v\n", err)
}
for i := 0; i < PER_TRIAL; i++ {
- _, err = coll.FindOne(obj);
+ _, err = coll.FindOne(obj)
if err != nil {
t.Errorf("findOneSmall FindOne: %v\n", err)
}
}
}
type mediumStruct struct {
- Integer int;
- Number float64;
- Boolean bool;
- Array []string;
- X int;
+ Integer int
+ Number float64
+ Boolean bool
+ Array []string
+ X int
}
func singleInsertMedium(coll *mongo.Collection, t *testing.T) {
- ms := &mediumStruct{5, 5.05, false, []string{"test", "benchmark"}, 0};
+ ms := &mediumStruct{5, 5.05, false, []string{"test", "benchmark"}, 0}
for i := 0; i < PER_TRIAL; i++ {
- ms.X = i;
- obj, err := mongo.Marshal(ms);
+ ms.X = i
+ obj, err := mongo.Marshal(ms)
if err != nil {
t.Errorf("singleInsertMedium Marshal: %v\n", err)
}
- err = coll.Insert(obj);
+ err = coll.Insert(obj)
if err != nil {
t.Errorf("singleInsertMedium Insert: %v\n", err)
}
}
}