diff options
| author | Marcos Magueta <maguetamarcos@gmail.com> | 2025-11-11 00:00:43 -0300 |
|---|---|---|
| committer | Marcos Magueta <maguetamarcos@gmail.com> | 2025-11-11 00:00:43 -0300 |
| commit | 3063608df6ff1668f683d9d22c57fea29d728d0d (patch) | |
| tree | 36be7f6c6d901c5984c045d4ce823bd24c37e9af /src | |
| parent | b5dbdaa989a86477669aac0bf342029e1d2b6028 (diff) | |
| download | Sakura-3063608df6ff1668f683d9d22c57fea29d728d0d.tar.gz Sakura-3063608df6ff1668f683d9d22c57fea29d728d0d.zip | |
migrating to erlang to attach to mnesia
Diffstat (limited to '')
| -rw-r--r-- | src/main.app.src | 15 | ||||
| -rw-r--r-- | src/main.erl | 7 | ||||
| -rw-r--r-- | src/operations.erl | 15 | ||||
| -rw-r--r-- | src/relation.erl | 42 |
4 files changed, 79 insertions, 0 deletions
diff --git a/src/main.app.src b/src/main.app.src new file mode 100644 index 0000000..0b7700a --- /dev/null +++ b/src/main.app.src @@ -0,0 +1,15 @@ +{application, main, [ + {description, "A relational algebra engine"}, + {vsn, "0.1.0"}, + {registered, []}, + {mod, {main_app, []}}, + {applications, [ + kernel, + stdlib, + mnesia + ]}, + {env, []}, + {modules, []}, + {licenses, ["Apache-2.0"]}, + {links, []} + ]}. diff --git a/src/main.erl b/src/main.erl new file mode 100644 index 0000000..3c34a31 --- /dev/null +++ b/src/main.erl @@ -0,0 +1,7 @@ +-module(main). +-export([fibonacci/1]). + +fibonacci(0) -> 0; +fibonacci(1) -> 1; +fibonacci(N) -> + fibonacci(N-2) + fibonacci(N-1). diff --git a/src/operations.erl b/src/operations.erl new file mode 100644 index 0000000..7846caf --- /dev/null +++ b/src/operations.erl @@ -0,0 +1,15 @@ +-module(operations). +-export([select/2, project/2, cartesian_product/2]). + +%% @doc Selection - filter tuples based on a predicate +%% `Predicate` is a function that takes a tuple and returns true/false +select(Relation, Predicate) -> + sets:filter(Predicate, Relation). + +%% @doc Projection - select specific attributes from a relation +%% Note that `AttributeNames` can have duplicate keys, which are ignored +project(Relation, AttributeNames) when is_list(AttributeNames) -> + sets:map(fun (Tuple) -> maps:with(AttributeNames, Tuple) end, Relation). + +cartesian_product(Relation1, Relation2) -> + sets:from_list([maps:merge(Tuple1, Tuple2) || Tuple1 <- sets:to_list(Relation1), Tuple2 <- sets:to_list(Relation2)], [{version, 2}]). diff --git a/src/relation.erl b/src/relation.erl new file mode 100644 index 0000000..66930d2 --- /dev/null +++ b/src/relation.erl @@ -0,0 +1,42 @@ +-module(relation). +-export([new_attribute/2, new_tuple/1, new_relation/1]). + +%% Attribute: A pair of (Name, Value), eg. ("Name", "Augusto") +%% Tuple: A set of attributes, eg. (("Name", "Augusto"), ("Age", 20), ("Surname", "Rengel")) +%% Relation: A set of tuples organized through some selector +%% Example Relation: f(x: Nat) = x + 1 +%% |{(0, 1), (1, 2), (2, 3), ...}| = |N| +%% RelationCustomer = [(Username, Text), (Password, Text)] +%% RelationProduct = [(Name, Text), (Price, Real)] +%% RelationOrder = [(OrderId, Natural), ((Username, Text), +%% (Password, Text), RelationCustomer), ((Name, Text), +%% (Price, Real), RelationProduct)] + +new_attribute(Name, Value) -> + #{name => Name, value => Value}. + +new_tuple(Attributes) when is_list(Attributes) -> + Fun = fun (#{name := Name, value := Value}, Acc) -> + maps:put(Name, Value, Acc) + end, + lists:foldl(Fun, #{}, Attributes). + +new_relation(Tuples) when is_list(Tuples) -> + sets:from_list(Tuples, [{version, 2}]). + +%% SoapName1 = relation:new_attribute(name, "ABC"). +%% SoapPrice1 = relation:new_attribute(price, 1.0). +%% SoapName2 = relation:new_attribute(name, "XYZ"). +%% SoapPrice2 = relation:new_attribute(price, 1.25). +%% BazookaName = relation:new_attribute(name, "Bazzum"). +%% BazookaPrice = relation:new_attribute(price, 413.0). +%% SwordName = relation:new_attribute(name, "Sword"). +%% SwordPrice = relation:new_attribute(price, 100.0). +%% relation:new_relation([relation:new_tuple([SoapName, SoapPrice]), relation:new_tuple([BazookaName, BazookaPrice]), relation:new_tuple([SwordName, SwordPrice])]). + +%% Jules = relation:new_attribute(customer_name, "Jules"). +%% Emily = relation:new_attribute(customer_name, "Emily"). +%% Customers = relation:new_relation([relation:new_tuple([Jules]), +%% relation:new_tuple([Emily])]). + +%% operations:project(operations:select(operations:cartesian_product(relation:new_relation([relation:new_tuple([SoapName1, SoapPrice1]), relation:new_tuple([SoapName2, SoapPrice2]), relation:new_tuple([BazookaName, BazookaPrice]), relation:new_tuple([SwordName, SwordPrice])]), Customers), fun (Tuple) -> maps:get(price, Tuple) =< 1.0 end), [name, customer_name]). |
