檢視原始碼 wrap_log_reader (kernel v10.2)

一個用於讀取內部格式化環狀磁碟日誌的服務。

此模組使得讀取內部格式化環狀磁碟日誌成為可能,請參閱 disk_logwrap_log_reader 不會干擾 disk_log 的活動;然而,這個版本的 wrap_log_reader 中存在一個錯誤,請參閱已知限制章節。

一個環狀磁碟日誌檔案由許多稱為索引檔案的檔案組成。一個日誌檔案可以開啟和關閉。此外,單一索引檔案可以單獨開啟。如果開啟不存在或非內部格式化的檔案,則會返回錯誤訊息。如果檔案損壞,則不會嘗試修復它,但會返回錯誤訊息。

如果日誌配置為分散式,則可能不是所有項目都記錄在所有節點上。wrap_log_reader 只會讀取被呼叫節點上的日誌;使用者必須確保讀取所有項目。

已知限制

這個版本的 wrap_log_reader 無法偵測在呼叫 wrap_log_reader:open/1 和第一次呼叫 wrap_log_reader:chunk/1 之間,disk_log 是否換到新的索引檔案。如果發生這種情況,呼叫 chunk/1 會讀取日誌檔案中最後記錄的項目,因為開啟的索引檔案已被 disk_log 截斷。

摘要

型別

open/1,2chunk/1,2 返回的 Continuation。

函式

可以有效率地讀取附加到日誌中的項目。透過從檔案中讀取 64 KB 的區塊來最小化磁碟 I/O。

正確關閉日誌檔案。

等同於 open(Filename, ...),但會讀取整個環狀日誌檔案。

Filename 指定要讀取的檔案名稱。

型別

-type chunk_ret() ::
          {Continuation2 :: term(), Terms :: [term()]} |
          {Continuation2 :: term(), Terms :: [term()], Badbytes :: non_neg_integer()} |
          {Continuation2 :: term(), eof} |
          {error, Reason :: term()}.
-opaque continuation()

open/1,2chunk/1,2 返回的 Continuation。

-type open_ret() :: {ok, Continuation :: continuation()} | {error, Reason :: tuple()}.

函式

-spec chunk(Continuation) -> chunk_ret() when Continuation :: continuation().

等同於 chunk(Continuation, infinity)

此函式的連結

chunk(Continuation, N)

檢視原始碼
-spec chunk(Continuation, N) -> chunk_ret()
               when Continuation :: continuation(), N :: infinity | pos_integer().

可以有效率地讀取附加到日誌中的項目。透過從檔案中讀取 64 KB 的區塊來最小化磁碟 I/O。

第一次呼叫 chunk/2 時,必須提供從 open/1open/2 返回的初始 Continuation。

當呼叫 chunk/2 時,N 控制每次從日誌讀取的最大項目數量。infinity 表示讀取包含在 8KB 區塊中的所有項目。如果返回的項目少於 N 個,並不一定表示已到達檔案結尾。

返回一個元組 {Continuation2, Terms},其中 Terms 是在日誌中找到的項目列表。Continuation2 是另一個 Continuation,必須傳遞給後續任何對 chunk() 的呼叫。透過一系列對 chunk() 的呼叫,就可以從日誌中提取所有項目。

如果日誌以唯讀模式開啟,且讀取的區塊損壞,則返回元組 {Continuation2, Terms, Badbytes}Badbytes 表示在區塊中找到的非 Erlang 項目的數量。請注意,日誌並未修復。

當到達日誌結尾時,返回 {Continuation2, eof},如果發生錯誤則返回 {error, Reason}

返回的 Continuation 在下次呼叫此函式時可能有效也可能無效。這是因為日誌可以循環並刪除 Continuation 指向的檔案。為了確保不會發生這種情況,可以在搜尋期間封鎖日誌。

-spec close(Continuation) -> ok | {error, Reason}
               when Continuation :: continuation(), Reason :: file:posix().

正確關閉日誌檔案。

-spec open(Filename) -> open_ret() when Filename :: string() | atom().

等同於 open(Filename, ...),但會讀取整個環狀日誌檔案。

-spec open(Filename, N) -> open_ret() when Filename :: string() | atom(), N :: integer().

Filename 指定要讀取的檔案名稱。

N 指定要讀取的檔案的索引。使用 open/1 來讀取整個環狀日誌。

如果成功開啟日誌/索引檔案,則返回 {ok, Continuation}Continuation 用於分塊或關閉檔案時。

對於所有錯誤,返回 {error, Reason}