import pymongo
import bson

from datastore import DataStore

class MongoDBDataStore(pymongo.Connection, DataStore):
    def _store(self, uid, content, data=None):
        """Store the given dict of content at uid. Nothing returned."""
        doc = dict(uid=uid)
        if data:
            doc.update(data=bson.Binary(data))
        doc.update(content)
        self.librarypaste.pastes.save(doc)

    def _storeLog(self, nick, time, uid):
        """Adds the nick & uid to the log for a given time/order. No return."""
        query = dict(uid=uid)
        update = {'$set': dict(nick=nick, time=time)}
        self.librarypaste.pastes.update(query, update)

    def _retrieve(self, uid):
        """Return a dict with the contents of the paste, including the raw
        data, if any, as the key 'data'. Must pass in uid, not shortid."""
        query = dict(uid=uid)
        return self.librarypaste.pastes.find_one(query)

    def lookup(self, nick):
        """Looks for the most recent paste by a given nick.
        Returns the uid or None"""
        query = dict(nick=nick)
        order = dict(time=pymongo.DESCENDING)
        recs = self.librarypaste.pastes.find(query).order(order).limit(1)
        try:
            return next(recs)['uid']
        except StopIteration:
            pass

    def _lookupUid(self, shortid):
        query = dict(shortid=shortid)
        rec = self.librarypaste.pastes.find_one(query)
        return rec['uid']

@@ -105,24 +105,121 @@
 DataStore):%0A
+    db_name = 'librarypaste'%0A%0A    @property%0A    def db(self):%0A        return self%5Bself.db_name%5D%0A%0A
     def _sto
@@ -249,24 +249,24 @@
 data=None):%0A
-
         %22%22%22S
@@ -454,36 +454,26 @@
        self.
-librarypaste
+db
 .pastes.save
@@ -694,36 +694,26 @@
        self.
-librarypaste
+db
 .pastes.upda
@@ -957,36 +957,26 @@
 return self.
-librarypaste
+db
 .pastes.find
@@ -1205,36 +1205,26 @@
 recs = self.
-librarypaste
+db
 .pastes.find
@@ -1443,28 +1443,18 @@
  = self.
-librarypaste
+db
 .pastes.

Allow database name to be specified