Пример #1
0
    def _build(self):
        self.__fragment_lock.acquire()
        result = self.result_set()
        log.debug('Building a query result for request number {}'.format(self._request_id))

        try:
            # Query result chunking, yields JSON
            for ch in chunks(result, 1000):
                result_rows = []
                for t in ch:
                    if any(t):
                        result_row = {self.sink.map('?' + v).lstrip('?'): t[v] for v in t}
                        result_rows.append(result_row)
                if result_rows:
                    yield json.dumps(result_rows), {'state': 'streaming', 'source': 'store',
                                                    'response_to': self.sink.message_id,
                                                    'submitted_on': calendar.timegm(datetime.now().timetuple()),
                                                    'submitted_by': self.sink.submitted_by,
                                                    'format': 'json'}
        except Exception, e:
            log.error(e.message)
            raise
Пример #2
0
    def _build(self):
        """
        This function yields nothing only when the new state is 'streaming'
        :return: Quads like (context, subject, predicate, object)
        """

        timestamp = calendar.timegm(dt.now().timetuple())
        fragment = None

        self.sink.lock.acquire()
        try:
            fragment, streaming = self.fragment(timestamp=timestamp)
            if streaming:
                self.sink.stream = True
                if fragment:
                    self.sink.delivery = 'mixing'
                else:
                    self.sink.delivery = 'streaming'
            else:
                if fragment:
                    self.sink.delivery = 'pushing'
                    log.debug('Fragment retrieved from cache for request number {}'.format(self._request_id))
                else:
                    self.sink.delivery = 'sent'
                    log.debug('Sending end stream signal since there is no fragment and stream is disabled')
                    yield (), {'state': 'end', 'format': 'tuple'}
                self.sink.stream = False
        except Exception as e:
            log.warning(e.message)
            self.sink.stream = True
            self.sink.delivery = 'streaming'
        finally:
            self.sink.lock.release()

        if fragment:
            log.info('Building a stream result from cache for request number {}...'.format(self._request_id))
            filter_mapping = self.sink.filter_mapping
            self.__fragment_lock.acquire()
            try:
                for ch in chunks(fragment, 1000):
                    if ch:
                        rows = []
                        for (c, s, p, o) in ch:
                            real_context = map_variables(c, self.sink.mapping, filter_mapping)
                            consume = True
                            if self.sink.map(c[2]) in filter_mapping:
                                consume = match_filter(o, real_context[2])
                            if consume and self.sink.map(c[0]) in filter_mapping:
                                consume = match_filter(s, real_context[0])
                            if consume:
                                rows.append((real_context, s.n3(), p.n3(), o.n3()))
                        yield rows, {'source': 'store', 'format': 'tuple',
                                     'state': 'streaming',
                                     'response_to': self.sink.message_id,
                                     'submitted_on': calendar.timegm(
                                         datetime.now().timetuple()),
                                     'submitted_by': self.sink.submitted_by}
            finally:
                self.__fragment_lock.release()

        self.sink.lock.acquire()
        try:
            if self.sink.delivery == 'pushing' or (self.sink.delivery == 'mixing' and not self.sink.stream):
                self.sink.delivery = 'sent'
                log.info(
                    'The response stream of request {} is completed. Notifying...'.format(self.sink.request_id))
                yield (), {'state': 'end', 'format': 'tuple'}
            elif self.sink.delivery == 'mixing' and self.sink.stream:
                self.sink.delivery = 'streaming'
        finally:
            self.sink.lock.release()