/
OClusterPositionMap.java
executable file
·444 lines (373 loc) · 13.8 KB
/
OClusterPositionMap.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
*
* * Copyright 2014 Orient Technologies LTD (info(at)orientechnologies.com)
* *
* * Licensed 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.
* *
* * For more information: http://www.orientechnologies.com
*
*/
package com.orientechnologies.orient.core.storage.impl.local.paginated;
import com.orientechnologies.orient.core.exception.OStorageException;
import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.index.hashindex.local.cache.OCacheEntry;
import com.orientechnologies.orient.core.index.hashindex.local.cache.ODiskCache;
import com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage;
import com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation;
import com.orientechnologies.orient.core.storage.impl.local.paginated.base.ODurableComponent;
import com.orientechnologies.orient.core.storage.impl.local.paginated.base.ODurablePage;
import java.io.IOException;
import java.util.Arrays;
/**
* @author Andrey Lomakin (a.lomakin-at-orientechnologies.com)
* @since 10/7/13
*/
public class OClusterPositionMap extends ODurableComponent {
public static final String DEF_EXTENSION = ".cpm";
private final ODiskCache diskCache;
private String name;
private long fileId;
private boolean useWal;
public OClusterPositionMap(OAbstractPaginatedStorage storage, ODiskCache diskCache, String name, boolean useWal) {
acquireExclusiveLock();
try {
this.diskCache = diskCache;
this.name = name;
this.useWal = useWal;
init(storage);
} finally {
releaseExclusiveLock();
}
}
public void setUseWal(boolean useWal) {
acquireExclusiveLock();
try {
this.useWal = useWal;
} finally {
releaseExclusiveLock();
}
}
public void open() throws IOException {
acquireExclusiveLock();
try {
fileId = diskCache.openFile(name + DEF_EXTENSION);
} finally {
releaseExclusiveLock();
}
}
public void create() throws IOException {
acquireExclusiveLock();
try {
fileId = diskCache.addFile(name + DEF_EXTENSION);
} finally {
releaseExclusiveLock();
}
}
public void flush() throws IOException {
acquireSharedLock();
try {
diskCache.flushFile(fileId);
} finally {
releaseSharedLock();
}
}
public void close(boolean flush) throws IOException {
acquireExclusiveLock();
try {
diskCache.closeFile(fileId, flush);
} finally {
releaseExclusiveLock();
}
}
public void truncate() throws IOException {
acquireExclusiveLock();
try {
diskCache.truncateFile(fileId);
} finally {
releaseExclusiveLock();
}
}
public void delete() throws IOException {
acquireExclusiveLock();
try {
diskCache.deleteFile(fileId);
} finally {
releaseExclusiveLock();
}
}
public void rename(String newName) throws IOException {
acquireExclusiveLock();
try {
diskCache.renameFile(fileId, this.name + DEF_EXTENSION, newName + DEF_EXTENSION);
name = newName;
} finally {
releaseExclusiveLock();
}
}
public long add(long pageIndex, int recordPosition) throws IOException {
acquireExclusiveLock();
try {
OAtomicOperation atomicOperation = storage.getAtomicOperationsManager().getCurrentOperation();
long lastPage = getFilledUpTo(atomicOperation, diskCache, fileId) - 1;
OCacheEntry cacheEntry = diskCache.load(fileId, lastPage, false);
if (cacheEntry == null)
cacheEntry = diskCache.allocateNewPage(fileId);
cacheEntry.acquireExclusiveLock();
try {
atomicOperation = startAtomicOperation();
OClusterPositionMapBucket bucket = new OClusterPositionMapBucket(cacheEntry, getChangesTree(atomicOperation, cacheEntry));
if (bucket.isFull()) {
cacheEntry.releaseExclusiveLock();
diskCache.release(cacheEntry);
cacheEntry = diskCache.allocateNewPage(fileId);
cacheEntry.acquireExclusiveLock();
bucket = new OClusterPositionMapBucket(cacheEntry, getChangesTree(atomicOperation, cacheEntry));
}
final long index = bucket.add(pageIndex, recordPosition);
final long result = index + cacheEntry.getPageIndex() * OClusterPositionMapBucket.MAX_ENTRIES;
endAtomicOperation(false);
return result;
} catch (Throwable e) {
endAtomicOperation(true);
throw new OStorageException("Error during creation of mapping between logical adn physical record position.", e);
} finally {
cacheEntry.releaseExclusiveLock();
diskCache.release(cacheEntry);
}
} finally {
releaseExclusiveLock();
}
}
public OClusterPositionMapBucket.PositionEntry get(final long clusterPosition) throws IOException {
acquireSharedLock();
try {
long pageIndex = clusterPosition / OClusterPositionMapBucket.MAX_ENTRIES;
int index = (int) (clusterPosition % OClusterPositionMapBucket.MAX_ENTRIES);
final OAtomicOperation atomicOperation = storage.getAtomicOperationsManager().getCurrentOperation();
if (pageIndex >= getFilledUpTo(atomicOperation, diskCache, fileId))
return null;
final OCacheEntry cacheEntry = diskCache.load(fileId, pageIndex, false);
try {
final OClusterPositionMapBucket bucket = new OClusterPositionMapBucket(cacheEntry, getChangesTree(atomicOperation,
cacheEntry));
return bucket.get(index);
} finally {
diskCache.release(cacheEntry);
}
} finally {
releaseSharedLock();
}
}
public OClusterPositionMapBucket.PositionEntry remove(final long clusterPosition) throws IOException {
acquireExclusiveLock();
try {
long pageIndex = clusterPosition / OClusterPositionMapBucket.MAX_ENTRIES;
int index = (int) (clusterPosition % OClusterPositionMapBucket.MAX_ENTRIES);
final OCacheEntry cacheEntry = diskCache.load(fileId, pageIndex, false);
cacheEntry.acquireExclusiveLock();
try {
final OAtomicOperation atomicOperation = startAtomicOperation();
final OClusterPositionMapBucket bucket = new OClusterPositionMapBucket(cacheEntry, getChangesTree(atomicOperation,
cacheEntry));
OClusterPositionMapBucket.PositionEntry positionEntry = bucket.remove(index);
if (positionEntry == null)
return null;
endAtomicOperation(false);
return positionEntry;
} catch (Throwable e) {
endAtomicOperation(true);
throw new OStorageException("Error during removal of mapping between logical and physical record position.", e);
} finally {
cacheEntry.releaseExclusiveLock();
diskCache.release(cacheEntry);
}
} finally {
releaseExclusiveLock();
}
}
public long[] higherPositions(final long clusterPosition) throws IOException {
acquireSharedLock();
try {
if (clusterPosition == Long.MAX_VALUE)
return new long[0];
return ceilingPositions(clusterPosition + 1);
} finally {
releaseSharedLock();
}
}
public long[] ceilingPositions(long clusterPosition) throws IOException {
acquireSharedLock();
try {
if (clusterPosition < 0)
clusterPosition = 0;
long pageIndex = clusterPosition / OClusterPositionMapBucket.MAX_ENTRIES;
int index = (int) (clusterPosition % OClusterPositionMapBucket.MAX_ENTRIES);
OAtomicOperation atomicOperation = storage.getAtomicOperationsManager().getCurrentOperation();
final long filledUpTo = getFilledUpTo(atomicOperation, diskCache, fileId);
if (pageIndex >= filledUpTo)
return new long[0];
long[] result = null;
do {
OCacheEntry cacheEntry = diskCache.load(fileId, pageIndex, false);
OClusterPositionMapBucket bucket = new OClusterPositionMapBucket(cacheEntry, getChangesTree(atomicOperation, cacheEntry));
int resultSize = bucket.getSize() - index;
if (resultSize <= 0) {
diskCache.release(cacheEntry);
pageIndex++;
index = 0;
} else {
int entriesCount = 0;
long startIndex = cacheEntry.getPageIndex() * OClusterPositionMapBucket.MAX_ENTRIES + index;
result = new long[resultSize];
for (int i = 0; i < resultSize; i++) {
if (bucket.exists(i + index)) {
result[entriesCount] = startIndex + i;
entriesCount++;
}
}
if (entriesCount == 0) {
result = null;
pageIndex++;
index = 0;
} else
result = Arrays.copyOf(result, entriesCount);
diskCache.release(cacheEntry);
}
} while (result == null && pageIndex < filledUpTo);
if (result == null)
result = new long[0];
return result;
} finally {
releaseSharedLock();
}
}
public long[] lowerPositions(final long clusterPosition) throws IOException {
acquireSharedLock();
try {
if (clusterPosition == 0)
return new long[0];
return floorPositions(clusterPosition - 1);
} finally {
releaseSharedLock();
}
}
public long[] floorPositions(final long clusterPosition) throws IOException {
acquireSharedLock();
try {
if (clusterPosition < 0)
return new long[0];
long pageIndex = clusterPosition / OClusterPositionMapBucket.MAX_ENTRIES;
int index = (int) (clusterPosition % OClusterPositionMapBucket.MAX_ENTRIES);
OAtomicOperation atomicOperation = storage.getAtomicOperationsManager().getCurrentOperation();
final long filledUpTo = getFilledUpTo(atomicOperation, diskCache, fileId);
long[] result;
if (pageIndex >= filledUpTo) {
pageIndex = filledUpTo - 1;
index = Integer.MIN_VALUE;
}
do {
OCacheEntry cacheEntry = diskCache.load(fileId, pageIndex, false);
OClusterPositionMapBucket bucket = new OClusterPositionMapBucket(cacheEntry, getChangesTree(atomicOperation, cacheEntry));
if (index == Integer.MIN_VALUE)
index = bucket.getSize() - 1;
int resultSize = index + 1;
int entriesCount = 0;
long startPosition = cacheEntry.getPageIndex() * OClusterPositionMapBucket.MAX_ENTRIES;
result = new long[resultSize];
for (int i = 0; i < resultSize; i++) {
if (bucket.exists(i)) {
result[entriesCount] = startPosition + i;
entriesCount++;
}
}
if (entriesCount == 0) {
result = null;
pageIndex--;
index = Integer.MIN_VALUE;
} else
result = Arrays.copyOf(result, entriesCount);
diskCache.release(cacheEntry);
} while (result == null && pageIndex >= 0);
if (result == null)
result = new long[0];
return result;
} finally {
releaseSharedLock();
}
}
public long getFirstPosition() throws IOException {
acquireSharedLock();
try {
OAtomicOperation atomicOperation = storage.getAtomicOperationsManager().getCurrentOperation();
final long filledUpTo = getFilledUpTo(atomicOperation, diskCache, fileId);
for (long pageIndex = 0; pageIndex < filledUpTo; pageIndex++) {
OCacheEntry cacheEntry = diskCache.load(fileId, pageIndex, false);
try {
OClusterPositionMapBucket bucket = new OClusterPositionMapBucket(cacheEntry, getChangesTree(atomicOperation, cacheEntry));
int bucketSize = bucket.getSize();
for (int index = 0; index < bucketSize; index++) {
if (bucket.exists(index))
return pageIndex * OClusterPositionMapBucket.MAX_ENTRIES + index;
}
} finally {
diskCache.release(cacheEntry);
}
}
return ORID.CLUSTER_POS_INVALID;
} finally {
releaseSharedLock();
}
}
public long getLastPosition() throws IOException {
acquireSharedLock();
try {
OAtomicOperation atomicOperation = storage.getAtomicOperationsManager().getCurrentOperation();
final long filledUpTo = getFilledUpTo(atomicOperation, diskCache, fileId);
for (long pageIndex = filledUpTo - 1; pageIndex >= 0; pageIndex--) {
OCacheEntry cacheEntry = diskCache.load(fileId, pageIndex, false);
try {
OClusterPositionMapBucket bucket = new OClusterPositionMapBucket(cacheEntry, getChangesTree(atomicOperation, cacheEntry));
final int bucketSize = bucket.getSize();
for (int index = bucketSize - 1; index >= 0; index--) {
if (bucket.exists(index))
return pageIndex * OClusterPositionMapBucket.MAX_ENTRIES + index;
}
} finally {
diskCache.release(cacheEntry);
}
}
return ORID.CLUSTER_POS_INVALID;
} finally {
releaseSharedLock();
}
}
public boolean wasSoftlyClosed() throws IOException {
acquireSharedLock();
try {
return diskCache.wasSoftlyClosed(fileId);
} finally {
releaseSharedLock();
}
}
@Override
protected void endAtomicOperation(final boolean rollback) throws IOException {
if (useWal)
super.endAtomicOperation(rollback);
}
@Override
protected OAtomicOperation startAtomicOperation() throws IOException {
if (useWal)
return super.startAtomicOperation();
return null;
}
}