/
StandardTable.java
837 lines (772 loc) · 29.4 KB
/
StandardTable.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
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
/*
* Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.lealone.db.table;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.lealone.api.DatabaseEventListener;
import org.lealone.api.ErrorCode;
import org.lealone.common.message.DbException;
import org.lealone.common.message.Trace;
import org.lealone.common.util.MathUtils;
import org.lealone.common.util.New;
import org.lealone.common.value.DataType;
import org.lealone.common.value.Value;
import org.lealone.db.Constants;
import org.lealone.db.DbObject;
import org.lealone.db.Session;
import org.lealone.db.SysProperties;
import org.lealone.db.constraint.Constraint;
import org.lealone.db.constraint.ConstraintReferential;
import org.lealone.db.index.Cursor;
import org.lealone.db.index.GlobalUniqueIndex;
import org.lealone.db.index.HashIndex;
import org.lealone.db.index.Index;
import org.lealone.db.index.IndexType;
import org.lealone.db.index.NonUniqueHashIndex;
import org.lealone.db.index.StandardDelegateIndex;
import org.lealone.db.index.StandardIndex;
import org.lealone.db.index.StandardPrimaryIndex;
import org.lealone.db.index.StandardSecondaryIndex;
import org.lealone.db.result.Row;
import org.lealone.db.result.SortOrder;
import org.lealone.db.schema.SchemaObject;
import org.lealone.storage.StorageEngine;
import org.lealone.transaction.Transaction;
public class StandardTable extends TableBase {
private final StandardPrimaryIndex primaryIndex;
private final ArrayList<Index> indexes = New.arrayList();
private long lastModificationId;
private volatile Session lockExclusiveSession;
// using a ConcurrentHashMap as a set
private final ConcurrentHashMap<Session, Session> lockSharedSessions = new ConcurrentHashMap<>();
/**
* The queue of sessions waiting to lock the table. It is a FIFO queue to
* prevent starvation, since Java's synchronized locking is biased.
*/
private final ArrayDeque<Session> waitingSessions = new ArrayDeque<>();
private final Trace traceLock;
private int changesSinceAnalyze;
private int nextAnalyze;
private boolean containsLargeObject;
private Column rowIdColumn;
private final StorageEngine storageEngine;
private final Map<String, String> storageEngineParams;
private final String mapType;
private boolean containsGlobalUniqueIndex;
// private final TransactionMap<Long, Long> rowVersionMap;
public StandardTable(CreateTableData data, StorageEngine storageEngine) {
super(data);
nextAnalyze = database.getSettings().analyzeAuto;
this.storageEngine = storageEngine;
this.storageEngineParams = data.storageEngineParams;
this.isHidden = data.isHidden;
for (Column col : getColumns()) {
if (DataType.isLargeObject(col.getType())) {
containsLargeObject = true;
}
}
traceLock = database.getTrace(Trace.LOCK);
// rowVersionMap = storageEngine.openMap(data.session, getName() + "_row_version", new ObjectDataType(),
// new ObjectDataType());
if (storageEngineParams != null) {
if (database.getSettings().databaseToUpper)
mapType = storageEngineParams.get("MAP_TYPE");
else
mapType = storageEngineParams.get("map_type");
} else
mapType = null;
primaryIndex = new StandardPrimaryIndex(data.session, this);
indexes.add(primaryIndex);
}
public String getMapName() {
return primaryIndex.getMapName();
}
public String getMapType() {
return mapType;
}
public StorageEngine getStorageEngine() {
return storageEngine;
}
@Override
public boolean lock(Session session, boolean exclusive, boolean forceLockEvenInMvcc) {
int lockMode = database.getLockMode();
if (lockMode == Constants.LOCK_MODE_OFF) {
return false;
}
if (!forceLockEvenInMvcc && database.isMultiVersion()) {
// MVCC: update, delete, and insert use a shared lock.
// Select doesn't lock except when using FOR UPDATE and
// the system property selectForUpdateMvcc
// is not enabled
if (exclusive) {
exclusive = false;
} else {
if (lockExclusiveSession == null) {
return false;
}
}
}
if (lockExclusiveSession == session) {
return true;
}
if (!exclusive && lockSharedSessions.containsKey(session)) {
return true;
}
synchronized (getLockSyncObject()) {
if (!exclusive && lockSharedSessions.containsKey(session)) {
return true;
}
session.setWaitForLock(this, Thread.currentThread());
waitingSessions.addLast(session);
try {
doLock1(session, lockMode, exclusive);
} finally {
session.setWaitForLock(null, null);
waitingSessions.remove(session);
}
}
return false;
}
/**
* The the object on which to synchronize and wait on. For the
* multi-threaded mode, this is this object, but for non-multi-threaded, it
* is the database, as in this case all operations are synchronized on the
* database object.
*
* @return the lock sync object
*/
private Object getLockSyncObject() {
if (database.isMultiThreaded()) {
return this;
}
return database;
}
private void doLock1(Session session, int lockMode, boolean exclusive) {
traceLock(session, exclusive, "requesting for");
// don't get the current time unless necessary
long max = 0;
boolean checkDeadlock = false;
while (true) {
// if I'm the next one in the queue
if (waitingSessions.getFirst() == session) {
if (doLock2(session, lockMode, exclusive)) {
return;
}
}
if (checkDeadlock) {
ArrayList<Session> sessions = checkDeadlock(session, null, null);
if (sessions != null) {
throw DbException.get(ErrorCode.DEADLOCK_1, getDeadlockDetails(sessions, exclusive));
}
} else {
// check for deadlocks from now on
checkDeadlock = true;
}
long now = System.currentTimeMillis();
if (max == 0) {
// try at least one more time
max = now + session.getLockTimeout();
} else if (now >= max) {
traceLock(session, exclusive, "timeout after " + session.getLockTimeout());
throw DbException.get(ErrorCode.LOCK_TIMEOUT_1, getName());
}
try {
traceLock(session, exclusive, "waiting for");
if (database.getLockMode() == Constants.LOCK_MODE_TABLE_GC) {
for (int i = 0; i < 20; i++) {
long free = Runtime.getRuntime().freeMemory();
System.gc();
long free2 = Runtime.getRuntime().freeMemory();
if (free == free2) {
break;
}
}
}
// don't wait too long so that deadlocks are detected early
long sleep = Math.min(Constants.DEADLOCK_CHECK, max - now);
if (sleep == 0) {
sleep = 1;
}
getLockSyncObject().wait(sleep);
} catch (InterruptedException e) {
// ignore
}
}
}
private boolean doLock2(Session session, int lockMode, boolean exclusive) {
if (exclusive) {
if (lockExclusiveSession == null) {
if (lockSharedSessions.isEmpty()) {
traceLock(session, exclusive, "added for");
session.addLock(this);
lockExclusiveSession = session;
return true;
} else if (lockSharedSessions.size() == 1 && lockSharedSessions.containsKey(session)) {
traceLock(session, exclusive, "add (upgraded) for ");
lockExclusiveSession = session;
return true;
}
}
} else {
if (lockExclusiveSession == null) {
if (lockMode == Constants.LOCK_MODE_READ_COMMITTED) {
if (!database.isMultiThreaded() && !database.isMultiVersion()) {
// READ_COMMITTED: a read lock is acquired,
// but released immediately after the operation
// is complete.
// When allowing only one thread, no lock is
// required.
// Row level locks work like read committed.
return true;
}
}
if (!lockSharedSessions.containsKey(session)) {
traceLock(session, exclusive, "ok");
session.addLock(this);
lockSharedSessions.put(session, session);
}
return true;
}
}
return false;
}
private static String getDeadlockDetails(ArrayList<Session> sessions, boolean exclusive) {
// We add the thread details here to make it easier for customers to
// match up these error messages with their own logs.
StringBuilder buff = new StringBuilder();
for (Session s : sessions) {
Table lock = s.getWaitForLock();
Thread thread = s.getWaitForLockThread();
buff.append("\nSession ").append(s.toString()).append(" on thread ").append(thread.getName())
.append(" is waiting to lock ").append(lock.toString())
.append(exclusive ? " (exclusive)" : " (shared)").append(" while locking ");
int i = 0;
for (Table t : s.getLocks()) {
if (i++ > 0) {
buff.append(", ");
}
buff.append(t.toString());
if (t instanceof StandardTable) {
if (((StandardTable) t).lockExclusiveSession == s) {
buff.append(" (exclusive)");
} else {
buff.append(" (shared)");
}
}
}
buff.append('.');
}
return buff.toString();
}
@Override
public ArrayList<Session> checkDeadlock(Session session, Session clash, Set<Session> visited) {
// only one deadlock check at any given time
synchronized (StandardTable.class) {
if (clash == null) {
// verification is started
clash = session;
visited = New.hashSet();
} else if (clash == session) {
// we found a circle where this session is involved
return New.arrayList();
} else if (visited.contains(session)) {
// we have already checked this session.
// there is a circle, but the sessions in the circle need to
// find it out themselves
return null;
}
visited.add(session);
ArrayList<Session> error = null;
for (Session s : lockSharedSessions.keySet()) {
if (s == session) {
// it doesn't matter if we have locked the object already
continue;
}
Table t = s.getWaitForLock();
if (t != null) {
error = t.checkDeadlock(s, clash, visited);
if (error != null) {
error.add(session);
break;
}
}
}
// take a local copy so we don't see inconsistent data, since we are not locked
// while checking the lockExclusiveSession value
Session copyOfLockExclusiveSession = lockExclusiveSession;
if (error == null && copyOfLockExclusiveSession != null) {
Table t = copyOfLockExclusiveSession.getWaitForLock();
if (t != null) {
error = t.checkDeadlock(copyOfLockExclusiveSession, clash, visited);
if (error != null) {
error.add(session);
}
}
}
return error;
}
}
private void traceLock(Session session, boolean exclusive, String s) {
if (traceLock.isDebugEnabled()) {
traceLock.debug("{0} {1} {2} {3}", session.getId(),
exclusive ? "exclusive write lock" : "shared read lock", s, getName());
}
}
@Override
public boolean isLockedExclusively() {
return lockExclusiveSession != null;
}
@Override
public boolean isLockedExclusivelyBy(Session session) {
return lockExclusiveSession == session;
}
@Override
public void unlock(Session s) {
if (database != null) {
traceLock(s, lockExclusiveSession == s, "unlock");
if (lockExclusiveSession == s) {
lockExclusiveSession = null;
}
synchronized (getLockSyncObject()) {
if (lockSharedSessions.size() > 0) {
lockSharedSessions.remove(s);
}
if (!waitingSessions.isEmpty()) {
getLockSyncObject().notifyAll();
}
}
}
}
@Override
public boolean canTruncate() {
if (getCheckForeignKeyConstraints() && database.getReferentialIntegrity()) {
ArrayList<Constraint> constraints = getConstraints();
if (constraints != null) {
for (int i = 0, size = constraints.size(); i < size; i++) {
Constraint c = constraints.get(i);
if (!(c.getConstraintType().equals(Constraint.REFERENTIAL))) {
continue;
}
ConstraintReferential ref = (ConstraintReferential) c;
if (ref.getRefTable() == this) {
return false;
}
}
}
}
return true;
}
@Override
public void close(Session session) {
// ignore
}
@Override
public Row getRow(Session session, long key) {
return primaryIndex.getRow(session, key);
}
@Override
public Index addIndex(Session session, String indexName, int indexId, IndexColumn[] cols, IndexType indexType,
boolean create, String indexComment) {
if (indexType.isPrimaryKey()) {
for (IndexColumn c : cols) {
Column column = c.column;
if (column.isNullable()) {
throw DbException.get(ErrorCode.COLUMN_MUST_NOT_BE_NULLABLE_1, column.getName());
}
column.setPrimaryKey(true);
}
}
boolean isSessionTemporary = isTemporary() && !isGlobalTemporary();
if (!isSessionTemporary) {
database.lockMeta(session);
}
Index index;
int mainIndexColumn = getMainIndexColumn(indexType, cols);
if (indexType.isDelegate()) {
index = createDelegateIndex(indexId, indexName, indexType, mainIndexColumn);
} else {
if (database.isStarting()) {
if (database.getStorage(storageEngine).hasMap(getMapNameForIndex(indexId))) {
mainIndexColumn = -1;
}
} else if (primaryIndex.getRowCountMax() != 0) {
mainIndexColumn = -1;
}
if (mainIndexColumn != -1) {
index = createDelegateIndex(indexId, indexName, indexType, mainIndexColumn);
} else if (isGlobalUniqueIndex(session, indexType)) {
index = new GlobalUniqueIndex(session, this, indexId, indexName, cols, indexType);
containsGlobalUniqueIndex = true;
} else if (indexType.isHash() && cols.length <= 1) { // TODO 是否要支持多版本
if (indexType.isUnique()) {
index = new HashIndex(this, indexId, indexName, cols, indexType);
} else {
index = new NonUniqueHashIndex(this, indexId, indexName, cols, indexType);
}
} else {
index = new StandardSecondaryIndex(session, this, indexId, indexName, cols, indexType);
}
if (index instanceof StandardIndex && index.needRebuild()) {
rebuildIndex(session, (StandardIndex) index, indexName);
}
}
index.setTemporary(isTemporary());
if (index.getCreateSQL() != null) {
index.setComment(indexComment);
if (isSessionTemporary) {
session.addLocalTempTableIndex(index);
} else {
database.addSchemaObject(session, index);
}
}
indexes.add(index);
setModified();
return index;
}
private boolean isGlobalUniqueIndex(Session session, IndexType indexType) {
return indexType.isUnique() && !indexType.isPrimaryKey() && Session.isClusterMode()
&& session.getConnectionInfo() != null && !session.getConnectionInfo().isEmbedded(); // &&
// !session.isLocal();
}
private StandardDelegateIndex createDelegateIndex(int indexId, String indexName, IndexType indexType,
int mainIndexColumn) {
primaryIndex.setMainIndexColumn(mainIndexColumn);
return new StandardDelegateIndex(this, indexId, indexName, primaryIndex, indexType);
}
private void rebuildIndex(Session session, StandardIndex index, String indexName) {
try {
if (index.isInMemory()) {
// in-memory
rebuildIndexBuffered(session, index);
} else {
rebuildIndexBlockMerge(session, index);
}
} catch (DbException e) {
getSchema().freeUniqueName(indexName);
try {
index.remove(session);
} catch (DbException e2) {
// this could happen, for example on failure in the storage
// but if that is not the case it means
// there is something wrong with the database
trace.error(e2, "could not remove index");
throw e2;
}
throw e;
}
}
private void rebuildIndexBlockMerge(Session session, StandardIndex index) {
// Read entries in memory, sort them, write to a new map (in sorted
// order); repeat (using a new map for every block of 1 MB) until all
// record are read. Merge all maps to the target (using merge sort;
// duplicates are detected in the target). For randomly ordered data,
// this should use relatively few write operations.
// A possible optimization is: change the buffer size from "row count"
// to "amount of memory", and buffer index keys instead of rows.
Index scan = getScanIndex(session);
long remaining = scan.getRowCount(session);
long total = remaining;
Cursor cursor = scan.find(session, null, null);
long i = 0;
int bufferSize = database.getMaxMemoryRows() / 2;
ArrayList<Row> buffer = New.arrayList(bufferSize);
String n = getName() + ":" + index.getName();
int t = MathUtils.convertLongToInt(total);
ArrayList<String> bufferNames = New.arrayList();
while (cursor.next()) {
Row row = cursor.get();
buffer.add(row);
database.setProgress(DatabaseEventListener.STATE_CREATE_INDEX, n, MathUtils.convertLongToInt(i++), t);
if (buffer.size() >= bufferSize) {
sortRows(buffer, index);
String mapName = database.getStorage(storageEngine).nextTemporaryMapName();
index.addRowsToBuffer(session, buffer, mapName);
bufferNames.add(mapName);
buffer.clear();
}
remaining--;
}
sortRows(buffer, index);
if (bufferNames.size() > 0) {
String mapName = database.getStorage(storageEngine).nextTemporaryMapName();
index.addRowsToBuffer(session, buffer, mapName);
bufferNames.add(mapName);
buffer.clear();
index.addBufferedRows(session, bufferNames);
} else {
addRowsToIndex(session, buffer, index);
}
if (SysProperties.CHECK && remaining != 0) {
DbException.throwInternalError("rowcount remaining=" + remaining + " " + getName());
}
}
private void rebuildIndexBuffered(Session session, Index index) {
Index scan = getScanIndex(session);
long remaining = scan.getRowCount(session);
long total = remaining;
Cursor cursor = scan.find(session, null, null);
long i = 0;
int bufferSize = (int) Math.min(total, database.getMaxMemoryRows());
ArrayList<Row> buffer = New.arrayList(bufferSize);
String n = getName() + ":" + index.getName();
int t = MathUtils.convertLongToInt(total);
while (cursor.next()) {
Row row = cursor.get();
buffer.add(row);
database.setProgress(DatabaseEventListener.STATE_CREATE_INDEX, n, MathUtils.convertLongToInt(i++), t);
if (buffer.size() >= bufferSize) {
addRowsToIndex(session, buffer, index);
}
remaining--;
}
addRowsToIndex(session, buffer, index);
if (SysProperties.CHECK && remaining != 0) {
DbException.throwInternalError("rowcount remaining=" + remaining + " " + getName());
}
}
private int getMainIndexColumn(IndexType indexType, IndexColumn[] cols) {
if (primaryIndex.getMainIndexColumn() != -1) {
return -1;
}
if (!indexType.isPrimaryKey() || cols.length != 1) {
return -1;
}
IndexColumn first = cols[0];
if (first.sortType != SortOrder.ASCENDING) {
return -1;
}
switch (first.column.getType()) {
case Value.BYTE:
case Value.SHORT:
case Value.INT:
case Value.LONG:
break;
default:
return -1;
}
return first.column.getColumnId();
}
private static void addRowsToIndex(Session session, ArrayList<Row> list, Index index) {
sortRows(list, index);
for (Row row : list) {
index.add(session, row);
}
list.clear();
}
private static void sortRows(ArrayList<Row> list, final Index index) {
Collections.sort(list, new Comparator<Row>() {
@Override
public int compare(Row r1, Row r2) {
return index.compareRows(r1, r2);
}
});
}
@Override
public void removeRow(Session session, Row row) {
lastModificationId = database.getNextModificationDataId();
Transaction t = session.getTransaction();
long savepointId = t.getSavepointId();
try {
for (int i = indexes.size() - 1; i >= 0; i--) {
Index index = indexes.get(i);
index.remove(session, row);
}
} catch (Throwable e) {
t.rollbackToSavepoint(savepointId);
throw DbException.convert(e);
}
analyzeIfRequired(session);
// rowVersionMap.remove(row.getKey());
}
@Override
public void truncate(Session session) {
lastModificationId = database.getNextModificationDataId();
for (int i = indexes.size() - 1; i >= 0; i--) {
Index index = indexes.get(i);
index.truncate(session);
}
changesSinceAnalyze = 0;
// rowVersionMap.clear();
}
@Override
public void addRow(Session session, Row row) {
lastModificationId = database.getNextModificationDataId();
Transaction t = session.getTransaction();
long savepointId = t.getSavepointId();
try {
for (int i = 0, size = indexes.size(); i < size; i++) {
Index index = indexes.get(i);
index.add(session, row);
}
} catch (Throwable e) {
t.rollbackToSavepoint(savepointId);
throw DbException.convert(e);
}
analyzeIfRequired(session);
// rowVersionMap = rowVersionMap.getInstance(session.getTransaction(), Long.MAX_VALUE);
// rowVersionMap.put(row.getKey(), (long) 1);
}
@Override
protected void analyzeIfRequired(Session session) {
if (nextAnalyze == 0 || nextAnalyze > changesSinceAnalyze++) {
return;
}
changesSinceAnalyze = 0;
int n = 2 * nextAnalyze;
if (n > 0) {
nextAnalyze = n;
}
// TODO
// int rows = session.getDatabase().getSettings().analyzeSample / 10;
// Analyze.analyzeTable(session, this, rows, false);
}
@Override
public void checkSupportAlter() {
// ok
}
@Override
public String getTableType() {
return Table.TABLE;
}
@Override
public Index getScanIndex(Session session) {
return primaryIndex;
}
@Override
public Index getUniqueIndex() {
return primaryIndex;
}
@Override
public ArrayList<Index> getIndexes() {
return indexes;
}
@Override
public long getMaxDataModificationId() {
return lastModificationId;
}
@Override
public boolean getContainsLargeObject() {
return containsLargeObject;
}
@Override
public boolean isDeterministic() {
return true;
}
@Override
public boolean canGetRowCount() {
return true;
}
@Override
public boolean canDrop() {
return true;
}
@Override
public void removeChildrenAndResources(Session session) {
if (containsLargeObject) {
// unfortunately, the data is gone on rollback
truncate(session);
database.getLobStorage().removeAllForTable(getId());
database.lockMeta(session);
}
super.removeChildrenAndResources(session);
// go backwards because database.removeIndex will
// call table.removeIndex
while (indexes.size() > 1) {
Index index = indexes.get(1);
if (index.getName() != null) {
database.removeSchemaObject(session, index);
}
// needed for session temporary indexes
indexes.remove(index);
}
if (SysProperties.CHECK) {
for (SchemaObject obj : database.getAllSchemaObjects(DbObject.INDEX)) {
Index index = (Index) obj;
if (index.getTable() == this) {
DbException.throwInternalError("index not dropped: " + index.getName());
}
}
}
primaryIndex.remove(session);
database.removeMeta(session, getId());
close(session);
invalidate();
}
@Override
public long getRowCount(Session session) {
return primaryIndex.getRowCount(session);
}
@Override
public long getRowCountApproximation() {
return primaryIndex.getRowCountApproximation();
}
@Override
public long getDiskSpaceUsed() {
return primaryIndex.getDiskSpaceUsed();
}
@Override
public void checkRename() {
// ok
}
@Override
public Column getRowIdColumn() {
if (rowIdColumn == null) {
rowIdColumn = new Column(Column.ROWID, Value.LONG);
rowIdColumn.setTable(this, -1);
}
return rowIdColumn;
}
@Override
public String toString() {
return getSQL();
}
@Override
public boolean isMVStore() {
return true;
}
/**
* Mark the transaction as committed, so that the modification counter of
* the database is incremented.
*/
public void commit() {
if (database != null) {
lastModificationId = database.getNextModificationDataId();
}
}
@Override
public boolean containsGlobalUniqueIndex() {
return containsGlobalUniqueIndex;
}
// @Override
// public long getRowVersion(long rowKey) {
// return rowVersionMap.get(rowKey);
// }
//
// @Override
// public TransactionMap<Long, Long> getRowVersionMap() {
// return rowVersionMap;
// }
public static String getMapNameForTable(int id) {
return getMapName("table", id);
}
public static String getMapNameForIndex(int id) {
return getMapName("index", id);
}
private static String getMapName(Object... args) {
StringBuilder name = new StringBuilder();
for (Object arg : args) {
if (name.length() > 0)
name.append(Constants.NAME_SEPARATOR);
name.append(arg.toString());
}
return name.toString();
}
}