<commit_before>require 'mongo_mapper'

module MongoMapper
  class SessionStore < ActionController::Session::AbstractStore
    
    class Session
      include MongoMapper::Document

      key :session_id, String, :required => true
      key :data, String, :default => [Marshal.dump({})].pack("m*")
      timestamps!
      
      #ensure each session_id is unique
      MongoMapper.ensure_index(Session, :session_id, :unique => true)
      MongoMapper.ensure_indexes!
    end
    
    # The class used for session storage.
    cattr_accessor :session_class
    self.session_class = Session

    SESSION_RECORD_KEY = 'rack.session.record'.freeze

    private
      def get_session(env, sid)
        sid ||= generate_sid
        session = find_session(sid)
        env[SESSION_RECORD_KEY] = session
        [sid, unpack(session.data)]
      end
    
      def set_session(env, sid, session_data)
        record = env[SESSION_RECORD_KEY] ||= find_session(sid)
        record.data = pack(session_data)
        #per rack spec: Should return true or false dependant on whether or not the session was saved or not.
        record.save ? true : false
      end
    
      def find_session(id)
        @@session_class.first(:session_id => id) ||
          @@session_class.new(:session_id => id)
      end
    
      def pack(data)
        [Marshal.dump(data)].pack("m*")
      end

      def unpack(packed)
        return nil unless packed
        Marshal.load(packed.unpack("m*").first)
      end
    
  end
end<commit_msg>now only one session id - Mongo::ObjectID<commit_after>require 'mongo_mapper'

module MongoMapper
  class SessionStore < ActionController::Session::AbstractStore
    
    class Session
      include MongoMapper::Document
      key :data, String, :default => [Marshal.dump({})].pack("m*")
      timestamps!
      
      MongoMapper.ensure_indexes!
    end
    
    # The class used for session storage.
    cattr_accessor :session_class
    self.session_class = Session

    SESSION_RECORD_KEY = 'rack.session.record'.freeze

    private
      def generate_sid
        Mongo::ObjectID.new
      end
      
      def get_session(env, sid)
        sid ||= generate_sid
        session = find_session(sid)
        env[SESSION_RECORD_KEY] = session
        [sid, unpack(session.data)]
      end
    
      def set_session(env, sid, session_data)
        record = env[SESSION_RECORD_KEY] ||= find_session(sid)
        record.data = pack(session_data)
        #per rack spec: Should return true or false dependant on whether or not the session was saved or not.
        record.save ? true : false
      end
    
      def find_session(id)
        if id.class == String
          id=Mongo::ObjectID.from_string(id)
        end
        @@session_class.find(id) ||
          @@session_class.new(:id=>id)
      end
    
      def pack(data)
        [Marshal.dump(data)].pack("m*")
      end

      def unpack(packed)
        return nil unless packed
        Marshal.load(packed.unpack("m*").first)
      end
      
  end
end
{'fraction_non_alphanumeric': 0.09514370664023786, 'fraction_numerical': 0.0, 'mean_word_length': 2.3794642857142856, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, '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': '2387731', 'n_tokens_mistral': 919, 'n_tokens_neox': 892, 'n_words': 219}