Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning empty result instead of nil. #4

Open
bbense opened this issue Oct 24, 2014 · 2 comments
Open

Returning empty result instead of nil. #4

bbense opened this issue Oct 24, 2014 · 2 comments

Comments

@bbense
Copy link
Contributor

bbense commented Oct 24, 2014

The problem seems to be that the new :matching option causes the code to return one empty array before it returns a nil at the end of the search. I did add the necessary e dir after checking out the
code from github. The problem appears whether you use the matching code or not.

iex -S mix
Erlang/OTP 17 [erts-6.2] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Interactive Elixir (1.0.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> {:ok, walker} = DirWalker.start_link("test/dir", matching: ~r(a|f))
{:ok, #PID<0.79.0>}
iex(2)> file = DirWalker.next(walker)
["test/dir/a.txt"]
iex(3)> file = DirWalker.next(walker)
["test/dir/c/d/f.txt"]
iex(4)> file = DirWalker.next(walker)
[]
iex(5)> file = DirWalker.next(walker)
nil
iex(6)> {:ok, walker} = DirWalker.start_link("test/dir")
{:ok, #PID<0.85.0>}
iex(7)> file = DirWalker.next(walker)
["test/dir/a.txt"]
iex(8)> file = DirWalker.next(walker)
["test/dir/b.txt"]
iex(9)> file = DirWalker.next(walker)
["test/dir/c/d/f.txt"]
iex(10)> file = DirWalker.next(walker)
[]
iex(11)> file = DirWalker.next(walker)
nil

@bbense
Copy link
Contributor Author

bbense commented Oct 24, 2014

If you use a count larger than the default of one it works just fine.

iex(7)> {:ok, walker} = DirWalker.start_link("test/dir")
{:ok, #PID<0.86.0>}
iex(8)> files = DirWalker.next(walker,2)
["test/dir/b.txt", "test/dir/a.txt"]
iex(9)> files = DirWalker.next(walker,2)
["test/dir/c/d/f.txt"]
iex(10)> files = DirWalker.next(walker,2)
nil

@bbense
Copy link
Contributor Author

bbense commented Oct 25, 2014

I found the problem, since the matching function can return an empty list w/o processing a list, the call handler needs to check for returning { [] , [] } and instead set the result to nil if returning two empty arrays.

iex -S mix
Erlang/OTP 17 [erts-6.2] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Compiled lib/dir_walker.ex
Generated dir_walker.app
Interactive Elixir (1.0.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> {:ok, walker} = DirWalker.start_link("test/dir")
{:ok, #PID<0.116.0>}
iex(2)> file = DirWalker.next(walker)

20:20:18.903 [debug] Handling test/dir/a.txt

20:20:18.906 [debug] Replying with ["test/dir/a.txt"] ["test/dir/b.txt", ["test/dir/badlink", "test/dir/c", "test/dir/goodlink"]]
["test/dir/a.txt"]
iex(3)> file = DirWalker.next(walker)

20:20:27.541 [debug] Handling test/dir/b.txt

20:20:27.541 [debug] Replying with ["test/dir/b.txt"] ["test/dir/badlink", ["test/dir/c", "test/dir/goodlink"]]
["test/dir/b.txt"]
iex(4)> file = DirWalker.next(walker)

20:20:33.013 [info] Dangling symlink found: test/dir/badlink

20:20:33.013 [debug] Handling test/dir/badlink

20:20:33.013 [debug] Replying with ["test/dir/badlink"] ["test/dir/c", ["test/dir/goodlink"]]
["test/dir/badlink"]
iex(5)> file = DirWalker.next(walker)

20:20:37.798 [debug] Handling test/dir/c/d/f.txt

20:20:37.798 [debug] Replying with ["test/dir/c/d/f.txt"] ["test/dir/goodlink"]
["test/dir/c/d/f.txt"]
iex(6)> file = DirWalker.next(walker)

20:20:42.461 [debug] Handling test/dir/goodlink

20:20:42.461 [debug] Replying with ["test/dir/goodlink"] []
["test/dir/goodlink"]
iex(7)> file = DirWalker.next(walker)
nil
iex(8)> {:ok, walker} = DirWalker.start_link("test/dir", matching: ~r(b) )
{:ok, #PID<0.124.0>}
iex(9)> file = DirWalker.next(walker)

20:21:57.695 [debug] Handling test/dir/a.txt

20:21:57.695 [debug] Moving on to rest [["test/dir/b.txt", "test/dir/badlink", "test/dir/c", "test/dir/goodlink"]]

20:21:57.695 [debug] Handling test/dir/b.txt

20:21:57.695 [debug] Replying with ["test/dir/b.txt"] ["test/dir/badlink", ["test/dir/c", "test/dir/goodlink"]]
["test/dir/b.txt"]
iex(10)> file = DirWalker.next(walker)

20:22:04.351 [info] Dangling symlink found: test/dir/badlink

20:22:04.351 [debug] Handling test/dir/badlink

20:22:04.351 [debug] Replying with ["test/dir/badlink"] ["test/dir/c", ["test/dir/goodlink"]]
["test/dir/badlink"]
iex(11)> file = DirWalker.next(walker)

20:22:07.887 [debug] Handling test/dir/c/d/f.txt

20:22:07.887 [debug] Moving on to rest [["test/dir/goodlink"]]

20:22:07.888 [debug] Handling test/dir/goodlink

20:22:07.888 [debug] Moving on to rest []

20:22:07.888 [debug] Replying with [] []
[]

The Logger debug statements are in my current version. The fix was to add this to the main handle_call routine.

def handle_call({:get_next, n}, _from, {path_list, mappers}) do
{result, new_path_list} = first_n(path_list, n, mappers, _result=[])
Logger.debug("Replying with #{inspect(result)} #{inspect(new_path_list)}")
if( {result, new_path_list} == { [] , [] }) do
result = nil
end
{ :reply, result, {new_path_list, mappers} }
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant