<commit_before>module Hypostasis::Document
  module Findable
    extend ActiveSupport::Concern

    module ClassMethods
      def find(id)
        document_keys = []
        namespace.transact do |tr|
          document_keys = tr.get_range_start_with(namespace.for_document(self, id)).to_a
        end
        raise Hypostasis::Errors::DocumentNotFound if document_keys.empty?
        attributes = {}
        id = Hypostasis::Tuple.unpack(document_keys.first.key.split('\\')[1]).to_a[1]
        document_keys.each do |key|
          attribute_tuple = key.key.split('\\')[2]
          next if attribute_tuple.nil?
          unpacked_key = Hypostasis::Tuple.unpack(attribute_tuple)
          raw_value = key.value
          attributes[unpacked_key.to_a[0].to_sym] = reconstitute_value(unpacked_key, raw_value)
        end
        document = self.new(attributes)
        document.set_id(id)
        document
      end

      def find_where(field_value_pairs)
        results = []
        namespace.transact do |tr|
          field_value_pairs.each do |field, value|
            results << tr.get_range_start_with(namespace.for_index(self, field, value)).to_a
          end
        end
        results.flatten!
        results.collect! {|result| Hypostasis::Tuple.unpack(result.key.split('\\').last).to_a.last }.compact!
        results.select! {|e| results.count(e) == field_value_pairs.size}
        results.uniq!
        results.collect! {|result| find(result) }
      end
    end
  end<commit_msg>Extracted logic for reconstituting document from keys into separate method in Hypostasis::Document::Findable.<commit_after>module Hypostasis::Document
  module Findable
    extend ActiveSupport::Concern

    module ClassMethods
      def find(id)
        document_keys = namespace.transact do |tr|
          tr.get_range_start_with(namespace.for_document(self, id)).to_a
        end
        raise Hypostasis::Errors::DocumentNotFound if document_keys.empty?
        reconstitute_document(document_keys)
      end

      def find_where(field_value_pairs)
        results = []
        namespace.transact do |tr|
          field_value_pairs.each do |field, value|
            results << tr.get_range_start_with(namespace.for_index(self, field, value)).to_a
          end
        end
        results.flatten!
        results.collect! {|result| Hypostasis::Tuple.unpack(result.key.split('\\').last).to_a.last }.compact!
        results.select! {|e| results.count(e) == field_value_pairs.size}
        results.uniq!
        results.collect! {|result| find(result) }
      end

      def reconstitute_document(keys)
        attributes = {}
        keys.each do |key|
          attribute_tuple = key.key.split('\\')[2]
          next if attribute_tuple.nil?
          unpacked_key = Hypostasis::Tuple.unpack(attribute_tuple)
          raw_value = key.value
          attributes[unpacked_key.to_a[0].to_sym] = reconstitute_value(unpacked_key, raw_value)
        end
        document = self.new(attributes)
        document.set_id(Hypostasis::Tuple.unpack(keys.first.key.split('\\')[1]).to_a[1])
        document
      end
    end
  end
{'fraction_non_alphanumeric': 0.10726865193723983, 'fraction_numerical': 0.002561639449247518, 'mean_word_length': 2.615740740740741, 'pattern_counts': {'":': 0, '<': 7, '<?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': '4022819', 'n_tokens_mistral': 968, 'n_tokens_neox': 942, 'n_words': 167}