%%% Copyright (c) 2014, NORDUnet A/S. %%% See LICENSE for licensing information. %% Implements an interface to a file pair (basename and basename.chksum) %% that stores an ordered list of fixed-size entries. Entries can be %% added at the end and are retrieved by index. The list can also be %% truncated. %% %% Writes(add, truncate, addlast) need to be serialized. %% TODO: Checksums -module(index). -export([get/2, getrange/3, add/3, addlast/2, truncate/2]). -define(ENTRYSIZE, 32). -define(ENTRYSIZEINFILE, (?ENTRYSIZE*2+1)). -spec add(string(), integer() | last, binary()) -> ok. add(Basepath, Index, Entry) when is_binary(Entry), size(Entry) == ?ENTRYSIZE -> case file:open(Basepath, [read, write, binary]) of {ok, File} -> {ok, Position} = file:position(File, eof), case Index of last when Position rem ?ENTRYSIZEINFILE == 0 -> ok; Index when is_integer(Index), Index * ?ENTRYSIZEINFILE == Position -> ok end, EntryText = hex:bin_to_hexstr(Entry) ++ "\n", ok = file:write(File, EntryText), ok = file:close(File), util:fsync([Basepath, filename:dirname(Basepath)]); {error, Error} -> util:exit_with_error(Error, writefile, "Error opening file for writing") end. truncate(Basepath, Index) -> case file:open(Basepath, [read, write, binary]) of {ok, File} -> {ok, _Position} = file:position(File, Index * ?ENTRYSIZEINFILE), ok = file:truncate(File), ok = file:close(File), util:fsync([Basepath, filename:dirname(Basepath)]); {error, Error} -> util:exit_with_error(Error, writefile, "Error opening file for writing") end. -spec addlast(string(), integer()) -> ok. addlast(Basepath, Entry) -> add(Basepath, last, Entry). decodedata(Binary) -> lists:reverse(decodedata(Binary, [])). decodedata(<<>>, Acc) -> Acc; decodedata(<>, Acc) -> decodedata(Rest, [mochihex:to_bin(binary_to_list(Entry)) | Acc]); decodedata(<<_:?ENTRYSIZE/binary-unit:16, _>>, _Acc) -> util:exit_with_error(badformat, readindex, "Index line not ending with linefeed"). -spec get(string(), integer()) -> binary(). get(Basepath, Index) -> case getrange(Basepath, Index, Index) of noentry -> noentry; [Entry] -> Entry end. -spec getrange(string(), integer(), integer()) -> [binary()]. getrange(Basepath, Start, End) when Start =< End -> case file:open(Basepath, [read, binary]) of {ok, File} -> {ok, Filesize} = file:position(File, eof), if End * ?ENTRYSIZEINFILE + ?ENTRYSIZEINFILE =< Filesize -> {ok, _Position} = file:position(File, Start * ?ENTRYSIZEINFILE), {ok, EntryText} = file:read(File, ?ENTRYSIZEINFILE * (End - Start + 1)), Entry = decodedata(EntryText), file:close(File), Entry; true -> noentry end; {error, Error} -> util:exit_with_error(Error, readfile, "Error opening file for reading") end.