-
Notifications
You must be signed in to change notification settings - Fork 376
/
memtx_space.c
1424 lines (1324 loc) · 41.5 KB
/
memtx_space.c
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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2010-2016, Tarantool AUTHORS, please see AUTHORS file.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "memtx_space.h"
#include "space.h"
#include "iproto_constants.h"
#include "txn.h"
#include "memtx_tx.h"
#include "tuple.h"
#include "xrow_update.h"
#include "xrow.h"
#include "memtx_hash.h"
#include "memtx_tree.h"
#include "memtx_rtree.h"
#include "memtx_bitset.h"
#include "memtx_engine.h"
#include "column_mask.h"
#include "sequence.h"
#include "memtx_tuple_compression.h"
#include "schema.h"
#include "result.h"
/*
* Yield every 1K tuples while building a new index or checking
* a space format. In debug mode yield more often for testing
* purposes.
*
* Yields do not happen during recovery. At this point of time
* iproto aready accepts requests, and yielding would allow them
* to be proccessed while data is not fully recovered.
*/
#ifdef NDEBUG
enum { MEMTX_DDL_YIELD_LOOPS = 1000 };
#else
enum { MEMTX_DDL_YIELD_LOOPS = 10 };
#endif
static void
memtx_space_destroy(struct space *space)
{
TRASH(space);
free(space);
}
static size_t
memtx_space_bsize(struct space *space)
{
struct memtx_space *memtx_space = (struct memtx_space *)space;
return memtx_space->bsize;
}
/* {{{ DML */
void
memtx_space_update_bsize(struct space *space, struct tuple *old_tuple,
struct tuple *new_tuple)
{
assert(space->vtab->destroy == &memtx_space_destroy);
struct memtx_space *memtx_space = (struct memtx_space *)space;
ssize_t old_bsize = old_tuple ? box_tuple_bsize(old_tuple) : 0;
ssize_t new_bsize = new_tuple ? box_tuple_bsize(new_tuple) : 0;
assert((ssize_t)memtx_space->bsize + new_bsize - old_bsize >= 0);
memtx_space->bsize += new_bsize - old_bsize;
}
/**
* A version of space_replace for a space which has
* no indexes (is not yet fully built).
*/
int
memtx_space_replace_no_keys(struct space *space, struct tuple *old_tuple,
struct tuple *new_tuple,
enum dup_replace_mode mode, struct tuple **result)
{
(void)old_tuple;
(void)new_tuple;
(void)mode;
(void)result;
struct index *index = index_find(space, 0);
assert(index == NULL); /* not reached. */
(void) index;
return -1;
}
/**
* A short-cut version of replace() used during bulk load
* from snapshot.
*/
int
memtx_space_replace_build_next(struct space *space, struct tuple *old_tuple,
struct tuple *new_tuple,
enum dup_replace_mode mode,
struct tuple **result)
{
assert(old_tuple == NULL && mode == DUP_INSERT);
(void)mode;
*result = NULL;
if (old_tuple) {
/*
* Called from txn_rollback() In practice
* is impossible: all possible checks for tuple
* validity are done before the space is changed,
* and WAL is off, so this part can't fail.
*/
panic("Failed to commit transaction when loading "
"from snapshot");
}
if (index_build_next(space->index[0], new_tuple) != 0)
return -1;
memtx_space_update_bsize(space, NULL, new_tuple);
tuple_ref(new_tuple);
return 0;
}
/**
* A short-cut version of replace() used when loading
* data from XLOG files.
*/
int
memtx_space_replace_primary_key(struct space *space, struct tuple *old_tuple,
struct tuple *new_tuple,
enum dup_replace_mode mode,
struct tuple **result)
{
struct tuple *successor;
if (index_replace(space->index[0], old_tuple,
new_tuple, mode, &old_tuple, &successor) != 0)
return -1;
memtx_space_update_bsize(space, old_tuple, new_tuple);
if (new_tuple != NULL)
tuple_ref(new_tuple);
*result = old_tuple;
return 0;
}
/**
* @brief A single method to handle REPLACE, DELETE and UPDATE.
*
* @param space space
* @param old_tuple the tuple that should be removed (can be NULL)
* @param new_tuple the tuple that should be inserted (can be NULL)
* @param mode dup_replace_mode, used only if new_tuple is not
* NULL and old_tuple is NULL, and only for the
* primary key.
*
* For DELETE, new_tuple must be NULL. old_tuple must be
* previously found in the primary key.
*
* For REPLACE, old_tuple must be NULL. The additional
* argument dup_replace_mode further defines how REPLACE
* should proceed.
*
* For UPDATE, both old_tuple and new_tuple must be given,
* where old_tuple must be previously found in the primary key.
*
* Let's consider these three cases in detail:
*
* 1. DELETE, old_tuple is not NULL, new_tuple is NULL
* The effect is that old_tuple is removed from all
* indexes. dup_replace_mode is ignored.
*
* 2. REPLACE, old_tuple is NULL, new_tuple is not NULL,
* has one simple sub-case and two with further
* ramifications:
*
* A. dup_replace_mode is DUP_INSERT. Attempts to insert the
* new tuple into all indexes. If *any* of the unique indexes
* has a duplicate key, deletion is aborted, all of its
* effects are removed, and an error is thrown.
*
* B. dup_replace_mode is DUP_REPLACE. It means an existing
* tuple has to be replaced with the new one. To do it, tries
* to find a tuple with a duplicate key in the primary index.
* If the tuple is not found, throws an error. Otherwise,
* replaces the old tuple with a new one in the primary key.
* Continues on to secondary keys, but if there is any
* secondary key, which has a duplicate tuple, but one which
* is different from the duplicate found in the primary key,
* aborts, puts everything back, throws an exception.
*
* For example, if there is a space with 3 unique keys and
* two tuples { 1, 2, 3 } and { 3, 1, 2 }:
*
* This REPLACE/DUP_REPLACE is OK: { 1, 5, 5 }
* This REPLACE/DUP_REPLACE is not OK: { 2, 2, 2 } (there
* is no tuple with key '2' in the primary key)
* This REPLACE/DUP_REPLACE is not OK: { 1, 1, 1 } (there
* is a conflicting tuple in the secondary unique key).
*
* C. dup_replace_mode is DUP_REPLACE_OR_INSERT. If
* there is a duplicate tuple in the primary key, behaves the
* same way as DUP_REPLACE, otherwise behaves the same way as
* DUP_INSERT.
*
* 3. UPDATE has to delete the old tuple and insert a new one.
* dup_replace_mode is ignored.
* Note that old_tuple primary key doesn't have to match
* new_tuple primary key, thus a duplicate can be found.
* For this reason, and since there can be duplicates in
* other indexes, UPDATE is the same as DELETE +
* REPLACE/DUP_INSERT.
*
* @return old_tuple. DELETE, UPDATE and REPLACE/DUP_REPLACE
* always produce an old tuple. REPLACE/DUP_INSERT always returns
* NULL. REPLACE/DUP_REPLACE_OR_INSERT may or may not find
* a duplicate.
*
* The method is all-or-nothing in all cases. Changes are either
* applied to all indexes, or nothing applied at all.
*
* Note, that even in case of REPLACE, dup_replace_mode only
* affects the primary key, for secondary keys it's always
* DUP_INSERT.
*
* The call never removes more than one tuple: if
* old_tuple is given, dup_replace_mode is ignored.
* Otherwise, it's taken into account only for the
* primary key.
*/
int
memtx_space_replace_all_keys(struct space *space, struct tuple *old_tuple,
struct tuple *new_tuple,
enum dup_replace_mode mode,
struct tuple **result)
{
struct memtx_engine *memtx = (struct memtx_engine *)space->engine;
/*
* Ensure we have enough slack memory to guarantee
* successful statement-level rollback.
*/
if (memtx_index_extent_reserve(memtx, new_tuple != NULL ?
RESERVE_EXTENTS_BEFORE_REPLACE :
RESERVE_EXTENTS_BEFORE_DELETE) != 0)
return -1;
uint32_t i = 0;
/* Update the primary key */
struct index *pk = index_find(space, 0);
if (pk == NULL)
return -1;
assert(pk->def->opts.is_unique);
/* Replace must be done in transaction, except ephemeral spaces. */
assert(space->def->opts.is_ephemeral ||
(in_txn() != NULL && txn_current_stmt(in_txn()) != NULL));
/*
* Don't use MVCC engine for ephemeral in any case.
* MVCC engine requires txn to be present as a storage for
* reads/writes/conflicts.
* Also, now there's no way to turn MVCC engine off: once MVCC engine
* starts to manage a space - direct access to it must be prohibited.
* Since modification of ephemeral spaces are allowed without txn,
* we must not use MVCC for those spaces even if txn is present now.
*/
if (memtx_tx_manager_use_mvcc_engine && !space->def->opts.is_ephemeral) {
struct txn_stmt *stmt = txn_current_stmt(in_txn());
return memtx_tx_history_add_stmt(stmt, old_tuple, new_tuple,
mode, result);
}
/*
* If old_tuple is not NULL, the index has to
* find and delete it, or return an error.
*/
struct tuple *successor;
if (index_replace(pk, old_tuple, new_tuple, mode,
&old_tuple, &successor) != 0)
return -1;
assert(old_tuple || new_tuple);
/* Update secondary keys. */
for (i++; i < space->index_count; i++) {
struct tuple *unused;
struct index *index = space->index[i];
if (index_replace(index, old_tuple, new_tuple,
DUP_INSERT, &unused, &unused) != 0)
goto rollback;
}
memtx_space_update_bsize(space, old_tuple, new_tuple);
if (new_tuple != NULL)
tuple_ref(new_tuple);
*result = old_tuple;
return 0;
rollback:
for (; i > 0; i--) {
struct tuple *unused;
struct index *index = space->index[i - 1];
/* Rollback must not fail. */
if (index_replace(index, new_tuple, old_tuple,
DUP_INSERT, &unused, &unused) != 0) {
diag_log();
unreachable();
panic("failed to rollback change");
}
}
return -1;
}
static inline enum dup_replace_mode
dup_replace_mode(uint16_t op)
{
return op == IPROTO_INSERT ? DUP_INSERT : DUP_REPLACE_OR_INSERT;
}
/**
* Call replace method in memtx space and fill txn statement in case of
* success. @A new_tuple is expected to be referenced once and must be
* unreferenced by caller in case of failure.
*/
static inline int
memtx_space_replace_tuple(struct space *space, struct txn_stmt *stmt,
struct tuple *old_tuple, struct tuple *new_tuple,
enum dup_replace_mode mode)
{
struct memtx_space *memtx_space = (struct memtx_space *)space;
struct tuple *result;
struct tuple *orig_new_tuple = new_tuple;
bool was_referenced = false;
if (new_tuple != NULL && space->format->is_compressed) {
new_tuple = memtx_tuple_compress(new_tuple);
if (new_tuple == NULL)
return -1;
tuple_ref(new_tuple);
was_referenced = true;
}
int rc = memtx_space->replace(space, old_tuple, new_tuple,
mode, &result);
if (rc != 0)
goto finish;
txn_stmt_prepare_rollback_info(stmt, result, new_tuple);
stmt->engine_savepoint = stmt;
stmt->new_tuple = orig_new_tuple;
stmt->old_tuple = result;
if (stmt->old_tuple != NULL) {
struct tuple *orig_old_tuple = stmt->old_tuple;
stmt->old_tuple = memtx_tuple_decompress(stmt->old_tuple);
if (stmt->old_tuple == NULL)
return -1;
tuple_ref(stmt->old_tuple);
tuple_unref(orig_old_tuple);
}
finish:
/*
* Regardless of whether the function ended with success or
* failure, we should unref new_tuple if it was explicitly
* referenced. If function returns with error we unref tuple
* and immidiatly delete it, otherwise we unref tuple, but it
* still alive because tuple is referenced by the primary key.
*/
if (was_referenced)
tuple_unref(new_tuple);
return rc;
}
static int
memtx_space_execute_replace(struct space *space, struct txn *txn,
struct request *request, struct tuple **result)
{
struct txn_stmt *stmt = txn_current_stmt(txn);
enum dup_replace_mode mode = dup_replace_mode(request->type);
struct tuple *new_tuple =
space->format->vtab.tuple_new(space->format, request->tuple,
request->tuple_end);
if (new_tuple == NULL)
return -1;
tuple_ref(new_tuple);
if (memtx_space_replace_tuple(space, stmt, NULL, new_tuple,
mode) != 0) {
tuple_unref(new_tuple);
return -1;
}
*result = stmt->new_tuple;
return 0;
}
static int
memtx_space_execute_delete(struct space *space, struct txn *txn,
struct request *request, struct tuple **result)
{
struct txn_stmt *stmt = txn_current_stmt(txn);
/* Try to find the tuple by unique key. */
struct index *pk = index_find_unique(space, request->index_id);
if (pk == NULL)
return -1;
const char *key = request->key;
uint32_t part_count = mp_decode_array(&key);
if (exact_key_validate(pk->def->key_def, key, part_count) != 0)
return -1;
struct tuple *old_tuple;
if (index_get_raw(pk, key, part_count, &old_tuple) != 0)
return -1;
if (old_tuple == NULL) {
*result = NULL;
return 0;
}
if (memtx_space_replace_tuple(space, stmt, old_tuple, NULL,
DUP_REPLACE_OR_INSERT) != 0)
return -1;
*result = result_process(space, stmt->old_tuple);
return *result == NULL ? -1 : 0;
}
static int
memtx_space_execute_update(struct space *space, struct txn *txn,
struct request *request, struct tuple **result)
{
struct txn_stmt *stmt = txn_current_stmt(txn);
/* Try to find the tuple by unique key. */
struct index *pk = index_find_unique(space, request->index_id);
if (pk == NULL)
return -1;
const char *key = request->key;
uint32_t part_count = mp_decode_array(&key);
if (exact_key_validate(pk->def->key_def, key, part_count) != 0)
return -1;
struct tuple *old_tuple;
if (index_get_raw(pk, key, part_count, &old_tuple) != 0)
return -1;
if (old_tuple == NULL) {
*result = NULL;
return 0;
}
struct tuple *decompressed = memtx_tuple_decompress(old_tuple);
if (decompressed == NULL)
return -1;
tuple_bless(decompressed);
decompressed = result_process(space, decompressed);
if (decompressed == NULL)
return -1;
/* Update the tuple; legacy, request ops are in request->tuple */
uint32_t new_size = 0, bsize;
struct tuple_format *format = space->format;
const char *old_data = tuple_data_range(decompressed, &bsize);
const char *new_data =
xrow_update_execute(request->tuple, request->tuple_end,
old_data, old_data + bsize, format,
&new_size, request->index_base, NULL);
if (new_data == NULL)
return -1;
struct tuple *new_tuple =
space->format->vtab.tuple_new(format, new_data,
new_data + new_size);
if (new_tuple == NULL)
return -1;
tuple_ref(new_tuple);
if (memtx_space_replace_tuple(space, stmt, old_tuple, new_tuple,
DUP_REPLACE) != 0) {
tuple_unref(new_tuple);
return -1;
}
*result = stmt->new_tuple;
return 0;
}
static int
memtx_space_execute_upsert(struct space *space, struct txn *txn,
struct request *request)
{
struct txn_stmt *stmt = txn_current_stmt(txn);
/*
* Check all tuple fields: we should produce an error on
* malformed tuple even if upsert turns into an update.
*/
if (tuple_validate_raw(space->format, request->tuple))
return -1;
struct index *index = index_find_unique(space, 0);
if (index == NULL)
return -1;
uint32_t part_count = index->def->key_def->part_count;
/* Extract the primary key from tuple. */
const char *key = tuple_extract_key_raw(request->tuple,
request->tuple_end,
index->def->key_def,
MULTIKEY_NONE, NULL);
if (key == NULL)
return -1;
/* Cut array header */
mp_decode_array(&key);
/* Try to find the tuple by primary key. */
struct tuple *old_tuple;
if (index_get_raw(index, key, part_count, &old_tuple) != 0)
return -1;
struct tuple_format *format = space->format;
struct tuple *new_tuple = NULL;
if (old_tuple == NULL) {
/**
* Old tuple was not found. A write optimized
* engine may only know this after commit, so
* some errors which happen on this branch would
* only make it to the error log in it.
* To provide identical semantics, we should not throw
* anything. However, considering the kind of
* error which may occur, throwing it won't break
* cross-engine compatibility:
* - update ops are checked before commit
* - OOM may happen at any time
* - duplicate key has to be checked by
* write-optimized engine before commit, so if
* we get it here, it's also OK to throw it
* @sa https://github.com/tarantool/tarantool/issues/1156
*/
if (xrow_update_check_ops(request->ops, request->ops_end,
format, request->index_base) != 0) {
return -1;
}
new_tuple =
space->format->vtab.tuple_new(format, request->tuple,
request->tuple_end);
if (new_tuple == NULL)
return -1;
tuple_ref(new_tuple);
} else {
struct tuple *decompressed = memtx_tuple_decompress(old_tuple);
if (decompressed == NULL)
return -1;
tuple_bless(decompressed);
decompressed = result_process(space, decompressed);
if (decompressed == NULL)
return -1;
uint32_t new_size = 0, bsize;
const char *old_data = tuple_data_range(decompressed, &bsize);
/*
* Update the tuple.
* xrow_upsert_execute() fails on totally wrong
* tuple ops, but ignores ops that not suitable
* for the tuple.
*/
uint64_t column_mask = COLUMN_MASK_FULL;
const char *new_data =
xrow_upsert_execute(request->ops, request->ops_end,
old_data, old_data + bsize,
format, &new_size,
request->index_base, false,
&column_mask);
if (new_data == NULL)
return -1;
new_tuple =
space->format->vtab.tuple_new(format, new_data,
new_data + new_size);
if (new_tuple == NULL)
return -1;
tuple_ref(new_tuple);
struct index *pk = space->index[0];
if (!key_update_can_be_skipped(pk->def->key_def->column_mask,
column_mask) &&
tuple_compare(old_tuple, HINT_NONE, new_tuple,
HINT_NONE, pk->def->key_def) != 0) {
/* Primary key is changed: log error and do nothing. */
diag_set(ClientError, ER_CANT_UPDATE_PRIMARY_KEY,
space_name(space));
diag_log();
tuple_unref(new_tuple);
return 0;
}
}
assert(new_tuple != NULL);
/*
* It's OK to use DUP_REPLACE_OR_INSERT: we don't risk
* inserting a new tuple if the old one exists, since
* we checked this case explicitly and skipped the upsert
* above.
*/
if (memtx_space_replace_tuple(space, stmt, old_tuple, new_tuple,
DUP_REPLACE_OR_INSERT) != 0) {
tuple_unref(new_tuple);
return -1;
}
/* Return nothing: UPSERT does not return data. */
return 0;
}
/**
* This function simply creates new memtx tuple, refs it and calls space's
* replace function. In constrast to original memtx_space_execute_replace(), it
* doesn't handle any transaction routine.
* Ephemeral spaces shouldn't be involved in transaction routine, since
* they are used only for internal purposes. Moreover, ephemeral spaces
* can be created and destroyed within one transaction and rollback of already
* destroyed space may lead to undefined behaviour. For this reason it
* doesn't take txn as an argument.
*/
static int
memtx_space_ephemeral_replace(struct space *space, const char *tuple,
const char *tuple_end)
{
struct memtx_space *memtx_space = (struct memtx_space *)space;
struct tuple *new_tuple =
space->format->vtab.tuple_new(space->format, tuple, tuple_end);
if (new_tuple == NULL)
return -1;
struct tuple *old_tuple;
if (memtx_space->replace(space, NULL, new_tuple,
DUP_REPLACE_OR_INSERT, &old_tuple) != 0) {
space->format->vtab.tuple_delete(space->format, new_tuple);
return -1;
}
if (old_tuple != NULL)
tuple_unref(old_tuple);
return 0;
}
/**
* Delete tuple with given key from primary index. Tuple checking is omitted
* due to the ability of ephemeral spaces to hold nulls in primary key.
* Generally speaking, it is not correct behaviour owing to ambiguity when
* fetching/deleting tuple from space with several tuples containing
* nulls in PK. On the other hand, ephemeral spaces are used only for internal
* needs, so if it is guaranteed that no such situation occur
* (when several tuples with nulls in PK exist), it is OK to allow
* insertion nulls in PK.
*
* Similarly to ephemeral replace function,
* it isn't involved in any transaction routine.
*/
static int
memtx_space_ephemeral_delete(struct space *space, const char *key)
{
struct memtx_space *memtx_space = (struct memtx_space *)space;
struct index *primary_index = space_index(space, 0 /* primary index*/);
if (primary_index == NULL)
return -1;
uint32_t part_count = mp_decode_array(&key);
struct tuple *old_tuple;
if (index_get(primary_index, key, part_count, &old_tuple) != 0)
return -1;
if (old_tuple != NULL) {
if (memtx_space->replace(space, old_tuple, NULL,
DUP_REPLACE, &old_tuple) != 0)
return -1;
tuple_unref(old_tuple);
}
return 0;
}
static int
memtx_space_ephemeral_rowid_next(struct space *space, uint64_t *rowid)
{
assert(rowid != NULL);
struct memtx_space *memtx_space = (struct memtx_space *)space;
*rowid = memtx_space->rowid++;
return 0;
}
/* }}} DML */
/* {{{ DDL */
static int
memtx_space_check_index_def(struct space *space, struct index_def *index_def)
{
struct key_def *key_def = index_def->key_def;
if (key_def->is_nullable) {
if (index_def->iid == 0) {
diag_set(ClientError, ER_NULLABLE_PRIMARY,
space_name(space));
return -1;
}
if (index_def->type != TREE) {
diag_set(ClientError, ER_UNSUPPORTED,
index_type_strs[index_def->type],
"nullable parts");
return -1;
}
}
switch (index_def->type) {
case HASH:
if (! index_def->opts.is_unique) {
diag_set(ClientError, ER_MODIFY_INDEX,
index_def->name, space_name(space),
"HASH index must be unique");
return -1;
}
if (key_def->is_multikey) {
diag_set(ClientError, ER_MODIFY_INDEX,
index_def->name, space_name(space),
"HASH index cannot be multikey");
return -1;
}
if (key_def->for_func_index) {
diag_set(ClientError, ER_MODIFY_INDEX,
index_def->name, space_name(space),
"HASH index can not use a function");
return -1;
}
break;
case TREE:
/* TREE index has no limitations. */
break;
case RTREE:
if (key_def->part_count != 1) {
diag_set(ClientError, ER_MODIFY_INDEX,
index_def->name, space_name(space),
"RTREE index key can not be multipart");
return -1;
}
if (index_def->opts.is_unique) {
diag_set(ClientError, ER_MODIFY_INDEX,
index_def->name, space_name(space),
"RTREE index can not be unique");
return -1;
}
if (key_def->parts[0].type != FIELD_TYPE_ARRAY) {
diag_set(ClientError, ER_MODIFY_INDEX,
index_def->name, space_name(space),
"RTREE index field type must be ARRAY");
return -1;
}
if (key_def->is_multikey) {
diag_set(ClientError, ER_MODIFY_INDEX,
index_def->name, space_name(space),
"RTREE index cannot be multikey");
return -1;
}
if (key_def->for_func_index) {
diag_set(ClientError, ER_MODIFY_INDEX,
index_def->name, space_name(space),
"RTREE index can not use a function");
return -1;
}
/* no furter checks of parts needed */
return 0;
case BITSET:
if (key_def->part_count != 1) {
diag_set(ClientError, ER_MODIFY_INDEX,
index_def->name, space_name(space),
"BITSET index key can not be multipart");
return -1;
}
if (index_def->opts.is_unique) {
diag_set(ClientError, ER_MODIFY_INDEX,
index_def->name, space_name(space),
"BITSET can not be unique");
return -1;
}
if (key_def->parts[0].type != FIELD_TYPE_UNSIGNED &&
key_def->parts[0].type != FIELD_TYPE_STRING &&
key_def->parts[0].type != FIELD_TYPE_VARBINARY) {
diag_set(ClientError, ER_MODIFY_INDEX,
index_def->name, space_name(space),
"BITSET index field type must be "
"NUM or STR or VARBINARY");
return -1;
}
if (key_def->is_multikey) {
diag_set(ClientError, ER_MODIFY_INDEX,
index_def->name, space_name(space),
"BITSET index cannot be multikey");
return -1;
}
if (key_def->for_func_index) {
diag_set(ClientError, ER_MODIFY_INDEX,
index_def->name, space_name(space),
"BITSET index can not use a function");
return -1;
}
/* no furter checks of parts needed */
return 0;
default:
diag_set(ClientError, ER_INDEX_TYPE,
index_def->name, space_name(space));
return -1;
}
/* Only HASH and TREE indexes checks parts there */
/* Check that there are no ANY, ARRAY, MAP parts */
for (uint32_t i = 0; i < key_def->part_count; i++) {
struct key_part *part = &key_def->parts[i];
if (part->type <= FIELD_TYPE_ANY ||
part->type >= FIELD_TYPE_INTERVAL) {
diag_set(ClientError, ER_MODIFY_INDEX,
index_def->name, space_name(space),
tt_sprintf("field type '%s' is not supported",
field_type_strs[part->type]));
return -1;
}
}
return 0;
}
static struct snapshot_iterator *
sequence_data_index_create_snapshot_iterator(struct index *index)
{
(void)index;
return sequence_data_iterator_create();
}
static struct index *
sequence_data_index_new(struct memtx_engine *memtx, struct index_def *def)
{
struct index *index = memtx_hash_index_new(memtx, def);
if (index == NULL)
return NULL;
static struct index_vtab vtab;
static bool vtab_initialized;
if (!vtab_initialized) {
vtab = *index->vtab;
vtab.create_snapshot_iterator =
sequence_data_index_create_snapshot_iterator;
vtab_initialized = true;
}
index->vtab = &vtab;
return index;
}
static struct index *
memtx_space_create_index(struct space *space, struct index_def *index_def)
{
struct memtx_engine *memtx = (struct memtx_engine *)space->engine;
if (space->def->id == BOX_SEQUENCE_DATA_ID) {
/*
* The content of _sequence_data is not updated
* when a sequence is used for auto increment in
* a space. To make sure all sequence values are
* written to snapshot, use a special snapshot
* iterator that walks over the sequence cache.
*/
return sequence_data_index_new(memtx, index_def);
}
switch (index_def->type) {
case HASH:
return memtx_hash_index_new(memtx, index_def);
case TREE:
return memtx_tree_index_new(memtx, index_def);
case RTREE:
return memtx_rtree_index_new(memtx, index_def);
case BITSET:
return memtx_bitset_index_new(memtx, index_def);
default:
unreachable();
return NULL;
}
}
/**
* Replicate engine state in a newly created space.
* This function is invoked when executing a replace into _index
* space originating either from a snapshot or from the binary
* log. It brings the newly created space up to date with the
* engine recovery state: if the event comes from the snapshot,
* then the primary key is not built, otherwise it's created
* right away.
*/
static int
memtx_space_add_primary_key(struct space *space)
{
struct memtx_space *memtx_space = (struct memtx_space *)space;
struct memtx_engine *memtx = (struct memtx_engine *)space->engine;
switch (memtx->state) {
case MEMTX_INITIALIZED:
panic("can't create a new space before snapshot recovery");
break;
case MEMTX_INITIAL_RECOVERY:
index_begin_build(space->index[0]);
memtx_space->replace = memtx_space_replace_build_next;
break;
case MEMTX_FINAL_RECOVERY:
memtx_space->replace = memtx_space_replace_primary_key;
break;
case MEMTX_OK:
memtx_space->replace = memtx_space_replace_all_keys;
break;
}
return 0;
}
/*
* Ongoing index build or format check state used by
* corrseponding on_replace triggers.
*/
struct memtx_ddl_state {
/* The index being built. */
struct index *index;
/* New format to be enforced. */
struct tuple_format *format;
/* Operation cursor. Marks the last processed tuple to date */
struct tuple *cursor;
/* Primary key key_def to compare new tuples with cursor. */
struct key_def *cmp_def;
struct diag diag;
int rc;
};
static int
memtx_check_on_replace(struct trigger *trigger, void *event)
{
struct txn *txn = event;
struct memtx_ddl_state *state = trigger->data;
struct txn_stmt *stmt = txn_current_stmt(txn);
/* Nothing to check on DELETE. */
if (stmt->new_tuple == NULL)
return 0;
/* We have already failed. */
if (state->rc != 0)
return 0;
/*
* Only check format for already processed part of the space,
* all the tuples inserted below cursor will be checked by the
* main routine later.
*/
if (tuple_compare(state->cursor, HINT_NONE, stmt->new_tuple, HINT_NONE,
state->cmp_def) < 0)
return 0;
state->rc = memtx_tuple_validate(state->format, stmt->new_tuple);
if (state->rc != 0)
diag_move(diag_get(), &state->diag);
return 0;
}
static int
memtx_space_check_format(struct space *space, struct tuple_format *format)
{
struct txn *txn = in_txn();
if (space->index_count == 0)
return 0;
struct index *pk = space->index[0];
if (index_size(pk) == 0)
return 0;
if (txn_check_singlestatement(txn, "space format check") != 0)
return -1;
struct iterator *it = index_create_iterator(pk, ITER_ALL, NULL, 0);
if (it == NULL)
return -1;
struct memtx_engine *memtx = (struct memtx_engine *)space->engine;
struct memtx_ddl_state state;
state.format = format;
state.cmp_def = pk->def->key_def;
state.rc = 0;
diag_create(&state.diag);
struct trigger on_replace;
trigger_create(&on_replace, memtx_check_on_replace, &state, NULL);
trigger_add(&space->on_replace, &on_replace);
int rc;
struct tuple *tuple;
size_t count = 0;
while ((rc = iterator_next_raw(it, &tuple)) == 0 && tuple != NULL) {
/*
* Check that the tuple is OK according to the
* new format.
*/
rc = memtx_tuple_validate(format, tuple);