1 |
|
%% Copyright (c) 2022 Peter Morgan <peter.james.morgan@gmail.com> |
2 |
|
%% |
3 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
4 |
|
%% you may not use this file except in compliance with the License. |
5 |
|
%% You may obtain a copy of the License at |
6 |
|
%% |
7 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
8 |
|
%% |
9 |
|
%% Unless required by applicable law or agreed to in writing, software |
10 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
11 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 |
|
%% See the License for the specific language governing permissions and |
13 |
|
%% limitations under the License. |
14 |
|
|
15 |
|
|
16 |
|
-module(pgec_mcd_emulator_text). |
17 |
|
|
18 |
|
|
19 |
|
-export([recv/1]). |
20 |
|
-include_lib("kernel/include/logger.hrl"). |
21 |
|
-include_lib("stdlib/include/ms_transform.hrl"). |
22 |
|
|
23 |
|
|
24 |
|
recv(#{message := #{command := quit}}) -> |
25 |
:-( |
stop; |
26 |
|
|
27 |
|
recv(#{message := #{command := get, keys := Keys}} = Arg) -> |
28 |
4 |
?LOG_DEBUG(#{arg => Arg}), |
29 |
4 |
{continue, |
30 |
|
lists:foldl( |
31 |
|
fun |
32 |
|
(Key, A) -> |
33 |
4 |
try lookup(ptk(Key)) of |
34 |
|
{ok, #{metadata := Metadata, row := Row}} -> |
35 |
3 |
?LOG_DEBUG(#{key => Key, |
36 |
|
metadata => Metadata, |
37 |
3 |
row => Row}), |
38 |
|
|
39 |
3 |
[{encode, |
40 |
|
#{command => value, |
41 |
|
key => Key, |
42 |
|
flags => 0, |
43 |
|
expiry => 0, |
44 |
|
data => jsx:encode(row(Metadata, Row))}} | A]; |
45 |
|
|
46 |
|
not_found -> |
47 |
1 |
?LOG_DEBUG(#{key => Key, lookup => not_found}), |
48 |
1 |
A |
49 |
|
catch |
50 |
|
Class:Exception:Stacktrace -> |
51 |
:-( |
?LOG_DEBUG(#{class => Class, |
52 |
|
exception => Exception, |
53 |
:-( |
stacktrace => Stacktrace}), |
54 |
:-( |
A |
55 |
|
end |
56 |
|
end, |
57 |
|
[{encode, #{command => 'end'}}], |
58 |
|
Keys)}; |
59 |
|
|
60 |
|
recv(#{message := #{command := Command}} = Arg) -> |
61 |
:-( |
?LOG_DEBUG(#{arg => Arg}), |
62 |
:-( |
{continue, |
63 |
|
{encode, |
64 |
|
#{command => client_error, |
65 |
|
reason => io_lib:format("~p: not implemented", [Command])}}}. |
66 |
|
|
67 |
|
|
68 |
|
lookup(PTK) -> |
69 |
4 |
?LOG_DEBUG(#{ptk => PTK}), |
70 |
|
|
71 |
4 |
case metadata(PTK) of |
72 |
|
[{_, Metadata}] -> |
73 |
4 |
case lookup(Metadata, PTK) of |
74 |
|
[Row] -> |
75 |
3 |
{ok, #{metadata => Metadata, row => Row, ptk => PTK}}; |
76 |
|
|
77 |
|
[] -> |
78 |
1 |
not_found |
79 |
|
end; |
80 |
|
|
81 |
|
[] -> |
82 |
:-( |
not_found |
83 |
|
end. |
84 |
|
|
85 |
|
|
86 |
|
lookup(Metadata, PTK) -> |
87 |
4 |
Key = key(Metadata, PTK), |
88 |
4 |
?LOG_DEBUG(#{metadata => Metadata, ptk => PTK, key => Key}), |
89 |
4 |
case pgec_storage_sync:read(PTK#{key := Key}) of |
90 |
|
{ok, Value} -> |
91 |
3 |
[pgec_storage_common:row(Key, Value, Metadata)]; |
92 |
|
|
93 |
|
not_found -> |
94 |
1 |
[] |
95 |
|
end. |
96 |
|
|
97 |
|
|
98 |
|
key(Metadata, #{key := Encoded} = PTK) -> |
99 |
4 |
?LOG_DEBUG(#{metadata => Metadata, ptk => PTK}), |
100 |
4 |
pgec_kv:key(Metadata, string:split(Encoded, "/", all)). |
101 |
|
|
102 |
|
|
103 |
|
metadata(#{publication := Publication, table := Table} = Arg) -> |
104 |
4 |
?LOG_DEBUG(#{arg => Arg}), |
105 |
4 |
case pgec_storage_sync:metadata(Arg) of |
106 |
|
{ok, Metdata} -> |
107 |
4 |
[{{Publication, Table}, Metdata}]; |
108 |
|
|
109 |
|
not_found -> |
110 |
:-( |
[] |
111 |
|
end. |
112 |
|
|
113 |
|
|
114 |
|
ptk(PTK) -> |
115 |
4 |
?LOG_DEBUG(#{ptk => PTK}), |
116 |
4 |
case string:split(PTK, ".", all) of |
117 |
|
[Publication, Table, Key] -> |
118 |
4 |
#{publication => Publication, table => Table, key => Key}; |
119 |
|
|
120 |
|
_Otherwise -> |
121 |
:-( |
error(badarg, [PTK]) |
122 |
|
end. |
123 |
|
|
124 |
|
|
125 |
|
row(Metadata, Row) -> |
126 |
3 |
Result = pgec_kv:row(Metadata, <<"application/json">>, Row), |
127 |
3 |
?LOG_DEBUG(#{metadata => Metadata, row => Row, result => Result}), |
128 |
3 |
Result. |