github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/mongo/collections.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package mongo 5 6 import "gopkg.in/mgo.v2" 7 8 // CollectionFromName returns a named collection on the specified database, 9 // initialised with a new session. Also returned is a close function which 10 // must be called when the collection is no longer required. 11 func CollectionFromName(db *mgo.Database, coll string) (Collection, func()) { 12 session := db.Session.Copy() 13 newColl := db.C(coll).With(session) 14 return WrapCollection(newColl), session.Close 15 } 16 17 // Collection imperfectly insulates clients from the capacity to write to 18 // MongoDB. Query results can still be used to write; and the Writeable 19 // method exposes the underlying *mgo.Collection when absolutely required; 20 // but the general expectation in juju is that writes will occur only via 21 // mgo/txn, and any layer-skipping is done only in exceptional and well- 22 // supported circumstances. 23 type Collection interface { 24 25 // Name returns the name of the collection. 26 Name() string 27 28 // Count, Find, and FindId methods act as documented for *mgo.Collection. 29 Count() (int, error) 30 Find(query interface{}) *mgo.Query 31 FindId(id interface{}) *mgo.Query 32 33 // Writeable gives access to methods that enable direct DB access. It 34 // should be used with judicious care, and for only the best of reasons. 35 Writeable() WriteCollection 36 } 37 38 // WriteCollection allows read/write access to a MongoDB collection. 39 type WriteCollection interface { 40 Collection 41 42 // Underlying returns the underlying *mgo.Collection. 43 Underlying() *mgo.Collection 44 45 // All other methods act as documented for *mgo.Collection. 46 Insert(docs ...interface{}) error 47 Upsert(selector interface{}, update interface{}) (info *mgo.ChangeInfo, err error) 48 UpsertId(id interface{}, update interface{}) (info *mgo.ChangeInfo, err error) 49 Update(selector interface{}, update interface{}) error 50 UpdateId(id interface{}, update interface{}) error 51 Remove(sel interface{}) error 52 RemoveId(id interface{}) error 53 RemoveAll(sel interface{}) (*mgo.ChangeInfo, error) 54 } 55 56 // WrapCollection returns a Collection that wraps the supplied *mgo.Collection. 57 func WrapCollection(coll *mgo.Collection) Collection { 58 return collectionWrapper{coll} 59 } 60 61 // collectionWrapper wraps a *mgo.Collection and implements Collection and 62 // WriteCollection. 63 type collectionWrapper struct { 64 *mgo.Collection 65 } 66 67 // Name is part of the Collection interface. 68 func (cw collectionWrapper) Name() string { 69 return cw.Collection.Name 70 } 71 72 // Writeable is part of the Collection interface. 73 func (cw collectionWrapper) Writeable() WriteCollection { 74 return cw 75 } 76 77 // Underlying is part of the WriteCollection interface. 78 func (cw collectionWrapper) Underlying() *mgo.Collection { 79 return cw.Collection 80 }