Exemplo n.º 1
0
def delete_row(table, rowKey):
    """
    deletes a complete row (rowKey) from a given table
    """

    hasBinaryFiles = ["Elements"]
    hasGraphData = ["Elements"]

    transport = roboearth.openDBTransport()
    client = transport['client']
    try:
        client.deleteAllRow(table, rowKey)
        roboearth.closeDBTransport(transport)
        if table in hasBinaryFiles:
            hdfs.rm_dir(
                os.path.join(roboearth.UPLOAD_DIR, table.lower(),
                             rowKey.replace('.', '/')))
        if table in hasGraphData:
            sesame.rm(rowKey, table)

    except IOError, err:
        roboearth.closeDBTransport(transport)
        print table, rowKey
        raise roboearth.DBWriteErrorException("Can't delete data: " +
                                              err.__str__())
Exemplo n.º 2
0
def update(id_, data, author):
    """
    update an existing object description at the database

    id_ : complete identifier (primary key) of the object

    data: dictionary with the updated data ('description' (human readable) and
    'object_description' (OWL))

    author: author of the description
    """

    transport = roboearth.openDBTransport()
    client = transport['client']
    try:
        mutation_list = []
        if data.has_key('description'):
            mutation_list.append(
                Mutation(column="info:description", value=data['description']))
        if data.has_key('object_description'):
            mutation_list.append(
                Mutation(column="owl:description",
                         value=data['object_description']))

        client.mutateRow("Elements", id_,
                         [Mutation(column="info:modified_by", value=author)] +
                         mutation_list)

        if data.has_key('object_description'):
            sesame.rm(id_, "Elements")
            sesame.set(data['object_description'], id_, "Elements")

        # push update to subscribers
        scanner = client.scannerOpenWithPrefix("Elements", id_, [])
        res = client.scannerGet(scanner)
        for r in res[0].columns:
            if r.startswith("subscriber:"):
                client.mutateRow(
                    "Users", res[0].columns[r].value,
                    [Mutation(column="news:", value="Objects#" + id_)])

        client.scannerClose(scanner)
        roboearth.closeDBTransport(transport)

        return True
    except (IOError, IllegalArgument), err:
        raise roboearth.DBWriteErrorException(
            "Can't write data to Elements table: " + err.__str__())
Exemplo n.º 3
0
def update(id_, data, author):
    """
    update an existing object description at the database

    id_ : complete identifier (primary key) of the object

    data: dictionary with the updated data ('description' (human readable) and
    'object_description' (OWL))

    author: author of the description
    """

    transport = roboearth.openDBTransport()
    client = transport['client']
    try:
        mutation_list = [ ]
        if data.has_key('description'):
            mutation_list.append(Mutation(column="info:description", value=data['description']))            
        if data.has_key('object_description'):
            mutation_list.append(Mutation(column="owl:description", value=data['object_description']))            
        
        client.mutateRow("Elements", id_,
                         [Mutation(column="info:modified_by", value=author)] +
                         mutation_list)

        if data.has_key('object_description'):
            sesame.rm(id_, "Elements")
            sesame.set(data['object_description'], id_, "Elements")

        # push update to subscribers
        scanner = client.scannerOpenWithPrefix("Elements", id_, [ ])
        res = client.scannerGet(scanner)
        for r in res[0].columns:
            if r.startswith("subscriber:"):
                client.mutateRow("Users", res[0].columns[r].value,
                                 [Mutation(column="news:", value="Objects#"+id_)])

        client.scannerClose(scanner)
        roboearth.closeDBTransport(transport)

        return True
    except (IOError, IllegalArgument), err:
        raise roboearth.DBWriteErrorException("Can't write data to Elements table: " + err.__str__())
Exemplo n.º 4
0
def delete_row(table, rowKey):
    """
    deletes a complete row (rowKey) from a given table
    """

    hasBinaryFiles = ["Elements"]
    hasGraphData = ["Elements"]

    transport = roboearth.openDBTransport()
    client = transport['client']
    try:
        client.deleteAllRow(table, rowKey)
        roboearth.closeDBTransport(transport)
        if table in hasBinaryFiles:
            hdfs.rm_dir(os.path.join(roboearth.UPLOAD_DIR, table.lower(), rowKey.replace('.', '/')))
        if table in hasGraphData:
            sesame.rm(rowKey, table)
            
    except IOError, err:
        roboearth.closeDBTransport(transport)
        print table, rowKey
        raise roboearth.DBWriteErrorException("Can't delete data: " + err.__str__())
Exemplo n.º 5
0
def set(id_, class_, description, object_description, author, files=None):
    """
    write a object description to the database

    id_ : object identifier

    class_: object class

    description: human readable description

    object_description: owl description

    author: author of the description

    files: dictionary of binary files (file identifier : file)
    """
    logging.basicConfig(filename='example.log', level=logging.DEBUG)
    transport = roboearth.openDBTransport()
    client = transport['client']

    identifier = class_.replace(' ',
                                '').lower().strip('.') + '.' + id_.replace(
                                    ' ', '').lower().strip('.')

    #create paths
    path = class_.replace(' ', '').lower().strip('.') + '.' + id_.replace(
        ' ', '').lower().strip('.')
    wwwPath = roboearth.DOMAIN + os.path.join("data/", 'elements/',
                                              path.replace('.', '/'))
    path = os.path.join(roboearth.UPLOAD_DIR, 'elements/',
                        path.replace('.', '/'))

    try:
        # object already exists
        if get(query=identifier, exact=True):
            return None

        # upload files and build file mutation list for hbase operation
        file_mutation_list = []
        if files:
            for file_ID, file_ in files.items():
                hdfs.upload_file(file_, path)
                file_mutation_list.append(
                    Mutation(column="file:" + file_ID,
                             value=wwwPath + "/" + file_.name))

        # now write to hbase
        client.mutateRow("Elements", identifier, [
            Mutation(column="info:description", value=description),
            Mutation(column="info:author", value=author),
            Mutation(column="info:rating", value="1"),
            Mutation(column="info:type", value="object"),
            Mutation(column="owl:description", value=object_description)
        ] + file_mutation_list)

        #write data to sesame
        sesame_ret = sesame.set(object_description, identifier, "elements")
        if sesame_ret != "0":
            hbase_op.delete_row("Elements", identifier)
            print 'raising shit'
            raise IllegalArgument(sesame_ret)

        client.mutateRow("Users", author,
                         [Mutation(column="element:" + identifier, value="")])

        roboearth.closeDBTransport(transport)

        #if not roboearth.local_installation:
        #    roboearth.send_twitter_message("upload", "Object", identifier, author)

        return {
            'id': identifier,
            'description': description,
            'object_description': object_description
        }

    except (IOError, IllegalArgument), err:
        try:  # try clean up
            hdfs.rm_dir(path)
            hbase_op.delete_row("Elements", identifier)
            sesame.rm(identifier, "Elements")
        except:
            sys.exc_clear()
        import traceback
        logging.info(traceback.format_exc())
        raise roboearth.DBWriteErrorException(
            "Can't write data to Object table: HEREEEEE " + err.__str__())
Exemplo n.º 6
0
def set(id_, class_, description, object_description, author, files=None):
    """
    write a object description to the database

    id_ : object identifier

    class_: object class

    description: human readable description

    object_description: owl description

    author: author of the description

    files: dictionary of binary files (file identifier : file)
    """
    logging.basicConfig(filename='example.log',level=logging.DEBUG)
    transport = roboearth.openDBTransport()
    client = transport['client']

    identifier = class_.replace(' ', '').lower().strip('.') + '.' + id_.replace(' ', '').lower().strip('.')

    #create paths
    path = class_.replace(' ', '').lower().strip('.') +  '.' +id_.replace(' ', '').lower().strip('.')
    wwwPath = roboearth.DOMAIN + os.path.join("data/", 'elements/', path.replace('.', '/'))
    path = os.path.join(roboearth.UPLOAD_DIR, 'elements/', path.replace('.', '/'))

    try:
        # object already exists
        if get(query=identifier, exact=True):
            return None
        
        # upload files and build file mutation list for hbase operation
        file_mutation_list = [ ]
        if files:
            for file_ID, file_ in files.items():
                hdfs.upload_file(file_, path)
                file_mutation_list.append(Mutation(column="file:"+file_ID, value=wwwPath+"/"+file_.name))

        # now write to hbase
        client.mutateRow("Elements", identifier,
                         [Mutation(column="info:description", value=description),
                          Mutation(column="info:author", value=author),
                          Mutation(column="info:rating", value="1"),
                          Mutation(column="info:type", value="object"),
                          Mutation(column="owl:description", value=object_description)]+
                         file_mutation_list)
        
        #write data to sesame
        sesame_ret = sesame.set(object_description, identifier, "elements")
        if sesame_ret != "0":
            hbase_op.delete_row("Elements", identifier)
            print 'raising shit'
            raise IllegalArgument(sesame_ret)

        client.mutateRow("Users", author,
                         [Mutation(column="element:"+identifier, value="")])

        roboearth.closeDBTransport(transport)

        #if not roboearth.local_installation:
        #    roboearth.send_twitter_message("upload", "Object", identifier, author)

        return {'id' : identifier, 'description' : description, 'object_description' : object_description}

    except (IOError, IllegalArgument), err:
        try: # try clean up
            hdfs.rm_dir(path)
            hbase_op.delete_row("Elements", identifier)
            sesame.rm(identifier, "Elements")
        except:
            sys.exc_clear()
        import traceback
        logging.info(traceback.format_exc())
        raise roboearth.DBWriteErrorException("Can't write data to Object table: HEREEEEE " + err.__str__())