예제 #1
0
    def __init__(self, config):
        """
        Initializes a resource object.

        :param root_uri: the base URL of Rexster.

        """
        self.config = config
        self.registry = Registry(config)
        self.scripts = Scripts()
        dir_name = os.path.dirname(__file__)
        self.scripts.override(get_file_path(dir_name, "gremlin.groovy"))
        self.registry.add_scripts("gremlin", self.scripts)
        self.type_system, content_type = get_type_system(config)
        self.request = RexsterRequest(config, content_type=content_type)
예제 #2
0
    def __init__(self,config):
        """
        Initializes a resource object.

        :param root_uri: the base URL of Rexster.

        """
        self.config = config
        self.registry = Registry(config)
        self.scripts = Scripts() 
        dir_name = os.path.dirname(__file__)
        self.scripts.override(get_file_path(dir_name,"gremlin.groovy"))
        self.registry.add_scripts("gremlin",self.scripts)
        self.type_system, content_type = get_type_system(config)
        self.request = RexsterRequest(config,content_type=content_type)
예제 #3
0
class RexsterResource(Resource):

    vertex_path = "vertices"
    edge_path = "edges"
    index_path = "indices"
    gremlin_path = "tp/gremlin"
    transaction_path = "tp/batch/tx"
    multi_get_path = "tp/batch"

    def __init__(self, config):
        """
        Initializes a resource object.

        :param root_uri: the base URL of Rexster.

        """
        self.config = config
        self.registry = Registry(config)
        self.scripts = Scripts()
        dir_name = os.path.dirname(__file__)
        self.scripts.override(get_file_path(dir_name, "gremlin.groovy"))
        self.registry.add_scripts("gremlin", self.scripts)
        self.type_system, content_type = get_type_system(config)
        self.request = RexsterRequest(config, content_type=content_type)
        #self.index_class = ManualIndex

    #def convert_to_db(self,data):
    #    rexster_data = dict()
    #    for key, value in data.items():
    #        rexster_data[key] = self.type_system.to_db(value)
    #    return rexster_data

    #
    # Gremlin
    #
    def gremlin(self, script):
        params = dict(script=script)
        return self.request.post(self.gremlin_path, params)

    #
    # Vertices
    #
    def create_vertex(self, data):
        #data = self.convert_to_db(data)
        return self.request.post(self.vertex_path, data)

    def get_vertex(self, _id):
        path = build_path(self.vertex_path, _id)
        return self.request.get(path, params=None)

    def update_vertex(self, _id, data):
        #data = self.convert_to_db(data)
        path = build_path(self.vertex_path, _id)
        return self.request.put(path, data)

    def delete_vertex(self, _id):
        path = build_path(self.vertex_path, _id)
        return self.request.delete(path, params=None)

    #
    # Edges
    #
    def create_edge(self, outV, label, inV, data={}):
        #data = self.convert_to_db(data)
        edge_data = dict(_outV=outV, _label=label, _inV=inV)
        data.update(edge_data)
        return self.request.post(self.edge_path, data)

    def get_edge(self, _id):
        path = build_path(self.edge_path, _id)
        return self.request.get(path, params=None)

    def update_edge(self, _id, data):
        #data = self.convert_to_db(data)
        path = build_path(self.edge_path, _id)
        return self.request.put(path, data)

    def delete_edge(self, _id):
        path = build_path(self.edge_path, _id)
        return self.request.delete(path, params=None)

    #
    # Vertex Index
    #

    def create_automatic_vertex_index(self,
                                      index_name,
                                      element_class,
                                      keys=None):
        keys = json.dumps(keys) if keys else "null"
        params = dict(index_name=index_name,
                      element_class=element_class,
                      keys=keys)
        script = self.scripts.get('create_automatic_vertex_index', params)
        return self.gremlin(script)

    def create_indexed_vertex_automatic(self, data, index_name):
        data = json.dumps(data)
        params = dict(data=data, index_name=index_name)
        script = self.scripts.get('create_automatic_indexed_vertex', params)
        return self.gremlin(script)

    def create_vertex_index(self, name, *args, **kwds):
        path = build_path(self.index_path, name)
        index_type = kwds.get('index_type', 'automatic')
        index_keys = kwds.get('index_keys', None)
        params = {'class': 'vertex', 'type': index_type}
        if index_keys:
            params.update({'keys': index_keys})
        return self.request.post(path, params)

    def create_edge_index(self, name, *args, **kwds):
        path = build_path(self.index_path, name)
        index_type = kwds.get('index_type', 'automatic')
        index_keys = kwds.get('index_keys', None)
        params = {'class': 'edge', 'type': index_type}
        if index_keys:
            params.update({'keys': index_keys})
        return self.request.post(path, params)

    def get_index(self, name):
        path = build_path(self.index_path, name)
        return self.request.get(path, params=None)

    def get_vertex_index(self, name):
        return self.get_index(name)

    def get_edge_index(self, name):
        return self.get_index(name)

    def get_all_indices(self):
        return self.request.get(self.index_path, params=None)

    def delete_index(self, name):
        path = build_path(self.index_path, name)
        return self.request.delete(path, params=None)

    def delete_vertex_index(self, name):
        self.delete_index(name)

    def delete_edge_index(self, name):
        self.delete_index(name)

    # Indexed vertices
    def put_vertex(self, name, key, value, _id):
        # Rexster's API only supports string lookups so convert value to a string
        path = build_path(self.index_path, name)
        params = {
            'key': key,
            'value': str(value),
            'class': 'vertex',
            'id': _id
        }
        return self.request.post(path, params)

    def put_edge(self, name, key, value, _id):
        # Rexster's API only supports string lookups so convert value to a string
        path = build_path(self.index_path, name)
        params = {'key': key, 'value': str(value), 'class': 'edge', 'id': _id}
        return self.request.post(path, params)

    def lookup_vertex(self, index_name, key, value):
        path = build_path(self.index_path, index_name)
        params = dict(key=key, value=value)
        return self.request.get(path, params)

    def remove_vertex(self, name, _id, key=None, value=None):
        # Can Rexster have None for key and value?
        path = build_path(self.index_path, name)
        params = {'key': key, 'value': value, 'class': 'vertex', 'id': _id}
        return self.request.delete(path, params)

    def remove_edge(self, name, _id, key=None, value=None):
        # Can Rexster have None for key and value?
        path = build_path(self.index_path, name)
        params = {'key': key, 'value': value, 'class': 'edge', 'id': _id}
        return self.request.delete(path, params)

    # Rexster-specific index mehthods
    def index_count(self, name, key, value):
        path = build_path(self.index_path, name, "count")
        params = dict(key=key, value=value)
        return self.request.get(path, params)

    def index_keys(self, name):
        path = build_path(self.index_path, name, "keys")
        return self.request.get(path, params=None)

    # Model
    def create_indexed_vertex(self, index_name, data={}, keys=None):
        script = self.scripts.create_indexed_vertex(index_name, data, keys)
        return self.gremlin(script)

    #
    # Model Proxy - Vertex
    #

    def create_indexed_vertex(self, data, index_name, keys=None):
        params = dict(data=data, index_name=index_name, keys=keys)
        script = self.scripts.get("create_indexed_vertex")
        return self.gremlin(script, params)

    def update_indexed_vertex(self, _id, data, index_name, keys=None):
        params = dict(_id=_id, data=data, index_name=index_name, keys=keys)
        script = self.scripts.get("update_indexed_vertex")
        return self.gremlin(script, params)

    #
    # Model Proxy - Edge
    #

    def create_indexed_edge(self, data, index_name, keys=None):
        params = dict(data=data, index_name=index_name, keys=keys)
        script = self.scripts.get("create_indexed_edge")
        return self.gremlin(script, params)

    def update_indexed_edge(self, _id, data, index_name, keys=None):
        params = dict(_id=_id, data=data, index_name=index_name, keys=keys)
        script = self.scripts.get("update_indexed_edge")
        return self.gremlin(script, params)

    #
    # Rexster Specific Stuff
    #
    def rebuild_vertex_index(self, index_name):
        params = dict(index_name=index_name)
        script = self.scripts.get('rebuild_vertex_index', params)
        return self.gremlin(script)

    def rebuild_edge_index(self, index_name):
        params = dict(index_name=index_name)
        script = self.scripts.get('rebuild_edge_index', params)
        return self.gremlin(script)

    # TODO: manual/custom index API

    def multi_get_vertices(self, id_list):
        path = build_path(self.multi_get_path, "vertices")
        idList = build_url_list(id_list)
        params = dict(idList=idList)
        return self.request.get(path, params)

    def multi_get_edges(self, id_list):
        path = build_path(self.multi_get_path, "edges")
        idList = build_url_list(id_list)
        params = dict(idList=idList)
        return self.request.get(path, params)

    def execute_transaction(self, transaction):
        params = dict(tx=transaction.actions)
        return self.request.post(self.transction_path, params)
