<commit_before>require 'adapter'
require 'forwardable'
require 'cassanity'

module Adapter
  module Cassanity
    extend Forwardable

    # Public
    def read(key, options = nil)
      operation_options = {where: where(key)}
      adapter_options = with_default_consistency(@options[:read])
      arguments = update_arguments(operation_options, adapter_options, options)

      rows = @client.select(arguments)
      rows.empty? ? nil : rows.first
    end

    # Public
    def write(key, attributes, options = nil)
      operation, operation_options = prepare_operation(options, :update, set: attributes, where: where(key))
      adapter_options = with_default_consistency(@options[:write])
      arguments = update_arguments(operation_options, adapter_options, options)

      @client.send(operation, arguments)
    end

    # Public
    def delete(key, options = nil)
      operation_options = {where: where(key)}
      adapter_options = with_default_consistency(@options[:delete])
      arguments = update_arguments(operation_options, adapter_options, options)

      @client.delete(arguments)
    end

    # Public
    def clear(options = nil)
      @client.truncate
    end

    # Private
    def where(key)
      primary_key = @options.fetch(:primary_key)
      {primary_key => key}
    end

    # Private.
    def prepare_operation(options, operation, operation_options)
      if options && (modifications = options.delete(:modifications)) && modifications.any?
        operation_options = {modifications: [ [operation, operation_options], *modifications ]}
        operation = :batch
      end
      [ operation, operation_options ]
    end

    # Private
    def update_arguments(operation_options, adapter_options, method_options)
      keys = operation_options.keys

      if !adapter_options.nil? && !adapter_options.empty?
        filtered_options = adapter_options.reject { |key| keys.include?(key) }
        operation_options.update(filtered_options)
      end

      if !method_options.nil? && !method_options.empty?
        filtered_options = method_options.reject { |key| keys.include?(key) }<commit_msg>Don't modify the hash.<commit_after>require 'adapter'
require 'forwardable'
require 'cassanity'

module Adapter
  module Cassanity
    extend Forwardable

    # Public
    def read(key, options = nil)
      operation_options = {where: where(key)}
      adapter_options = with_default_consistency(@options[:read])
      arguments = update_arguments(operation_options, adapter_options, options)

      rows = @client.select(arguments)
      rows.empty? ? nil : rows.first
    end

    # Public
    def write(key, attributes, options = nil)
      operation, operation_options = prepare_operation(options, :update, set: attributes, where: where(key))
      adapter_options = with_default_consistency(@options[:write])
      arguments = update_arguments(operation_options, adapter_options, options)

      @client.send(operation, arguments)
    end

    # Public
    def delete(key, options = nil)
      operation_options = {where: where(key)}
      adapter_options = with_default_consistency(@options[:delete])
      arguments = update_arguments(operation_options, adapter_options, options)

      @client.delete(arguments)
    end

    # Public
    def clear(options = nil)
      @client.truncate
    end

    # Private
    def where(key)
      primary_key = @options.fetch(:primary_key)
      {primary_key => key}
    end

    # Private.
    def prepare_operation(options, operation, operation_options)
      if options && (modifications = options[:modifications]) && modifications.any?
        operation_options = {modifications: [ [operation, operation_options], *modifications ]}
        operation = :batch
      end
      [ operation, operation_options ]
    end

    # Private
    def update_arguments(operation_options, adapter_options, method_options)
      keys = operation_options.keys

      if !adapter_options.nil? && !adapter_options.empty?
        filtered_options = adapter_options.reject { |key| keys.include?(key) }
        operation_options.update(filtered_options)
      end

      if !method_options.nil? && !method_options.empty?
        filtered_options = method_options.reject { |key| keys.include?(key) }
{'fraction_non_alphanumeric': 0.09623529411764706, 'fraction_numerical': 0.0, 'mean_word_length': 3.522340425531915, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2193169', 'n_tokens_mistral': 1184, 'n_tokens_neox': 1146, 'n_words': 261}