aboutsummaryrefslogtreecommitdiffstats
path: root/src/xml_server.erl
blob: 4eef4c90fcc63d0a5d850b266bad49f333d8543a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
%%% @doc XML Server for Lazy Relation Consumption
%%%
%%% Provides a TCP server that accepts query plans as Erlang term strings
%%% and returns relation tuples as XML. The server maintains lazy iterators
%%% and allows clients to consume tuples on demand.
%%%
%%% == Protocol ==
%%%
%%% Client sends commands as line-delimited strings:
%%% <pre>
%%% QUERY &lt;erlang-term-string&gt;   - Execute a query plan, returns session ID
%%% NEXT &lt;session-id&gt; &lt;count&gt;    - Get next N tuples from iterator
%%% CLOSE &lt;session-id&gt;           - Close iterator and free resources
%%% </pre>
%%%
%%% Server responds with XML:
%%% <pre>
%%% &lt;?xml version="1.0"?&gt;
%%% &lt;response&gt;
%%%   &lt;session&gt;abc123&lt;/session&gt;
%%%   &lt;tuples&gt;
%%%     &lt;tuple&gt;
%%%       &lt;attribute name="id"&gt;1&lt;/attribute&gt;
%%%       &lt;attribute name="name"&gt;Alice&lt;/attribute&gt;
%%%     &lt;/tuple&gt;
%%%   &lt;/tuples&gt;
%%% &lt;/response&gt;
%%% </pre>
%%%
%%% == Usage ==
%%% <pre>
%%% % Start server on port 8080
%%% xml_server:start(8080).
%%%
%%% % Client connects and sends:
%%% % QUERY {scan, employees}\n
%%% % NEXT &lt;session-id&gt; 10\n
%%% % NEXT &lt;session-id&gt; 10\n
%%% % CLOSE &lt;session-id&gt;\n
%%% </pre>
%%%
%%% @author Nekoma Team
%%% @copyright 2025

-module(xml_server).

-export([start/1, start/2, stop/0]).
-export([init/2, handle_client/2]).

-include("operations.hrl").

%%% Public API

%% @doc Start the XML server on specified port with example database.
%%
%% @param Port TCP port number to listen on
%% @returns {ok, Pid} on success
-spec start(pos_integer()) -> {ok, pid()}.
start(Port) ->
    DB = repl:example_db(),
    start(Port, DB).

%% @doc Start the XML server on specified port with custom database.
%%
%% @param Port TCP port number to listen on
%% @param DB Database state to use for queries
%% @returns {ok, Pid} on success
-spec start(pos_integer(), term()) -> {ok, pid()}.
start(Port, DB) ->
    spawn(?MODULE, init, [Port, DB]),
    io:format("XML Server started on port ~p~n", [Port]),
    {ok, whereis(xml_server)}.

%% @doc Stop the XML server.
%%
%% @returns ok
-spec stop() -> ok.
stop() ->
    case whereis(xml_server) of
        undefined -> ok;
        Pid ->
            Pid ! stop,
            ok
    end.

%%% Internal Functions

%% @private
%% @doc Initialize server and start listening.
init(Port, DB) ->
    register(xml_server, self()),
    {ok, ListenSocket} = gen_tcp:listen(Port, [
        binary,
        {packet, line},
        {active, false},
        {reuseaddr, true}
    ]),
    io:format("Listening for connections...~n"),
    accept_loop(ListenSocket, DB).

%% @private
%% @doc Accept incoming client connections.
accept_loop(ListenSocket, DB) ->
    receive
        stop ->
            gen_tcp:close(ListenSocket),
            io:format("Server stopped~n"),
            ok
    after 0 ->
        case gen_tcp:accept(ListenSocket, 1000) of
            {ok, ClientSocket} ->
                io:format("Client connected: ~p~n", [ClientSocket]),
                spawn(?MODULE, handle_client, [ClientSocket, DB]),
                accept_loop(ListenSocket, DB);
            {error, timeout} ->
                accept_loop(ListenSocket, DB);
            {error, Reason} ->
                io:format("Accept error: ~p~n", [Reason]),
                accept_loop(ListenSocket, DB)
        end
    end.

%% @private
%% @doc Handle client session with command processing.
handle_client(Socket, DB) ->
    Sessions = #{},
    client_loop(Socket, DB, Sessions).