예제 #4
0
class RexsterResource(Resource):
    
    vertex_path = "vertices"
    edge_path = "edges"
    index_path = "indices"
    gremlin_path = "tp/gremlin"
    transaction_path = "tp/batch/tx"
    multi_get_path = "tp/batch"

    def __init__(self,config):
        """
        Initializes a resource object.

        :param root_uri: the base URL of Rexster.

        """
        self.config = config
        self.registry = Registry(config)
        self.scripts = Scripts() 
        dir_name = os.path.dirname(__file__)
        self.scripts.override(get_file_path(dir_name,"gremlin.groovy"))
        self.registry.add_scripts("gremlin",self.scripts)
        self.type_system, content_type = get_type_system(config)
        self.request = RexsterRequest(config,content_type=content_type)
        #self.index_class = ManualIndex


    #def convert_to_db(self,data):
    #    rexster_data = dict()
    #    for key, value in data.items():
    #        rexster_data[key] = self.type_system.to_db(value)
    #    return rexster_data

    #
    # Gremlin
    #
    def gremlin(self,script): 
        params = dict(script=script)
        return self.request.post(self.gremlin_path,params)

    #
    # Vertices
    #
    def create_vertex(self,data):
        #data = self.convert_to_db(data)
        return self.request.post(self.vertex_path,data)

    def get_vertex(self,_id):
        path = build_path(self.vertex_path,_id)
        return self.request.get(path,params=None)

    def update_vertex(self,_id,data):
        #data = self.convert_to_db(data)
        path = build_path(self.vertex_path,_id)
        return self.request.put(path,data)
        
    def delete_vertex(self,_id):
        path = build_path(self.vertex_path,_id)
        return self.request.delete(path,params=None)

    #
    # Edges
    #
    def create_edge(self,outV,label,inV,data={}): 
        #data = self.convert_to_db(data)
        edge_data = dict(_outV=outV,_label=label,_inV=inV)
        data.update(edge_data)
        return self.request.post(self.edge_path,data)

    def get_edge(self,_id):
        path = build_path(self.edge_path,_id)
        return self.request.get(path,params=None)

    def update_edge(self,_id,data):
        #data = self.convert_to_db(data)
        path = build_path(self.edge_path,_id)
        return self.request.put(path,data)

    def delete_edge(self,_id):
        path = build_path(self.edge_path,_id)
        return self.request.delete(path,params=None)

    #
    # Vertex Index
    #

    def create_automatic_vertex_index(self,index_name,element_class,keys=None):
        keys = json.dumps(keys) if keys else "null"
        params = dict(index_name=index_name,element_class=element_class,keys=keys)
        script = self.scripts.get('create_automatic_vertex_index',params)
        return self.gremlin(script)
        
    def create_indexed_vertex_automatic(self,data,index_name):
        data = json.dumps(data)
        params = dict(data=data,index_name=index_name)
        script = self.scripts.get('create_automatic_indexed_vertex',params)
        return self.gremlin(script)

    def create_vertex_index(self,name,*args,**kwds):
        path = build_path(self.index_path,name)
        index_type = kwds.get('index_type','automatic')
        index_keys = kwds.get('index_keys',None)                              
        params = {'class':'vertex','type':index_type}
        if index_keys: 
            params.update({'keys':index_keys})
        return self.request.post(path,params)

    def create_edge_index(self,name,*args,**kwds):
        path = build_path(self.index_path,name)
        index_type = kwds.get('index_type','automatic')
        index_keys = kwds.get('index_keys',None)                              
        params = {'class':'edge','type':index_type}
        if index_keys: 
            params.update({'keys':index_keys})
        return self.request.post(path,params)
        
    def get_index(self,name):
        path = build_path(self.index_path,name)
        return self.request.get(path,params=None)

    def get_vertex_index(self,name):
        return self.get_index(name)

    def get_edge_index(self,name):
        return self.get_index(name)

    def get_all_indices(self):
        return self.request.get(self.index_path,params=None)
        
    def delete_index(self,name): 
        path = build_path(self.index_path,name)
        return self.request.delete(path,params=None)

    def delete_vertex_index(self,name):
        self.delete_index(name)

    def delete_edge_index(self,name):
        self.delete_index(name)

    # Indexed vertices
    def put_vertex(self,name,key,value,_id):
        # Rexster's API only supports string lookups so convert value to a string 
        path = build_path(self.index_path,name)
        params = {'key':key,'value':str(value),'class':'vertex','id':_id}
        return self.request.post(path,params)

    def put_edge(self,name,key,value,_id):
        # Rexster's API only supports string lookups so convert value to a string 
        path = build_path(self.index_path,name)
        params = {'key':key,'value':str(value),'class':'edge','id':_id}
        return self.request.post(path,params)

    def lookup_vertex(self,index_name,key,value):
        path = build_path(self.index_path,index_name)
        params = dict(key=key,value=value)
        return self.request.get(path,params)

    def remove_vertex(self,name,_id,key=None,value=None):
        # Can Rexster have None for key and value?
        path = build_path(self.index_path,name)
        params = {'key':key,'value':value,'class':'vertex','id':_id}
        return self.request.delete(path,params)

    def remove_edge(self,name,_id,key=None,value=None):
        # Can Rexster have None for key and value?
        path = build_path(self.index_path,name)
        params = {'key':key,'value':value,'class':'edge','id':_id}
        return self.request.delete(path,params)
    
    # Rexster-specific index mehthods
    def index_count(self,name,key,value):
        path = build_path(self.index_path,name,"count")
        params = dict(key=key,value=value)
        return self.request.get(path,params)

    def index_keys(self,name):
        path = build_path(self.index_path,name,"keys")
        return self.request.get(path,params=None)


    # Model
    def create_indexed_vertex(self,index_name,data={},keys=None):
        script = self.scripts.create_indexed_vertex(index_name,data,keys)
        return self.gremlin(script)


    #
    # Model Proxy - Vertex
    #

    def create_indexed_vertex(self,data,index_name,keys=None):
        params = dict(data=data,index_name=index_name,keys=keys)
        script = self.scripts.get("create_indexed_vertex")
        return self.gremlin(script,params)
    
    def update_indexed_vertex(self,_id,data,index_name,keys=None):
        params = dict(_id=_id,data=data,index_name=index_name,keys=keys)
        script = self.scripts.get("update_indexed_vertex")
        return self.gremlin(script,params)


    #
    # Model Proxy - Edge
    #

    def create_indexed_edge(self,data,index_name,keys=None):
        params = dict(data=data,index_name=index_name,keys=keys)
        script = self.scripts.get("create_indexed_edge")
        return self.gremlin(script,params)
    
    def update_indexed_edge(self,_id,data,index_name,keys=None):
        params = dict(_id=_id,data=data,index_name=index_name,keys=keys)
        script = self.scripts.get("update_indexed_edge")
        return self.gremlin(script,params)


    #
    # Rexster Specific Stuff
    #
    def rebuild_vertex_index(self,index_name):
        params = dict(index_name=index_name)
        script = self.scripts.get('rebuild_vertex_index',params)
        return self.gremlin(script)

    def rebuild_edge_index(self,index_name):
        params = dict(index_name=index_name)
        script = self.scripts.get('rebuild_edge_index',params)
        return self.gremlin(script)


    # TODO: manual/custom index API

    def multi_get_vertices(self,id_list):
        path = build_path(self.multi_get_path,"vertices")
        idList = build_url_list(id_list)
        params = dict(idList=idList)
        return self.request.get(path,params)

    def multi_get_edges(self,id_list):
        path = build_path(self.multi_get_path,"edges")
        idList = build_url_list(id_list)
        params = dict(idList=idList)
        return self.request.get(path,params)

    def execute_transaction(self,transaction):
        params = dict(tx=transaction.actions)
        return self.request.post(self.transction_path,params)