<commit_before>/**
 * @license
 * Copyright 2012 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
CLASS({
  name: 'SplitDAO',

  extendsModel: 'ProxyDAO',

  properties: [
    {
      name:  'model',
      label: 'Model',
      type:  'Model',
      hidden: true,
      required: true
    },
    {
      name: 'remote',
      type: 'DAO',
      mode: "read-only",
      hidden: true,
      required: true
    },
    {
      name: 'syncManager',
      factory: function() {
        return this.X.SyncManager.create({
          srcDAO: this.remote,
          dstDAO: this.delegate,
        });
      },
      hidden: true
    },
    {
      name: 'remoteFlags',
      factory: function() {
        return {};
      },
    }
  ],

  methods: {
    init: function() {
      this.SUPER();
    },

    put: function(obj, sink) {
      var self = this;
      this.remote.put(obj, {
        put: function(obj) {
          self.delegate.put(obj, sink);
          self.notify_('put', [obj]);
        },
        error: ( sink && sink.error ) ? sink.error.bind(sink) : function(){}
      });
    },

    remove: function(obj, sink) {
      var self = this;
      this.remove.remove(obj, {
        remove: function(obj) {
          self.delegate.remove(obj, sink);
          self.notify_('remove', [obj]);
        },
        error: ( sink && sink.error ) ? sink.error.bind(sink) : function(){}
      });
    },

    find: function(key, sink) {
      var delegate = this.delegate;
      var remove = this.remove;

      this.delegate.find(key, {
        put: function(obj) {
          sink && sink.put && sink.put(obj);
        },
        error: function() {
          remote.find(key, {
            put: function(obj) {
              sink && sink.put && sink.put(obj);
              delegate.put(obj);
              self.notify_('put', [obj]);
            },
            error: (sink && sink.error) ? sink.error.bind(sink) : function(){}
          });
        }
      });
    },

    select: function(sink, options) {
      var key = [
        'limit=' + options.limit,
        'skip=' + options.skip,
        'query=' + (options.query ? options.query.toSQL() : ''),
        'order=' + (options.order ? options.order.toSQL() : '')
      ];

      if ( CountExpr.isInstance(sink) ) {
        return this.remote.select(sink, options);
      }

      if ( ! this.remoteFlags[key] ) {
        this.remoteFlags[key] = true;

        this.remote.select({
          put: (function(obj) {
            this.delegate.find(obj.id, {
              put: (function(existing) {
                var modified = this.syncManager.modifiedProperty.name;
                if ( obj[modified].compareTo(existing[modified]) > 0 )
                  this.delegate.put(obj);
              }).bind(this),
              error: (function() {
                this.delegate.put(obj);
              }).bind(this)
            });
          }).bind(this),
          eof: (function() {
            delete this.remoteFlags[key];
          }).bind(this),
          error: (function() {
            delete this.remoteFlags[key];
          }).bind(this)
        }, options);
      }
      return this.delegate.select(sink, options);
    }
  }
});<commit_msg>Re-write SplitDAO as a write-though cache that uses PositionIndexes in MDAO for a cache, but writes through to the network.<commit_after>/**
 * @license
 * Copyright 2012 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
CLASS({
  name: 'SplitDAO',

  requires: [
    'MDAO'
  ],

  extendsModel: 'ProxyDAO',

  properties: [
    {
      name:  'model',
      label: 'Model',
      type:  'Model',
      hidden: true,
      required: true
    },
    {
      name: 'remote',
      type: 'DAO',
      mode: "read-only",
      hidden: true,
      required: true
    },
    {
      model_: 'FunctionProperty',
      name: 'placeholderFactory'
    },
    {
      model_: 'IntProperty',
      name: 'staleTimeout',
      defaultValue: 5000
    },
    {
      name: 'delegate',
      factory: function() {
        // TODO: Cleanup the index setup, it shouldn't be this hard.
        var dao = this.MDAO.create({
          model: this.model,
        });
        dao.index = AltIndex.create(
          AutoPositionIndex.create(
            this.placeholderFactory,
            dao,
            this.remote,
            this.staleTimeout),
          TreeIndex.create(this.model.getProperty(this.model.ids[0])));
        dao.root = [[]];
        return dao;
      }
    }
  ],

  methods: {
    init: function() {
      this.SUPER();
      var self = this;
      this.remote.listen({
        put: function(obj) {
          debugger;
          self.delegate.put(obj);
        },
        remove: function(obj) {
          debugger;
          self.delegate.remove(obj);
        }
      });
    },

    put: function(obj, sink) {
      this.remote.put(obj, sink);
    },

    remove: function(obj, sink) {
      this.remote.remove(obj, sink);
    },

    find: function(key, sink) {
      var remote = this.remote;
      var delegate = this.delegate
      this.SUPER(key, {
        put: function(obj) {
        },
        error: function() {
          remote.find(key, {
            put: function(obj) {
              sink && sink.put && sink.put(obj);
              delegate.put(obj);
            },
            error: (sink && sink.error) ? sink.error.bind(sink) : undefined
          })
        }
      });
    }
  }
});
{'fraction_non_alphanumeric': 0.1356297093649085, 'fraction_numerical': 0.0033830539750884206, 'mean_word_length': 1.8119325551232166, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, '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': '299476', 'n_tokens_mistral': 1930, 'n_tokens_neox': 1893, 'n_words': 484}