Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
241782 views
1
#################################################################################
2
#
3
# (c) Copyright 2010 William Stein
4
#
5
# This file is part of PSAGE
6
#
7
# PSAGE is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# PSAGE is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
#
20
#################################################################################
21
22
23
"""
24
This module implements a compressed key to Python object store using MongoDB.
25
26
This module defines one class ObjectDB, which we instantiate using a
27
MongoDB database db. The resulting instance then works somewhat like
28
a dictionary, except that objects are pickled and stored on disk in
29
MongoDB.
30
"""
31
32
class ObjectDB:
33
def __init__(self, db):
34
from gridfs import GridFS
35
self.gridfs = GridFS(db)
36
37
def __setitem__(self, key, obj):
38
self.save(obj, key)
39
40
def __getitem__(self, key):
41
return self.load(key)
42
43
def __delitem__(self, key):
44
from pymongo.objectid import ObjectId
45
if not isinstance(key, ObjectId):
46
id = self.gridfs.get_last_version(key)._id
47
else:
48
id = key
49
self.gridfs.delete(id)
50
51
def __repr__(self):
52
return "Key-value database"
53
54
def keys(self):
55
"""Return list of filenames of objects in the gridfs store."""
56
return self.gridfs.list()
57
58
def object_ids(self):
59
"""Return list of id's of objects in the gridfs store, which
60
are not id's of objects with filenames."""
61
v = self.gridfs._GridFS__files.find({'filename':{'$exists':False}},['_id'])
62
return [x['_id'] for x in v]
63
64
def has_key(self, key):
65
return self.gridfs.exists(filename=key)
66
67
def save(self, obj, key=None, compress=None):
68
"""Save Python object obj to the grid file system self.gridfs.
69
If key is None, the file is stored by MongoDB assigned
70
ObjectID, and that id is returned.
71
"""
72
from sage.all import dumps
73
data = dumps(obj, compress=compress)
74
if key is not None:
75
self.gridfs.put(data, filename=key)
76
return key
77
else:
78
# store by MongoDB assigned _id only, and return that id.
79
return self.gridfs.put(data)
80
81
def load(self, key, compress=True):
82
from pymongo.objectid import ObjectId
83
if isinstance(key, ObjectId):
84
data = self.gridfs.get(key).read()
85
else:
86
data = self.gridfs.get_last_version(key).read()
87
from sage.all import loads
88
return loads(data, compress=compress)
89
90