/deps/rabbit/src/rabbit_msg_file.erl

https://github.com/rabbitmq/rabbitmq-server · Erlang · 114 lines · 84 code · 16 blank · 14 comment · 0 complexity · 260031508a4704132e33881ced77537e MD5 · raw file

  1. %% This Source Code Form is subject to the terms of the Mozilla Public
  2. %% License, v. 2.0. If a copy of the MPL was not distributed with this
  3. %% file, You can obtain one at https://mozilla.org/MPL/2.0/.
  4. %%
  5. %% Copyright (c) 2007-2022 VMware, Inc. or its affiliates. All rights reserved.
  6. %%
  7. -module(rabbit_msg_file).
  8. -export([append/3, read/2, scan/4]).
  9. %%----------------------------------------------------------------------------
  10. -include_lib("rabbit_common/include/rabbit_msg_store.hrl").
  11. -define(INTEGER_SIZE_BYTES, 8).
  12. -define(INTEGER_SIZE_BITS, (8 * ?INTEGER_SIZE_BYTES)).
  13. -define(WRITE_OK_SIZE_BITS, 8).
  14. -define(WRITE_OK_MARKER, 255).
  15. -define(FILE_PACKING_ADJUSTMENT, (1 + ?INTEGER_SIZE_BYTES)).
  16. -define(MSG_ID_SIZE_BYTES, 16).
  17. -define(MSG_ID_SIZE_BITS, (8 * ?MSG_ID_SIZE_BYTES)).
  18. -define(SCAN_BLOCK_SIZE, 4194304). %% 4MB
  19. %%----------------------------------------------------------------------------
  20. -type io_device() :: any().
  21. -type position() :: non_neg_integer().
  22. -type msg_size() :: non_neg_integer().
  23. -type file_size() :: non_neg_integer().
  24. -type message_accumulator(A) ::
  25. fun (({rabbit_types:msg_id(), msg_size(), position(), binary()}, A) ->
  26. A).
  27. %%----------------------------------------------------------------------------
  28. -spec append(io_device(), rabbit_types:msg_id(), msg()) ->
  29. rabbit_types:ok_or_error2(msg_size(), any()).
  30. append(FileHdl, MsgId, MsgBody)
  31. when is_binary(MsgId) andalso size(MsgId) =:= ?MSG_ID_SIZE_BYTES ->
  32. MsgBodyBin = term_to_binary(MsgBody),
  33. MsgBodyBinSize = size(MsgBodyBin),
  34. Size = MsgBodyBinSize + ?MSG_ID_SIZE_BYTES,
  35. case file_handle_cache:append(FileHdl,
  36. <<Size:?INTEGER_SIZE_BITS,
  37. MsgId:?MSG_ID_SIZE_BYTES/binary,
  38. MsgBodyBin:MsgBodyBinSize/binary,
  39. ?WRITE_OK_MARKER:?WRITE_OK_SIZE_BITS>>) of
  40. ok -> {ok, Size + ?FILE_PACKING_ADJUSTMENT};
  41. KO -> KO
  42. end.
  43. -spec read(io_device(), msg_size()) ->
  44. rabbit_types:ok_or_error2({rabbit_types:msg_id(), msg()},
  45. any()).
  46. read(FileHdl, TotalSize) ->
  47. Size = TotalSize - ?FILE_PACKING_ADJUSTMENT,
  48. BodyBinSize = Size - ?MSG_ID_SIZE_BYTES,
  49. case file_handle_cache:read(FileHdl, TotalSize) of
  50. {ok, <<Size:?INTEGER_SIZE_BITS,
  51. MsgId:?MSG_ID_SIZE_BYTES/binary,
  52. MsgBodyBin:BodyBinSize/binary,
  53. ?WRITE_OK_MARKER:?WRITE_OK_SIZE_BITS>>} ->
  54. {ok, {MsgId, binary_to_term(MsgBodyBin)}};
  55. KO -> KO
  56. end.
  57. -spec scan(io_device(), file_size(), message_accumulator(A), A) ->
  58. {'ok', A, position()}.
  59. scan(FileHdl, FileSize, Fun, Acc) when FileSize >= 0 ->
  60. scan(FileHdl, FileSize, <<>>, 0, 0, Fun, Acc).
  61. scan(_FileHdl, FileSize, _Data, FileSize, ScanOffset, _Fun, Acc) ->
  62. {ok, Acc, ScanOffset};
  63. scan(FileHdl, FileSize, Data, ReadOffset, ScanOffset, Fun, Acc) ->
  64. Read = lists:min([?SCAN_BLOCK_SIZE, (FileSize - ReadOffset)]),
  65. case file_handle_cache:read(FileHdl, Read) of
  66. {ok, Data1} ->
  67. {Data2, Acc1, ScanOffset1} =
  68. scanner(<<Data/binary, Data1/binary>>, ScanOffset, Fun, Acc),
  69. ReadOffset1 = ReadOffset + size(Data1),
  70. scan(FileHdl, FileSize, Data2, ReadOffset1, ScanOffset1, Fun, Acc1);
  71. _KO ->
  72. {ok, Acc, ScanOffset}
  73. end.
  74. scanner(<<>>, Offset, _Fun, Acc) ->
  75. {<<>>, Acc, Offset};
  76. scanner(<<0:?INTEGER_SIZE_BITS, _Rest/binary>>, Offset, _Fun, Acc) ->
  77. {<<>>, Acc, Offset}; %% Nothing to do other than stop.
  78. scanner(<<Size:?INTEGER_SIZE_BITS, MsgIdAndMsg:Size/binary,
  79. WriteMarker:?WRITE_OK_SIZE_BITS, Rest/binary>>, Offset, Fun, Acc) ->
  80. TotalSize = Size + ?FILE_PACKING_ADJUSTMENT,
  81. case WriteMarker of
  82. ?WRITE_OK_MARKER ->
  83. %% Here we take option 5 from
  84. %% https://www.erlang.org/cgi-bin/ezmlm-cgi?2:mss:1569 in
  85. %% which we read the MsgId as a number, and then convert it
  86. %% back to a binary in order to work around bugs in
  87. %% Erlang's GC.
  88. <<MsgIdNum:?MSG_ID_SIZE_BITS, Msg/binary>> =
  89. <<MsgIdAndMsg:Size/binary>>,
  90. <<MsgId:?MSG_ID_SIZE_BYTES/binary>> =
  91. <<MsgIdNum:?MSG_ID_SIZE_BITS>>,
  92. scanner(Rest, Offset + TotalSize, Fun,
  93. Fun({MsgId, TotalSize, Offset, Msg}, Acc));
  94. _ ->
  95. scanner(Rest, Offset + TotalSize, Fun, Acc)
  96. end;
  97. scanner(Data, Offset, _Fun, Acc) ->
  98. {Data, Acc, Offset}.