Exemplo n.º 1
0
    def delete(self, **kwargs):
        """
        Delete the current edge from the graph.
        """
        if self.__abstract__:  # pragma: no cover
            raise GoblinQueryError('cant delete abstract elements')
        if self._id is None:
            return self

        future = connection.get_future(kwargs)
        future_result = self._delete_edge()

        def on_read(f2):
            try:
                result = f2.result()
            except Exception as e:
                future.set_exception(e)
            else:
                future.set_result(result)

        def on_delete(f):
            try:
                stream = f.result()
            except Exception as e:
                future.set_exception(e)
            else:
                future_read = stream.read()
                future_read.add_done_callback(on_read)

        future_result.add_done_callback(on_delete)

        return future
 def on_read(f):
     try:
         result = f.result()
     except Exception as e:
         future.set_exception(e)
     else:
         if not result:
             future.set_exception(GoblinQueryError("Does not exist"))
         future.set_result(result[0])
Exemplo n.º 3
0
 def id_handler(results):
     try:
         results = list(filter(None, results))
     except TypeError:
         raise cls.DoesNotExist
     if len(results) != len(ids) and match_length:
         raise GoblinQueryError(
             "the number of results don't match the number of " +
             "ids requested")
     return results
Exemplo n.º 4
0
        def edge_handler(results):
            try:
                results = list(filter(None, results))
            except TypeError:
                raise cls.DoesNotExist
            if len(results) != len(ids):
                raise GoblinQueryError(
                    "the number of results don't match the number of edge " +
                    "ids requested")

            objects = []
            for r in results:
                try:
                    objects += [Element.deserialize(r)]
                except KeyError:  # pragma: no cover
                    raise GoblinQueryError('Edge type "%s" is unknown' % '')

            if as_dict:  # pragma: no cover
                return {e._id: e for e in objects}

            return objects
Exemplo n.º 5
0
        def result_handler(results):
            objects = []
            for r in results:
                if deserialize:
                    try:
                        objects += [Element.deserialize(r)]
                    except KeyError:  # pragma: no cover
                        raise GoblinQueryError(
                            'Vertex type "%s" is unknown' % r.get('label', ''))
                else:
                    objects = results

            if as_dict:  # pragma: no cover
                return {v._id: v for v in objects}

            return objects
Exemplo n.º 6
0
    def all(cls, ids, as_dict=False, *args, **kwargs):
        """
        Load all edges with the given edge_ids from the graph. By default this
        will return a list of edges but if as_dict is True then it will return
        a dictionary containing edge_ids as keys and edges found as values.

        :param ids: A list of titan IDs
        :type ids: list
        :param as_dict: Toggle whether to return a dictionary or list
        :type as_dict: boolean
        :rtype: dict | list
        """
        if not isinstance(ids, array_types):
            raise GoblinQueryError("ids must be of type list or tuple")

        # strids = [str(i) for i in ids]
        def edge_handler(results):
            try:
                results = list(filter(None, results))
            except TypeError:
                raise cls.DoesNotExist
            if len(results) != len(ids):
                raise GoblinQueryError(
                    "the number of results don't match the number of edge " +
                    "ids requested")

            objects = []
            for r in results:
                try:
                    objects += [Element.deserialize(r)]
                except KeyError:  # pragma: no cover
                    raise GoblinQueryError('Edge type "%s" is unknown' % '')

            if as_dict:  # pragma: no cover
                return {e._id: e for e in objects}

            return objects

        return connection.execute_query("g.E(*eids)",
                                        bindings={'eids': ids},
                                        handler=edge_handler,
                                        **kwargs)
 def has(self, key, value, compare=EQUAL):
     """
     :param key: key to lookup
     :type key: str | goblin.properties.GraphProperty
     :param value: value to compare
     :type value: str, float, int
     :param compare: comparison keyword
     :type compare: str
     :rtype: Query
     """
     q = copy.copy(self)
     if issubclass(type(key), property):
         msg = "Use %s.get_property_by_name" % (self.__class__.__name__)
         logger.error(msg)
         raise GoblinQueryError(msg)
     binding = self._get_binding(value)
     if compare in [INSIDE, OUTSIDE, BETWEEN, WITHIN]:
         step = "has('{}', {}(*{}))".format(key, compare, binding)
     else:
         step = "has('{}', {}({}))".format(key, compare, binding)
     q._steps.append(step)
     return q
Exemplo n.º 8
0
    def all(cls, ids=[], as_dict=False, match_length=True, *args, **kwargs):
        """
        Load all vertices with the given ids from the graph. By default this
        will return a list of vertices but if as_dict is True then it will
        return a dictionary containing ids as keys and vertices found as
        values.

        :param ids: A list of titan ids
        :type ids: list
        :param as_dict: Toggle whether to return a dictionary or list
        :type as_dict: boolean
        :rtype: dict | list

        """
        if not isinstance(ids, array_types):
            raise GoblinQueryError("ids must be of type list or tuple")

        deserialize = kwargs.pop('deserialize', True)
        handlers = []
        future = connection.get_future(kwargs)
        if len(ids) == 0:
            future_results = connection.execute_query(
                'g.V.hasLabel(x)', bindings={"x": cls.get_label()}, **kwargs)

        else:
            strids = [str(i) for i in ids]
            # Need to test sending complex bindings with client
            vids = ", ".join(strids)
            future_results = connection.execute_query(
                'g.V(%s)' % vids, **kwargs)

            def id_handler(results):
                try:
                    results = list(filter(None, results))
                except TypeError:
                    raise cls.DoesNotExist
                if len(results) != len(ids) and match_length:
                    raise GoblinQueryError(
                        "the number of results don't match the number of " +
                        "ids requested")
                return results

            handlers.append(id_handler)

        def result_handler(results):
            objects = []
            for r in results:
                if deserialize:
                    try:
                        objects += [Element.deserialize(r)]
                    except KeyError:  # pragma: no cover
                        raise GoblinQueryError(
                            'Vertex type "%s" is unknown' % r.get('label', ''))
                else:
                    objects = results

            if as_dict:  # pragma: no cover
                return {v._id: v for v in objects}

            return objects

        handlers.append(result_handler)

        def on_all(f):
            try:
                stream = f.result()
            except Exception as e:
                future.set_exception(e)
            else:
                [stream.add_handler(h) for h in handlers]
                future.set_result(stream)

        future_results.add_done_callback(on_all)

        return future