Fix wrong index preparation in mrdb:index_read_/3 #1

Merged
uwiger merged 4 commits from uw-catchup into master 2024-08-14 18:24:18 +09:00
uwiger commented 2024-08-14 18:16:51 +09:00 (Migrated from gitlab.com)

In the case when index is of type {_}, index is returned as is
instead of putting it into a tuple {Ix, ordered}.

An example

Given the following index plugin implementation:
-module(key6).

-record(cgh, {key, val}).

-export([k6/3
, add_index/0
, create_table/0
, tst_bad_type/0
]).

k6(_, _, #cgh{key = {Name, K8, _Cid, _Vn}}) ->
[list_to_binary([atom_to_binary(Name),
list_to_binary(lists:sublist(K8, 6))])].

add_index() ->
mnesia_schema:add_index_plugin({k6}, key6, k6).

create_table() ->
mnesia:create_table(cgh,
[{attributes, record_info(fields, cgh)},
{type, ordered_set},
{index, [{k6}]},
{rocksdb_copies, [node()]},
{local_content, true}]).

tst_bad_type() ->
E1 = #cgh{key = {n1, "u1vgrjkh",{ecgi,238,6,213020,50},3}, val = ok},
mnesia:dirty_write(E1),
Index1 = list_to_binary([atom_to_binary(n1),
list_to_binary(lists:sublist("u1vgrjkh", 6))]),
mrdb:index_read(cgh, Index1, {k6}).

-- testing --
Eshell V14.2.2 (press Ctrl+G to abort, type help(). for help)
(rock@bang)2> mnesia:create_schema([node()]).
ok
(rock@bang)3> mnesia:start().
ok
(rock@bang)4> mnesia_rocksdb:register().
{ok,rocksdb_copies}
(rock@bang)5> l(key6).
{module,key6}
(rock@bang)6> key6:add_index().
{atomic,ok}
(rock@bang)7> key6:create_table().
{atomic,ok}
(rock@bang)8> key6:tst_bad_type().
** exception exit: {aborted,{bad_type,{cgh,index,{k6}}}}
in function mnesia:abort/1 (mnesia.erl, line 362)
in call from mrdb:ensure_ref/1 (/projects/erlang/github/mnesia_rocksdb/src/mrdb.erl, line 609)
in call from mrdb:index_read_/3 (
/projects/erlang/github/mnesia_rocksdb/src/mrdb.erl, line 945)

-- after patch --

(rock@bang)9> c(mrdb).
Recompiling ~/projects/erlang/github/mnesia_rocksdb/src/mrdb.erl
{ok,mrdb}
(rock@bang)10> key6:tst_bad_type().
[{cgh,{n1,"u1vgrjkh",{ecgi,238,6,213020,50},3},ok}]
(rock@bang)11>

In the case when index is of type {_}, index is returned as is instead of putting it into a tuple {Ix, ordered}. An example Given the following index plugin implementation: -module(key6). -record(cgh, {key, val}). -export([k6/3 , add_index/0 , create_table/0 , tst_bad_type/0 ]). k6(_, _, #cgh{key = {Name, K8, _Cid, _Vn}}) -> [list_to_binary([atom_to_binary(Name), list_to_binary(lists:sublist(K8, 6))])]. add_index() -> mnesia_schema:add_index_plugin({k6}, key6, k6). create_table() -> mnesia:create_table(cgh, [{attributes, record_info(fields, cgh)}, {type, ordered_set}, {index, [{k6}]}, {rocksdb_copies, [node()]}, {local_content, true}]). tst_bad_type() -> E1 = #cgh{key = {n1, "u1vgrjkh",{ecgi,238,6,213020,50},3}, val = ok}, mnesia:dirty_write(E1), Index1 = list_to_binary([atom_to_binary(n1), list_to_binary(lists:sublist("u1vgrjkh", 6))]), mrdb:index_read(cgh, Index1, {k6}). -- testing -- Eshell V14.2.2 (press Ctrl+G to abort, type help(). for help) (rock@bang)2> mnesia:create_schema([node()]). ok (rock@bang)3> mnesia:start(). ok (rock@bang)4> mnesia_rocksdb:register(). {ok,rocksdb_copies} (rock@bang)5> l(key6). {module,key6} (rock@bang)6> key6:add_index(). {atomic,ok} (rock@bang)7> key6:create_table(). {atomic,ok} (rock@bang)8> key6:tst_bad_type(). ** exception exit: {aborted,{bad_type,{cgh,index,{k6}}}} in function mnesia:abort/1 (mnesia.erl, line 362) in call from mrdb:ensure_ref/1 (~/projects/erlang/github/mnesia_rocksdb/src/mrdb.erl, line 609) in call from mrdb:index_read_/3 (~/projects/erlang/github/mnesia_rocksdb/src/mrdb.erl, line 945) -- after patch -- (rock@bang)9> c(mrdb). Recompiling ~/projects/erlang/github/mnesia_rocksdb/src/mrdb.erl {ok,mrdb} (rock@bang)10> key6:tst_bad_type(). [{cgh,{n1,"u1vgrjkh",{ecgi,238,6,213020,50},3},ok}] (rock@bang)11>
uwiger commented 2024-08-14 18:16:52 +09:00 (Migrated from gitlab.com)

requested review from @zxq9, @dimitar.p.ivanov, and @hans_sv

requested review from @zxq9, @dimitar.p.ivanov, and @hans_sv
uwiger commented 2024-08-14 18:16:52 +09:00 (Migrated from gitlab.com)

assigned to @uwiger

assigned to @uwiger
uwiger commented 2024-08-14 18:24:18 +09:00 (Migrated from gitlab.com)

mentioned in commit d9d82d7ead

mentioned in commit d9d82d7ead782bb9bda1785dd03aa1d93c0de518
uwiger (Migrated from gitlab.com) merged commit d9d82d7ead into master 2024-08-14 18:24:19 +09:00
Sign in to join this conversation.
No description provided.