-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial work on a clean pqueue/mqueue/journal setup.
- Loading branch information
Showing
3 changed files
with
65 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,64 @@ | ||
-module(journal). | ||
-export(...). | ||
-export([enqueue/2, dequeue/1]). | ||
|
||
start_journal_for_queue_path(QueuePath) -> | ||
spawn(fun() -> manage_journal(QueuePath) end). | ||
|
||
manage_journal(QueuePath) -> | ||
enqueue(QueueName, Data) -> | ||
JournalPid = get_journal_pid(QueueName), | ||
JournalPid ! {set, Data, self()}, | ||
%% I bet you can just use "receive" as the last statement. TODO: Try this. | ||
%% If not, there's surely a better idiom for this in Erlang, so figure it out. | ||
receive | ||
%% {cmd, open} -> do_open(QueuePath); | ||
%% {cmd, erase} -> do_erase(QueuePath); | ||
%% {cmd, roll} -> do_roll(QueuePath); | ||
%% {cmd, close} -> do_close(QueuePath); | ||
%% {cmd, add} -> do_add(QueuePath); | ||
%% {cmd, add_with_xid} -> do_add_with_xid(QueuePath); | ||
%% remove | ||
%% remove_tentative | ||
%% unremove | ||
%% confirm -> | ||
X -> X | ||
end. | ||
|
||
erase(Journal) -> | ||
close it and delete it. | ||
|
||
write_items(Journal, Items) -> | ||
... | ||
dequeue(QueueName) -> | ||
JournalPid = get_journal_pid(QueueName), | ||
JournalPid ! {get, self()}, | ||
receive | ||
X -> X | ||
end. | ||
|
||
|
||
replay(QueueName) -> | ||
{ok, Terms} = file:consult(File), | ||
Terms. | ||
|
||
|
||
get_queue_pid(QueueName) -> | ||
JournalNameAtom = list_to_atom("journal:" ++ QueueName), | ||
case whereis(JournalNameAtom) of | ||
undefined -> register(JournalNameAtom, | ||
spawn(fun() -> setup_journal(QueueName) end)); | ||
ExistingJournalPid -> ExistingJournalPid | ||
end. | ||
|
||
|
||
%% From p. 235 of Programming Erlang | ||
unconsult(File, L) -> | ||
{ok, S} = file:open(File, write), | ||
lists:foreach(fun(X) -> io:format(S, "~p.~n",[X]) end, L), | ||
file:close(S). | ||
|
||
|
||
|
||
setup_journal(QueueName) -> | ||
% Open the files, | ||
File = file:open(File, [write, append]) | ||
manage_journal(File). | ||
|
||
|
||
manage_journal(File) -> | ||
receive | ||
{set, Data, ReplyPid} -> | ||
write_op(File, {set, Data}), | ||
%% TODO: Actually check results | ||
ReplyPid ! ok; | ||
{get, ReplyPid} -> | ||
write_op(File, get), | ||
%% TODO: Actually check results | ||
ReplyPid ! ok; | ||
Other -> | ||
io:format("Unexpected message: ~p~n", [Other]) | ||
end, | ||
manage_journal(File). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters