##// END OF EJS Templates
update connections and diagrams for reduced sockets
update connections and diagrams for reduced sockets

File last commit:

r3646:97ce9f8c
r3658:8fb951e7
Show More
mongodb.py
80 lines | 2.7 KiB | text/x-python | PythonLexer
MinRK
Started DB backend with mongoDB support.
r3579 """A TaskRecord backend using mongodb"""
#-----------------------------------------------------------------------------
# Copyright (C) 2010 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
from datetime import datetime
from pymongo import Connection
MinRK
Add SQLite backend, DB backends are Configurable...
r3646 from pymongo.binary import Binary
from IPython.utils.traitlets import Dict, List, CUnicode
MinRK
Started DB backend with mongoDB support.
r3579
MinRK
eliminate relative imports
r3642 from .dictdb import BaseDB
MinRK
resort imports in a cleaner order
r3631
#-----------------------------------------------------------------------------
MinRK
Started DB backend with mongoDB support.
r3579 # MongoDB class
MinRK
resort imports in a cleaner order
r3631 #-----------------------------------------------------------------------------
MinRK
Refactor newparallel to use Config system...
r3604 class MongoDB(BaseDB):
MinRK
Started DB backend with mongoDB support.
r3579 """MongoDB TaskRecord backend."""
MinRK
Add SQLite backend, DB backends are Configurable...
r3646
connection_args = List(config=True)
connection_kwargs = Dict(config=True)
database = CUnicode(config=True)
_table = Dict()
def __init__(self, **kwargs):
super(MongoDB, self).__init__(**kwargs)
self._connection = Connection(*self.connection_args, **self.connection_kwargs)
if not self.database:
self.database = self.session
self._db = self._connection[self.database]
MinRK
Started DB backend with mongoDB support.
r3579 self._records = self._db['task_records']
MinRK
Add SQLite backend, DB backends are Configurable...
r3646
def _binary_buffers(self, rec):
for key in ('buffers', 'result_buffers'):
if key in rec:
rec[key] = map(Binary, rec[key])
MinRK
Started DB backend with mongoDB support.
r3579
def add_record(self, msg_id, rec):
"""Add a new Task Record, by msg_id."""
# print rec
MinRK
Add SQLite backend, DB backends are Configurable...
r3646 rec = _binary_buffers(rec)
MinRK
Started DB backend with mongoDB support.
r3579 obj_id = self._records.insert(rec)
self._table[msg_id] = obj_id
def get_record(self, msg_id):
"""Get a specific Task Record, by msg_id."""
return self._records.find_one(self._table[msg_id])
def update_record(self, msg_id, rec):
"""Update the data in an existing record."""
MinRK
Add SQLite backend, DB backends are Configurable...
r3646 rec = _binary_buffers(rec)
MinRK
Started DB backend with mongoDB support.
r3579 obj_id = self._table[msg_id]
MinRK
improved client.get_results() behavior
r3598 self._records.update({'_id':obj_id}, {'$set': rec})
MinRK
Started DB backend with mongoDB support.
r3579
def drop_matching_records(self, check):
"""Remove a record from the DB."""
self._records.remove(check)
def drop_record(self, msg_id):
"""Remove a record from the DB."""
obj_id = self._table.pop(msg_id)
self._records.remove(obj_id)
def find_records(self, check, id_only=False):
"""Find records matching a query dict."""
matches = list(self._records.find(check))
if id_only:
MinRK
improved client.get_results() behavior
r3598 return [ rec['msg_id'] for rec in matches ]
else:
data = {}
for rec in matches:
data[rec['msg_id']] = rec
return data
MinRK
Started DB backend with mongoDB support.
r3579