forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EntryImpl.java
208 lines (182 loc) · 5.89 KB
/
EntryImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.bookkeeper.mledger.impl;
import com.google.common.annotations.VisibleForTesting;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.Recycler;
import io.netty.util.Recycler.Handle;
import io.netty.util.ReferenceCounted;
import org.apache.bookkeeper.client.api.LedgerEntry;
import org.apache.bookkeeper.mledger.Entry;
import org.apache.bookkeeper.mledger.util.AbstractCASReferenceCounted;
public final class EntryImpl extends AbstractCASReferenceCounted implements Entry, Comparable<EntryImpl>,
ReferenceCounted {
private static final Recycler<EntryImpl> RECYCLER = new Recycler<EntryImpl>() {
@Override
protected EntryImpl newObject(Handle<EntryImpl> handle) {
return new EntryImpl(handle);
}
};
private final Handle<EntryImpl> recyclerHandle;
private long timestamp;
private long ledgerId;
private long entryId;
private PositionImpl position;
ByteBuf data;
private Runnable onDeallocate;
public static EntryImpl create(LedgerEntry ledgerEntry) {
EntryImpl entry = RECYCLER.get();
entry.timestamp = System.nanoTime();
entry.ledgerId = ledgerEntry.getLedgerId();
entry.entryId = ledgerEntry.getEntryId();
entry.data = ledgerEntry.getEntryBuffer();
entry.data.retain();
entry.setRefCnt(1);
return entry;
}
@VisibleForTesting
public static EntryImpl create(long ledgerId, long entryId, byte[] data) {
EntryImpl entry = RECYCLER.get();
entry.timestamp = System.nanoTime();
entry.ledgerId = ledgerId;
entry.entryId = entryId;
entry.data = Unpooled.wrappedBuffer(data);
entry.setRefCnt(1);
return entry;
}
public static EntryImpl create(long ledgerId, long entryId, ByteBuf data) {
EntryImpl entry = RECYCLER.get();
entry.timestamp = System.nanoTime();
entry.ledgerId = ledgerId;
entry.entryId = entryId;
entry.data = data;
entry.data.retain();
entry.setRefCnt(1);
return entry;
}
public static EntryImpl create(PositionImpl position, ByteBuf data) {
EntryImpl entry = RECYCLER.get();
entry.timestamp = System.nanoTime();
entry.ledgerId = position.getLedgerId();
entry.entryId = position.getEntryId();
entry.data = data;
entry.data.retain();
entry.setRefCnt(1);
return entry;
}
public static EntryImpl create(EntryImpl other) {
EntryImpl entry = RECYCLER.get();
entry.timestamp = System.nanoTime();
entry.ledgerId = other.ledgerId;
entry.entryId = other.entryId;
entry.data = other.data.retainedDuplicate();
entry.setRefCnt(1);
return entry;
}
private EntryImpl(Recycler.Handle<EntryImpl> recyclerHandle) {
this.recyclerHandle = recyclerHandle;
}
public void onDeallocate(Runnable r) {
if (this.onDeallocate == null) {
this.onDeallocate = r;
} else {
// this is not expected to happen
Runnable previous = this.onDeallocate;
this.onDeallocate = () -> {
try {
previous.run();
} finally {
r.run();
}
};
}
}
public long getTimestamp() {
return timestamp;
}
@Override
public ByteBuf getDataBuffer() {
return data;
}
@Override
public byte[] getData() {
byte[] array = new byte[data.readableBytes()];
data.getBytes(data.readerIndex(), array);
return array;
}
// Only for test
@Override
public byte[] getDataAndRelease() {
byte[] array = getData();
release();
return array;
}
@Override
public int getLength() {
return data.readableBytes();
}
@Override
public PositionImpl getPosition() {
if (position == null) {
position = PositionImpl.get(ledgerId, entryId);
}
return position;
}
@Override
public long getLedgerId() {
return ledgerId;
}
@Override
public long getEntryId() {
return entryId;
}
@Override
public int compareTo(EntryImpl other) {
if (this.ledgerId != other.ledgerId) {
return this.ledgerId < other.ledgerId ? -1 : 1;
}
if (this.entryId != other.entryId) {
return this.entryId < other.entryId ? -1 : 1;
}
return 0;
}
@Override
public ReferenceCounted touch(Object hint) {
return this;
}
@Override
protected void deallocate() {
// This method is called whenever the ref-count of the EntryImpl reaches 0, so that now we can recycle it
if (onDeallocate != null) {
try {
onDeallocate.run();
} finally {
onDeallocate = null;
}
}
data.release();
data = null;
timestamp = -1;
ledgerId = -1;
entryId = -1;
position = null;
recyclerHandle.recycle(this);
}
}