Skip to content

Commit 12d8066

Browse files
committed
fixed bugs
1 parent d477d55 commit 12d8066

File tree

5 files changed

+26
-24
lines changed

5 files changed

+26
-24
lines changed

lib/datastructures/hash.ex

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,11 @@ defmodule Remixdb.Hash do
405405
end
406406

407407
def handle_call({:hstrlen, hash_name, key}, _from, table) do
408-
res = get_hash_key(table, hash_name, key) |>
409-
:erlang.byte_size
408+
val = get_hash_key(table, hash_name, key)
409+
res = case val do
410+
nil -> 0
411+
_ -> :erlang.byte_size(val)
412+
end
410413

411414
{:reply, res, table}
412415
end
@@ -432,19 +435,10 @@ defmodule Remixdb.Hash do
432435
end
433436

434437
def handle_call({:hgetall, hash_name}, _from, table) do
435-
# The match spec:
436-
# - Pattern: keys matching {hash_name, $"$1"} with value $"$2"
437-
# - No guards.
438-
# - Return a tuple {field, value} for each match.
439-
match_spec = [{{{hash_name, :"$1"}, :"$2"}, [], [ {:"$1", :"$2"} ]}]
440-
441-
# ETS.select returns a list of tuples, e.g. [{field1, value1}, {field2, value2}, ...]
442-
result = :ets.select(table, match_spec)
443-
444-
# If you want the results as an interleaved list [field1, value1, field2, value2, ...], you can flatten:
445-
interleaved = Enum.flat_map(result, fn {k, v} -> [k, v] end)
446-
447-
{:reply, interleaved, table}
438+
result =
439+
get_all_entries(table, hash_name)
440+
|> Enum.flat_map(fn({k, v}) -> [k, v] end)
441+
{:reply, result, table}
448442
end
449443

450444
def handle_call(:flushall, _from, table) do

lib/redis/connection.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ defmodule Remixdb.RedisConnection do
103103
end)
104104

105105
case res do
106-
true -> "OK"
107-
_ -> "ERR no such key"
106+
false -> {:error, "ERR no such key"}
107+
_ -> "OK"
108108
end
109109

110110
{:renamenx, [_old_name, _new_name]} ->

lib/redis/response_handler.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ defmodule Remixdb.Redis.ResponseHandler do
2727
socket
2828
end
2929

30+
def send_response(socket, val) when is_boolean(val) do
31+
case val do
32+
true -> send_response(socket, 1)
33+
_ -> send_response(socket, 0)
34+
end
35+
end
36+
3037
def send_response(socket, val) when is_list(val) do
3138
header = "*" <> (val |> Enum.count |> Integer.to_string) <> "\r\n"
3239
:gen_tcp.send socket, header

lib/tcp_server.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ defmodule Remixdb.TcpServer do
1313
end
1414

1515
def handle_info(:long_init, %{port: port} = state) do
16-
{:ok, socket} = :gen_tcp.listen(port, [:binary, packet: :line, active: false, reuseaddr: true, backlog: 1_000])
16+
{:ok, socket} = :gen_tcp.listen(port, [:binary, packet: :line, active: false, reuseaddr: true, backlog: 4096])
1717
:io.format("~p at port: ~p ~n", [__MODULE__, port])
1818

1919
updated_state = state |> Map.put(:socket, socket)

test/remixdb_test.exs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ defmodule RemixdbTest do
199199
assert val === vv
200200

201201
unknown_key = :erlang.make_ref() |> inspect()
202-
{:ok, val} = client |> :eredis.q(["RENAME", unknown_key, :erlang.make_ref() |> inspect()])
202+
{:error, val} = client |> :eredis.q(["RENAME", unknown_key, :erlang.make_ref() |> inspect()])
203203
assert val === "ERR no such key"
204204
end
205205

@@ -591,10 +591,10 @@ defmodule RemixdbTest do
591591
{:ok, val} = client |> :eredis.q(["SREM", key1] ++ full_list)
592592
assert val === "0"
593593

594-
# SantoshTODO
595-
# val = client |> :eredis.q(["SREM", "key1"] ++ full_list)
596-
# val = client |> :eredis.q(["EXISTS", "key1"])
597-
# assert val === "0"
594+
{:ok, val} = client |> :eredis.q(["SREM", "key1"] ++ full_list)
595+
assert val === "0"
596+
{:ok, val} = client |> :eredis.q(["EXISTS", "key1"])
597+
assert val === "0"
598598
end
599599

600600
test "SPOP", %{client: client} do
@@ -739,8 +739,9 @@ defmodule RemixdbTest do
739739
assert val === "1"
740740
{:ok, val} = client |> :eredis.q(["HSET", my_hash, "name", "john"])
741741
assert val === "0"
742+
# Adding a new key to an existing hash
742743
client |> :eredis.q(["HSET", my_hash, "age", "30"])
743-
assert val === "1"
744+
assert val === "0"
744745

745746
{:ok, val} = client |> :eredis.q(["HLEN", my_hash])
746747
assert val === "2"

0 commit comments

Comments
 (0)