本文整理汇总了Golang中gopkg/in/mgo/v2/bson.Unmarshal函数的典型用法代码示例。如果您正苦于以下问题:Golang Unmarshal函数的具体用法?Golang Unmarshal怎么用?Golang Unmarshal使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Unmarshal函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestBSONTimestampManual
func TestBSONTimestampManual(t *testing.T) {
defer os.Setenv("TZ", os.Getenv("TZ"))
os.Setenv("TZ", "UTC")
for i, elem := range testData {
tt, err := ParseDateString(elem.out)
if err != nil {
t.Fatal(err)
}
buf, err := bson.Marshal(bson.M{"ts": elem.in})
if err != nil {
t.Fatal(err)
}
ts := &tsTest{}
if err := bson.Unmarshal(buf, &ts); err != nil {
t.Fatal(err)
}
if expect, got := tt, ts.TS; expect.Sub(got.Time) != 0 {
t.Errorf("[%d] Unexpected result.\nExpect:\t%s\nGot:\t%s\n", i, expect, got)
}
buf2, err := bson.Marshal(ts)
if err != nil {
t.Fatal(err)
}
if expect, got := fmt.Sprintf("%d", tt.Unix()), string(buf2); !strings.Contains(got, expect) {
t.Fatalf("Unexpected result.\nExpect:\t%s\nGot:\t%s\n", expect, got)
}
}
}
示例2: TestUnmarshalAllItemsWithPtrSetter
func (s *S) TestUnmarshalAllItemsWithPtrSetter(c *C) {
for _, item := range allItems {
for i := 0; i != 2; i++ {
var field *setterType
if i == 0 {
obj := &ptrSetterDoc{}
err := bson.Unmarshal([]byte(wrapInDoc(item.data)), obj)
c.Assert(err, IsNil)
field = obj.Field
} else {
obj := &valSetterDoc{}
err := bson.Unmarshal([]byte(wrapInDoc(item.data)), obj)
c.Assert(err, IsNil)
field = &obj.Field
}
if item.data == "" {
// Nothing to unmarshal. Should be untouched.
if i == 0 {
c.Assert(field, IsNil)
} else {
c.Assert(field.received, IsNil)
}
} else {
expected := item.obj.(bson.M)["_"]
c.Assert(field, NotNil, Commentf("Pointer not initialized (%#v)", expected))
c.Assert(field.received, DeepEquals, expected)
}
}
}
}
示例3: TestOpUnmarshal
func TestOpUnmarshal(t *testing.T) {
t.Parallel()
testCmd := bson.D{{"z", 1}, {"a", 1}}
testQuery := bson.D{{"a", 1}, {"z", 1}}
testCmdDoc := bson.D{
{"ts", time.Unix(testTime, 0)},
{"ns", "foo"},
{"op", "command"},
{"command", testCmd},
}
testQueryDoc := bson.D{
{"ts", time.Unix(testTime, 0)},
{"ns", "foo"},
{"op", "query"},
{"query", testQuery},
{"ntoskip", 1},
{"ntoreturn", 2},
}
// marshal to byte form so we can unmarshal into struct
testCmdDocBytes, err := bson.Marshal(testCmdDoc)
ensure.Nil(t, err)
testQueryDocBytes, err := bson.Marshal(testQueryDoc)
ensure.Nil(t, err)
var testCmdOp, testQueryOp Op
err = bson.Unmarshal(testCmdDocBytes, &testCmdOp)
ensure.Nil(t, err)
err = bson.Unmarshal(testQueryDocBytes, &testQueryOp)
ensure.Nil(t, err)
ensure.Subset(
t,
testCmdOp,
Op{
Timestamp: time.Unix(testTime, 0),
Ns: "foo",
Type: Command,
CommandDoc: testCmd,
},
)
ensure.Subset(
t,
testQueryOp,
Op{
Timestamp: time.Unix(testTime, 0),
Ns: "foo",
Type: Query,
QueryDoc: testQuery,
NToSkip: 1,
NToReturn: 2,
},
)
}
示例4: TestUnmarshalMapDocumentTooShort
func (s *S) TestUnmarshalMapDocumentTooShort(c *C) {
for _, data := range corruptedData {
err := bson.Unmarshal([]byte(data), bson.M{})
c.Assert(err, ErrorMatches, "Document is corrupted")
err = bson.Unmarshal([]byte(data), &struct{}{})
c.Assert(err, ErrorMatches, "Document is corrupted")
}
}
示例5: All
// TODO(fluffle): Dedupe this with Prefix when less hungover.
func (bucket *boltBucket) All(key Key, value interface{}) error {
// This entirely stolen from mgo's Iter.All() \o/
vv := reflect.ValueOf(value)
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Slice {
panic("All() requires a pointer-to-slice.")
}
sv := vv.Elem()
sv = sv.Slice(0, sv.Cap())
et := sv.Type().Elem()
return bucket.db.View(func(tx *bolt.Tx) error {
b, last, err := bucketFor(key, tx.Bucket(bucket.name))
if err != nil {
return err
}
// All implies that the last key elem is also a bucket,
// but we support a zero-length key to perform a scan
// over the root bucket.
cs := []*bolt.Cursor{b.Cursor()}
if len(last) > 0 {
if b = b.Bucket(last); b == nil {
return bolt.ErrBucketNotFound
}
cs[0] = b.Cursor()
}
var i int
var c *bolt.Cursor
for len(cs) > 0 {
c, cs = cs[0], cs[1:]
for k, v := c.First(); k != nil; k, v = c.Next() {
if v == nil {
// All flattens the nested buckets under key.
if nest := b.Bucket(k); nest != nil {
cs = append(cs, nest.Cursor())
}
continue
}
if sv.Len() == i {
ev := reflect.New(et)
if err := bson.Unmarshal(v, ev.Interface()); err != nil {
return err
}
sv = reflect.Append(sv, ev.Elem())
sv = sv.Slice(0, sv.Cap())
} else {
if err := bson.Unmarshal(v, sv.Index(i).Addr().Interface()); err != nil {
return err
}
}
i++
}
}
vv.Elem().Set(sv.Slice(0, i))
return nil
})
}
示例6: SetBSON
// SetBSON allows us to use dependency representation of both
// just task Ids and of true Dependency structs.
// TODO eventually drop all of this switching
func (d *Dependency) SetBSON(raw bson.Raw) error {
// copy the Dependency type to remove this SetBSON method but preserve bson struct tags
type nakedDep Dependency
var depCopy nakedDep
if err := raw.Unmarshal(&depCopy); err == nil {
if depCopy.TaskId != "" {
*d = Dependency(depCopy)
return nil
}
}
// hack to support the legacy depends_on, since we can't just unmarshal a string
strBytes, _ := bson.Marshal(bson.RawD{{"str", raw}})
var strStruct struct {
String string `bson:"str"`
}
if err := bson.Unmarshal(strBytes, &strStruct); err == nil {
if strStruct.String != "" {
d.TaskId = strStruct.String
d.Status = evergreen.TaskSucceeded
return nil
}
}
return bson.SetZero
}
示例7: TestParseLargeBSON
func TestParseLargeBSON(t *testing.T) {
largeArray := make([]interface{}, 5000)
for i := 0; i < 5000; i++ {
largeArray[i] = float64(i)
}
expectedOp := map[string]interface{}{
"ts": bson.MongoTimestamp(6048257058866724865), "h": int64(-6825742652110581687), "v": 2, "op": "i", "ns": "testdb.testdb", "o": map[string]interface{}{
"_id": bson.ObjectIdHex("53efb9c067fd92348e823860"),
"val": largeArray}}
f, err := os.Open("./largetestdata.bson")
if err != nil {
t.Fatal("Error loading file", err)
}
defer f.Close()
foundExpectedOp := false
scanner := New(f)
for scanner.Scan() {
op := map[string]interface{}{}
if err := bson.Unmarshal(scanner.Bytes(), &op); err != nil {
t.Fatal("Error unmarshalling: ", err)
}
if reflect.DeepEqual(op, expectedOp) {
foundExpectedOp = true
}
}
if scanner.Err() != nil {
t.Fatal("Scanner error: ", scanner.Err())
}
if !foundExpectedOp {
t.Fatal("Didn't find the expected operation")
}
}
示例8: Insert
func (self *MConn) Insert(table string, arguments ...interface{}) (_id string) {
//Create a Session Copy and be responsible for Closing it.
session := self.Session.Copy()
db := session.DB(self.Dbname)
defer session.Close()
var out interface{}
if len(arguments) > 1 {
out = arguments[1]
} else {
out = nil
}
doc := arguments[0]
coll := db.C(table)
err := coll.Insert(doc)
if err != nil {
panic(err)
}
if out != nil {
stream, merr := bson.Marshal(doc)
if merr == nil {
bson.Unmarshal(stream, out)
}
}
return
}
示例9: TestSetBSON
func TestSetBSON(t *testing.T) {
d0 := &fakeDoc{
Search: NewKeywords(),
}
d0.Search["foo"] = "xxx"
d0.Search["bar"] = "123"
d1 := &fakeDoc{
Search: NewKeywords(),
}
doc := bson.M{
"search": []bson.M{
{"group": "foo", "key": "xxx"},
{"group": "bar", "key": "123"},
},
}
out, err := bson.Marshal(doc)
if err != nil {
t.Fatalf("Error: %v", err)
}
err = bson.Unmarshal(out, d1)
if err != nil {
t.Fatalf("Error: %v", err)
}
if !reflect.DeepEqual(d0, d1) {
t.Errorf("Unexpected difference")
t.Logf("expected: %+v\n", d0)
t.Logf("got : %+v\n", d1)
}
}
示例10: TestParse32
func TestParse32(t *testing.T) {
var mongodb MongoDBPlugin
stub3_2_0 := `
{"host":"mbp13.local","advisoryHostFQDNs":["mbp13.local"],"version":"3.2.0","process":"mongod","pid":20843,"uptime":725,"uptimeMillis":724266,"uptimeEstimate":625,"localTime":"2016-01-06T08:02:12.187Z","asserts":{"regular":0,"warning":0,"msg":0,"user":0,"rollovers":0},"connections":{"current":1,"available":2047,"totalCreated":26},"extra_info":{"note":"fieldsvarybyplatform","page_faults":457},"globalLock":{"totalTime":724259000,"currentQueue":{"total":0,"readers":0,"writers":0},"activeClients":{"total":8,"readers":0,"writers":0}},"locks":{"Global":{"acquireCount":{"r":400,"w":2,"W":4}},"Database":{"acquireCount":{"r":197,"W":2}},"Collection":{"acquireCount":{"r":197}},"Metadata":{"acquireCount":{"w":1}}},"network":{"bytesIn":11962,"bytesOut":419980,"numRequests":174},"opcounters":{"insert":0,"query":1,"update":0,"delete":0,"getmore":0,"command":175},"opcountersRepl":{"insert":0,"query":0,"update":0,"delete":0,"getmore":0,"command":0},"storageEngine":{"name":"wiredTiger","supportsCommittedReads":true},"wiredTiger":{"uri":"statistics:","LSM":{"sleepforLSMcheckpointthrottle":0,"sleepforLSMmergethrottle":0,"rowsmergedinanLSMtree":0,"applicationworkunitscurrentlyqueued":0,"mergeworkunitscurrentlyqueued":0,"treequeuehitmaximum":0,"switchworkunitscurrentlyqueued":0,"treemaintenanceoperationsscheduled":0,"treemaintenanceoperationsdiscarded":0,"treemaintenanceoperationsexecuted":0},"async":{"numberofallocationstateraces":0,"numberofoperationslotsviewedforallocation":0,"currentworkqueuelength":0,"numberofflushcalls":0,"numberoftimesoperationallocationfailed":0,"maximumworkqueuelength":0,"numberoftimesworkerfoundnowork":0,"totalallocations":0,"totalcompactcalls":0,"totalinsertcalls":0,"totalremovecalls":0,"totalsearchcalls":0,"totalupdatecalls":0},"block-manager":{"mappedbytesread":0,"bytesread":4096,"byteswritten":86016,"mappedblocksread":0,"blockspre-loaded":0,"blocksread":1,"blockswritten":19},"cache":{"trackeddirtybytesinthecache":0,"trackedbytesbelongingtointernalpagesinthecache":1398,"bytescurrentlyinthecache":16143,"trackedbytesbelongingtoleafpagesinthecache":14745,"maximumbytesconfigured":9663676416,"trackedbytesbelongingtooverflowpagesinthecache":0,"bytesreadintocache":0,"byteswrittenfromcache":13061,"pagesevictedbyapplicationthreads":0,"checkpointblockedpageeviction":0,"unmodifiedpagesevicted":0,"pagesplitduringevictiondeepenedthetree":0,"modifiedpagesevicted":0,"pagesselectedforevictionunabletobeevicted":0,"pagesevictedbecausetheyexceededthein-memorymaximum":0,"pagesevictedbecausetheyhadchainsofdeleteditems":0,"failedevictionofpagesthatexceededthein-memorymaximum":0,"hazardpointerblockedpageeviction":0,"internalpagesevicted":0,"maximumpagesizeateviction":0,"evictionservercandidatequeueemptywhentoppingup":0,"evictionservercandidatequeuenotemptywhentoppingup":0,"evictionserverevictingpages":0,"evictionserverpopulatingqueue,butnotevictingpages":0,"evictionserverunabletoreachevictiongoal":0,"internalpagessplitduringeviction":0,"leafpagessplitduringeviction":0,"pageswalkedforeviction":0,"evictionworkerthreadevictingpages":0,"in-memorypagesplits":0,"in-memorypagepassedcriteriatobesplit":0,"lookasidetableinsertcalls":0,"lookasidetableremovecalls":0,"percentageoverhead":8,"trackeddirtypagesinthecache":0,"pagescurrentlyheldinthecache":11,"pagesreadintocache":0,"pagesreadintocacherequiringlookasideentries":0,"pageswrittenfromcache":12,"pagewrittenrequiringlookasiderecords":0,"pageswrittenrequiringin-memoryrestoration":0},"connection":{"pthreadmutexconditionwaitcalls":90553,"filescurrentlyopen":9,"memoryallocations":12590,"memoryfrees":12013,"memoryre-allocations":2360,"totalreadI/Os":8,"pthreadmutexsharedlockread-lockcalls":951,"pthreadmutexsharedlockwrite-lockcalls":706,"totalwriteI/Os":44},"cursor":{"cursorcreatecalls":131,"cursorinsertcalls":25,"cursornextcalls":13,"cursorprevcalls":3,"cursorremovecalls":0,"cursorresetcalls":56,"cursorrestartedsearches":0,"cursorsearchcalls":145,"cursorsearchnearcalls":1,"truncatecalls":0,"cursorupdatecalls":0},"data-handle":{"connectiondatahandlescurrentlyactive":6,"sessiondhandlesswept":0,"sessionsweepattempts":20,"connectionsweepdhandlesclosed":0,"connectionsweepcandidatebecamereferenced":0,"connectionsweepdhandlesremovedfromhashlist":1,"connectionsweeptime-of-deathsets":1,"connectionsweeps":72},"log":{"totallogbuffersize":33554432,"logbytesofpayloaddata":7566,"logbyteswritten":9856,"yieldswaitingforpreviouslogfileclose":0,"totalsizeofcompressedrecords":5927,"totalin-memorysizeofcompressedrecords":10292,"logrecordstoosmalltocompress":10,"logrecordsnotcompressed":7,"logrecordscompressed":8,"logflushoperations":7037,"maximumlogfilesize":104857600,"pre-allocatedlogfilesprepared":2,"numberofpre-allocatedlogfilestocreate":2,"pre-allocatedlogfilesnotreadyandmissed":1,"pre-allocatedlogfilesused":0,"logreleaseadvanceswriteLSN":8,"recordsprocessedbylogscan":0,"logscanrecordsrequiringtworeads":0,"logscanoperations":0,"consolidatedslotclosures":20847,"writtenslotscoalesced":0,"loggingbytesconsolidated":9472,"consolidatedslotjoins":25,"consolidatedslotjoinraces":0,"busyreturnsattemptingtoswitchslots":0,"consolidatedslotjointransitions":20847,"consolidatedslotunbufferedwrites":0,"logsyncoperations":12,"logsync_diroperations":1,"logserverthreadadvanceswriteLSN":5,"logwriteoperations":25,"logfilesmanuallyzero-filled":0},"reconciliation":{"pagesdeleted":0,"fast-pathpagesdeleted":0,"pagereconciliationcalls":12,"pagereconciliationcallsforeviction":0,"splitbytescurrentlyawaitingfree":0,"splitobjectscurrentlyawaitingfree":0},"session":{"opencursorcount":13,"opensessioncount":15},"thread-yield":{"pageacquirebusyblocked":0,"pageacquireevictionblocked":0,"pageacquirelockedblocked":0,"pageacquirereadblocked":0,"pageacquiretimesleeping(usecs)":0},"transaction":{"transactionbegins":16,"transactioncheckpoints":12,"transactioncheckpointgeneration":12,"transactioncheckpointcurrentlyrunning":0,"transactioncheckpointmaxtime(msecs)":77,"transactioncheckpointmintime(msecs)":0,"transactioncheckpointmostrecenttime(msecs)":0,"transactioncheckpointtotaltime(msecs)":138,"transactionscommitted":3,"transactionfailuresduetocacheoverflow":0,"transactionrangeofIDscurrentlypinnedbyacheckpoint":0,"transactionrangeofIDscurrentlypinned":0,"transactionrangeofIDscurrentlypinnedbynamedsnapshots":0,"transactionsrolledback":13,"numberofnamedsnapshotscreated":0,"numberofnamedsnapshotsdropped":0,"transactionsynccalls":0},"concurrentTransactions":{"write":{"out":0,"available":128,"totalTickets":128},"read":{"out":0,"available":128,"totalTickets":128}}},"writeBacksQueued":false,"mem":{"bits":64,"resident":52,"virtual":2511,"supported":true,"mapped":0,"mappedWithJournal":0},"metrics":{"commands":{"buildInfo":{"failed":0,"total":23},"getLog":{"failed":0,"total":2},"getnonce":{"failed":0,"total":3},"isMaster":{"failed":0,"total":71},"ping":{"failed":0,"total":6},"replSetGetStatus":{"failed":23,"total":23},"serverStatus":{"failed":0,"total":24},"whatsmyuri":{"failed":0,"total":23}},"cursor":{"timedOut":0,"open":{"noTimeout":0,"pinned":0,"total":0}},"document":{"deleted":0,"inserted":0,"returned":0,"updated":0},"getLastError":{"wtime":{"num":0,"totalMillis":0},"wtimeouts":0},"operation":{"fastmod":0,"idhack":0,"scanAndOrder":0,"writeConflicts":0},"queryExecutor":{"scanned":0,"scannedObjects":0},"record":{"moves":0},"repl":{"executor":{"counters":{"eventCreated":0,"eventWait":0,"cancels":0,"waits":0,"scheduledNetCmd":0,"scheduledDBWork":0,"scheduledXclWork":0,"scheduledWorkAt":0,"scheduledWork":0,"schedulingFailures":0},"queues":{"networkInProgress":0,"dbWorkInProgress":0,"exclusiveInProgress":0,"sleepers":0,"ready":0,"free":0},"unsignaledEvents":0,"eventWaiters":0,"shuttingDown":false,"networkInterface":"NetworkInterfaceASIOinShutdown:0"},"apply":{"batches":{"num":0,"totalMillis":0},"ops":0},"buffer":{"count":0,"maxSizeBytes":268435456,"sizeBytes":0},"network":{"bytes":0,"getmores":{"num":0,"totalMillis":0},"ops":0,"readersCreated":0},"preload":{"docs":{"num":0,"totalMillis":0},"indexes":{"num":0,"totalMillis":0}}},"storage":{"freelist":{"search":{"bucketExhausted":0,"requests":0,"scanned":0}}},"ttl":{"deletedDocuments":0,"passes":12}},"ok":1}
`
var v interface{}
err := json.Unmarshal([]byte(stub3_2_0), &v)
if err != nil {
t.Errorf("Error: %s", err.Error())
}
bsonStats, err := bson.Marshal(v)
if err != nil {
t.Errorf("Error: %s", err.Error())
}
var m bson.M
err = bson.Unmarshal(bsonStats, &m)
if err != nil {
t.Errorf("Error: %s", err.Error())
}
stat, err := mongodb.parseStatus(m)
fmt.Println(stat)
assert.Nil(t, err)
// Mongodb Stats
assert.EqualValues(t, reflect.TypeOf(stat["opcounters_command"]).String(), "float64")
assert.EqualValues(t, stat["opcounters_command"], 175)
}
示例11: printBSON
func printBSON(raw bson.Raw, indentLevel int, out io.Writer) error {
indent := strings.Repeat("\t", indentLevel)
fmt.Fprintf(out, "%v--- new object ---\n", indent)
fmt.Fprintf(out, "%v\tsize : %v\n", indent, len(raw.Data))
//Convert raw into an array of RawD we can iterate over.
var rawD bson.RawD
err := bson.Unmarshal(raw.Data, &rawD)
if err != nil {
return err
}
for _, rawElem := range rawD {
fmt.Fprintf(out, "%v\t\t%v\n", indent, rawElem.Name)
// the size of an element is the combined size of the following:
// 1. 1 byte for the BSON type
// 2. 'e_name' : the BSON key, which is a null-terminated cstring
// 3. The BSON value
// So size == 1 [size of type byte] + 1 [null byte for cstring key] + len(bson key) + len(bson value)
// see http://bsonspec.org/spec.html for more details
fmt.Fprintf(out, "%v\t\t\ttype: %4v size: %v\n", indent, int8(rawElem.Value.Kind),
2+len(rawElem.Name)+len(rawElem.Value.Data))
//For nested objects or arrays, recurse.
if rawElem.Value.Kind == 0x03 || rawElem.Value.Kind == 0x04 {
err = printBSON(rawElem.Value, indentLevel+3, out)
if err != nil {
return err
}
}
}
return nil
}
示例12: TestParse26
func TestParse26(t *testing.T) {
var mongodb MongoDBPlugin
stub2_6_11 := `
{"asserts":{"msg":0,"regular":0,"rollovers":0,"user":0,"warning":0},"backgroundFlushing":{"average_ms":0,"flushes":0,"last_finished":"1970-01-01T09:00:00+09:00","last_ms":0,"total_ms":0},"connections":{"available":818,"current":1,"totalCreated":1},"cursors":{"clientCursors_size":0,"note":"deprecated, use server status metrics","pinned":0,"timedOut":0,"totalNoTimeout":0,"totalOpen":0},"dur":{"commits":30,"commitsInWriteLock":0,"compression":0,"earlyCommits":0,"journaledMB":0,"timeMs":{"dt":3074,"prepLogBuffer":0,"remapPrivateView":0,"writeToDataFiles":0,"writeToJournal":0},"writeToDataFilesMB":0},"extra_info":{"heap_usage_bytes":62512008,"note":"fields vary by platform","page_faults":228},"globalLock":{"activeClients":{"readers":0,"total":0,"writers":0},"currentQueue":{"readers":0,"total":0,"writers":0},"lockTime":68622,"totalTime":6583000},"host":"08ea07b5a8fd","indexCounters":{"accesses":2,"hits":5,"missRatio":0,"misses":0,"resets":0},"localTime":"2015-08-17T15:08:44.187+09:00","locks":{".":{"timeAcquiringMicros":{"R":254,"W":94},"timeLockedMicros":{"R":520,"W":68622}},"admin":{"timeAcquiringMicros":{"r":28,"w":0},"timeLockedMicros":{"r":338,"w":0}},"local":{"timeAcquiringMicros":{"r":22,"w":0},"timeLockedMicros":{"r":46,"w":0}}},"mem":{"bits":64,"mapped":80,"mappedWithJournal":160,"resident":36,"supported":true,"virtual":342},"metrics":{"cursor":{"open":{"noTimeout":0,"pinned":0,"total":0},"timedOut":0},"document":{"deleted":0,"inserted":1,"returned":0,"updated":0},"getLastError":{"wtime":{"num":0,"totalMillis":0},"wtimeouts":0},"operation":{"fastmod":0,"idhack":0,"scanAndOrder":0},"queryExecutor":{"scanned":0,"scannedObjects":0},"record":{"moves":0},"repl":{"apply":{"batches":{"num":0,"totalMillis":0},"ops":0},"buffer":{"count":0,"maxSizeBytes":268435456,"sizeBytes":0},"network":{"bytes":0,"getmores":{"num":0,"totalMillis":0},"ops":0,"readersCreated":0},"preload":{"docs":{"num":0,"totalMillis":0},"indexes":{"num":0,"totalMillis":0}}},"storage":{"freelist":{"search":{"bucketExhausted":0,"requests":6,"scanned":11}}},"ttl":{"deletedDocuments":0,"passes":0}},"network":{"bytesIn":224,"bytesOut":380,"numRequests":4},"ok":1,"opcounters":{"command":6,"delete":0,"getmore":0,"insert":1,"query":1,"update":0},"opcountersRepl":{"command":0,"delete":0,"getmore":0,"insert":0,"query":0,"update":0},"pid":1,"process":"mongod","recordStats":{"accessesNotInMemory":0,"admin":{"accessesNotInMemory":0,"pageFaultExceptionsThrown":0},"local":{"accessesNotInMemory":0,"pageFaultExceptionsThrown":0},"pageFaultExceptionsThrown":0},"uptime":7,"uptimeEstimate":6,"uptimeMillis":6581,"version":"2.6.11","writeBacksQueued":false}
`
var v interface{}
err := json.Unmarshal([]byte(stub2_6_11), &v)
if err != nil {
t.Errorf("Error: %s", err.Error())
}
bsonStats, err := bson.Marshal(v)
if err != nil {
t.Errorf("Error: %s", err.Error())
}
var m bson.M
err = bson.Unmarshal(bsonStats, &m)
if err != nil {
t.Errorf("Error: %s", err.Error())
}
stat, err := mongodb.parseStatus(m)
fmt.Println(stat)
assert.Nil(t, err)
// Mongodb Stats
assert.EqualValues(t, reflect.TypeOf(stat["btree_hits"]).String(), "float64")
assert.EqualValues(t, stat["btree_hits"], 5.0)
}
示例13: TestParse22
func TestParse22(t *testing.T) {
var mongodb MongoDBPlugin
stub2_2_7 := `
{"asserts":{"msg":0,"regular":0,"rollovers":0,"user":0,"warning":0},"backgroundFlushing":{"average_ms":0,"flushes":0,"last_finished":"1970-01-01T09:00:00+09:00","last_ms":0,"total_ms":0},"connections":{"available":818,"current":1},"cursors":{"clientCursors_size":0,"timedOut":0,"totalOpen":0},"dur":{"commits":30,"commitsInWriteLock":0,"compression":0,"earlyCommits":0,"journaledMB":0,"timeMs":{"dt":3074,"prepLogBuffer":0,"remapPrivateView":0,"writeToDataFiles":0,"writeToJournal":0},"writeToDataFilesMB":0},"extra_info":{"heap_usage_bytes":25585584,"note":"fields vary by platform","page_faults":136},"globalLock":{"activeClients":{"readers":0,"total":0,"writers":0},"currentQueue":{"readers":0,"total":0,"writers":0},"lockTime":1638,"totalTime":35489000},"host":"58a1c98acba3","indexCounters":{"btree":{"accesses":0,"hits":5,"missRatio":0,"misses":0,"resets":0}},"localTime":"2015-08-17T15:08:02.677+09:00","locks":{".":{"timeAcquiringMicros":{"R":1593,"W":279},"timeLockedMicros":{"R":1906,"W":1638}},"admin":{"timeAcquiringMicros":{},"timeLockedMicros":{}},"local":{"timeAcquiringMicros":{"r":9,"w":0},"timeLockedMicros":{"r":44,"w":0}}},"mem":{"bits":64,"mapped":0,"mappedWithJournal":0,"resident":30,"supported":true,"virtual":128},"network":{"bytesIn":510,"bytesOut":2319,"numRequests":9},"ok":1,"opcounters":{"command":10,"delete":0,"getmore":0,"insert":0,"query":0,"update":0},"pid":1,"process":"mongod","recordStats":{"accessesNotInMemory":0,"local":{"accessesNotInMemory":0,"pageFaultExceptionsThrown":0},"pageFaultExceptionsThrown":0},"uptime":35,"uptimeEstimate":34,"uptimeMillis":35489,"version":"2.2.7","writeBacksQueued":false}
`
var v interface{}
err := json.Unmarshal([]byte(stub2_2_7), &v)
if err != nil {
t.Errorf("Error: %s", err.Error())
}
bsonStats, err := bson.Marshal(v)
if err != nil {
t.Errorf("Error: %s", err.Error())
}
var m bson.M
err = bson.Unmarshal(bsonStats, &m)
if err != nil {
t.Errorf("Error: %s", err.Error())
}
stat, err := mongodb.parseStatus(m)
fmt.Println(stat)
assert.Nil(t, err)
// Mongodb Stats
assert.EqualValues(t, reflect.TypeOf(stat["btree_hits"]).String(), "float64")
assert.EqualValues(t, stat["btree_hits"], 5.0)
}
示例14: TestParse24
func TestParse24(t *testing.T) {
var mongodb MongoDBPlugin
stub2_4_14 := `
{"asserts":{"msg":0,"regular":0,"rollovers":0,"user":0,"warning":0},"backgroundFlushing":{"average_ms":0.6153846153846154,"flushes":26,"last_finished":"2015-08-17T14:55:58.622+09:00","last_ms":0,"total_ms":16},"connections":{"available":818,"current":1,"totalCreated":10},"cursors":{"clientCursors_size":0,"timedOut":0,"totalOpen":0},"dur":{"commits":30,"commitsInWriteLock":0,"compression":0,"earlyCommits":0,"journaledMB":0,"timeMs":{"dt":3074,"prepLogBuffer":0,"remapPrivateView":0,"writeToDataFiles":0,"writeToJournal":0},"writeToDataFilesMB":0},"extra_info":{"heap_usage_bytes":62256840,"note":"fields vary by platform","page_faults":181},"globalLock":{"activeClients":{"readers":0,"total":0,"writers":0},"currentQueue":{"readers":0,"total":0,"writers":0},"lockTime":143869,"totalTime":1603601000},"host":"bcd5355930ff","indexCounters":{"accesses":0,"hits":5,"missRatio":0,"misses":0,"resets":0},"localTime":"2015-08-17T14:56:42.209+09:00","locks":{".":{"timeAcquiringMicros":{"R":66884,"W":12244},"timeLockedMicros":{"R":86058,"W":143869}},"admin":{"timeAcquiringMicros":{},"timeLockedMicros":{}},"local":{"timeAcquiringMicros":{"r":513,"w":0},"timeLockedMicros":{"r":11886,"w":0}}},"mem":{"bits":64,"mapped":80,"mappedWithJournal":160,"resident":38,"supported":true,"virtual":341},"metrics":{"document":{"deleted":0,"inserted":1,"returned":0,"updated":0},"getLastError":{"wtime":{"num":0,"totalMillis":0},"wtimeouts":0},"operation":{"fastmod":0,"idhack":0,"scanAndOrder":0},"queryExecutor":{"scanned":0},"record":{"moves":0},"repl":{"apply":{"batches":{"num":0,"totalMillis":0},"ops":0},"buffer":{"count":0,"maxSizeBytes":268435456,"sizeBytes":0},"network":{"bytes":0,"getmores":{"num":0,"totalMillis":0},"ops":0,"readersCreated":0},"oplog":{"insert":{"num":0,"totalMillis":0},"insertBytes":0},"preload":{"docs":{"num":0,"totalMillis":0},"indexes":{"num":0,"totalMillis":0}}},"ttl":{"deletedDocuments":0,"passes":26}},"network":{"bytesIn":1940,"bytesOut":18064,"numRequests":33},"ok":1,"opcounters":{"command":35,"delete":0,"getmore":0,"insert":1,"query":26,"update":0},"opcountersRepl":{"command":0,"delete":0,"getmore":0,"insert":0,"query":0,"update":0},"pid":1,"process":"mongod","recordStats":{"accessesNotInMemory":0,"local":{"accessesNotInMemory":0,"pageFaultExceptionsThrown":0},"pageFaultExceptionsThrown":0},"uptime":1604,"uptimeEstimate":1581,"uptimeMillis":1603600,"version":"2.4.14","writeBacksQueued":false}
`
var v interface{}
err := json.Unmarshal([]byte(stub2_4_14), &v)
if err != nil {
t.Errorf("Error: %s", err.Error())
}
bsonStats, err := bson.Marshal(v)
if err != nil {
t.Errorf("Error: %s", err.Error())
}
var m bson.M
err = bson.Unmarshal(bsonStats, &m)
if err != nil {
t.Errorf("Error: %s", err.Error())
}
stat, err := mongodb.parseStatus(m)
fmt.Println(stat)
assert.Nil(t, err)
// Mongodb Stats
assert.EqualValues(t, reflect.TypeOf(stat["btree_hits"]).String(), "float64")
assert.EqualValues(t, stat["btree_hits"], 5.0)
}
示例15: loginRun
func (socket *mongoSocket) loginRun(db string, query, result interface{}, f func() error) error {
var mutex sync.Mutex
var replyErr error
mutex.Lock()
op := queryOp{}
op.query = query
op.collection = db + ".$cmd"
op.limit = -1
op.replyFunc = func(err error, reply *replyOp, docNum int, docData []byte) {
defer mutex.Unlock()
if err != nil {
replyErr = err
return
}
err = bson.Unmarshal(docData, result)
if err != nil {
replyErr = err
} else {
// Must handle this within the read loop for the socket, so
// that concurrent login requests are properly ordered.
replyErr = f()
}
}
err := socket.Query(&op)
if err != nil {
return err
}
mutex.Lock() // Wait.
return replyErr
}