%% @private
%% @doc Main client command processing loop.
client_loop(Socket, DB, Sessions) ->
    case gen_tcp:recv(Socket, 0) of
        {ok, Data} ->
            Line = binary_to_list(Data),
            Command = string:trim(Line),
            io:format("Received command: ~s~n", [Command]),

            case parse_command(Command) of
                {query, QueryStr} ->
                    {NewSessions, Response} = handle_query(QueryStr, DB, Sessions),
                    send_response(Socket, Response),
                    client_loop(Socket, DB, NewSessions);

                {next, SessionId, Count} ->
                    {NewSessions, Response} = handle_next(SessionId, Count, Sessions),
                    send_response(Socket, Response),
                    client_loop(Socket, DB, NewSessions);

                {close, SessionId} ->
                    {NewSessions, Response} = handle_close(SessionId, Sessions),
                    send_response(Socket, Response),
                    client_loop(Socket, DB, NewSessions);

                {schema} ->
                    Response = handle_schema(DB),
                    send_response(Socket, Response),
                    client_loop(Socket, DB, Sessions);

                {error, Msg} ->
                    Response = error_response(Msg),
                    send_response(Socket, Response),
                    client_loop(Socket, DB, Sessions)
            end;

        {error, closed} ->
            io:format("Client disconnected~n"),
            cleanup_sessions(Sessions),
            ok;

        {error, Reason} ->
            io:format("Receive error: ~p~n", [Reason]),
            cleanup_sessions(Sessions),
            ok
    end.

%% @private
%% @doc Parse client command.
parse_command(Command) ->
    case string:split(Command, " ", leading) of
        ["QUERY", QueryStr] ->
            {query, QueryStr};
        ["NEXT", Rest] ->
            case string:split(Rest, " ", leading) of
                [SessionId, CountStr] ->
                    try
                        Count = list_to_integer(CountStr),
                        {next, SessionId, Count}
                    catch
                        _:_ -> {error, "Invalid count"}
                    end;
                _ ->
                    {error, "Invalid NEXT syntax"}
            end;
        ["CLOSE", SessionId] ->
            {close, SessionId};
        ["SCHEMA"] ->
            {schema};
        _ ->
            {error, "Unknown command"}
    end.

%% @private
%% @doc Handle QUERY command - execute query and create iterator.
handle_query(QueryStr, DB, Sessions) ->
    try
        % Parse and evaluate the query plan from string using erl_eval
        % This allows parsing of function literals
        {ok, Tokens, _} = erl_scan:string(QueryStr ++ "."),
        {ok, Exprs} = erl_parse:parse_exprs(Tokens),
        {value, QueryPlan, _} = erl_eval:exprs(Exprs, erl_eval:new_bindings()),

        % Prepare and execute query to get iterator
        PreparedPlan = query_planner:prepare(QueryPlan),
        Iterator = query_planner:execute(DB, PreparedPlan),

        % Generate session ID
        SessionId = generate_session_id(),

        % Store iterator in sessions
        NewSessions = maps:put(SessionId, Iterator, Sessions),

        % Return session ID
        Response = session_response(SessionId),
        {NewSessions, Response}
    catch
        Error:Reason:Stack ->
            io:format("Query error: ~p:~p~n~p~n", [Error, Reason, Stack]),
            {Sessions, error_response(io_lib:format("Query failed: ~p", [Reason]))}
    end.

%% @private
%% @doc Handle NEXT command - fetch next N tuples from iterator.
handle_next(SessionId, Count, Sessions) ->
    case maps:get(SessionId, Sessions, undefined) of
        undefined ->
            {Sessions, error_response("Invalid session ID")};
        Iterator ->
            try
                % Collect next Count tuples
                {Tuples, NewIterator} = collect_n_tuples(Iterator, Count),

                % Update session with new iterator state
                NewSessions = maps:put(SessionId, NewIterator, Sessions),

                % Return tuples as XML
                Response = tuples_response(Tuples),
                {NewSessions, Response}
            catch
                Error:Reason:Stack ->
                    io:format("Next error: ~p:~p~n~p~n", [Error, Reason, Stack]),
                    {Sessions, error_response(io_lib:format("Next failed: ~p", [Reason]))}
            end
    end.

%% @private
%% @doc Handle CLOSE command - close iterator and free resources.
handle_close(SessionId, Sessions) ->
    case maps:get(SessionId, Sessions, undefined) of
        undefined ->
            {Sessions, error_response("Invalid session ID")};
        Iterator ->
            operations:close_iterator(Iterator),
            NewSessions = maps:remove(SessionId, Sessions),
            Response = ok_response("Session closed"),
            {NewSessions, Response}
    end.

