Exemplo n.º 1
0
    def _force_get_uuid_for(cls, collection_name, uuid=None, **kwargs):
        """
        Do a look-up to find the UUID which would have been found when fetching
        a document for the given collection and UUID.

        This goes one step further than _get_uuid_for, since it will query the
        database to find the UUID. This is not necessary when simply wanting to
        fetch one entry.

        Args:
            collection_name: Name of the MongoDB collection to find a UUID for.
            uuid: Potentially a UUID, in which case it will be used as-is and
                not checked for validity.
            **kwargs: Extra keyword arguments to give to MongoDBConnection.

        Returns:
            The UUID of the document that would have been returned when fetching
            one document for the given collection and UUID.
        """
        uuid = cls._get_uuid_for(collection_name, uuid)
        if uuid is not None:
            return uuid

        # We're using whatever MongoDB returns first, so find that
        with MongoDBConnection(**kwargs) as client:
            db = client.ontodb
            collection = getattr(db, collection_name)
            first_document = collection.find_one({})
            if first_document is None:
                return None
            return str(first_document['_id'])
Exemplo n.º 2
0
 def _save_as_new(self, **kwargs):
     """
     Save this object to the database collection as a new document.
     """
     document = self._get_as_document()
     with MongoDBConnection(**kwargs) as client:
         db = client.ontodb
         collection = getattr(db, self.get_collection_name())
         assigned_id = collection.insert_one(document).inserted_id
     return str(assigned_id)
Exemplo n.º 3
0
def _get_uuid_for_outdated(graph_identifier, **kwargs):
    with MongoDBConnection(**kwargs) as client:
        db = client.ontodb
        doc = db.dataframe.find_one({
            'graphType':
            graph_identifier.graph_type,
            'graphUuid':
            graph_identifier.graph_uuid,
            'otherParameters':
            graph_identifier.other_parameters,
        })
        if doc is None:
            return None
        return doc['_id']
Exemplo n.º 4
0
 def _save_with_uuid(self, uuid, **kwargs):
     """
     Save this object to the database collection using the given UUID.
     """
     document = self._get_as_document()
     with MongoDBConnection(**kwargs) as client:
         db = client.ontodb
         collection = getattr(db, self.get_collection_name())
         collection.replace_one(
             {'_id': ObjectId(uuid)},
             document,
             upsert=True,
         )
     return uuid
Exemplo n.º 5
0
    def find_all_ids(cls, **kwargs):
        """
        Return the UUIDs of all documents in the database collection.

        Args:
            **kwargs: Extra keyword arguments to give to MongoDBConnection.

        Returns:
            List of UUIDs, one for each document in the database collection.
        """
        with MongoDBConnection(**kwargs) as client:
            db = client.ontodb
            collection = getattr(db, cls.get_collection_name())
            ids = [str(i) for i in collection.distinct('_id')]
        return ids
Exemplo n.º 6
0
    def remove_by_uuid(cls, uuid, **kwargs):
        """
        Remove the document with the given UUID from the database collection.

        Args:
            uuid: The UUID of the document to remove from the database.
            **kwargs: Extra keyword arguments to give to MongoDBConnection.

        Returns:
            True if a document was actually removed, False if not.
        """
        with MongoDBConnection(**kwargs) as client:
            db = client.ontodb
            collection = getattr(db, cls.get_collection_name())
            r = collection.delete_one({'_id': ObjectId(uuid)})
            num_deleted = r.deleted_count
        return num_deleted > 0
Exemplo n.º 7
0
def store(df, graph_identifier, **kwargs):
    with MongoDBConnection(**kwargs) as client:
        db = client.ontodb
        j = df.to_json(orient='split')
        doc = {
            'df': j,
            'graphType': graph_identifier.graph_type,
            'graphUuid': graph_identifier.graph_uuid,
            'lastModified': graph_identifier.last_modified,
            'otherParameters': graph_identifier.other_parameters,
        }
        existing_id = _get_uuid_for_outdated(graph_identifier, **kwargs)
        is_update = existing_id is not None
        if is_update:
            db.dataframe.replace_one(
                {'_id': existing_id},
                doc,
            )
            return str(existing_id)
        else:
            return str(db.dataframe.insert_one(doc).inserted_id)
Exemplo n.º 8
0
    def from_uuid(cls, uuid=None, **kwargs):
        """
        Fetch an instance of this class from the database.

        Args:
            uuid: UUID of the instance to fetch. If not given, an environment
                variable named <COLLECTION_NAME>_UUID will be used. If that one
                does not exist, a MissingUuidWarning will be emitted and the
                first instance returned from the database is used.
            **kwargs: Extra keyword arguments to give to MongoDBConnection.

        Returns:
            An instance of this class, filled with information from the
            database.

        Warnings:
            MissingUuidWarning: If no UUID is given as an argument or an
                environment variable.

        Raises:
            ValueError: If no document with the specified UUID can be found, or
                if no document exists when not specifying the UUID.
        """
        collection_name = cls.get_collection_name()
        uuid = cls.find_uuid(uuid)

        criteria = None
        if uuid is not None:
            criteria = {'_id': ObjectId(uuid)}

        with MongoDBConnection(**kwargs) as client:
            db = client.ontodb
            collection = getattr(db, collection_name)

            document = collection.find_one(criteria)

            if document is None:
                raise ValueError('No {} found with the UUID "{}"'.format(
                    collection_name, uuid))
        return cls.from_document(document)
Exemplo n.º 9
0
def get(graph_identifier, **kwargs):
    with MongoDBConnection(**kwargs) as client:
        db = client.ontodb
        doc = db.dataframe.find_one({
            'graphType':
            graph_identifier.graph_type,
            'graphUuid':
            graph_identifier.graph_uuid,
            'lastModified':
            graph_identifier.last_modified,
            'otherParameters':
            graph_identifier.other_parameters,
        })
        if doc is None:
            return None
        js = json.loads(doc['df'])
    js['columns'] = list(map(lambda x: URIRef(x), js['columns']))
    js['index'] = list(map(lambda x: URIRef(x), js['index']))
    df = pd.DataFrame(data=js['data'],
                      index=js['index'],
                      columns=js['columns'])
    return df
Exemplo n.º 10
0
def store(json, **kwargs):
    with MongoDBConnection(**kwargs) as client:
        db = client.ontodb
        db.log.insert_one(json)