<commit_before>'use strict'

const stream = require('pull-stream')
const pullDecode = require('pull-utf8-decoder')
const PullBlobStore = require('idb-pull-blob-store')
const PeerId = require('peer-id')

const deferred = require('deferred')

module.exports = class DatabaseManager {
  constructor (fileMetadata, EE) {
    this.metadata = fileMetadata
    this.config = new PullBlobStore('config')
    this._EE = EE
    this.myId

    this._EE.on('IncomingQueryRequest', this.onIncomingQueryRequest.bind(this))
    this._EE.on('IncomingFileRequest', this.onIncomingFileRequest.bind(this))
    this._EE.on('IncomingFileResponse', this.onIncomingFileResponse.bind(this))
  }

  onIncomingQueryRequest (request) {
    let self = this
    self.queryMetadata(request.getQuery())
      .then((queryResult) => {
        let response
        if (request.isRequestOriginThisNode()) {
          response = {id: 'local', result: queryResult}
        } else {
          response = {id: self.myId, result: queryResult}
        }
        request.setResult([response])
        self._EE.emit('IncomingResponse', request)
      })
      .catch((err) => console.error(err.stack))
  }
  onIncomingFileRequest (request) {
    let self = this
    let fileHash = request.getFile()
    if (request.isRequestOriginThisNode()) {
      stream(<commit_msg>Add number of hops to result<commit_after>'use strict'

const stream = require('pull-stream')
const pullDecode = require('pull-utf8-decoder')
const PullBlobStore = require('idb-pull-blob-store')
const PeerId = require('peer-id')

const deferred = require('deferred')

module.exports = class DatabaseManager {
  constructor (fileMetadata, EE) {
    this.metadata = fileMetadata
    this.config = new PullBlobStore('config')
    this._EE = EE
    this.myId

    this._EE.on('IncomingQueryRequest', this.onIncomingQueryRequest.bind(this))
    this._EE.on('IncomingFileRequest', this.onIncomingFileRequest.bind(this))
    this._EE.on('IncomingFileResponse', this.onIncomingFileResponse.bind(this))
  }

  onIncomingQueryRequest (request) {
    let self = this
    self.queryMetadata(request.getQuery())
      .then((queryResult) => {
        let response
        if (request.isRequestOriginThisNode()) {
          response = {id: 'local', result: queryResult}
        } else {
          response = {id: self.myId, hops:(request.getRoute().length - 1), result: queryResult}
        }
        request.setResult([response])
        self._EE.emit('IncomingResponse', request)
      })
      .catch((err) => console.error(err.stack))
  }
  onIncomingFileRequest (request) {
    let self = this
    let fileHash = request.getFile()
    if (request.isRequestOriginThisNode()) {
      stream(
{'fraction_non_alphanumeric': 0.12966383450314, 'fraction_numerical': 0.0011082379017362395, 'mean_word_length': 3.4983388704318936, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3357642', 'n_tokens_mistral': 819, 'n_tokens_neox': 799, 'n_words': 156}