##// END OF EJS Templates
Merge pull request #1687 from minrk/binary...
Min RK -
r6691:9d6e2588 merge
parent child Browse files
Show More
@@ -1,117 +1,117 b''
1 """A TaskRecord backend using mongodb
1 """A TaskRecord backend using mongodb
2
2
3 Authors:
3 Authors:
4
4
5 * Min RK
5 * Min RK
6 """
6 """
7 #-----------------------------------------------------------------------------
7 #-----------------------------------------------------------------------------
8 # Copyright (C) 2010-2011 The IPython Development Team
8 # Copyright (C) 2010-2011 The IPython Development Team
9 #
9 #
10 # Distributed under the terms of the BSD License. The full license is in
10 # Distributed under the terms of the BSD License. The full license is in
11 # the file COPYING, distributed as part of this software.
11 # the file COPYING, distributed as part of this software.
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13
13
14 from pymongo import Connection
14 from pymongo import Connection
15 from pymongo.binary import Binary
15 from bson import Binary
16
16
17 from IPython.utils.traitlets import Dict, List, Unicode, Instance
17 from IPython.utils.traitlets import Dict, List, Unicode, Instance
18
18
19 from .dictdb import BaseDB
19 from .dictdb import BaseDB
20
20
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 # MongoDB class
22 # MongoDB class
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25 class MongoDB(BaseDB):
25 class MongoDB(BaseDB):
26 """MongoDB TaskRecord backend."""
26 """MongoDB TaskRecord backend."""
27
27
28 connection_args = List(config=True,
28 connection_args = List(config=True,
29 help="""Positional arguments to be passed to pymongo.Connection. Only
29 help="""Positional arguments to be passed to pymongo.Connection. Only
30 necessary if the default mongodb configuration does not point to your
30 necessary if the default mongodb configuration does not point to your
31 mongod instance.""")
31 mongod instance.""")
32 connection_kwargs = Dict(config=True,
32 connection_kwargs = Dict(config=True,
33 help="""Keyword arguments to be passed to pymongo.Connection. Only
33 help="""Keyword arguments to be passed to pymongo.Connection. Only
34 necessary if the default mongodb configuration does not point to your
34 necessary if the default mongodb configuration does not point to your
35 mongod instance."""
35 mongod instance."""
36 )
36 )
37 database = Unicode(config=True,
37 database = Unicode(config=True,
38 help="""The MongoDB database name to use for storing tasks for this session. If unspecified,
38 help="""The MongoDB database name to use for storing tasks for this session. If unspecified,
39 a new database will be created with the Hub's IDENT. Specifying the database will result
39 a new database will be created with the Hub's IDENT. Specifying the database will result
40 in tasks from previous sessions being available via Clients' db_query and
40 in tasks from previous sessions being available via Clients' db_query and
41 get_result methods.""")
41 get_result methods.""")
42
42
43 _connection = Instance(Connection) # pymongo connection
43 _connection = Instance(Connection) # pymongo connection
44
44
45 def __init__(self, **kwargs):
45 def __init__(self, **kwargs):
46 super(MongoDB, self).__init__(**kwargs)
46 super(MongoDB, self).__init__(**kwargs)
47 if self._connection is None:
47 if self._connection is None:
48 self._connection = Connection(*self.connection_args, **self.connection_kwargs)
48 self._connection = Connection(*self.connection_args, **self.connection_kwargs)
49 if not self.database:
49 if not self.database:
50 self.database = self.session
50 self.database = self.session
51 self._db = self._connection[self.database]
51 self._db = self._connection[self.database]
52 self._records = self._db['task_records']
52 self._records = self._db['task_records']
53 self._records.ensure_index('msg_id', unique=True)
53 self._records.ensure_index('msg_id', unique=True)
54 self._records.ensure_index('submitted') # for sorting history
54 self._records.ensure_index('submitted') # for sorting history
55 # for rec in self._records.find
55 # for rec in self._records.find
56
56
57 def _binary_buffers(self, rec):
57 def _binary_buffers(self, rec):
58 for key in ('buffers', 'result_buffers'):
58 for key in ('buffers', 'result_buffers'):
59 if rec.get(key, None):
59 if rec.get(key, None):
60 rec[key] = map(Binary, rec[key])
60 rec[key] = map(Binary, rec[key])
61 return rec
61 return rec
62
62
63 def add_record(self, msg_id, rec):
63 def add_record(self, msg_id, rec):
64 """Add a new Task Record, by msg_id."""
64 """Add a new Task Record, by msg_id."""
65 # print rec
65 # print rec
66 rec = self._binary_buffers(rec)
66 rec = self._binary_buffers(rec)
67 self._records.insert(rec)
67 self._records.insert(rec)
68
68
69 def get_record(self, msg_id):
69 def get_record(self, msg_id):
70 """Get a specific Task Record, by msg_id."""
70 """Get a specific Task Record, by msg_id."""
71 r = self._records.find_one({'msg_id': msg_id})
71 r = self._records.find_one({'msg_id': msg_id})
72 if not r:
72 if not r:
73 # r will be '' if nothing is found
73 # r will be '' if nothing is found
74 raise KeyError(msg_id)
74 raise KeyError(msg_id)
75 return r
75 return r
76
76
77 def update_record(self, msg_id, rec):
77 def update_record(self, msg_id, rec):
78 """Update the data in an existing record."""
78 """Update the data in an existing record."""
79 rec = self._binary_buffers(rec)
79 rec = self._binary_buffers(rec)
80
80
81 self._records.update({'msg_id':msg_id}, {'$set': rec})
81 self._records.update({'msg_id':msg_id}, {'$set': rec})
82
82
83 def drop_matching_records(self, check):
83 def drop_matching_records(self, check):
84 """Remove a record from the DB."""
84 """Remove a record from the DB."""
85 self._records.remove(check)
85 self._records.remove(check)
86
86
87 def drop_record(self, msg_id):
87 def drop_record(self, msg_id):
88 """Remove a record from the DB."""
88 """Remove a record from the DB."""
89 self._records.remove({'msg_id':msg_id})
89 self._records.remove({'msg_id':msg_id})
90
90
91 def find_records(self, check, keys=None):
91 def find_records(self, check, keys=None):
92 """Find records matching a query dict, optionally extracting subset of keys.
92 """Find records matching a query dict, optionally extracting subset of keys.
93
93
94 Returns list of matching records.
94 Returns list of matching records.
95
95
96 Parameters
96 Parameters
97 ----------
97 ----------
98
98
99 check: dict
99 check: dict
100 mongodb-style query argument
100 mongodb-style query argument
101 keys: list of strs [optional]
101 keys: list of strs [optional]
102 if specified, the subset of keys to extract. msg_id will *always* be
102 if specified, the subset of keys to extract. msg_id will *always* be
103 included.
103 included.
104 """
104 """
105 if keys and 'msg_id' not in keys:
105 if keys and 'msg_id' not in keys:
106 keys.append('msg_id')
106 keys.append('msg_id')
107 matches = list(self._records.find(check,keys))
107 matches = list(self._records.find(check,keys))
108 for rec in matches:
108 for rec in matches:
109 rec.pop('_id')
109 rec.pop('_id')
110 return matches
110 return matches
111
111
112 def get_history(self):
112 def get_history(self):
113 """get all msg_ids, ordered by time submitted."""
113 """get all msg_ids, ordered by time submitted."""
114 cursor = self._records.find({},{'msg_id':1}).sort('submitted')
114 cursor = self._records.find({},{'msg_id':1}).sort('submitted')
115 return [ rec['msg_id'] for rec in cursor ]
115 return [ rec['msg_id'] for rec in cursor ]
116
116
117
117
General Comments 0
You need to be logged in to leave comments. Login now