/
persistenttable.cpp
783 lines (685 loc) · 26.5 KB
/
persistenttable.cpp
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
/* This file is part of VoltDB.
* Copyright (C) 2008-2010 VoltDB L.L.C.
*
* This file contains original code and/or modifications of original code.
* Any modifications made by VoltDB L.L.C. are licensed under the following
* terms and conditions:
*
* VoltDB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VoltDB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VoltDB. If not, see <http://www.gnu.org/licenses/>.
*/
/* Copyright (C) 2008 by H-Store Project
* Brown University
* Massachusetts Institute of Technology
* Yale University
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include <sstream>
#include <cassert>
#include <cstdio>
#include "storage/persistenttable.h"
#include "common/debuglog.h"
#include "common/serializeio.h"
#include "common/FailureInjection.h"
#include "common/tabletuple.h"
#include "common/UndoQuantum.h"
#include "common/executorcontext.hpp"
#include "common/FatalException.hpp"
#include "indexes/tableindex.h"
#include "storage/table.h"
#include "storage/tableiterator.h"
#include "storage/TupleStreamWrapper.h"
#include "storage/TableStats.h"
#include "storage/PersistentTableStats.h"
#include "storage/PersistentTableUndoInsertAction.h"
#include "storage/PersistentTableUndoDeleteAction.h"
#include "storage/PersistentTableUndoUpdateAction.h"
#include "storage/ConstraintFailureException.h"
#include "storage/MaterializedViewMetadata.h"
#include "storage/CopyOnWriteContext.h"
namespace voltdb {
void* keyTupleStorage = NULL;
TableTuple keyTuple;
/**
* This value has to match the value in CopyOnWriteContext.cpp
*/
#define TABLE_BLOCKSIZE 2097152
PersistentTable::PersistentTable(ExecutorContext *ctx, bool exportEnabled) :
Table(TABLE_BLOCKSIZE), m_executorContext(ctx), m_uniqueIndexes(NULL), m_uniqueIndexCount(0), m_allowNulls(NULL),
m_indexes(NULL), m_indexCount(0), m_pkeyIndex(NULL), m_wrapper(NULL),
tsSeqNo(0), m_viewCount(0), m_views(NULL), stats_(this), m_exportEnabled(exportEnabled),
m_COWContext(NULL)
{
if (exportEnabled)
{
m_wrapper = new TupleStreamWrapper(m_executorContext->m_partitionId,
m_executorContext->m_siteId,
m_id,
m_executorContext->m_lastTickTime);
}
}
PersistentTable::~PersistentTable() {
// delete all tuples to free strings
voltdb::TableIterator ti(this);
voltdb::TableTuple tuple(m_schema);
while (ti.next(tuple)) {
// indexes aren't released as they don't have ownership of strings
tuple.freeObjectColumns();
tuple.setDeletedTrue();
}
for (int i = 0; i < m_indexCount; ++i) {
TableIndex *index = m_indexes[i];
if (index != m_pkeyIndex) {
delete index;
}
}
if (m_pkeyIndex) delete m_pkeyIndex;
if (m_uniqueIndexes) delete[] m_uniqueIndexes;
if (m_allowNulls) delete[] m_allowNulls;
if (m_indexes) delete[] m_indexes;
if (m_views) {
// note this class has ownership of the views, even if they
// were allocated by VoltDBEngine
for (int i = 0; i < m_viewCount; i++)
delete m_views[i];
delete[] m_views;
}
delete m_wrapper;
}
// ------------------------------------------------------------------
// OPERATIONS
// ------------------------------------------------------------------
void PersistentTable::deleteAllTuples(bool freeAllocatedStrings) {
// nothing interesting
voltdb::TableIterator ti(this);
voltdb::TableTuple tuple(m_schema);
while (ti.next(tuple)) {
deleteTuple(tuple, true);
}
}
void setSearchKeyFromTuple(TableTuple &source) {
keyTuple.setNValue(0, source.getNValue(1));
keyTuple.setNValue(1, source.getNValue(2));
}
/*
* Regular tuple insertion that does an allocation and copy for
* uninlined strings and creates and registers an UndoAction.
*/
bool PersistentTable::insertTuple(TableTuple &source) {
size_t elMark = 0;
// not null checks at first
FAIL_IF(!checkNulls(source)) {
throw ConstraintFailureException(this, m_id, source, TableTuple(),
voltdb::CONSTRAINT_TYPE_NOT_NULL);
}
//
// First get the next free tuple
// This will either give us one from the free slot list, or
// grab a tuple at the end of our chunk of memory
//
nextFreeTuple(&m_tmpTarget1);
m_tupleCount++;
//
// Then copy the source into the target
//
m_tmpTarget1.copyForPersistentInsert(source); // tuple in freelist must be already cleared
m_tmpTarget1.setDeletedFalse();
/**
* Inserts never "dirty" a tuple since the tuple is new, but... The
* COWIterator may still be scanning and if the tuple came from the free
* list then it may need to be marked as dirty so it will be skipped. If COW
* is on have it decide. COW should always set the dirty to false unless the
* tuple is in a to be scanned area.
*/
if (m_COWContext.get() != NULL) {
m_COWContext->markTupleDirty(m_tmpTarget1, true);
} else {
m_tmpTarget1.setDirtyFalse();
}
m_tmpTarget1.isDirty();
if (!tryInsertOnAllIndexes(&m_tmpTarget1)) {
// Careful to delete allocated objects
m_tmpTarget1.freeObjectColumns();
deleteTupleStorage(m_tmpTarget1);
throw ConstraintFailureException(this, m_id, source, TableTuple(),
voltdb::CONSTRAINT_TYPE_UNIQUE);
}
// if EL is enabled, append the tuple to the buffer
// eltxxx: memoizing this more cache friendly?
if (m_exportEnabled) {
elMark =
appendToELBuffer(m_tmpTarget1, tsSeqNo++, TupleStreamWrapper::INSERT);
}
/*
* Create and register an undo action.
*/
voltdb::UndoQuantum *undoQuantum = m_executorContext->getCurrentUndoQuantum();
assert(undoQuantum);
voltdb::Pool *pool = undoQuantum->getDataPool();
assert(pool);
voltdb::PersistentTableUndoInsertAction *ptuia =
new (pool->allocate(sizeof(voltdb::PersistentTableUndoInsertAction)))
voltdb::PersistentTableUndoInsertAction(m_tmpTarget1, this, pool, elMark);
undoQuantum->registerUndoAction(ptuia);
// handle any materialized views
if (m_views) {
for (int i = 0; i < m_viewCount; i++)
m_views[i]->processTupleInsert(source);
}
return true;
}
/*
* Insert a tuple but don't allocate a new copy of the uninlineable
* strings or create an UndoAction or update a materialized view.
*/
void PersistentTable::insertTupleForUndo(TableTuple &source, size_t wrapperOffset) {
// not null checks at first
if (!checkNulls(source)) {
throwFatalException("Failed to insert tuple into table %d for undo:"
" null constraint violation\n%s\n", m_id,
source.debugNoHeader().c_str());
}
// rollback ELT
if (m_exportEnabled) {
m_wrapper->rollbackTo(wrapperOffset);
}
// First get the next free tuple This will either give us one from
// the free slot list, or grab a tuple at the end of our chunk of
// memory
nextFreeTuple(&m_tmpTarget1);
m_tupleCount++;
// Then copy the source into the target
m_tmpTarget1.copy(source);
m_tmpTarget1.setDeletedFalse();
/**
* See the comments in insertTuple for why this has to be done. The same situation applies here
* in the undo case. When the tuple was deleted a copy was made for the COW. Even though it is being
* reintroduced here it should be considered a new tuple and marked as dirty if the COWIterator will scan it
* otherwise two copies will appear. The one reintroduced by the undo action and the copy made when the tuple
* was originally deleted.
*/
if (m_COWContext.get() != NULL) {
m_COWContext->markTupleDirty(m_tmpTarget1, true);
} else {
m_tmpTarget1.setDirtyFalse();
}
m_tmpTarget1.isDirty();
if (!tryInsertOnAllIndexes(&m_tmpTarget1)) {
deleteTupleStorage(m_tmpTarget1);
throwFatalException("Failed to insert tuple into table %d for undo:"
" unique constraint violation\n%s\n", m_id,
m_tmpTarget1.debugNoHeader().c_str());
}
if (m_exportEnabled) {
m_wrapper->rollbackTo(wrapperOffset);
}
}
/*
* Regular tuple update function that does a copy and allocation for
* updated strings and creates an UndoAction.
*/
bool PersistentTable::updateTuple(TableTuple &source, TableTuple &target, bool updatesIndexes) {
size_t elMark = 0;
/*
* Create and register an undo action and then use the copy of
* the target (old value with no updates)
*/
voltdb::UndoQuantum *undoQuantum = m_executorContext->getCurrentUndoQuantum();
assert(undoQuantum);
voltdb::Pool *pool = undoQuantum->getDataPool();
assert(pool);
voltdb::PersistentTableUndoUpdateAction *ptuua =
new (pool->allocate(sizeof(voltdb::PersistentTableUndoUpdateAction)))
voltdb::PersistentTableUndoUpdateAction(target, this, pool);
if (m_COWContext.get() != NULL) {
m_COWContext->markTupleDirty(target, false);
}
source.setDeletedFalse();
//Copy the dirty status that was set by markTupleDirty.
if (target.isDirty()) {
source.setDirtyTrue();
} else {
source.setDirtyFalse();
}
target.copyForPersistentUpdate(source);
ptuua->setNewTuple(target, pool);
if (!undoQuantum->isDummy()) {
//DummyUndoQuantum calls destructor upon register.
undoQuantum->registerUndoAction(ptuua);
}
// the planner should determine if this update can affect indexes.
// if so, update the indexes here
if (updatesIndexes) {
if (!tryUpdateOnAllIndexes(ptuua->getOldTuple(), target)) {
throw ConstraintFailureException(this, m_id, ptuua->getOldTuple(),
target,
voltdb::CONSTRAINT_TYPE_UNIQUE);
}
//If the CFE is thrown the Undo action should not attempt to revert the
//indexes.
ptuua->needToRevertIndexes();
updateFromAllIndexes(ptuua->getOldTuple(), target);
}
// if EL is enabled, append the tuple to the buffer
if (m_exportEnabled) {
// only need the earliest mark
elMark = appendToELBuffer(ptuua->getOldTuple(), tsSeqNo, TupleStreamWrapper::DELETE);
appendToELBuffer(target, tsSeqNo++, TupleStreamWrapper::INSERT);
ptuua->setELMark(elMark);
}
// handle any materialized views
if (m_views) {
for (int i = 0; i < m_viewCount; i++)
m_views[i]->processTupleUpdate(ptuua->getOldTuple(), target);
}
/**
* Check for nulls after the update has been performed because the source tuple may have garbage in
* some columns
*/
FAIL_IF(!checkNulls(target)) {
throw ConstraintFailureException(this, m_id, ptuua->getOldTuple(),
target,
voltdb::CONSTRAINT_TYPE_NOT_NULL);
}
if (undoQuantum->isDummy()) {
//DummyUndoQuantum calls destructor upon register so it can't be called
//earlier
undoQuantum->registerUndoAction(ptuua);
}
return true;
}
/*
* Source contains the tuple before the update and target is a
* reference to the updated tuple including the actual table
* storage. First backup the target to a temp tuple so it will be
* available for updating indexes. Then revert the tuple to the
* original preupdate values by copying the source to the target. Then
* update the indexes to use the new key value (if the key has
* changed). The backup is necessary because the indexes expect the
* data ptr that will be used as the value in the index.
*/
void PersistentTable::updateTupleForUndo(TableTuple &source, TableTuple &target,
bool revertIndexes, size_t wrapperOffset) {
//Need to back up the updated version of the tuple to provide to
//the indexes when updating The indexes expect source's data Ptr
//to point into the table so it is necessary to copy source to
//target. Without this backup the target would be lost and it
//there would be nothing to provide to the index to lookup. In
//regular updateTuple this storage is provided by the undo
//quantum.
TableTuple targetBackup = tempTuple();
targetBackup.copy(target);
bool dirty = target.isDirty();
// this is the actual in-place revert to the old version
target.copy(source);
if (dirty) {
target.setDirtyTrue();
} else {
target.setDirtyFalse();
}
target.isDirty();
//If the indexes were never updated there is no need to revert them.
if (revertIndexes) {
if (!tryUpdateOnAllIndexes(targetBackup, target)) {
// TODO: this might be too strict. see insertTuple()
throwFatalException("Failed to update tuple in table %d for undo:"
" unique constraint violation\n%s\n%s\n", m_id,
targetBackup.debugNoHeader().c_str(),
target.debugNoHeader().c_str());
}
updateFromAllIndexes(targetBackup, target);
}
if (m_exportEnabled) {
m_wrapper->rollbackTo(wrapperOffset);
}
}
bool PersistentTable::deleteTuple(TableTuple &target, bool deleteAllocatedStrings) {
// May not delete an already deleted tuple.
assert(target.isActive());
// The tempTuple is forever!
assert(&target != &m_tempTuple);
// Just like insert, we want to remove this tuple from all of our indexes
deleteFromAllIndexes(&target);
/**
* A user initiated delete needs to have the tuple "marked dirty" so that the copy is made.
*/
if (m_COWContext.get() != NULL) {
m_COWContext->markTupleDirty(target, false);
}
m_tmpTarget1.isDirty();
/*
* Create and register an undo action.
*/
voltdb::UndoQuantum *undoQuantum = m_executorContext->getCurrentUndoQuantum();
assert(undoQuantum);
voltdb::Pool *pool = undoQuantum->getDataPool();
assert(pool);
voltdb::PersistentTableUndoDeleteAction *ptuda = new (pool->allocate(sizeof(voltdb::PersistentTableUndoDeleteAction))) voltdb::PersistentTableUndoDeleteAction( target, this, pool);
// handle any materialized views
if (m_views) {
for (int i = 0; i < m_viewCount; i++)
m_views[i]->processTupleDelete(target);
}
// if EL is enabled, append the tuple to the buffer
if (m_exportEnabled) {
size_t elMark = appendToELBuffer(target, tsSeqNo++, TupleStreamWrapper::DELETE);
ptuda->setELMark(elMark);
}
undoQuantum->registerUndoAction(ptuda);
deleteTupleStorage(target);
return true;
}
/*
* Delete a tuple by looking it up via table scan or a primary key
* index lookup. An undo initiated delete like deleteTupleForUndo
* is in response to the insertion of a new tuple by insertTuple
* and that by definition is a tuple that is of no interest to
* the COWContext. The COWContext set the tuple to have the
* correct dirty setting when the tuple was originally inserted.
* TODO remove duplication with regular delete. Also no view updates.
*/
void PersistentTable::deleteTupleForUndo(voltdb::TableTuple &tupleCopy, size_t wrapperOffset) {
TableTuple target = lookupTuple(tupleCopy);
if (target.isNullTuple()) {
throwFatalException("Failed to delete tuple from table %d:"
" tuple does not exist\n%s\n", m_id,
tupleCopy.debugNoHeader().c_str());
}
else {
// Make sure that they are not trying to delete the same tuple twice
assert(target.isActive());
// Also make sure they are not trying to delete our m_tempTuple
assert(&target != &m_tempTuple);
// rollback ELT
if (m_exportEnabled) {
m_wrapper->rollbackTo(wrapperOffset);
}
// Just like insert, we want to remove this tuple from all of our indexes
deleteFromAllIndexes(&target);
// Delete the strings/objects
target.freeObjectColumns();
deleteTupleStorage(target);
}
}
voltdb::TableTuple PersistentTable::lookupTuple(TableTuple tuple) {
voltdb::TableTuple nullTuple(m_schema);//Null tuple
voltdb::TableIndex *pkeyIndex = primaryKeyIndex();
if (pkeyIndex == NULL) {
/*
* Do a table scan.
*/
TableTuple tableTuple(m_schema);
int tableIndex = 0;
for (int tupleCount = 0; tupleCount < m_tupleCount; tupleCount++) {
/*
* Find the next active tuple
*/
do {
tableTuple.move(dataPtrForTuple(tableIndex++));
} while (!tableTuple.isActive());
if (tableTuple.equalsNoSchemaCheck(tuple)) {
return tableTuple;
}
}
return nullTuple;
}
bool foundTuple = pkeyIndex->moveToTuple(&tuple);
if (!foundTuple) {
return nullTuple;
}
return pkeyIndex->nextValueAtKey();
}
void PersistentTable::insertIntoAllIndexes(TableTuple *tuple) {
for (int i = m_indexCount - 1; i >= 0;--i) {
if (!m_indexes[i]->addEntry(tuple)) {
throwFatalException("Failed to insert tuple into index");
}
}
}
void PersistentTable::deleteFromAllIndexes(TableTuple *tuple) {
for (int i = m_indexCount - 1; i >= 0;--i) {
if (!m_indexes[i]->deleteEntry(tuple)) {
throwFatalException("Failed to delete tuple from index");
}
}
}
void PersistentTable::updateFromAllIndexes(TableTuple &targetTuple, const TableTuple &sourceTuple) {
for (int i = m_indexCount - 1; i >= 0;--i) {
if (!m_indexes[i]->replaceEntry(&targetTuple, &sourceTuple)) {
throwFatalException("Failed to update tuple in index");
}
}
}
bool PersistentTable::tryInsertOnAllIndexes(TableTuple *tuple) {
for (int i = m_indexCount - 1; i >= 0; --i) {
FAIL_IF(!m_indexes[i]->addEntry(tuple)) {
VOLT_DEBUG("Failed to insert into index %s,%s",
m_indexes[i]->getTypeName().c_str(),
m_indexes[i]->getName().c_str());
for (int j = i + 1; j < m_indexCount; ++j) {
m_indexes[j]->deleteEntry(tuple);
}
return false;
}
}
return true;
}
bool PersistentTable::tryUpdateOnAllIndexes(TableTuple &targetTuple, const TableTuple &sourceTuple) {
for (int i = m_uniqueIndexCount - 1; i >= 0;--i) {
if (m_uniqueIndexes[i]->checkForIndexChange(&targetTuple, &sourceTuple) == false)
continue; // no update is needed for this index
// if there is a change, the new_key has to be checked
FAIL_IF (m_uniqueIndexes[i]->exists(&sourceTuple)) {
VOLT_WARN("Unique Index '%s' complained to the update",
m_uniqueIndexes[i]->debug().c_str());
return false; // cannot insert the new value
}
}
return true;
}
bool PersistentTable::checkNulls(TableTuple &tuple) const {
assert (m_columnCount == tuple.sizeInValues());
for (int i = m_columnCount - 1; i >= 0; --i) {
if (tuple.isNull(i) && !m_allowNulls[i]) {
VOLT_TRACE ("%d th attribute was NULL. It is non-nillable attribute.", i);
return false;
}
}
return true;
}
void PersistentTable::setMaterializedViews(const std::vector<MaterializedViewMetadata*> &views) {
m_viewCount = static_cast<int32_t>(views.size());
m_views = new MaterializedViewMetadata*[m_viewCount];
// this actually transfers ownership of the view structures, they
// must be deleted by the table
for (int i = 0; i < m_viewCount; i++)
m_views[i] = views[i];
}
// ------------------------------------------------------------------
// UTILITY
// ------------------------------------------------------------------
std::string PersistentTable::tableType() const {
return "PersistentTable";
}
std::string PersistentTable::debug() {
std::ostringstream buffer;
buffer << Table::debug();
buffer << "\tINDEXES: " << m_indexCount << "\n";
// Indexes
buffer << "===========================================================\n";
for (int index_ctr = 0; index_ctr < m_indexCount; ++index_ctr) {
if (m_indexes[index_ctr]) {
buffer << "\t[" << index_ctr << "] " << m_indexes[index_ctr]->debug();
//
// Primary Key
//
if (m_pkeyIndex != NULL && m_pkeyIndex->getName().compare(m_indexes[index_ctr]->getName()) == 0) {
buffer << " [PRIMARY KEY]";
}
buffer << "\n";
}
}
return buffer.str();
}
// ------------------------------------------------------------------
// Accessors
// ------------------------------------------------------------------
// Index
TableIndex *PersistentTable::index(std::string name) {
for (int i = 0; i < m_indexCount; ++i) {
TableIndex *index = m_indexes[i];
if (index->getName().compare(name) == 0) {
return index;
}
}
std::stringstream errorString;
errorString << "Could not find Index with name " << name << std::endl;
for (int i = 0; i < m_indexCount; ++i) {
TableIndex *index = m_indexes[i];
errorString << index->getName() << std::endl;
}
throwFatalException( "%s", errorString.str().c_str());
}
std::vector<TableIndex*> PersistentTable::allIndexes() const {
std::vector<TableIndex*> retval;
for (int i = 0; i < m_indexCount; i++)
retval.push_back(m_indexes[i]);
return retval;
}
void PersistentTable::onSetColumns() {
if (m_allowNulls != NULL) delete[] m_allowNulls;
m_allowNulls = new bool[m_columnCount];
for (int i = m_columnCount - 1; i >= 0; --i) {
m_allowNulls[i] = m_schema->columnAllowNull(i);
}
}
/*
* Implemented by persistent table and called by Table::loadTuplesFrom
* to do additional processing for views and ELT
*/
void PersistentTable::processLoadedTuple(bool allowELT, TableTuple &tuple) {
// handle any materialized views
if (m_views) {
for (int i = 0; i < m_viewCount; i++)
m_views[i]->processTupleInsert(m_tmpTarget1);
}
// if EL is enabled, append the tuple to the buffer
if (allowELT && m_exportEnabled) {
appendToELBuffer(m_tmpTarget1, tsSeqNo++,
TupleStreamWrapper::INSERT);
}
}
/*
* Implemented by persistent table and called by Table::loadTuplesFrom
* to do add tuples to indexes
*/
void PersistentTable::populateIndexes(int tupleCount) {
// populate indexes. walk the contiguous memory in the inner loop.
for (int i = m_indexCount - 1; i >= 0;--i) {
TableIndex *index = m_indexes[i];
for (int j = 0; j < tupleCount; ++j) {
m_tmpTarget1.move(dataPtrForTuple((int) m_usedTuples + j));
index->addEntry(&m_tmpTarget1);
}
}
}
size_t PersistentTable::appendToELBuffer(TableTuple &tuple, int64_t seqNo,
TupleStreamWrapper::Type type) {
return m_wrapper->appendTuple(m_executorContext->m_lastCommittedTxnId,
m_executorContext->currentTxnId(),
seqNo,
m_executorContext->currentTxnTimestamp(),
tuple, type);
}
/**
* Flush tuple stream wrappers. A negative time instructs an
* immediate flush.
*/
void PersistentTable::flushOldTuples(int64_t timeInMillis)
{
if (m_exportEnabled && m_wrapper) {
m_wrapper->periodicFlush(timeInMillis,
m_executorContext->m_lastTickTime,
m_executorContext->m_lastCommittedTxnId,
m_executorContext->currentTxnId());
}
}
StreamBlock*
PersistentTable::getCommittedEltBytes()
{
if (m_exportEnabled && m_wrapper)
{
return m_wrapper->getCommittedEltBytes();
}
return NULL;
}
bool
PersistentTable::releaseEltBytes(int64_t releaseOffset)
{
if (m_exportEnabled && m_wrapper)
{
return m_wrapper->releaseEltBytes(releaseOffset);
}
return false;
}
voltdb::TableStats* PersistentTable::getTableStats() {
return &stats_;
}
/**
* Switch the table to copy on write mode. Returns true if the table was already in copy on write mode.
*/
bool PersistentTable::activateCopyOnWrite(TupleSerializer *serializer, int32_t partitionId) {
if (m_COWContext != NULL) {
return true;
}
if (m_tupleCount == 0) {
return false;
}
m_COWContext.reset(new CopyOnWriteContext( this, serializer, partitionId));
return false;
}
/**
* Attempt to serialize more tuples from the table to the provided output stream.
* Returns true if there are more tuples and false if there are no more tuples waiting to be
* serialized.
*/
void PersistentTable::serializeMore(ReferenceSerializeOutput *out) {
if (m_COWContext == NULL) {
return;
}
const bool hasMore = m_COWContext->serializeMore(out);
if (!hasMore) {
m_COWContext.reset(NULL);
fflush(stdout);
}
return;
}
}