Question: My Rails 5 App only permits an admin or support user to create a user, when the user is created a password is generated and emailed to the user, on the first user login the app forces them to change the password. 

I have a password\_updated field in my schema that I want to be filled to true when the password is updated, however I am hitting a wall here, not sure if its coder eye and I just cant see what where im going wrong. 

my application controller: 

```
  # Force User To Change Password On First Login
  def after_sign_in_path_for(resource)
    if current_user.password_updated == "false"
      edit_passwords_path
    else
      authenticated_root_path
    end
  end

```

I have it set up so that if the user tries to skip or jump past the password change they are redirected to the password change. 

my passwords controller: 

```
class PasswordsController < ApplicationController
  def edit
    @user = current_user
  end

  def update
    if current_user.update_with_password(user_params)
      current_user.password_updated = "true"
      flash[:notice] = 'Your Password Has Been Sucessfully Updated.'
      redirect_to authenticated_root_path
    else
      flash[:error] = 'Oh No! Something Went Wrong, Please Try Again.'
      render :edit
    end
  end

  private
    def user_params
      params.require(:user).permit(:password_updated, :current_password, :password, :password_confirmation)
    end

end

```

Originally I had the application controller looking at the sign in count, however if the user closed out and waited long enough they could log back in and not have to change the password. I felt this was more of a risk. 

Any assistance here would be greatly appreciated.

Answer: 
Use memory-mapped files. Just think your file is already read into the memory in its entirety and do that very binary search in memory that you wanted. Let Windows care about reading the portions of file when you do your in-memory search.

* <https://en.wikipedia.org/wiki/Memory-mapped_file>
* <https://msdn.microsoft.com/en-us/library/ms810613.aspx>
* <https://stackoverflow.com/a/9609448/976391>
* <https://stackoverflow.com/a/726527/976391>
* <http://msdn.microsoft.com/en-us/library/aa366761%28VS.85.aspx>
* <https://msdn.microsoft.com/en-us/library/windows/desktop/aa366537.aspx>

You may take any of those sources for start, just do not forget to update them for Win64

<http://torry.net/quicksearchd.php?String=memory+mapped+files&Title=No>
Idea based on Aleksey Kharlanov answer. I accepted his answer.  

I only copy his idea here because he did not elaborate it (no pseudo-code or deeper analysis of the algorithm). I want to confirm it works before implementing it.

We sort both files (once).  

We load Big file in memory (once).  

We read Small file line by line from disk (once). 

Code:  

In the code below, sKey is the current key in Small file. bKey is the current key in the Big file:

```
LastPos:= 0
for sKey in SmallFile do 
 for CurPos:= LastPos to BigFile.Count do 
  if sKey = bKey 
  then 
    begin 
     SearchNext  // search (down) next entries for possible duplicate keys
     LastPos:= CurPos
    end
  else 
    if sKey < bKey 
    then break

```

It works because I know the last position (in Big file) of the last key. The next key can only be somewhere UNDER the last position; ON AVERAGE it should be in the next 440 entries. However, I don't even have to always read 440 entries below LastPos because if my sKey does not exist in the big file, it will be smaller than the bKey so I quickly break the inner loop and move on.

Thoughts?