예제 #1
0
def copy_database (target_db, admin_user = None, admin_password = None, force = False):
	""" Copy the current database to a new database name (see http://www.mongodb.org/display/DOCS/Clone+Database)
	"""
	with connection.protect():
		# save the current connection
		db_connection = connection.connection()
		db_connection_ = connection.connection_information()

		source_db = db_connection_["db"]
		if (source_db == target_db):
			logger.debug("Ignored request to copy '%s' into itself." % target_db)
			return

		# open a connection to the admin collection
		admin_connection = connection.connect(db = "admin", user = admin_user, password = admin_password)

		if (target_db in admin_connection.connection.database_names()):
			if (force):
				logger.debug("'%s' already exists and will be merged with content of '%s'." % (source_db, target_db))
			else:
				raise errors.DBOperationError("Unable to copy database '%s' to '%s': target already exists." % (source_db, target_db))

		# copy the database
		try:
			admin_connection.connection.copy_database(source_db, target_db)

		# restore the current connection
		finally:
			connection._connection = db_connection
			connection._connection_information = db_connection_

	logger.debug("Copy of '%s' into '%s' successful." % (source_db, target_db))
예제 #2
0
def drop_collection(collection):
    """ Drop a collection
	"""
    with connection.protect():
        connection.connection().drop_collection(collection)

    logger.debug("Collection '%s' was dropped." % collection)
예제 #3
0
	def remove (self):
		""" Remove this object from the database.

		.. note::
			- Relationships from and to this object are removed as well.
			- The object remains in memory, flagged as uncommitted.

		.. seealso::
			:meth:`~PersistentObject.remove_all`
		"""
		# remove all relationships with other objects
		for collection_name in methods.list_collections():
			if ("_id" in self._properties):
				for object in self._in_vertices(collection_name):
					object._disconnect_from(self, None)

			for object in self._out_vertices(collection_name):
				self._disconnect_from(object, None)

		# if the object has been committed at least once,
		if ("_id" in self._properties):
			# remove the object from the database
			with connection.protect():
				methods.remove_object(self)

			# and declare it has never having been committed
			del self._properties["_id"]

		self._committed = False
예제 #4
0
def drop_collection (collection):
	""" Drop a collection
	"""
	with connection.protect():
		connection.connection().drop_collection(collection)

	logger.debug("Collection '%s' was dropped." % collection)
예제 #5
0
    def remove(self):
        """ Remove this object from the database.

		.. note::
			- Relationships from and to this object are removed as well.
			- The object remains in memory, flagged as uncommitted.

		.. seealso::
			:meth:`~PersistentObject.remove_all`
		"""
        # remove all relationships with other objects
        for collection_name in methods.list_collections():
            if ("_id" in self._properties):
                for object in self._in_vertices(collection_name):
                    object._disconnect_from(self, None)

            for object in self._out_vertices(collection_name):
                self._disconnect_from(object, None)

        # if the object has been committed at least once,
        if ("_id" in self._properties):
            # remove the object from the database
            with connection.protect():
                methods.remove_object(self)

            # and declare it has never having been committed
            del self._properties["_id"]

        self._committed = False
예제 #6
0
    def commit(self):
        """ Commit this object to the database.

		.. note::
			- The commit will not be performed if the object has already been
			  committed once and no modification (property manipulation) has
			  been performed since then.
			- If an object already exists in the database with the same values
			  for properties flagged as unique a :class:`MetagenomeDB.errors.DuplicateObjectError`
			  exception is thrown.

		.. seealso::
			:meth:`~PersistentObject.is_committed`
		"""
        if (self._committed):
            return
        """
		# pre-flight: if some patch needs to be applied on the object's
		# properties, we temporary store the old values
		tmp = {}
		for (key, value) in patch.iteritems():
			assert (key != "_id") ###
			tmp[key] = self._properties[key]
			self._properties[key] = value
		"""

        with connection.protect():
            methods._commit(self)
        """
		# post-flight: we restore the object's properties, if needed
		for (key, value) in tmp.iteritems():
			self._properties[key] = value
		"""

        self._committed = True
