Page MenuHomec4science

mongodb_pymongo.py
No OneTemporary

File Metadata

Created
Thu, Dec 12, 10:52

mongodb_pymongo.py

# -*- coding: utf-8 -*-
## This file is part of Invenio.
## Copyright (C) 2013 CERN.
##
## Invenio is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2 of the
## License, or (at your option) any later version.
##
## Invenio is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Invenio; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
"""
invenio.utils.jsonalchemy.engines.mongo_pymongo
------------------------------------------
"""
from itertools import izip, imap
import pymongo
from invenio.utils.jsonalchemy.storage import Storage
class MongoDBStorage(Storage):
"""
Implements storage engine for MongoDB using the driver pymongo
"""
def __init__(self, model, **kwards):
"""
TBC
See also :meth:`~invenio.utils.jsonalchemy.storage:Storage.__init__`
"""
self.model = model
host = kwards.get('host', 'localhost')
port = kwards.get('port', 27017)
database = kwards.get('database', 'invenio')
self.__connection = pymongo.MongoClient(host=host,port=port)
self.__database = self.__connection[database]
self.__collection = self.__database[model]
def save_one(self, json, id=None):
"""See :meth:`~invenio.utils.jsonalchemy.storage:Storage.save_one`"""
if not id is None:
json['_id'] = id
return self.__collection.insert(json)
def save_many(self, jsons, ids=None):
"""See :meth:`~invenio.utils.jsonalchemy.storage:Storage.save_many`"""
if not ids is None:
def add_id(t):
t[0]['_id'] = t[1]
return t[0]
jsons = impa(add_id, izip(jsons, ids))
return self.__collection.insert(jsons, continue_on_error=True)
def update_one(self, json, id=None):
"""See :meth:`~invenio.utils.jsonalchemy.storage:Storage.update_one`"""
#FIXME: what if we get only the fields that have change
if not recid is None:
json['_id'] = recid
return self.__collection.save(json)
def update_many(self, jsons, ids=None):
"""See :meth:`~invenio.utils.jsonalchemy.storage:Storage.update_many`"""
if not recids is None:
def add_id(t):
t[0]['_id'] = t[1]
return t[0]
jsons = impa(add_id, izip(jsons, ids))
return imap(self.__collection.save, jsons)
def get_one(self, id):
"""See :meth:`~invenio.utils.jsonalchemy.storage:Storage.get_one`"""
return self.__collection.find_one(id)
def get_many(self, ids):
"""See :meth:`~invenio.utils.jsonalchemy.storage:Storage.get_many`"""
return self.__collection.find({'_id': {'$in':ids}})
def get_field_values(recids, field, repetitive_values=True, count=False,
include_recid=False, split_by=0):
"""See :meth:`~invenio.utils.jsonalchemy.storage:Storage.get_field_values`"""
raise NotImplementedError()
def get_fields_values(recids, fields, repetitive_values=True, count=False,
include_recid=False, split_by=0):
"""See :meth:`~invenio.utils.jsonalchemy.storage:Storage.get_fields_values`"""
raise NotImplementedError()

Event Timeline