<commit_before>/*
 * Copyright (c) 2002-2015 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This product is licensed to you under the Apache License, Version 2.0 (the "License").
 * You may not use this product except in compliance with the License.
 *
 * This product may include a number of subcomponents with
 * separate copyright notices and license terms. Your use of the source
 * code for these subcomponents is subject to the terms and
 * conditions of the subcomponent's license, as noted in the LICENSE file.
 *
 */

package org.neo4j.ogm.session.response;

import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.neo4j.ogm.session.result.ResultProcessingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author <NAME>
 */
public class JsonResponse implements Neo4jResponse<String> {

    private static final String COMMA = ",";
    private static final String START_RECORD_TOKEN = "{";
    private static final String NEXT_RECORD_TOKEN  = COMMA + START_RECORD_TOKEN;

    private static final String ERRORS_TOKEN = "],\"errors";
    private static final String COLUMNS_TOKEN = "{\"columns";

    private static final String GRAPH_TOKEN = "\"graph";
    private static final String ROW_TOKEN = "\"row";
    private static final String RESULTS_TOKEN = "\"results";
    private static final String STATS_TOKEN = "\"stats";

    private static final Logger LOGGER = LoggerFactory.getLogger(JsonResponse.class);

    private final InputStream results;
    private final Scanner scanner;
    private final CloseableHttpResponse response;
    private String scanToken = null;
    private String[] columns;
    private int currentRow = -1;


    public JsonResponse(CloseableHttpResponse response) {
        try {
            this.response = response;
            this.results = response.getEntity().getContent();
            this.scanner = new Scanner(results, "UTF-8");
        } catch (IOException ioException) {
            throw new RuntimeException(ioException);
        }
    }

    public void initialiseScan(ResponseRecord record) {
        this.scanToken = extractToken(record);
        this.scanner.useDelimiter(scanToken);
        // TODO: this currently assumes only ONE data[] element in the response stream.
        parseColumns();
    }

    public String next() {
        try {
            String json = scanner.next();

            while (!json.endsWith(NEXT_RECORD_TOKEN)) {
                // the scan token may be embedded in the current response record, we need to keep parsing...
                try {
                    String rest = scanner.next();
                    json = json + scanToken + rest;<commit_msg>DATAGRAPH-717 - Performance improvements: decoupled ResponseHandler from HttpClient to make perf tests easier to write<commit_after>/*
 * Copyright (c) 2002-2015 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This product is licensed to you under the Apache License, Version 2.0 (the "License").
 * You may not use this product except in compliance with the License.
 *
 * This product may include a number of subcomponents with
 * separate copyright notices and license terms. Your use of the source
 * code for these subcomponents is subject to the terms and
 * conditions of the subcomponent's license, as noted in the LICENSE file.
 *
 */

package org.neo4j.ogm.session.response;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.neo4j.ogm.session.result.ResultProcessingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

/**
 * @author <NAME>
 */
public class JsonResponse implements Neo4jResponse<String> {

    private static final String COMMA = ",";
    private static final String START_RECORD_TOKEN = "{";
    private static final String NEXT_RECORD_TOKEN  = COMMA + START_RECORD_TOKEN;

    private static final String ERRORS_TOKEN = "],\"errors";
    private static final String COLUMNS_TOKEN = "{\"columns";

    private static final String GRAPH_TOKEN = "\"graph";
    private static final String ROW_TOKEN = "\"row";
    private static final String RESULTS_TOKEN = "\"results";
    private static final String STATS_TOKEN = "\"stats";

    private static final Logger LOGGER = LoggerFactory.getLogger(JsonResponse.class);

    private final InputStream results;
    private final Scanner scanner;
    private final CloseableHttpResponse response;
    private String scanToken = null;
    private String[] columns;
    private int currentRow = -1;


    public JsonResponse(CloseableHttpResponse response) {
        try {
            this.response = response;
            this.results = response.getEntity().getContent();
            this.scanner = new Scanner(results, "UTF-8");
        } catch (IOException ioException) {
            throw new RuntimeException(ioException);
        }
    }

    public JsonResponse(InputStream inputStream)  {
        this.response = null;
        this.results = inputStream;
        this.scanner = new Scanner(results, "UTF-8");
    }

    public void initialiseScan(ResponseRecord record) {
        this.scanToken = extractToken(record);
        this.scanner.useDelimiter(scanToken);
        // TODO: this currently assumes only ONE data[] element in the response stream.
        parseColumns();
    }

    public String next() {
        try {
            String json = scanner.next();

            while (!json.endsWith(NEXT_RECORD_TOKEN)) {
                // the scan token may be embedded in the current response record, we need to keep parsing...
                try {
                    String rest = scanner.next();
                    json = json + scanToken + rest;
{'fraction_non_alphanumeric': 0.08629441624365482, 'fraction_numerical': 0.006429780033840948, 'mean_word_length': 3.03481228668942, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7058535', 'n_tokens_mistral': 1583, 'n_tokens_neox': 1526, 'n_words': 537}