<commit_before>// DropboxDataStoreAdapter
// Usage example:
//   App.ApplicationAdapter = DropboxDataStoreAdapter("your dropbox app key here", App);
function DropboxDataStoreAdapter(key, App) {
  // Dropbox Client
  var client = new Dropbox.Client({key: key});
  // Try to finish OAuth authorization.
  client.authenticate({interactive: true}, function(error) {
    if (error) {
      alert('Authentication error: ' + error);
    }
  });
  // Promise for Dropbox datastore
  var datastore = new Ember.RSVP.Promise(function(resolve, reject) {
    if (client.isAuthenticated()) {
      var datastoreManager = client.getDatastoreManager();
      datastoreManager.openDefaultDatastore(function(error, datastore) {
        if (error) {
          alert('Error opening default datastore: ' + error);
          reject(error);
        } else {
          observeRmoteChanges(datastore);
          resolve(datastore);
        }
      });
    }
  });
  // Observe other end of change in the Dropbox DataStore
  function observeRmoteChanges(datastore) {
    datastore.recordsChanged.addListener(function(event) {
      if (!event.isLocal()) {
        var dbRecords = event.affectedRecordsByTable();
        for (var dbTableName in dbRecords) {
          dbRecords[dbTableName].forEach(function(dbRecord) {
            var modelName = Ember.Inflector.inflector.singularize(dbTableName);
            // http://stackoverflow.com/a/19406357/338986
            var store = App.__container__.lookup('store:main');
            store.find(modelName, dbRecord.getId()).then(function(record) {
              if (record) {
                if (dbRecord.isDeleted()) {
                  // delete
                  record.deleteRecord();
                } else {
                  // update
                  record.setProperties(dbRecord.getFields());
                }
              } else {
                // insert
                // NOTE: find() just get the job done. It seems nothing to do here.
              }
            });
          });
        }
      }
    });
  }
  // convert a dropbox datastore ecord to a ember record
  function emberRecord(model, dbRecord) {
    var attrNames = Ember.get(model, 'attributes').keys.toArray();
    var dbFields = dbRecord.getFields();
    // Not a attribute, so keep it in noAttrs
    var noAttrs = {};
    for (var dbFieldName in dbFields) {
      if (!attrNames.contains(dbFieldName)) {
        noAttrs[dbFieldName] = dbFields[dbFieldName];
      }
    }
    return $.extend(dbFields, {"id": dbRecord.getId(), "noAttrs": noAttrs});
  }
  // Dropbox DataStore API Ember.js Data Adapter
  var adapter = DS.Adapter.extend({
    find: function(store, type, id) {
      return datastore.then(function(datastore) {
        var dbTableName = Ember.Inflector.inflector.pluralize(type.typeKey);
        var dbTable = datastore.getTable(dbTableName);
        var dbRecord = dbTable.get(id);
        var model = App.get(type.typeKey.capitalize());
        value = emberRecord(model, dbRecord);
        return Ember.RSVP.resolve(value);
      }).fail(function(error) {
         console.error(error);
      });
    },
    findAll: function(store, type, since) {
      return datastore.then(function(datastore) {
        var dbTableName = Ember.Inflector.inflector.pluralize(type.typeKey);
        var dbTable = datastore.getTable(dbTableName);
        var model = App.get(type.typeKey.capitalize());
        var values = $.map(dbTable.query(), function(dbRecord) {
          return emberRecord(model, dbRecord);
        });
        return Ember.RSVP.resolve(values);
      }).fail(function(error) {
         console.error(error);
      });
    },
    findQuery: function(store, type, query, recordArray) {
      return datastore.then(function(datastore) {
        var dbTableName = Ember.Inflector.inflector.pluralize(type.typeKey);
        var dbTable = datastore.getTable(dbTableName);
        var model = App.get(type.typeKey.capitalize());
        var dbQuery = typeof(model.dbQuery) == "function" ? model.dbQuery(query) : query;
        var values = $.map(dbTable.query(dbQuery), function(dbRecord) {
          return emberRecord(model, dbRecord);
        });
        return Ember.RSVP.resolve(values);
      }).fail(function(error) {
         console.error(error);
      });
    },
    createRecord: function(store, type, record) {
      return datastore.then(function(datastore) {
        var dbTableName = Ember.Inflector.inflector.pluralize(type.typeKey);
        var dbTable = datastore.getTable(dbTableName);
        var value = record.toJSON();
        var dbFields = $.extend(value, value['noAttrs']);
        delete dbFields['noAttrs'];
        var dbRecord = dbTable.insert(dbFields);
        value['id'] = dbRecord.getId();
        return Ember.RSVP.resolve(value);
      }).fail(function(error) {
         console.error(error);
      });
    },
    updateRecord: function(store, type, record) {
      return datastore.then(function(datastore) {
        var dbTableName = Ember.Inflector.inflector.pluralize(type.typeKey);
        var dbTable = datastore.getTable(dbTableName);
        var dbRecord = dbTable.get(record.id);
        var emberFields = record.toJSON();
        dbFields = $.extend(emberFields, emberFields['noAttrs']);
        delete dbFields['noAttrs'];
        dbRecord.update(dbFields);<commit_msg>Prevent echo back update of remote changes.<commit_after>// DropboxDataStoreAdapter
// Usage example:
//   App.ApplicationAdapter = DropboxDataStoreAdapter("your dropbox app key here", App);
function DropboxDataStoreAdapter(key, App) {
  // Dropbox Client
  var client = new Dropbox.Client({key: key});
  // Try to finish OAuth authorization.
  client.authenticate({interactive: true}, function(error) {
    if (error) {
      alert('Authentication error: ' + error);
    }
  });
  // Promise for Dropbox datastore
  var datastore = new Ember.RSVP.Promise(function(resolve, reject) {
    if (client.isAuthenticated()) {
      var datastoreManager = client.getDatastoreManager();
      datastoreManager.openDefaultDatastore(function(error, datastore) {
        if (error) {
          alert('Error opening default datastore: ' + error);
          reject(error);
        } else {
          observeRmoteChanges(datastore);
          resolve(datastore);
        }
      });
    }
  });
  // Observe other end of change in the Dropbox DataStore
  function observeRmoteChanges(datastore) {
    datastore.recordsChanged.addListener(function(event) {
      if (!event.isLocal()) {
        var dbRecords = event.affectedRecordsByTable();
        for (var dbTableName in dbRecords) {
          dbRecords[dbTableName].forEach(function(dbRecord) {
            var modelName = Ember.Inflector.inflector.singularize(dbTableName);
            // http://stackoverflow.com/a/19406357/338986
            var store = App.__container__.lookup('store:main');
            store.find(modelName, dbRecord.getId()).then(function(record) {
              if (record) {
                if (dbRecord.isDeleted()) {
                  // delete
                  record.deleteRecord();
                } else {
                  // update
                  record.setProperties(dbRecord.getFields());
                }
              } else {
                // insert
                // NOTE: find() just get the job done. It seems nothing to do here.
              }
            });
          });
        }
      }
    });
  }
  // Convert a dropbox datastore record to an ember record
  function emberRecord(model, dbRecord) {
    var attrNames = Ember.get(model, 'attributes').keys.toArray();
    var dbFields = dbRecord.getFields();
    // Not a attribute, so keep it in noAttrs
    var noAttrs = {};
    for (var dbFieldName in dbFields) {
      if (!attrNames.contains(dbFieldName)) {
        noAttrs[dbFieldName] = dbFields[dbFieldName];
      }
    }
    return $.extend(dbFields, {"id": dbRecord.getId(), "noAttrs": noAttrs});
  }
  // Return true if any localFields are changed against remoteFields
  function fieldsChanged(localFields, remoteFields) {
    for (var k in localFields) {
      if (localFields[k] != remoteFields[k]) {
        return true;
      }
    }
    return false;
  }
  // Dropbox DataStore API Ember.js Data Adapter
  var adapter = DS.Adapter.extend({
    find: function(store, type, id) {
      return datastore.then(function(datastore) {
        var dbTableName = Ember.Inflector.inflector.pluralize(type.typeKey);
        var dbTable = datastore.getTable(dbTableName);
        var dbRecord = dbTable.get(id);
        var model = App.get(type.typeKey.capitalize());
        value = emberRecord(model, dbRecord);
        return Ember.RSVP.resolve(value);
      }).fail(function(error) {
         console.error(error);
      });
    },
    findAll: function(store, type, since) {
      return datastore.then(function(datastore) {
        var dbTableName = Ember.Inflector.inflector.pluralize(type.typeKey);
        var dbTable = datastore.getTable(dbTableName);
        var model = App.get(type.typeKey.capitalize());
        var values = $.map(dbTable.query(), function(dbRecord) {
          return emberRecord(model, dbRecord);
        });
        return Ember.RSVP.resolve(values);
      }).fail(function(error) {
         console.error(error);
      });
    },
    findQuery: function(store, type, query, recordArray) {
      return datastore.then(function(datastore) {
        var dbTableName = Ember.Inflector.inflector.pluralize(type.typeKey);
        var dbTable = datastore.getTable(dbTableName);
        var model = App.get(type.typeKey.capitalize());
        var dbQuery = typeof(model.dbQuery) == "function" ? model.dbQuery(query) : query;
        var values = $.map(dbTable.query(dbQuery), function(dbRecord) {
          return emberRecord(model, dbRecord);
        });
        return Ember.RSVP.resolve(values);
      }).fail(function(error) {
         console.error(error);
      });
    },
    createRecord: function(store, type, record) {
      return datastore.then(function(datastore) {
        var dbTableName = Ember.Inflector.inflector.pluralize(type.typeKey);
        var dbTable = datastore.getTable(dbTableName);
        var value = record.toJSON();
        var dbFields = $.extend(value, value['noAttrs']);
        delete dbFields['noAttrs'];
        var dbRecord = dbTable.insert(dbFields);
        value['id'] = dbRecord.getId();
        return Ember.RSVP.resolve(value);
      }).fail(function(error) {
         console.error(error);
      });
    },
    updateRecord: function(store, type, record) {
      return datastore.then(function(datastore) {
        var dbTableName = Ember.Inflector.inflector.pluralize(type.typeKey);
        var dbTable = datastore.getTable(dbTableName);
        var dbRecord = dbTable.get(record.id);
        var emberFields = record.toJSON();
        var dbFields = $.extend(emberFields, emberFields['noAttrs']);
        delete dbFields['noAttrs'];
        // NOTE: Dropbox.Datastore will propagate current values even if no local changes. To prevent echo back of remote chages check if changes are made.
        if (fieldsChanged(dbFields, dbRecord.getFields())) {
          dbRecord.update(dbFields);
        }
{'fraction_non_alphanumeric': 0.12264907745788395, 'fraction_numerical': 0.0024957661110615917, 'mean_word_length': 2.5172413793103448, 'pattern_counts': {'":': 4, '<': 3, '<?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': '6418978', 'n_tokens_mistral': 3311, 'n_tokens_neox': 3289, 'n_words': 672}