<commit_before>/**
 * hive.js
 * Copyright (C) 2013-2016 <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the Mozilla Public License version 2
 * as published by the Mozilla Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the Mozilla Public License
 * along with this program.  If not, see <https://www.mozilla.org/en-US/MPL/2.0/>.
 */
"use strict";
var gulf = require('gulf')
  , WaterlineAdapter = require('./lib/waterline-adapter')

module.exports = setup
module.exports.consumes = ['orm', 'ot', 'queue', 'broadcast']
module.exports.provides = ['sync']

function setup(plugin, imports, register) {
  var orm = imports.orm
    , ot = imports.ot
    , queue = imports.queue
    , broadcast = imports.broadcast

  var sync = {
    documents: {}
  , createDocument: function*(type) {
      var ottype = ot.getOTType(type)
      if(!ottype) throw new Error('Specified document type is not available')

      var content = ottype.create()
        , adapter = new WaterlineAdapter(orm.collections)
      var doc = yield function(cb) {
        gulf.Document.create(adapter, ottype, content, cb)
      }
      wireDocument(doc.id, doc)
      this.documents[doc.id] = doc

      yield orm.collections.document.update({id: doc.id}, {type: type})

      return yield orm.collections.document.findOne({id: doc.id})
    }
  , getDocument: function*(docId) {
      docId = parseInt(docId)
      if(this.documents[docId]) return this.documents[docId]
      var doc = yield orm.collections.document.findOne({id: docId})
        , ottype = ot.getOTType(doc.type)
      var document
      yield function(cb) {
	document = gulf.Document.load(
	  new WaterlineAdapter(orm.collections)
	, ottype
	, docId
	, cb)
      }
      this.documents[docId] = document
      wireDocument(docId, document)
      return document
    }
  }

  function wireDocument(docId, doc) {
    // Use worker queue
    doc.queue = queue.document(docId)

    var b = broadcast.sync(docId)
    // send edits to other workers
    doc.on('edit', function(edit) {
      b.write(edit.pack())
    })
    // listen to, apply and distribute edits of other workers
    b.on('readable', function() {
      var chunk
      while(chunk = broadcast.read()) {
        var edit = gulf.Edit.unpack(chunk.toString(), doc.ottype)
        try {
          doc.applyEdit(edit)
        }catch(e) {
          console.warn('Applying foreign edit failed', edit, e.stack)
        }
        doc.distributeEdit(edit)
      }
    })
  }

  register(null, {sync: sync})
}
<commit_msg>[sync] getDocument: Add check for existence<commit_after>/**
 * hive.js
 * Copyright (C) 2013-2016 <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the Mozilla Public License version 2
 * as published by the Mozilla Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the Mozilla Public License
 * along with this program.  If not, see <https://www.mozilla.org/en-US/MPL/2.0/>.
 */
"use strict";
var gulf = require('gulf')
  , WaterlineAdapter = require('./lib/waterline-adapter')

module.exports = setup
module.exports.consumes = ['orm', 'ot', 'queue', 'broadcast']
module.exports.provides = ['sync']

function setup(plugin, imports, register) {
  var orm = imports.orm
    , ot = imports.ot
    , queue = imports.queue
    , broadcast = imports.broadcast

  var sync = {
    documents: {}
  , createDocument: function*(type) {
      var ottype = ot.getOTType(type)
      if(!ottype) throw new Error('Specified document type is not available')

      var content = ottype.create()
        , adapter = new WaterlineAdapter(orm.collections)
      var doc = yield function(cb) {
        gulf.Document.create(adapter, ottype, content, cb)
      }
      wireDocument(doc.id, doc)
      this.documents[doc.id] = doc

      yield orm.collections.document.update({id: doc.id}, {type: type})

      return yield orm.collections.document.findOne({id: doc.id})
    }
  , getDocument: function*(originalDocId) {
      var docId = parseInt(originalDocId)
      if(this.documents[docId]) return this.documents[docId]
      var doc = yield orm.collections.document.findOne({id: docId})
      if (!doc) throw new Error('Not found: There is no document with id: "'+originalDocId+'"')
      var ottype = ot.getOTType(doc.type)
      var document
      yield function(cb) {
        document = gulf.Document.load(
          new WaterlineAdapter(orm.collections)
        , ottype
        , docId
        , cb)
      }
      this.documents[docId] = document
      wireDocument(docId, document)
      return document
    }
  }

  function wireDocument(docId, doc) {
    // Use worker queue
    doc.queue = queue.document(docId)

    var b = broadcast.sync(docId)
    // send edits to other workers
    doc.on('edit', function(edit) {
      b.write(edit.pack())
    })
    // listen to, apply and distribute edits of other workers
    b.on('readable', function() {
      var chunk
      while(chunk = broadcast.read()) {
        var edit = gulf.Edit.unpack(chunk.toString(), doc.ottype)
        try {
          doc.applyEdit(edit)
        }catch(e) {
          console.warn('Applying foreign edit failed', edit, e.stack)
        }
        doc.distributeEdit(edit)
      }
    })
  }

  register(null, {sync: sync})
}

{'fraction_non_alphanumeric': 0.1182611672086257, 'fraction_numerical': 0.0037651891151805577, 'mean_word_length': 2.8321311475409834, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '778792', 'n_tokens_mistral': 1792, 'n_tokens_neox': 1739, 'n_words': 514}