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__())
def update(id_, environment, author): """ id_ : robot identifier environment : reference to the related environment author : author who submitted the data """ transport = roboearth.openDBTransport() client = transport['client'] try: client.mutateRow("Elements", id_, [Mutation(column="info:author", value=author)]) # 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="Robots#"+id_)]) client.scannerClose(scanner) roboearth.closeDBTransport(transport) return {'id' : id_} except (IOError, IllegalArgument), err: raise roboearth.DBWriteErrorException("Can't write data to Robots table: " + err.__str__())
def updateLocation(id_, posX, posY, posZ, delta, author): """ update existing object location at the database id_ : complete identifier (primary key) of the data posX, posY, posZ : coordiantes which describes the exact location delta : impreciseness of position data author: author of the description """ transport = roboearth.openDBTransport() client = transport['client'] try: oldLocation = getLocation(id_) object_ = oldLocation[0]['object'] environment = oldLocation[0]['environment'] room_number = oldLocation[0]['room_number'] newID = environment + '.' + room_number + '.' + posX + '.' + posY + '.' + posZ + '.' + delta client.mutateRow("ObjectLocations", newID, [ Mutation(column="object:id", value=object_), Mutation(column="info:author", value=author), Mutation(column="environment:id", value=environment), Mutation(column="environment:room", value=room_number), Mutation(column="position:x", value=posX), Mutation(column="position:y", value=posY), Mutation(column="position:z", value=posZ), Mutation(column="position:delta", value=delta) ]) roboearth.closeDBTransport(transport) hbase_op.delete_row("ObjectLocations", id_) return { 'id': id_, 'posX': posX, 'posY': posY, 'posZ': posZ, 'delta': delta, 'object': object_, 'environment': environment, 'room_number': room_number } except (IOError, IllegalArgument), err: raise roboearth.DBWriteErrorException( "Can't write data to Robots table: " + err.__str__())
def set(id_, class_, description, recipe, author): """ write recipe to the database id_ : recipe identifier class_: recipe class description: human readable description recipe: owl description author: author of the description """ transport = roboearth.openDBTransport() client = transport['client'] recipeName = class_.replace(' ', '').lower().strip('.') + '.' + id_.replace( ' ', '').lower().strip('.') try: # recipe already exist if get(query=recipeName, exact=True): return None #write data to hbase client.mutateRow("Elements", recipeName, [ Mutation(column="info:description", value=description), Mutation(column="info:author", value=author), Mutation(column="info:rating", value="1"), Mutation(column="info:type", value="recipe"), Mutation(column="owl:description", value=recipe) ]) #write data to sesame sesame_ret = sesame.set(recipe, recipeName, "elements") if sesame_ret != "0": hbase_op.delete_row("Elements", recipeName) raise IllegalArgument(sesame_ret) client.mutateRow("Users", author, [Mutation(column="element:" + recipeName, value="")]) roboearth.closeDBTransport(transport) return {'id': recipeName, 'description': description, 'recipe': recipe} except (IOError, IllegalArgument), err: raise roboearth.DBWriteErrorException( "Can't write data to Action Recipe table: " + err.__str__())
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__())
def create(user_name): """ create hbase table for a new user user_name : user name of the new user """ transport = roboearth.openDBTransport() client = transport['client'] try: client.mutateRow("Users", user_name, []) roboearth.closeDBTransport(transport) except (IOError, IllegalArgument), err: raise roboearth.DBWriteErrorException( "Can't write data to users table: " + err.__str__())
def set(id_, author, description, srdl, picture): """ write a robot location to the database id_ : robot identifier author : author who submitted the data description : description of the reobot srdl: Semantic Robot Description of the robot picture: picture of the robot """ transport = roboearth.openDBTransport() client = transport['client'] if get(query=id_, exact=True): return None try: #create paths path = id_.replace(' ', '').lower().strip('.') + '.' +id_.replace(' ', '').lower().strip('.') wwwPath = roboearth.DOMAIN + os.path.join("data/", 'elements/', id_.replace(' ', '').lower().strip('.'), picture.name) hdfs.upload_file(picture, os.path.join(roboearth.UPLOAD_DIR, 'elements/', id_.replace(' ', '').lower().strip('.'))) client.mutateRow("Elements", id_.lower(), [Mutation(column="info:author", value=author), Mutation(column="info:description", value=description), Mutation(column="owl:description", value=srdl), Mutation(column="info:picture", value=wwwPath), Mutation(column="info:type", value='robot')]) client.mutateRow("Users", author, [Mutation(column="element:"+id_, value="")]) roboearth.closeDBTransport(transport) return {'id' : id_, 'description' : description, 'srdl' : srdl} except (IOError, IllegalArgument), err: raise roboearth.DBWriteErrorException("Can't write data to Robot table: " + err.__str__())
def update_rating(id_, rating): """ update the rating of an existing recipe at the database id_ : complete identifier (primary key) of the recipe rating : new rating """ transport = roboearth.openDBTransport() client = transport['client'] try: client.mutateRow("Elements", id_, [Mutation(column="info:rating", value=str(rating))]) roboearth.closeDBTransport(transport) return True except (IOError, IllegalArgument), err: raise roboearth.DBWriteErrorException( "Can't update Action Recipe rating: " + err.__str__())
def upload_file(f, path): """ write file to hdfs f : file path : hdfs location of the file """ try: # write file to hdfs filename = os.path.join(path, f.name) if not os.path.exists(path): os.makedirs(path) if os.path.exists(filename): raise roboearth.DBWriteErrorException #os.remove(filename) destination = open(filename, 'w') for chunk in f.chunks(): destination.write(chunk) destination.close() except: raise roboearth.DBWriteErrorException("Can't write" + os.path.join(path, f.name) + " to hdfs")
def delete_column(table, rowKey, column): """ deletes a column from a given row (rowKey) in a specific table """ transport = roboearth.openDBTransport() client = transport['client'] scanner = client.scannerOpenWithPrefix(table, rowKey, []) res = client.scannerGet(scanner) for i in res[0].columns: if i == column: break client.scannerClose(scanner) try: client.deleteAll(table, rowKey, column) roboearth.closeDBTransport(transport) except IOError, err: roboearth.closeDBTransport(transport) raise roboearth.DBWriteErrorException("Can't delete column: " + err.__str__())
raise IllegalArgument(sesame_ret + 'bug' + envName + ' ' + environment) client.mutateRow("Users", author, [Mutation(column="element:" + envName, value="")]) roboearth.closeDBTransport(transport) return { 'id': envName, 'description': description, 'environment': environment } except (IOError, IllegalArgument), err: raise roboearth.DBWriteErrorException( "Can't write data to Environment table: " + err.__str__()) def update(id_, data, author): """ update an existing recipe at the database id_ : complete identifier (primary key) of the recipe data: dictionary with the updated data author: author of the description """ transport = roboearth.openDBTransport() client = transport['client']
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__())
def setLocation(environment, room_number, object_, posX, posY, posZ, delta, author): """ write a object location to the database environment : reference to an environment, stored at the database room_number: the room where the object is located object : reference to the object description posX, posY, posZ : coordiantes which describes the exact location delta : impreciseness of position data author : author of the description """ transport = roboearth.openDBTransport() client = transport['client'] try: scanner = client.scannerOpenWithPrefix("Objects", object_.lower(), []) if not client.scannerGet(scanner): raise roboearth.NoDBEntryFoundException( "Couldn't connect location with the named object. Object doesn't exits in database" ) scanner = client.scannerOpenWithPrefix("Environments", environment.lower(), []) if not client.scannerGet(scanner): raise roboearth.NoDBEntryFoundException( "Couldn't connect location with the named environment. Environment doesn't exits in database" ) id_ = environment + '.' + room_number + '.' + posX + '.' + posY + '.' + posZ + '.' + delta if getLocation(query=id_, exact=True): return None client.mutateRow("ObjectLocations", id_, [ Mutation(column="object:id", value=object_), Mutation(column="info:author", value=author), Mutation(column="environment:id", value=environment), Mutation(column="environment:room", value=room_number), Mutation(column="position:x", value=posX), Mutation(column="position:y", value=posY), Mutation(column="position:z", value=posZ), Mutation(column="position:delta", value=delta) ]) roboearth.closeDBTransport(transport) return { 'id': id_, 'posX': posX, 'posY': posY, 'posZ': posZ, 'delta': delta, 'object': object_, 'environment': environment, 'room_number': room_number } except (IOError, IllegalArgument), err: raise roboearth.DBWriteErrorException( "Can't write dataop to Object table: " + err.__str__())