예제 #7
0
def remove_object (object):
	""" Remove an object from a collection
	"""
	collection_name = object.__class__.__name__

	with connection.protect():
		connection.connection()[collection_name].remove({"_id": object["_id"]})

	del _cache[object["_id"]]

	logger.debug("Object %s was removed from collection '%s'." % (object, collection_name))
예제 #8
0
def remove_object(object):
    """ Remove an object from a collection
	"""
    collection_name = object.__class__.__name__

    with connection.protect():
        connection.connection()[collection_name].remove({"_id": object["_id"]})

    del _cache[object["_id"]]

    logger.debug("Object %s was removed from collection '%s'." %
                 (object, collection_name))
예제 #9
0
def list_collections (with_classes = False):
	""" Return a list of all existing collections in the database that can be
		mapped to an instance of a PersistentObject class or subclass.
	"""
	# list all collections in the database
	with connection.protect():
		collection_names = connection.connection().collection_names()

	# return collections represented by PersistentObject subclasses
	collections = []
	for collection_name in collection_names:
		if (collection_name in _classes):
			if (with_classes):
				collections.append((collection_name, _classes[collection_name]))
			else:
				collections.append(collection_name)

	return collections
예제 #10
0
def list_collections(with_classes=False):
    """ Return a list of all existing collections in the database that can be
		mapped to an instance of a PersistentObject class or subclass.
	"""
    # list all collections in the database
    with connection.protect():
        collection_names = connection.connection().collection_names()

    # return collections represented by PersistentObject subclasses
    collections = []
    for collection_name in collection_names:
        if (collection_name in _classes):
            if (with_classes):
                collections.append(
                    (collection_name, _classes[collection_name]))
            else:
                collections.append(collection_name)

    return collections
예제 #11
0
def copy_database(target_db,
                  admin_user=None,
                  admin_password=None,
                  force=False):
    """ Copy the current database to a new database name (see http://www.mongodb.org/display/DOCS/Clone+Database)
	"""
    with connection.protect():
        # save the current connection
        db_connection = connection.connection()
        db_connection_ = connection.connection_information()

        source_db = db_connection_["db"]
        if (source_db == target_db):
            logger.debug("Ignored request to copy '%s' into itself." %
                         target_db)
            return

        # open a connection to the admin collection
        admin_connection = connection.connect(db="admin",
                                              user=admin_user,
                                              password=admin_password)

        if (target_db in admin_connection.connection.database_names()):
            if (force):
                logger.debug(
                    "'%s' already exists and will be merged with content of '%s'."
                    % (source_db, target_db))
            else:
                raise errors.DBOperationError(
                    "Unable to copy database '%s' to '%s': target already exists."
                    % (source_db, target_db))

        # copy the database
        try:
            admin_connection.connection.copy_database(source_db, target_db)

        # restore the current connection
        finally:
            connection._connection = db_connection
            connection._connection_information = db_connection_

    logger.debug("Copy of '%s' into '%s' successful." % (source_db, target_db))
예제 #12
0
	def commit (self):
		""" Commit this object to the database.

		.. note::
			- The commit will not be performed if the object has already been
			  committed once and no modification (property manipulation) has
			  been performed since then.
			- If an object already exists in the database with the same values
			  for properties flagged as unique a :class:`MetagenomeDB.errors.DuplicateObjectError`
			  exception is thrown.

		.. seealso::
			:meth:`~PersistentObject.is_committed`
		"""
		if (self._committed):
			return

		"""
		# pre-flight: if some patch needs to be applied on the object's
		# properties, we temporary store the old values
		tmp = {}
		for (key, value) in patch.iteritems():
			assert (key != "_id") ###
			tmp[key] = self._properties[key]
			self._properties[key] = value
		"""

		with connection.protect():
			methods._commit(self)

		"""
		# post-flight: we restore the object's properties, if needed
		for (key, value) in tmp.iteritems():
			self._properties[key] = value
		"""

		self._committed = True