1919package org .apache .bookkeeper .client ;
2020
2121import com .google .common .annotations .VisibleForTesting ;
22- import com .google .common .collect .Lists ;
2322import io .netty .buffer .ByteBuf ;
2423import io .netty .buffer .Unpooled ;
2524import java .security .GeneralSecurityException ;
2625import java .util .ArrayDeque ;
2726import java .util .ArrayList ;
2827import java .util .Arrays ;
28+ import java .util .Collections ;
2929import java .util .Enumeration ;
3030import java .util .List ;
3131import java .util .Queue ;
3232import java .util .concurrent .CompletableFuture ;
33+ import java .util .concurrent .atomic .AtomicLong ;
3334import lombok .Getter ;
3435import org .apache .bookkeeper .client .AsyncCallback .AddCallback ;
3536import org .apache .bookkeeper .client .AsyncCallback .CloseCallback ;
5354 */
5455public class PulsarMockLedgerHandle extends LedgerHandle {
5556
56- final ArrayList <LedgerEntryImpl > entries = Lists . newArrayList ( );
57+ final List <LedgerEntryImpl > entries = Collections . synchronizedList ( new ArrayList <>() );
5758 final PulsarMockBookKeeper bk ;
5859 final long id ;
5960 final DigestType digest ;
@@ -63,6 +64,8 @@ public class PulsarMockLedgerHandle extends LedgerHandle {
6364 @ VisibleForTesting
6465 @ Getter
6566 boolean fenced = false ;
67+ // Count for total length of the entries
68+ final AtomicLong totalLengthCounter = new AtomicLong (0 );
6669
6770 public PulsarMockLedgerHandle (PulsarMockBookKeeper bk , long id ,
6871 DigestType digest , byte [] passwd ) throws GeneralSecurityException {
@@ -73,7 +76,8 @@ public PulsarMockLedgerHandle(PulsarMockBookKeeper bk, long id,
7376 this .digest = digest ;
7477 this .passwd = Arrays .copyOf (passwd , passwd .length );
7578
76- readHandle = new PulsarMockReadHandle (bk , id , getLedgerMetadata (), entries , bk ::getReadHandleInterceptor );
79+ readHandle = new PulsarMockReadHandle (bk , id , getLedgerMetadata (), entries ,
80+ bk ::getReadHandleInterceptor , totalLengthCounter );
7781 }
7882
7983 @ Override
@@ -159,6 +163,7 @@ public long addEntry(byte[] data) throws InterruptedException, BKException {
159163 }
160164
161165 lastEntry = entries .size ();
166+ totalLengthCounter .addAndGet (data .length );
162167 entries .add (LedgerEntryImpl .create (ledgerId , lastEntry , data .length , Unpooled .wrappedBuffer (data )));
163168 return lastEntry ;
164169 }
@@ -191,6 +196,7 @@ public void asyncAddEntry(final ByteBuf data, final AddCallback cb, final Object
191196 lastEntry = entries .size ();
192197 byte [] storedData = new byte [data .readableBytes ()];
193198 data .readBytes (storedData );
199+ totalLengthCounter .addAndGet (storedData .length );
194200 entries .add (LedgerEntryImpl .create (ledgerId , lastEntry ,
195201 storedData .length , Unpooled .wrappedBuffer (storedData )));
196202 return FutureUtils .value (lastEntry );
@@ -231,12 +237,7 @@ public long getLastAddConfirmed() {
231237
232238 @ Override
233239 public long getLength () {
234- long length = 0 ;
235- for (LedgerEntryImpl entry : entries ) {
236- length += entry .getLength ();
237- }
238-
239- return length ;
240+ return totalLengthCounter .get ();
240241 }
241242
242243
0 commit comments