<commit_before>module RailsEventStore
  module Repositories
    class EventRepository

      def initialize
        @adapter = ::RailsEventStore::Models::Event
      end
      attr_reader :adapter

      def find(condition)
        build_event_entity adapter.where(condition).first
      end

      def create(data)
        build_event_entity adapter.create(data)
      rescue ActiveRecord::RecordNotUnique
        raise EventCannotBeSaved
      end

      def delete(condition)
        adapter.destroy_all condition
        nil
      end

      def get_all_events
        adapter.find(:all, order: 'stream').map &method(:build_event_entity)
      end

      def last_stream_event(stream_name)
        build_event_entity adapter.where(stream: stream_name).last
      end

      def load_all_events_forward(stream_name)
        adapter.where(stream: stream_name).order('id ASC').map &method(:build_event_entity)
      end

      def load_events_batch(stream_name, start_point, count)
        adapter.where('id >= ? AND stream = ?', start_point, stream_name).limit(count).map &method(:build_event_entity)
      end

      private

      def build_event_entity(record)
        ::RailsEventStore::EventEntity.new(
          id:         record.id,
          stream:     record.stream,
          event_type: record.event_type,
          event_id:   record.event_id,
          metadata:   record.metadata,
          data:       record.data,
          created_at: record.created_at
        )
      end

    end<commit_msg>Use supported Rails methods for finding all events<commit_after>module RailsEventStore
  module Repositories
    class EventRepository

      def initialize
        @adapter = ::RailsEventStore::Models::Event
      end
      attr_reader :adapter

      def find(condition)
        build_event_entity adapter.where(condition).first
      end

      def create(data)
        build_event_entity adapter.create(data)
      rescue ActiveRecord::RecordNotUnique
        raise EventCannotBeSaved
      end

      def delete(condition)
        adapter.destroy_all condition
        nil
      end

      def get_all_events
        adapter.order('id ASC').order('stream').all.map &method(:build_event_entity)
      end

      def last_stream_event(stream_name)
        build_event_entity adapter.where(stream: stream_name).last
      end

      def load_all_events_forward(stream_name)
        adapter.where(stream: stream_name).order('id ASC').map &method(:build_event_entity)
      end

      def load_events_batch(stream_name, start_point, count)
        adapter.where('id >= ? AND stream = ?', start_point, stream_name).limit(count).map &method(:build_event_entity)
      end

      private

      def build_event_entity(record)
        ::RailsEventStore::EventEntity.new(
          id:         record.id,
          stream:     record.stream,
          event_type: record.event_type,
          event_id:   record.event_id,
          metadata:   record.metadata,
          data:       record.data,
          created_at: record.created_at
        )
      end

    end
{'fraction_non_alphanumeric': 0.07712333224861699, 'fraction_numerical': 0.0, 'mean_word_length': 2.411764705882353, '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': '5397731', 'n_tokens_mistral': 868, 'n_tokens_neox': 841, 'n_words': 173}