<commit_before>module Swift
  class Scheme
    alias_method :scheme, :class

    def initialize options = {}
      options.each{|k, v| send(:"#{k}=", v)}
    end

    def tuple
      @tuple ||= scheme.attributes.new_tuple
    end

    def update options = {}
      options.each{|k, v| send(:"#{k}=", v)}
      Swift.db.update(scheme, self)
    end

    def destroy
      Swift.db.destroy(scheme, self)
    end

    class << self
      def inherited klass
        klass.store(store)                 if store
        klass.attributes.push(*attributes) if attributes
        Swift.schema.push(klass)           if klass.name
      end

      def load tuple
        im = [self, *tuple.values_at(*attributes.keys)]
        unless scheme = Swift.db.identity_map.get(im)
          scheme = allocate
          scheme.tuple.update(tuple)
          Swift.db.identity_map.set(im, scheme)
        end
        scheme
      end

      def attributes
        @attributes ||= Attributes.new
      end

      def attribute name, type, options = {}
        attributes.push(attribute = type.new(self, name, options))
        (class << self; self end).send(:define_method, name, lambda{ attribute })
      end

      def store name = nil
        name ? @store = name : @store
      end

      def migration &migration
        (class << self; self end).send(:define_method, :migrate!, lambda{ Swift.db.instance_eval(&migration) })
      end

      def migrate!
        Swift.db.migrate!(self)
      end

      def create options = {}
        Swift.db.create(self, options)
      end

      #--
      # TODO: Think long and hard about this.
      # Adapter should be the only place with SQL. Add an Adapter#all method.
      # This will pay off when we add mongo, sphinx etc.
      def all where = '', *binds, &block
        where = "where #{exchange_names(where)}" unless where.empty?
        Swift.db.prepare(self, "select * from #{store} #{where}").execute(*binds, &block)<commit_msg>Tiny performance updates.<commit_after>module Swift
  class Scheme
    attr_accessor :tuple
    alias_method :scheme, :class

    def initialize options = {}
      @tuple = scheme.attributes.new_tuple
      options.each{|k, v| send(:"#{k}=", v)}
    end

    def update options = {}
      options.each{|k, v| send(:"#{k}=", v)}
      Swift.db.update(scheme, self)
    end

    def destroy
      Swift.db.destroy(scheme, self)
    end

    class << self
      attr_accessor :attributes

      def inherited klass
        klass.attributes = Attributes.new
        klass.attributes.push(*attributes) if attributes
        Swift.schema.push(klass)           if klass.name
      end

      def load tuple
        im = [self, *tuple.values_at(*attributes.keys)]
        unless scheme = Swift.db.identity_map.get(im)
          scheme       = allocate
          scheme.tuple = tuple
          Swift.db.identity_map.set(im, scheme)
        end
        scheme
      end

      def attribute name, type, options = {}
        attributes.push(attribute = type.new(self, name, options))
        (class << self; self end).send(:define_method, name, lambda{ attribute })
      end

      def store name = nil
        name ? @store = name : @store
      end

      def migration &migration
        (class << self; self end).send(:define_method, :migrate!, lambda{ Swift.db.instance_eval(&migration) })
      end

      def migrate!
        Swift.db.migrate!(self)
      end

      def create options = {}
        Swift.db.create(self, options)
      end

      #--
      # TODO: Think long and hard about this.
      # Adapter should be the only place with SQL. Add an Adapter#all method.
      # This will pay off when we add mongo, sphinx etc.
      def all where = '', *binds, &block
        where = "where #{exchange_names(where)}" unless where.empty?
        Swift.db.prepare(self, "select * from #{store} #{where}").execute(*binds, &block)
{'fraction_non_alphanumeric': 0.11408016443987667, 'fraction_numerical': 0.0, 'mean_word_length': 2.1420500403551253, 'pattern_counts': {'":': 0, '<': 15, '<?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': '614911', 'n_tokens_mistral': 1163, 'n_tokens_neox': 1124, 'n_words': 325}