%% @private
%% @doc Handle SCHEMA command - return database schema with all relations and their attributes.
handle_schema(DB) ->
    try
        % Get all relation names
        RelationNames = operations:get_relations(DB),

        % For each relation, get its hash and then the relation record
        Relations = lists:map(fun(Name) ->
            case operations:get_relation_hash(DB, Name) of
                {ok, Hash} ->
                    % Read the relation record from Mnesia
                    case mnesia:dirty_read(relation, Hash) of
                        [Relation] ->
                            {Name, Relation#relation.schema, Relation#relation.cardinality,
                             Relation#relation.constraints, Relation#relation.provenance};
                        [] ->
                            {Name, #{}, unknown, #{}, undefined}
                    end;
                {error, _} ->
                    {Name, #{}, unknown, #{}, undefined}
            end
        end, RelationNames),

        schema_response(Relations)
    catch
        Error:Reason:Stack ->
            io:format("Schema error: ~p:~p~n~p~n", [Error, Reason, Stack]),
            error_response(io_lib:format("Schema failed: ~p", [Reason]))
    end.

%% @private
%% @doc Collect N tuples from iterator lazily.
collect_n_tuples(Iterator, Count) ->
    collect_n_tuples(Iterator, Count, []).

collect_n_tuples(Iterator, 0, Acc) ->
    {lists:reverse(Acc), Iterator};
collect_n_tuples(Iterator, Count, Acc) ->
    case operations:next_tuple(Iterator) of
        {ok, Tuple} ->
            collect_n_tuples(Iterator, Count - 1, [Tuple | Acc]);
        done ->
            {lists:reverse(Acc), Iterator}
    end.

%% @private
%% @doc Generate unique session ID.
generate_session_id() ->
    Timestamp = erlang:system_time(microsecond),
    Random = rand:uniform(1000000),
    Hash = crypto:hash(md5, term_to_binary({Timestamp, Random})),
    binary_to_list(base64:encode(Hash)).

%% @private
%% @doc Cleanup all sessions on disconnect.
cleanup_sessions(Sessions) ->
    maps:foreach(
        fun(_SessionId, Iterator) ->
            operations:close_iterator(Iterator)
        end,
        Sessions).

%%% XML Response Formatters

%% @private
%% @doc Format session response.
session_response(SessionId) ->
    [
        "<?xml version=\"1.0\"?>\n",
        "<response>\n",
        "  <status>ok</status>\n",
        "  <session>", SessionId, "</session>\n",
        "</response>\n"
    ].

%% @private
%% @doc Format schema response as XML.
schema_response(Relations) ->
    RelationsXML = lists:map(fun({Name, Schema, Cardinality, Constraints, Provenance}) ->
        % Convert cardinality to string
        CardStr = case Cardinality of
            {finite, N} -> io_lib:format("finite(~p)", [N]);
            aleph_zero -> "infinite";
            continuum -> "continuum";
            _ -> "unknown"
        end,

        % Convert schema map to attribute elements
        % Wrap each attribute in a list to keep it as a single element when reversed
        Attributes = maps:fold(fun(AttrName, Type, Acc) ->
            [
                ["      <attribute name=\"", atom_to_list(AttrName), "\" type=\"",
                 atom_to_list(Type), "\"/>\n"]
                | Acc
            ]
        end, [], Schema),

        % Convert constraints to XML
        ConstraintsXML = case maps:size(Constraints) of
            0 -> [];
            _ -> [
                "      <constraints>\n",
                maps:fold(fun(AttrName, Constraint, Acc) ->
                    ConstraintStr = format_constraint(Constraint),
                    [["        <constraint attribute=\"", atom_to_list(AttrName),
                      "\">", ConstraintStr, "</constraint>\n"] | Acc]
                end, [], Constraints),
                "      </constraints>\n"
            ]
        end,

        % Convert provenance to XML
        ProvenanceXML = case Provenance of
            undefined -> [];
            _ -> [
                "      <provenance>", format_provenance(Provenance), "</provenance>\n"
            ]
        end,

        [
            "    <relation name=\"", atom_to_list(Name), "\" cardinality=\"", CardStr, "\">\n",
            lists:reverse(Attributes),
            ConstraintsXML,
            ProvenanceXML,
            "    </relation>\n"
        ]
    end, Relations),

    [
        "<?xml version=\"1.0\"?>\n",
        "<response>\n",
        "  <status>ok</status>\n",
        "  <schema>\n",
        RelationsXML,
        "  </schema>\n",
        "</response>\n"
    ].

%% @private
%% @doc Format constraint as string.
format_constraint({eq, Value}) -> lists:flatten(io_lib:format("= ~p", [Value]));
format_constraint({neq, Value}) -> lists:flatten(io_lib:format("!= ~p", [Value]));
format_constraint({lt, Value}) -> lists:flatten(io_lib:format("&lt; ~p", [Value]));
format_constraint({lte, Value}) -> lists:flatten(io_lib:format("&lt;= ~p", [Value]));
format_constraint({gt, Value}) -> lists:flatten(io_lib:format("&gt; ~p", [Value]));
format_constraint({gte, Value}) -> lists:flatten(io_lib:format("&gt;= ~p", [Value]));
format_constraint({in, List}) -> lists:flatten(io_lib:format("in ~p", [List]));
format_constraint({range, Min, Max}) -> lists:flatten(io_lib:format("in [~p, ~p]", [Min, Max]));
format_constraint({member_of, Relation}) -> lists:flatten(io_lib:format("in ~p", [Relation]));
format_constraint(Other) -> lists:flatten(io_lib:format("~p", [Other])).

%% @private
%% @doc Format provenance as string.
format_provenance(undefined) -> "undefined";
format_provenance({base, Relation}) -> lists:flatten(io_lib:format("base(~p)", [Relation]));
format_provenance({join, P1, P2}) ->
    lists:flatten(io_lib:format("join(~s, ~s)", [format_provenance(P1), format_provenance(P2)]));
format_provenance({select, P, _Constraints}) ->
    lists:flatten(io_lib:format("select(~s)", [format_provenance(P)]));
format_provenance({project, P, Attrs}) ->
    lists:flatten(io_lib:format("project(~s, ~p)", [format_provenance(P), Attrs]));
format_provenance({take, P, N}) ->
    lists:flatten(io_lib:format("take(~s, ~p)", [format_provenance(P), N]));
format_provenance(Other) -> lists:flatten(io_lib:format("~p", [Other])).

%% @private
%% @doc Format tuples response.
tuples_response(Tuples) ->
    [
        "<?xml version=\"1.0\"?>\n",
        "<response>\n",
        "  <status>ok</status>\n",
        "  <tuples>\n",
        lists:map(fun tuple_to_xml/1, Tuples),
        "  </tuples>\n",
        "</response>\n"
    ].

%% @private
%% @doc Format single tuple as XML.
tuple_to_xml(Tuple) ->
    Attributes = maps:to_list(Tuple),
    [
        "    <tuple>\n",
        lists:map(fun({Name, Value}) ->
            EscapedValue = escape_xml(format_value(Value)),
            io_lib:format("      <attribute name=\"~s\">~s</attribute>\n",
                         [Name, EscapedValue])
        end, Attributes),
        "    </tuple>\n"
    ].

%% @private
%% @doc Format value for XML output.
format_value(Value) when is_integer(Value) -> integer_to_list(Value);
format_value(Value) when is_float(Value) -> float_to_list(Value);
format_value(Value) when is_atom(Value) -> atom_to_list(Value);
format_value(Value) when is_binary(Value) -> binary_to_list(Value);
format_value(Value) when is_list(Value) -> Value;
format_value(Value) -> io_lib:format("~p", [Value]).

%% @private
%% @doc Escape XML special characters.
escape_xml(Str) ->
    lists:flatten(
        lists:map(
            fun($<) -> "&lt;";
               ($>) -> "&gt;";
               ($&) -> "&amp;";
               ($") -> "&quot;";
               ($') -> "&apos;";
               (C) -> C
            end,
            lists:flatten(Str))).

%% @private
%% @doc Format error response.
error_response(Message) ->
    [
        "<?xml version=\"1.0\"?>\n",
        "<response>\n",
        "  <status>error</status>\n",
        "  <message>", escape_xml(Message), "</message>\n",
        "</response>\n"
    ].

%% @private
%% @doc Format OK response.
ok_response(Message) ->
    [
        "<?xml version=\"1.0\"?>\n",
        "<response>\n",
        "  <status>ok</status>\n",
        "  <message>", escape_xml(Message), "</message>\n",
        "</response>\n"
    ].

%% @private
%% @doc Send response to client.
send_response(Socket, Response) ->
    gen_tcp:send(Socket, Response).