/
JournalBase.java
217 lines (168 loc) · 7.3 KB
/
JournalBase.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
209
210
211
212
213
214
215
216
217
/*
* Copyright 2005-2014 Red Hat, Inc.
* Red Hat 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.hornetq.core.journal.impl;
import org.hornetq.api.core.HornetQBuffer;
import org.hornetq.core.journal.EncodingSupport;
import org.hornetq.core.journal.IOCompletion;
import org.hornetq.core.journal.Journal;
import org.hornetq.core.journal.impl.dataformat.ByteArrayEncoding;
abstract class JournalBase implements Journal
{
protected final int fileSize;
private final boolean supportsCallback;
public JournalBase(boolean supportsCallback, int fileSize)
{
if (fileSize < JournalImpl.MIN_FILE_SIZE)
{
throw new IllegalArgumentException("File size cannot be less than " + JournalImpl.MIN_FILE_SIZE + " bytes");
}
this.supportsCallback = supportsCallback;
this.fileSize = fileSize;
}
public abstract void appendAddRecord(final long id, final byte recordType, final EncodingSupport record,
final boolean sync, final IOCompletion callback) throws Exception;
public abstract void appendAddRecordTransactional(final long txID, final long id, final byte recordType,
final EncodingSupport record) throws Exception;
public abstract void appendCommitRecord(final long txID, final boolean sync, final IOCompletion callback,
boolean lineUpContext) throws Exception;
public abstract void appendDeleteRecord(final long id, final boolean sync, final IOCompletion callback) throws Exception;
public abstract void appendDeleteRecordTransactional(final long txID, final long id, final EncodingSupport record) throws Exception;
public abstract void appendPrepareRecord(final long txID, final EncodingSupport transactionData, final boolean sync,
final IOCompletion callback) throws Exception;
public abstract void appendUpdateRecord(final long id, final byte recordType, final EncodingSupport record,
final boolean sync, final IOCompletion callback) throws Exception;
public abstract void appendUpdateRecordTransactional(final long txID, final long id, final byte recordType,
final EncodingSupport record) throws Exception;
public abstract void appendRollbackRecord(final long txID, final boolean sync, final IOCompletion callback) throws Exception;
public void appendAddRecord(long id, byte recordType, byte[] record, boolean sync) throws Exception
{
appendAddRecord(id, recordType, new ByteArrayEncoding(record), sync);
}
public void appendAddRecord(long id, byte recordType, EncodingSupport record, boolean sync) throws Exception
{
SyncIOCompletion callback = getSyncCallback(sync);
appendAddRecord(id, recordType, record, sync, callback);
if (callback != null)
{
callback.waitCompletion();
}
}
public void appendCommitRecord(final long txID, final boolean sync) throws Exception
{
SyncIOCompletion syncCompletion = getSyncCallback(sync);
appendCommitRecord(txID, sync, syncCompletion, true);
if (syncCompletion != null)
{
syncCompletion.waitCompletion();
}
}
public void appendCommitRecord(final long txID, final boolean sync, final IOCompletion callback) throws Exception
{
appendCommitRecord(txID, sync, callback, true);
}
public void appendUpdateRecord(final long id, final byte recordType, final byte[] record, final boolean sync) throws Exception
{
appendUpdateRecord(id, recordType, new ByteArrayEncoding(record), sync);
}
public void appendUpdateRecordTransactional(final long txID, final long id, final byte recordType,
final byte[] record) throws Exception
{
appendUpdateRecordTransactional(txID, id, recordType, new ByteArrayEncoding(record));
}
public void appendAddRecordTransactional(final long txID, final long id, final byte recordType, final byte[] record) throws Exception
{
appendAddRecordTransactional(txID, id, recordType, new ByteArrayEncoding(record));
}
public void appendDeleteRecordTransactional(final long txID, final long id) throws Exception
{
appendDeleteRecordTransactional(txID, id, NullEncoding.instance);
}
public void appendPrepareRecord(final long txID, final byte[] transactionData, final boolean sync) throws Exception
{
appendPrepareRecord(txID, new ByteArrayEncoding(transactionData), sync);
}
public void appendPrepareRecord(final long txID, final EncodingSupport transactionData, final boolean sync) throws Exception
{
SyncIOCompletion syncCompletion = getSyncCallback(sync);
appendPrepareRecord(txID, transactionData, sync, syncCompletion);
if (syncCompletion != null)
{
syncCompletion.waitCompletion();
}
}
public void appendDeleteRecordTransactional(final long txID, final long id, final byte[] record) throws Exception
{
appendDeleteRecordTransactional(txID, id, new ByteArrayEncoding(record));
}
public void
appendUpdateRecord(final long id, final byte recordType, final EncodingSupport record, final boolean sync) throws Exception
{
SyncIOCompletion callback = getSyncCallback(sync);
appendUpdateRecord(id, recordType, record, sync, callback);
if (callback != null)
{
callback.waitCompletion();
}
}
public void appendRollbackRecord(final long txID, final boolean sync) throws Exception
{
SyncIOCompletion syncCompletion = getSyncCallback(sync);
appendRollbackRecord(txID, sync, syncCompletion);
if (syncCompletion != null)
{
syncCompletion.waitCompletion();
}
}
public void appendDeleteRecord(final long id, final boolean sync) throws Exception
{
SyncIOCompletion callback = getSyncCallback(sync);
appendDeleteRecord(id, sync, callback);
if (callback != null)
{
callback.waitCompletion();
}
}
abstract void scheduleReclaim();
protected SyncIOCompletion getSyncCallback(final boolean sync)
{
if (supportsCallback)
{
if (sync)
{
return new SimpleWaitIOCallback();
}
return DummyCallback.getInstance();
}
return null;
}
private static final class NullEncoding implements EncodingSupport
{
private static NullEncoding instance = new NullEncoding();
public void decode(final HornetQBuffer buffer)
{
// no-op
}
public void encode(final HornetQBuffer buffer)
{
// no-op
}
public int getEncodeSize()
{
return 0;
}
}
public int getFileSize()
{
return fileSize;
}
}