Repository navigation
Expand file tree
/
Copy pathEngine.java
More file actions
623 lines (510 loc) · 19.3 KB
/
Copy pathEngine.java
File metadata and controls
623 lines (510 loc) · 19.3 KB
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*
* Copyright (c) 2012 Jan Kotek
*
* 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.
*/
package org.mapdb;
import java.io.Closeable;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* <p>
* Centerpiece for record management, {@code Engine} is simple key value store.
* Engine is low-level interface and is not meant to be used directly
* by user. For most operations user should use {@link org.mapdb.DB} class.
* </p><p>
*
* In this store key is primitive {@code long} number, typically pointer to index table.
* Value is class instance. To turn value into/from binary form serializer is
* required as extra argument for most operations.
* </p><p>
*
* Unlike other DBs MapDB does not expect user to (de)serialize data before
* they are passed as arguments. Instead MapDB controls (de)serialization itself.
* This gives DB a lot of flexibility: for example instances may be held in
* cache to minimise number of deserializations, or modified instance can
* be placed into queue and asynchronously written on background thread.
* </p><p>
*
* There is {@link Store} subinterface for raw persistence
* </p><p>
*
* In default configuration MapDB runs with this {@code Engine} stack:
* </p>
*
* <ol>
* <li> <b>DISK</b> - raw file or memory
* <li> {@link org.mapdb.StoreWAL} - permanent record store with transactions
* <li> <b>USER</b> - {@link DB} and collections
* </ol>
*
* <p>
* TODO Engine Wrappers are sort of obsole, update this whole section
* </p><p>
*
* Engine uses {@code recid} to identify records. There is zero error handling in case recid is invalid
* (random number or already deleted record). Passing illegal recid may result into anything
* (return null, throw EOF or even corrupt store). Engine is considered low-level component
* and it is responsibility of upper layers (collections) to ensure recid is consistent.
* Lack of error handling is trade of for speed (similar way as manual memory management in C++)
* </p><p>
*
* Engine must support {@code null} record values. You may insert, update and fetch null records.
* Nulls play important role in recid preallocation and asynchronous writes.
* </p><p>
* Recid can be reused after it was deleted. If your application relies on unique being unique,
* you should update record with null value, instead of delete.
* Null record consumes only 8 bytes in store and is preserved during defragmentation.
* </p>
* @author Jan Kotek
*/
public interface Engine extends Closeable {
/**
* <p>
* Content of this map is manipulated by {@link org.mapdb.DB} class.
* </p>
* There are 8 reserved record ids. They store information relevant to
* {@link org.mapdb.DB} and higher level functions. Those are preallocated when store is created.
*/
long RECID_NAME_CATALOG = 1;
/**
* <p>
* Points to class catalog. A list of classes used in {@link org.mapdb.SerializerPojo}
* to serialize java objects.
* </p>
* There are 8 reserved record ids. They store information relevant to
* {@link org.mapdb.DB} and higher level functions. Those are preallocated when store is created.
*/
long RECID_CLASS_CATALOG = 2;
/**
* <p>
* Recid used for 'record check'. This record is loaded when store is open,
* to ensure configuration such as encryption and compression is correctly set and \
* data are read-able.
* </p><p>
* There are 8 reserved record ids. They store information relevant to
* {@link org.mapdb.DB} and higher level functions. Those are preallocated when store is created.
* </p>
*/
long RECID_RECORD_CHECK = 3;
/**
* <p>
* There are 8 reserved record ids. They store information relevant to
* {@link org.mapdb.DB} and higher level functions. Those are preallocated when store is created.
* </p><p>
* This value is last reserved record id. User ids (recids returned by {@link Engine#put(Object, Serializer)})
* starts from {@code RECID_LAST_RESERVED+1}
* </p>
*/
long RECID_LAST_RESERVED = 7;
/**
* <p>
* There are 8 reserved record ids. They store information relevant to
* {@link org.mapdb.DB} and higher level functions. Those are preallocated when store is created.
* </p><p>
* This constant is first recid available to user. It is first value returned by {@link #put(Object, Serializer)} if store is empty.
* </p>
*/
long RECID_FIRST = RECID_LAST_RESERVED+1;
/**
* Preallocates recid for not yet created record. It does not insert any data into it.
* @return new recid
*/
//TODO in some cases recid is persisted and used between compaction. perhaps use put(null). Much latter: in what cases? I do not recall any.
//TODO clarify difference between put/update(null) and delete/preallocate
long preallocate();
/**
* Insert new record.
*
* @param value records to be added
* @param serializer used to convert record into/from binary form
* @return recid (record identifier) under which record is stored.
* @throws java.lang.NullPointerException if serializer is null
*/
<A> long put(A value, Serializer<A> serializer);
/**
* <p>
* Get existing record.
* </p><p>
*
* Recid must be a number returned by 'put' method.
* Behaviour for invalid recid (random number or already deleted record)
* is not defined, typically it returns null or throws 'EndOfFileException'
* </p>
*
* @param recid (record identifier) under which record was persisted
* @param serializer used to deserialize record from binary form
* @return record matching given recid, or null if record is not found under given recid.
* @throws java.lang.NullPointerException if serializer is null
*/
<A> A get(long recid, Serializer<A> serializer);
/**
* <p>
* Update existing record with new value.
* </p><p>
* Recid must be a number returned by 'put' method.
* Behaviour for invalid recid (random number or already deleted record)
* is not defined, typically it throws 'EndOfFileException',
* but it may also corrupt store.
* </p>
*
* @param recid (record identifier) under which record was persisted.
* @param value new record value to be stored
* @param serializer used to serialize record into binary form
* @throws java.lang.NullPointerException if serializer is null
*/
<A> void update(long recid, A value, Serializer<A> serializer);
/**
* <p>
* Updates existing record in atomic <a href="http://en.wikipedia.org/wiki/Compare-and-swap">(Compare And Swap)</a> manner.
* Value is modified only if old value matches expected value. There are three ways to match values, MapDB may use any of them:
* </p>
* <ol>
* <li>Equality check <code>oldValue==expectedOldValue</code> when old value is found in instance cache</li>
* <li>Deserializing <code>oldValue</code> using <code>serializer</code> and checking <code>oldValue.equals(expectedOldValue)</code></li>
* <li>Serializing <code>expectedOldValue</code> using <code>serializer </code> and comparing binary array with already serialized <code>oldValue</code>
* </ol>
* <p>
* Recid must be a number returned by 'put' method.
* Behaviour for invalid recid (random number or already deleted record)
* is not defined, typically it throws 'EndOfFileException',
* but it may also corrupt store.
* </p>
*
* @param recid (record identifier) under which record was persisted.
* @param expectedOldValue old value to be compared with existing record
* @param newValue to be written if values are matching
* @param serializer used to serialize record into binary form
* @return true if values matched and newValue was written
* @throws java.lang.NullPointerException if serializer is null
*/
<A> boolean compareAndSwap(long recid, A expectedOldValue, A newValue, Serializer<A> serializer);
/**
* <p>
* Remove existing record from store/cache
* </p><p>
*
* Recid must be a number returned by 'put' method.
* Behaviour for invalid recid (random number or already deleted record)
* is not defined, typically it throws 'EndOfFileException',
* but it may also corrupt store.
* </p>
*
* @param recid (record identifier) under which was record persisted
* @param serializer which may be used in some circumstances to deserialize and store old object
* @throws java.lang.NullPointerException if serializer is null
*/
<A> void delete(long recid, Serializer<A> serializer);
/**
* <p>
* Close store/cache. This method must be called before JVM exits to flush all caches and prevent store corruption.
* Also it releases resources used by MapDB (disk, memory..).
* </p><p>
*
* Engine can no longer be used after this method was called. If Engine is used after closing, it may
* throw any exception including <code>NullPointerException</code>
* </p><p>
*
* There is an configuration option {@link DBMaker.Maker#closeOnJvmShutdown()} which uses shutdown hook to automatically
* close Engine when JVM shutdowns.
* </p>
*/
void close();
/**
* Checks whether Engine was closed.
*
* @return true if engine was closed
*/
public boolean isClosed();
/**
* Makes all changes made since the previous commit/rollback permanent.
* In transactional mode (on by default) it means creating journal file and replaying it to storage.
* In other modes it may flush disk caches or do nothing at all (check your config options)
*/
void commit();
/**
* Undoes all changes made in the current transaction.
* If transactions are disabled it throws {@link UnsupportedOperationException}.
*
* @throws UnsupportedOperationException if transactions are disabled
*/
void rollback() throws UnsupportedOperationException;
/**
* Check if you can write into this Engine. It may be readonly in some cases (snapshot, read-only files).
*
* @return true if engine is read-only
*/
boolean isReadOnly();
/** @return true if engine supports rollback*/
boolean canRollback();
/** @return true if engine can create read-only snapshots*/
boolean canSnapshot();
/**
* Returns read-only snapshot of data in Engine.
*
* @throws UnsupportedOperationException if snapshots are not supported/enabled
*/
Engine snapshot() throws UnsupportedOperationException;
/** if this is wrapper return underlying engine, or null */
Engine getWrappedEngine();
/** clears any underlying cache */
void clearCache();
void compact();
abstract class ReadOnly implements Engine{
@Override
public long preallocate() {
throw new UnsupportedOperationException("Read-only");
}
@Override
public <A> boolean compareAndSwap(long recid, A expectedOldValue, A newValue, Serializer<A> serializer) {
throw new UnsupportedOperationException("Read-only");
}
@Override
public <A> long put(A value, Serializer<A> serializer) {
throw new UnsupportedOperationException("Read-only");
}
@Override
public void commit() {
throw new UnsupportedOperationException("Read-only");
}
@Override
public void rollback() {
throw new UnsupportedOperationException("Read-only");
}
@Override
public boolean isReadOnly() {
return true;
}
@Override
public <A> void update(long recid, A value, Serializer<A> serializer) {
throw new UnsupportedOperationException("Read-only");
}
@Override
public <A> void delete(long recid, Serializer<A> serializer){
throw new UnsupportedOperationException("Read-only");
}
@Override
public void compact() {
throw new UnsupportedOperationException("Read-only");
}
}
/**
* Wraps an <code>Engine</code> and throws
* <code>UnsupportedOperationException("Read-only")</code>
* on any modification attempt.
*/
final class ReadOnlyWrapper extends ReadOnly{
protected final Engine engine;
public ReadOnlyWrapper(Engine engine){
this.engine = engine;
}
@Override
public <A> A get(long recid, Serializer<A> serializer) {
return engine.get(recid, serializer);
}
@Override
public void close() {
engine.close();
}
@Override
public boolean isClosed() {
return engine.isClosed();
}
@Override
public boolean canRollback() {
return engine.canRollback();
}
@Override
public Engine getWrappedEngine() {
return engine;
}
@Override
public void clearCache() {
engine.clearCache();
}
@Override
public boolean canSnapshot() {
return engine.canSnapshot();
}
@Override
public Engine snapshot() throws UnsupportedOperationException {
return engine.snapshot();
}
}
/**
* Closes Engine on JVM shutdown using shutdown hook: {@link Runtime#addShutdownHook(Thread)}
* If engine was closed by user before JVM shutdown, hook is removed to save memory.
*/
class CloseOnJVMShutdown implements Engine{
final protected AtomicBoolean shutdownHappened = new AtomicBoolean(false);
final Runnable hookRunnable = new Runnable() {
@Override
public void run() {
shutdownHappened.set(true);
CloseOnJVMShutdown.this.hook = null;
if(CloseOnJVMShutdown.this.isClosed())
return;
CloseOnJVMShutdown.this.close();
}
};
protected final Engine engine;
protected Thread hook;
public CloseOnJVMShutdown(Engine engine) {
this.engine = engine;
hook = new Thread(hookRunnable,"MapDB shutdown hook");
Runtime.getRuntime().addShutdownHook(hook);
}
@Override
public long preallocate() {
return engine.preallocate();
}
@Override
public <A> long put(A value, Serializer<A> serializer) {
return engine.put(value,serializer);
}
@Override
public <A> A get(long recid, Serializer<A> serializer) {
return engine.get(recid,serializer);
}
@Override
public <A> void update(long recid, A value, Serializer<A> serializer) {
engine.update(recid,value,serializer);
}
@Override
public <A> boolean compareAndSwap(long recid, A expectedOldValue, A newValue, Serializer<A> serializer) {
return engine.compareAndSwap(recid,expectedOldValue,newValue,serializer);
}
@Override
public <A> void delete(long recid, Serializer<A> serializer) {
engine.delete(recid,serializer);
}
@Override
public void close() {
engine.close();
if(!shutdownHappened.get() && hook!=null){
Runtime.getRuntime().removeShutdownHook(hook);
}
hook = null;
}
@Override
public boolean isClosed() {
return engine.isClosed();
}
@Override
public void commit() {
engine.commit();
}
@Override
public void rollback() throws UnsupportedOperationException {
engine.rollback();
}
@Override
public boolean isReadOnly() {
return engine.isReadOnly();
}
@Override
public boolean canRollback() {
return engine.canRollback();
}
@Override
public boolean canSnapshot() {
return engine.canSnapshot();
}
@Override
public Engine snapshot() throws UnsupportedOperationException {
return engine.snapshot();
}
@Override
public Engine getWrappedEngine() {
return engine;
}
@Override
public void clearCache() {
engine.clearCache();
}
@Override
public void compact() {
engine.compact();
}
}
/** throws {@code IllegalAccessError("already closed")} on all access */
Engine CLOSED_ENGINE = new Engine(){
@Override
public long preallocate() {
throw new IllegalAccessError("already closed");
}
@Override
public <A> long put(A value, Serializer<A> serializer) {
throw new IllegalAccessError("already closed");
}
@Override
public <A> A get(long recid, Serializer<A> serializer) {
throw new IllegalAccessError("already closed");
}
@Override
public <A> void update(long recid, A value, Serializer<A> serializer) {
throw new IllegalAccessError("already closed");
}
@Override
public <A> boolean compareAndSwap(long recid, A expectedOldValue, A newValue, Serializer<A> serializer) {
throw new IllegalAccessError("already closed");
}
@Override
public <A> void delete(long recid, Serializer<A> serializer) {
throw new IllegalAccessError("already closed");
}
@Override
public void close() {
throw new IllegalAccessError("already closed");
}
@Override
public boolean isClosed() {
return true;
}
@Override
public void commit() {
throw new IllegalAccessError("already closed");
}
@Override
public void rollback() throws UnsupportedOperationException {
throw new IllegalAccessError("already closed");
}
@Override
public boolean isReadOnly() {
throw new IllegalAccessError("already closed");
}
@Override
public boolean canRollback() {
throw new IllegalAccessError("already closed");
}
@Override
public boolean canSnapshot() {
throw new IllegalAccessError("already closed");
}
@Override
public Engine snapshot() throws UnsupportedOperationException {
throw new IllegalAccessError("already closed");
}
@Override
public Engine getWrappedEngine() {
throw new IllegalAccessError("already closed");
}
@Override
public void clearCache() {
throw new IllegalAccessError("already closed");
}
@Override
public void compact() {
throw new IllegalAccessError("already closed");
}
};
}