BookKeeper流:实现与应用.pdfVIP

  • 0
  • 0
  • 约5.29千字
  • 约 6页
  • 2026-01-21 发布于北京
  • 举报

1概述

当使用BookKeeperAPI时,应用程序必须将要写入的数据拆分为条目,每个条目

都是一个字节数组。这对于许多应用程序来说是自然的。例如,当使用

BookKeeper进行预写日志记录时,应用程序通常希望写入与命令或事务相对应的修

改。然而,其他一些应用程序可能没有条目的自然边界,并且可能更写入和

字节流。这正是我们在BookKeeper上实现的流API的目的。

流API实现在Streaming包中,并包含两个主要类:

LedgerOutputStream和LedgerInputStream。类名表明了它们的功能。

2写入字节流

类LedgerOutputStream实现了两个构造函数和五个公共方法:

publicLedgerOutputStream(LedgerHandlelh)

其中:

•lh是一个先前创建并打开的账本的句柄。

publicLedgerOutputStream(LedgerHandlelh,intsize)

其中:

•lh是先前创建并打开的分类帐的句柄。

•size是用于写入字节的字节缓冲区的大小,在刷新之前。

关闭流。此调用通过刷新写入缓冲区来关闭流。

publicvoidclose()

该方法没有参数。

刷新流。此调用实际上会刷新写入缓冲区。

publicsynchronizedvoidflush()

没有参数。

写入字节。有三种调用可以将字节写入流中。

publicsynchronizedvoidwrite(byte[]b)

其中:

•b是要写入的字节数组。

publicsynchronizedvoidwrite(byte[]b,intoff,intlen)其中:

1Summary

WhenusingtheBookKeeperAPI,anapplicationhastosplitthedatatowriteintoentries,

eachentrybeingabytearray.Thisisnaturalformanyapplications.Forexample,when

usingBookKeeperforwrite-aheadlogging,anapplicationtypicallywantstowritethe

modificationscorrespondingtoacommandoratransaction.Someotherapplications,

however,mightnothaveanaturalboundaryforentries,andmayprefertowriteandread

streamsofbytes.ThisisexactlythepurposeofthestreamAPIwehaveimplementedontop

ofBookKeeper.

ThestreamAPIisimplementedinthepackageStreaming,anditcontainstwomain

classes:LedgerOutputStreamandLedgerInputStream.Theclassnamesare

indicativeofwhattheydo.

2Writingastreamofbytes

ClassLedgerOutputStreamimplementstwoconstructorsandfivepublicmethods:

publicLedgerOutputStream(LedgerHandlelh)

where:

•lhisaledgerhandleforapreviouslycreatedandopenledger.

publicLedgerOutputStream(LedgerHandlelh,intsize)

where:

•lhisaledgerhandlefor

文档评论(0)

1亿VIP精品文档

相关文档