<commit_before>

define(function(require) {

  // this actually defines remoteStorage globally.
  require('remotestorage');

  var moduleName = 'documents';

  remoteStorage.defineModule(moduleName, function(myBaseClient) {
    var errorHandlers=[];
    function fire(eventType, eventObj) {
      if(eventType == 'error') {
        for(var i=0; i<errorHandlers.length; i++) {
          errorHandlers[i](eventObj);
        }
      }
    }
    function getUuid() {
      var uuid = '',
      i,
      random;

      for ( i = 0; i < 32; i++ ) {
        random = Math.random() * 16 | 0;
        if ( i === 8 || i === 12 || i === 16 || i === 20 ) {
          uuid += '-';
        }
        uuid += ( i === 12 ? 4 : (i === 16 ? (random & 3 | 8) : random) ).toString( 16 );
      }
      return uuid;
    }
    function getPrivateList(listName) {
      myBaseClient.sync(listName+'/');
      function getIds() {
        return myBaseClient.getListing(listName+'/');
      }
      function getContent(id) {
        var obj = myBaseClient.getObject(listName+'/'+id);
        if(obj) {
          return obj.content;
        } else {
          return '';
        }
      }
      function getTitle(id) {
        return getContent(id).slice(0, 50);
      }
      function setContent(id, content) {
        if(content == '') {
          myBaseClient.remove(listName+'/'+id);
        } else {
          myBaseClient.storeObject('text', listName+'/'+id, {
            content: content
          });
        }
      }
      function add(content) {
        var id = getUuid();
        myBaseClient.storeObject('text', listName+'/'+id, {
          content: content
        });
        return id;
      }
      function on(eventType, cb) {
        myBaseClient.on(eventType, cb);
        if(eventType == 'error') {
          errorHandlers.push(cb);
        }
      }
      return {
        getIds        : getIds,
        getContent    : getContent,
        getTitle      : getTitle,
        setContent   : setContent,
        add           : add,
        on            : on
      };
    }

    function getBackboneStore(listName) {

      myBaseClient.sync(listName+'/');

      function absPath(path) {
        return listName + '/' + path;
      }

      function storeObject(model) {
        if(! model.id) {
          model.set(model.idAttribute, getUuid());
        }

        myBaseClient.storeObject('text', absPath(model.id), model);

        return model;
      }

      return {

        create: function() {
          console.log('CREATE', arguments);
          return storeObject.apply(this, arguments);
        },

        update: function() {
          console.log('UPDATE', arguments);
          return storeObject.apply(this, arguments);
        },

        find: function(model) {
          console.log('FIND', arguments);
          return myBaseClient.getObject(absPath(model.id));
        },

        findAll: function() {
          console.log('FIND ALL', arguments);
          var ids = myBaseClient.getListing(absPath(''));
          models = [];
          for(var i=0;i<ids;i++) {
            models.push(myBaseClient.getObject(absPath(ids[i])));
          }<commit_msg>documents module: store attributes instead of the whole model<commit_after>

define(function(require) {

  // this actually defines remoteStorage globally.
  require('remotestorage');

  var moduleName = 'documents';

  remoteStorage.defineModule(moduleName, function(myBaseClient) {
    var errorHandlers=[];
    function fire(eventType, eventObj) {
      if(eventType == 'error') {
        for(var i=0; i<errorHandlers.length; i++) {
          errorHandlers[i](eventObj);
        }
      }
    }
    function getUuid() {
      var uuid = '',
      i,
      random;

      for ( i = 0; i < 32; i++ ) {
        random = Math.random() * 16 | 0;
        if ( i === 8 || i === 12 || i === 16 || i === 20 ) {
          uuid += '-';
        }
        uuid += ( i === 12 ? 4 : (i === 16 ? (random & 3 | 8) : random) ).toString( 16 );
      }
      return uuid;
    }
    function getPrivateList(listName) {
      myBaseClient.sync(listName+'/');
      function getIds() {
        return myBaseClient.getListing(listName+'/');
      }
      function getContent(id) {
        var obj = myBaseClient.getObject(listName+'/'+id);
        if(obj) {
          return obj.content;
        } else {
          return '';
        }
      }
      function getTitle(id) {
        return getContent(id).slice(0, 50);
      }
      function setContent(id, content) {
        if(content == '') {
          myBaseClient.remove(listName+'/'+id);
        } else {
          myBaseClient.storeObject('text', listName+'/'+id, {
            content: content
          });
        }
      }
      function add(content) {
        var id = getUuid();
        myBaseClient.storeObject('text', listName+'/'+id, {
          content: content
        });
        return id;
      }
      function on(eventType, cb) {
        myBaseClient.on(eventType, cb);
        if(eventType == 'error') {
          errorHandlers.push(cb);
        }
      }
      return {
        getIds        : getIds,
        getContent    : getContent,
        getTitle      : getTitle,
        setContent   : setContent,
        add           : add,
        on            : on
      };
    }

    function getBackboneStore(listName) {

      myBaseClient.sync(listName+'/');

      function absPath(path) {
        return listName + '/' + path;
      }

      function storeObject(model) {
        if(! model.id) {
          model.set(model.idAttribute, getUuid());
        }

        myBaseClient.storeObject('text', absPath(model.id), model.attributes);

        return model;
      }

      return {

        create: function() {
          console.log('CREATE', arguments);
          return storeObject.apply(this, arguments);
        },

        update: function() {
          console.log('UPDATE', arguments);
          return storeObject.apply(this, arguments);
        },

        find: function(model) {
          console.log('FIND', arguments);
          return myBaseClient.getObject(absPath(model.id));
        },

        findAll: function() {
          console.log('FIND ALL', arguments);
          var ids = myBaseClient.getListing(absPath(''));
          models = [];
          for(var i=0;i<ids;i++) {
            models.push(myBaseClient.getObject(absPath(ids[i])));
          }
{'fraction_non_alphanumeric': 0.14136370728662817, 'fraction_numerical': 0.008425651427679825, 'mean_word_length': 1.7558039552880482, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, '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': '5195729', 'n_tokens_mistral': 1947, 'n_tokens_neox': 1866, 'n_words': 370}