github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/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  	Update(selector interface{}, update interface{}) error
    48  	UpdateId(id interface{}, update interface{}) error
    49  	Remove(sel interface{}) error
    50  	RemoveId(id interface{}) error
    51  	RemoveAll(sel interface{}) (*mgo.ChangeInfo, error)
    52  }
    53  
    54  // WrapCollection returns a Collection that wraps the supplied *mgo.Collection.
    55  func WrapCollection(coll *mgo.Collection) Collection {
    56  	return collectionWrapper{coll}
    57  }
    58  
    59  // collectionWrapper wraps a *mgo.Collection and implements Collection and
    60  // WriteCollection.
    61  type collectionWrapper struct {
    62  	*mgo.Collection
    63  }
    64  
    65  // Name is part of the Collection interface.
    66  func (cw collectionWrapper) Name() string {
    67  	return cw.Collection.Name
    68  }
    69  
    70  // Writeable is part of the Collection interface.
    71  func (cw collectionWrapper) Writeable() WriteCollection {
    72  	return cw
    73  }
    74  
    75  // Underlying is part of the WriteCollection interface.
    76  func (cw collectionWrapper) Underlying() *mgo.Collection {
    77  	return cw.Collection
    78  }