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
|
-module(poc).
-export([test/0, setup/0]).
test() ->
io:format("~nVolcano Iterator~n~n"),
%% Setup in-memory "database" (simulating Mnesia)
io:format("Setting up content-addressed storage...~n"),
Storage = setup(),
%% Insert data with automatic deduplication
io:format("Inserting products (note 'Soap' is stored only once)...~n"),
{Storage2, SoapHash} = store_value(Storage, "Soap"),
{Storage3, Price1Hash} = store_value(Storage2, 1.0),
{Storage4, BazookaHash} = store_value(Storage3, "Bazooka"),
{Storage5, Price2Hash} = store_value(Storage4, 413.0),
{Storage6, _SoapHash2} = store_value(Storage5, "Soap"), % Duplicate on purpose
{Storage7, Price3Hash} = store_value(Storage6, 50.0),
io:format("Unique values stored: ~p~n", [maps:size(maps:get(values, Storage7))]),
io:format("(Expected: 5, not 6 becase 'Soap' got deduplicated)~n~n"),
Tuple1 = #{name => SoapHash, price => Price1Hash},
Tuple2 = #{name => BazookaHash, price => Price2Hash},
Tuple3 = #{name => SoapHash, price => Price3Hash},
Tuples = [Tuple1, Tuple2, Tuple3],
io:format("Created 3 product tuples (referencing hashes)~n"),
io:format("Tuple1: #{name => ~s, price => ~s}~n",
[short_hash(SoapHash), short_hash(Price1Hash)]),
io:format("Tuple2: #{name => ~s, price => ~s}~n",
[short_hash(BazookaHash), short_hash(Price2Hash)]),
io:format("Tuple3: #{name => ~s, price => ~s}~n~n",
[short_hash(SoapHash), short_hash(Price3Hash)]),
io:format("Building query pipeline (Volcano Model):~n"),
io:format("Scan -> SELECT(price =< 100) -> Project(name)~n~n"),
ScanPid = spawn(fun() -> scan_iterator(Tuples, Storage7) end),
SelectPid = spawn(fun() ->
select_iterator(ScanPid, Storage7, fun(#{price := P}) -> P =< 100.0 end)
end),
ProjectPid = spawn(fun() ->
project_iterator(SelectPid, [name])
end),
io:format("Pulling results (on-demand, lazy evaluation):~n"),
Results = pull_all(ProjectPid),
io:format("~n"),
lists:foreach(fun(#{name := Name}) ->
io:format("-> #{name => ~p}~n", [Name])
end, Results),
ok.
setup() ->
#{values => #{}}. % Hash => Value map
store_value(Storage, Value) ->
Hash = value_hash(Value),
Values = maps:get(values, Storage),
case maps:is_key(Hash, Values) of
true ->
io:format("[DEDUP] Value ~p already exists (hash: ~s)~n",
[Value, short_hash(Hash)]),
{Storage, Hash};
false ->
io:format("[STORE] Value ~p (hash: ~s)~n",
[Value, short_hash(Hash)]),
NewValues = maps:put(Hash, Value, Values),
{Storage#{values => NewValues}, Hash}
end.
get_value(Storage, Hash) ->
Values = maps:get(values, Storage),
maps:get(Hash, Values).
value_hash(Value) ->
%% Simple hash for demo (in production: crypto:hash(sha256, term_to_binary(Value)))
erlang:phash2(Value).
short_hash(Hash) ->
lists:flatten(io_lib:format("~8.16.0B", [Hash])).
scan_iterator([], _Storage) ->
receive
{next, Caller} -> Caller ! done
end;
scan_iterator([TupleWithHashes | Rest], Storage) ->
receive
{next, Caller} ->
%% Unpacks all the hashes from each tuple
%% later we gotta use this on the merkle tree
%% for now it's just silly and inneficient
ResolvedTuple = maps:map(fun(_Attr, Hash) ->
get_value(Storage, Hash)
end, TupleWithHashes),
io:format("[SCAN] Emitting tuple: ~p~n", [ResolvedTuple]),
Caller ! {tuple, ResolvedTuple},
scan_iterator(Rest, Storage)
end.
select_iterator(SourcePid, Storage, Predicate) ->
receive
{next, Caller} ->
SourcePid ! {next, self()},
receive
done ->
Caller ! done;
{tuple, Tuple} ->
case Predicate(Tuple) of
true ->
io:format("[SELECT] Pass: ~p~n", [Tuple]),
Caller ! {tuple, Tuple},
select_iterator(SourcePid, Storage, Predicate);
false ->
io:format("[SELECT] Reject: ~p~n", [Tuple]),
%% Ignore this dude, and lock the process again
select_iterator_loop(SourcePid, Storage, Predicate, Caller)
end
end
end.
select_iterator_loop(SourcePid, Storage, Predicate, Caller) ->
SourcePid ! {next, self()},
receive
done ->
Caller ! done;
{tuple, Tuple} ->
case Predicate(Tuple) of
true ->
io:format("[SELECT] Pass: ~p~n", [Tuple]),
Caller ! {tuple, Tuple},
select_iterator(SourcePid, Storage, Predicate);
false ->
io:format("[SELECT] Reject: ~p~n", [Tuple]),
select_iterator_loop(SourcePid, Storage, Predicate, Caller)
end
end.
project_iterator(SourcePid, Attributes) ->
receive
{next, Caller} ->
SourcePid ! {next, self()},
receive
done ->
Caller ! done;
{tuple, Tuple} ->
ProjectedTuple = maps:with(Attributes, Tuple),
io:format("[PROJECT] ~p -> ~p~n", [Tuple, ProjectedTuple]),
Caller ! {tuple, ProjectedTuple},
project_iterator(SourcePid, Attributes)
end
end.
pull_all(IteratorPid) ->
pull_all(IteratorPid, []).
pull_all(IteratorPid, Acc) ->
IteratorPid ! {next, self()},
receive
done ->
lists:reverse(Acc);
{tuple, Tuple} ->
pull_all(IteratorPid, [Tuple | Acc])
after 1000 ->
{error, timeout}
end.
|