/
external_sort.c
4968 lines (4245 loc) · 138 KB
/
external_sort.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 2008 Search Solution Corporation
* Copyright 2016 CUBRID Corporation
*
* 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.
*
*/
/*
* external_sort.c - External sorting module
*/
#ident "$Id$"
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <math.h>
#include "error_manager.h"
#include "system_parameter.h"
#include "memory_alloc.h"
#include "external_sort.h"
#include "file_manager.h"
#include "page_buffer.h"
#include "log_manager.h"
#include "disk_manager.h"
#include "slotted_page.h"
#include "overflow_file.h"
#include "boot_sr.h"
#if defined(ENABLE_SYSTEMTAP)
#include "probes.h"
#endif /* ENABLE_SYSTEMTAP */
#if defined(SERVER_MODE)
#include "connection_error.h"
#endif /* SERVER_MODE */
#include "server_support.h"
#include "thread_entry_task.hpp"
#include "thread_manager.hpp" // for thread_get_thread_entry_info and thread_sleep
#include <functional>
/* Estimate on number of pages in the multipage temporary file */
#define SORT_MULTIPAGE_FILE_SIZE_ESTIMATE 20
/* Upper limit on the half of the total number of the temporary files.
* The exact upper limit on total number of temp files is twice this number.
* (i.e., this number specifies the upper limit on the number of total input
* or total output files at each stage of the merging process.
*/
#define SORT_MAX_HALF_FILES 4
/* Lower limit on the half of the total number of the temporary files.
* The exact lower limit on total number of temp files is twice this number.
* (i.e., this number specifies the lower limit on the number of total input
* or total output files at each stage of the merging process.
*/
#define SORT_MIN_HALF_FILES 2
/* Initial size of the dynamic array that keeps the file contents list */
#define SORT_INITIAL_DYN_ARRAY_SIZE 30
/* Expansion Ratio of the dynamic array that keeps the file contents list */
#define SORT_EXPAND_DYN_ARRAY_RATIO 1.5
#define SORT_MAXREC_LENGTH \
((ssize_t)(DB_PAGESIZE - sizeof(SLOTTED_PAGE_HEADER) - sizeof(SLOT)))
#define SORT_SWAP_PTR(a,b) { char **temp; temp = a; a = b; b = temp; }
#define SORT_CHECK_DUPLICATE(a, b) \
do { \
if (cmp == 0) { \
if (option == SORT_DUP) \
sort_append(a, b); \
*(a) = NULL; \
dup_num++; \
} \
} while (0)
typedef struct file_contents FILE_CONTENTS;
struct file_contents
{ /* node of the file_contents linked list */
int *num_pages; /* Dynamic array whose elements keep the sizes of the runs contained in the file in
* terms of number of slotted pages it occupies */
int num_slots; /* Total number of elements the array has */
int first_run; /* The index of the array element keeping the size of the first run of the file. */
int last_run; /* The index of the array element keeping the size of the last run of the file. */
};
typedef struct vol_info VOL_INFO;
struct vol_info
{ /* volume information */
INT16 volid; /* Volume identifier */
int file_cnt; /* Current number of files in the volume */
};
typedef struct vol_list VOL_LIST;
struct vol_list
{ /* temporary volume identifier list */
INT16 volid; /* main sorting disk volume */
int vol_ent_cnt; /* number of array entries */
int vol_cnt; /* number of temporary volumes */
VOL_INFO *vol_info; /* array of volume information */
};
/* Parallel eXecution and communition node */
typedef struct px_tree_node PX_TREE_NODE;
struct px_tree_node
{
int px_id; /* node ID */
#if defined(SERVER_MODE)
int px_status; /* node status; access through px_mtx */
#endif /* SERVER_MODE */
int px_height; /* tournament tree: node level */
int px_myself; /* tournament tree: node ID */
int px_tran_index;
void *px_arg; /* operation info */
char **px_buff;
char **px_vector;
long px_vector_size;
char **px_result; /* output */
long px_result_size; /* output */
};
typedef struct sort_param SORT_PARAM;
struct sort_param
{
VFID temp[2 * SORT_MAX_HALF_FILES]; /* Temporary file identifiers */
VFID multipage_file; /* Temporary file for multi page sorting records */
FILE_CONTENTS file_contents[2 * SORT_MAX_HALF_FILES]; /* Contents of each temporary file */
bool tde_encrypted; /* whether related temp files are encrypted (TDE) or not */
VOL_LIST vol_list; /* Temporary volume information list */
char *internal_memory; /* Internal_memory used for internal sorting phase and as input/output buffers for temp
* files during merging phase */
int tot_runs; /* Total number of runs */
int tot_buffers; /* Size of internal memory used in terms of number of buffers it occupies */
int tot_tempfiles; /* Total number of temporary files */
int half_files; /* Half number of temporary files */
int in_half; /* Which half of temp files is for input */
void *out_arg; /* Arguments to pass to the output function */
/* Comparison function to use in the internal sorting and the merging phases */
SORT_CMP_FUNC *cmp_fn;
void *cmp_arg;
SORT_DUP_OPTION option;
/* output function to apply on temporary records */
SORT_PUT_FUNC *put_fn;
void *put_arg;
/* Estimated number of pages in each temp file (used in initialization) */
int tmp_file_pgs;
/* Details about the "limit" clause */
int limit;
/* support parallelism */
#if defined(SERVER_MODE)
pthread_mutex_t px_mtx; /* px_node status mutex */
#endif
int px_height_max; /* px_node tournament tree max level */
int px_array_size; /* px_node array size */
PX_TREE_NODE *px_array; /* px_node array */
};
typedef struct sort_rec_list SORT_REC_LIST;
struct sort_rec_list
{
struct sort_rec_list *next; /* next sorted record item */
int rec_pos; /* record position */
bool is_duplicated; /* duplicated sort_key record flag */
}; /* Sort record list */
typedef struct slotted_pheader SLOTTED_PAGE_HEADER;
struct slotted_pheader
{
INT16 nslots; /* Number of allocated slots for the page */
INT16 nrecs; /* Number of records on page */
INT16 anchor_flag; /* Valid ANCHORED, ANCHORED_DONT_REUSE_SLOTS UNANCHORED_ANY_SEQUENCE,
* UNANCHORED_KEEP_SEQUENCE */
INT16 alignment; /* Alignment for records. */
INT16 waste_align; /* Number of bytes waste because of alignment */
INT16 tfree; /* Total free space on page */
INT16 cfree; /* Contiguous free space on page */
INT16 foffset; /* Byte offset from the beginning of the page to the first free byte area on the page. */
};
typedef struct slot SLOT;
struct slot
{
INT16 roffset; /* Byte Offset from the beginning of the page to the beginning of the record */
INT16 rlength; /* Length of record */
INT16 rtype; /* Record type described by slot. */
};
typedef struct run_struct RUN;
struct run_struct
{
long start;
long stop;
};
typedef struct srun SRUN;
struct srun
{
char low_high; /* location info LOW('L') : otherbase HIGH('H') : base */
unsigned short tree_depth; /* depth of this node : leaf is 1 */
long start;
long stop;
};
typedef struct sort_stack SORT_STACK;
struct sort_stack
{
int top;
SRUN *srun;
};
typedef void FIND_RUN_FN (char **, long *, SORT_STACK *, long, SORT_CMP_FUNC *, void *);
typedef void MERGE_RUN_FN (char **, char **, SORT_STACK *, SORT_CMP_FUNC *, void *);
#if !defined(NDEBUG)
static int sort_validate (char **vector, long size, SORT_CMP_FUNC * compare, void *comp_arg);
#endif
static PX_TREE_NODE *px_sort_assign (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, int px_id, char **px_buff,
char **px_vector, long px_vector_size, int px_height, int px_myself);
static int px_sort_myself (THREAD_ENTRY * thread_p, PX_TREE_NODE * px_node);
#if defined(SERVER_MODE)
static int px_sort_communicate (PX_TREE_NODE * px_node);
#endif
static int sort_inphase_sort (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, SORT_GET_FUNC * get_next,
void *arguments, unsigned int *total_numrecs);
static int sort_exphase_merge_elim_dup (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param);
static int sort_exphase_merge (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param);
static int sort_get_avg_numpages_of_nonempty_tmpfile (SORT_PARAM * sort_param);
static void sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param);
static int sort_add_new_file (THREAD_ENTRY * thread_p, VFID * vfid, int file_pg_cnt_est, bool force_alloc,
bool tde_encrypted);
static int sort_write_area (THREAD_ENTRY * thread_p, VFID * vfid, int first_page, INT32 num_pages, char *area_start);
static int sort_read_area (THREAD_ENTRY * thread_p, VFID * vfid, int first_page, INT32 num_pages, char *area_start);
static int sort_get_num_half_tmpfiles (int tot_buffers, int input_pages);
static int sort_checkalloc_numpages_of_outfiles (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param);
static int sort_get_numpages_of_active_infiles (const SORT_PARAM * sort_param);
static int sort_find_inbuf_size (int tot_buffers, int in_sections);
static char *sort_retrieve_longrec (THREAD_ENTRY * thread_p, RECDES * address, RECDES * memory);
static char **sort_run_sort (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, char **base, long limit,
long sort_numrecs, char **otherbase, long *srun_limit);
static int sort_run_add_new (FILE_CONTENTS * file_contents, int num_pages);
static void sort_run_remove_first (FILE_CONTENTS * file_contents);
static void sort_run_flip (char **start, char **stop);
static void sort_run_find (char **source, long *top, SORT_STACK * st_p, long limit, SORT_CMP_FUNC * compare,
void *comp_arg, SORT_DUP_OPTION option);
static void sort_run_merge (char **low, char **high, SORT_STACK * st_p, SORT_CMP_FUNC * compare, void *comp_arg,
SORT_DUP_OPTION option);
static int sort_run_flush (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, int out_curfile, int *cur_page,
char *output_buffer, char **index_area, int numrecs, int rec_type);
static int sort_get_num_file_contents (FILE_CONTENTS * file_contents);
#if defined(CUBRID_DEBUG)
static void sort_print_file_contents (const FILE_CONTENTS * file_contents);
#endif /* CUBRID_DEBUG */
static void sort_spage_initialize (PAGE_PTR pgptr, INT16 slots_type, INT16 alignment);
static INT16 sort_spage_get_numrecs (PAGE_PTR pgptr);
static INT16 sort_spage_insert (PAGE_PTR pgptr, RECDES * recdes);
static SCAN_CODE sort_spage_get_record (PAGE_PTR pgptr, INT16 slotid, RECDES * recdes, bool peek_p);
static int sort_spage_offsetcmp (const void *s1, const void *s2);
static int sort_spage_compact (PAGE_PTR pgptr);
static INT16 sort_spage_find_free (PAGE_PTR pgptr, SLOT ** sptr, INT16 length, INT16 type, INT16 * space);
#if defined(CUBRID_DEBUG)
static INT16 sort_spage_dump_sptr (SLOT * sptr, INT16 nslots, INT16 alignment);
static void sort_spage_dump_hdr (SLOTTED_PAGE_HEADER * sphdr);
static void sort_spage_dump (PAGE_PTR pgptr, int rec_p);
#endif /* CUBRID_DEBUG */
static void sort_append (const void *pk0, const void *pk1);
/*
* sort_spage_initialize () - Initialize a slotted page
* return: void
* pgptr(in): Pointer to slotted page
* slots_type(in): Flag which indicates the type of slots
* alignment(in): Type of alignment
*
* Note: A slotted page must be initialized before records are inserted on the
* page. The alignment indicates the valid offset where the records
* should be stored. This is a requirment for peeking records on pages
* according to their alignmnet restrictions.
*/
static void
sort_spage_initialize (PAGE_PTR pgptr, INT16 slots_type, INT16 alignment)
{
SLOTTED_PAGE_HEADER *sphdr;
sphdr = (SLOTTED_PAGE_HEADER *) pgptr;
sphdr->nslots = 0;
sphdr->nrecs = 0;
#if defined(CUBRID_DEBUG)
if (!spage_is_valid_anchor_type (slots_type))
{
(void) fprintf (stderr,
"sort_spage_initialize: **INTERFACE SYSTEM ERROR BAD value for slots_type = %d.\n"
" UNANCHORED_KEEP_SEQUENCE was assumed **\n", slots_type);
slots_type = UNANCHORED_KEEP_SEQUENCE;
}
if (!(alignment == CHAR_ALIGNMENT || alignment == SHORT_ALIGNMENT || alignment == INT_ALIGNMENT
|| alignment == LONG_ALIGNMENT || alignment == FLOAT_ALIGNMENT || alignment == DOUBLE_ALIGNMENT))
{
(void) fprintf (stderr,
"sort_spage_initialize: **INTERFACE SYSTEM ERROR BAD value = %d"
" for alignment. %d was assumed\n. Alignment must be"
" either SIZEOF char, short, int, long, float, or double", alignment, MAX_ALIGNMENT);
alignment = MAX_ALIGNMENT;
}
#endif /* CUBRID_DEBUG */
sphdr->anchor_flag = slots_type;
sphdr->tfree = DB_PAGESIZE - sizeof (SLOTTED_PAGE_HEADER);
sphdr->cfree = sphdr->tfree;
sphdr->foffset = sizeof (SLOTTED_PAGE_HEADER);
sphdr->alignment = alignment;
sphdr->waste_align = DB_WASTED_ALIGN (sphdr->foffset, alignment);
if (sphdr->waste_align != 0)
{
sphdr->foffset += sphdr->waste_align;
sphdr->cfree -= sphdr->waste_align;
sphdr->tfree -= sphdr->waste_align;
}
}
/*
* sort_spage_get_numrecs () - Return the total number of records on the slotted page
* return:
* pgptr(in): Pointer to slotted page
*/
static INT16
sort_spage_get_numrecs (PAGE_PTR pgptr)
{
SLOTTED_PAGE_HEADER *sphdr;
sphdr = (SLOTTED_PAGE_HEADER *) pgptr;
return sphdr->nrecs;
}
/*
* sort_spage_offsetcmp () - Compare the location (offset) of slots
* return: sp1 - sp2
* sp1(in): slot 1
* sp2(in): slot 2
*/
static int
sort_spage_offsetcmp (const void *sp1, const void *sp2)
{
SLOT *s1, *s2;
INT16 l1, l2;
s1 = *(SLOT **) sp1;
s2 = *(SLOT **) sp2;
l1 = s1->roffset;
l2 = s2->roffset;
return (l1 < l2) ? -1 : (l1 == l2) ? 0 : 1;
}
/*
* sort_spage_compact () - Compact an slotted page
* return: NO_ERROR
* pgptr(in): Pointer to slotted page
*
* Note: Only the records are compacted, the slots are not compacted.
*/
static int
sort_spage_compact (PAGE_PTR pgptr)
{
int i, j;
SLOTTED_PAGE_HEADER *sphdr;
SLOT *sptr;
SLOT **sortptr;
INT16 to_offset;
int ret = NO_ERROR;
sphdr = (SLOTTED_PAGE_HEADER *) pgptr;
sortptr = (SLOT **) calloc ((unsigned) sphdr->nrecs, sizeof (SLOT *));
if (sortptr == NULL)
{
er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_OUT_OF_VIRTUAL_MEMORY, 1, sphdr->nrecs * sizeof (SLOT *));
return ER_OUT_OF_VIRTUAL_MEMORY;
}
/* Populate the array to sort and then sort it */
sptr = (SLOT *) ((char *) pgptr + DB_PAGESIZE - sizeof (SLOT));
for (j = 0, i = 0; i < sphdr->nslots; sptr--, i++)
{
if (sptr->roffset != NULL_OFFSET)
{
sortptr[j++] = sptr;
}
}
qsort ((void *) sortptr, (size_t) sphdr->nrecs, (size_t) sizeof (SLOT *), sort_spage_offsetcmp);
to_offset = sizeof (SLOTTED_PAGE_HEADER);
for (i = 0; i < sphdr->nrecs; i++)
{
/* Make sure that the offset is aligned */
to_offset = DB_ALIGN (to_offset, sphdr->alignment);
if (to_offset == sortptr[i]->roffset)
{
/* Record slot is already in place */
to_offset += sortptr[i]->rlength;
}
else
{
/* Move the record */
memmove ((char *) pgptr + to_offset, (char *) pgptr + sortptr[i]->roffset, sortptr[i]->rlength);
sortptr[i]->roffset = to_offset;
to_offset += sortptr[i]->rlength;
}
}
/* Make sure that the next inserted record will be aligned */
to_offset = DB_ALIGN (to_offset, sphdr->alignment);
sphdr->cfree = sphdr->tfree = (DB_PAGESIZE - to_offset - sphdr->nslots * sizeof (SLOT));
sphdr->foffset = to_offset;
free_and_init (sortptr);
/* The page is set dirty somewhere else */
return ret;
}
/*
* sort_spage_find_free () - Find a free area/slot where a record of the given length
* can be inserted onto the given slotted page
* return: A slot identifier or NULL_SLOTID
* pgptr(in): Pointer to slotted page
* sptr(out): Pointer to slotted page array pointer
* length(in): Length of area/record
* type(in): Type of record to be inserted
* space(out): Space used/defined
*
* Note: If there is not enough space on the page, an error condition is
* indicated and NULLSLOTID is returned.
*/
static INT16
sort_spage_find_free (PAGE_PTR pgptr, SLOT ** sptr, INT16 length, INT16 type, INT16 * space)
{
SLOTTED_PAGE_HEADER *sphdr;
INT16 slotid;
INT16 waste;
sphdr = (SLOTTED_PAGE_HEADER *) pgptr;
/* Calculate the wasting space that this record will introduce. We need to take in consideration the wasting space
* when there is space saved */
waste = DB_WASTED_ALIGN (length, sphdr->alignment);
*space = length + waste;
/* Quickly check for available space. We may need to check again if a slot is created (instead of reused) */
if (*space > sphdr->tfree)
{
*sptr = NULL;
*space = 0;
return NULL_SLOTID;
}
/* Find a free slot. Try to reuse an unused slotid, instead of allocating a new one */
*sptr = (SLOT *) ((char *) pgptr + DB_PAGESIZE - sizeof (SLOT));
if (sphdr->nslots == sphdr->nrecs)
{
slotid = sphdr->nslots;
*sptr -= slotid;
}
else
{
for (slotid = 0; slotid < sphdr->nslots; (*sptr)--, slotid++)
{
if ((*sptr)->rtype == REC_HOME || (*sptr)->rtype == REC_BIGONE)
{
; /* go ahead */
}
else
{ /* impossible case */
assert (false);
break;
}
}
}
/* Make sure that there is enough space for the record and the slot */
assert (slotid <= sphdr->nslots);
if (slotid >= sphdr->nslots)
{
*space += sizeof (SLOT);
/* We are allocating a new slotid. Check for available space again */
if (*space > sphdr->tfree)
{
*sptr = NULL;
*space = 0;
return NULL_SLOTID;
}
else if (*space > sphdr->cfree && sort_spage_compact (pgptr) != NO_ERROR)
{
*sptr = NULL;
*space = 0;
return NULL_SLOTID;
}
/* Adjust the number of slots */
sphdr->nslots++;
}
else
{
/* We already know that there is total space available since the slot is reused and the space was check above */
if (*space > sphdr->cfree && sort_spage_compact (pgptr) != NO_ERROR)
{
*sptr = NULL;
*space = 0;
return NULL_SLOTID;
}
}
/* Now separate an empty area for the record */
(*sptr)->roffset = sphdr->foffset;
(*sptr)->rlength = length;
(*sptr)->rtype = type;
/* Adjust the header */
sphdr->nrecs++;
sphdr->tfree -= *space;
sphdr->cfree -= *space;
sphdr->foffset += length + waste;
sphdr->waste_align += waste;
/* The page is set dirty somewhere else */
return slotid;
}
/*
* sort_spage_insert () - Insert a record onto the given slotted page
* return: A slot identifier
* pgptr(in): Pointer to slotted page
* recdes(in): Pointer to a record descriptor
*
* Note: If the record does not fit on the page, an error condition is
* indicated and NULL_SLOTID is returned.
*/
static INT16
sort_spage_insert (PAGE_PTR pgptr, RECDES * recdes)
{
SLOTTED_PAGE_HEADER *sphdr;
SLOT *sptr;
INT16 slotid;
INT16 used_space;
if (recdes->length > SORT_MAXREC_LENGTH)
{
return NULL_SLOTID;
}
assert (recdes->type == REC_HOME || recdes->type == REC_BIGONE);
slotid = sort_spage_find_free (pgptr, &sptr, recdes->length, recdes->type, &used_space);
if (slotid != NULL_SLOTID)
{
/* Find the free slot and insert the record */
memcpy (((char *) pgptr + sptr->roffset), recdes->data, recdes->length);
/* Indicate that we are spending our savings */
sphdr = (SLOTTED_PAGE_HEADER *) pgptr;
}
return slotid;
}
/*
* sort_spage_get_record () - Get specific record
* return: S_SUCCESS, S_DOESNT_FIT, S_DOESNT_EXIST
* pgptr(in): Pointer to slotted page
* slotid(in): Slot identifier of current record
* recdes(in): Pointer to a record descriptor
* peek_p(in): Indicates whether the record is going to be copied or peeked
*
* Note: When ispeeking is PEEK, the desired available record is peeked onto
* the page. The address of the record descriptor is set to the portion
* of the buffer where the record is stored. Peeking a record should be
* executed with caution since the slotted module may decide to move
* the record around. In general, no other operation should be executed
* on the page until the peeking of the record is done. The page should
* be fixed and locked to avoid any funny behavior. RECORD should NEVER
* be MODIFIED DIRECTLY. Only reads should be performed, otherwise
* header information and other records may be corrupted.
*
* When ispeeking is COPY, the desired available record is
* read onto the area pointed by the record descriptor. If the record
* does not fit in such an area, the length of the record is returned
* as a negative value in recdes->length and an error is indicated in the
* return value.
*/
static SCAN_CODE
sort_spage_get_record (PAGE_PTR pgptr, INT16 slotid, RECDES * recdes, bool peek_p)
{
SLOTTED_PAGE_HEADER *sphdr;
SLOT *sptr;
sphdr = (SLOTTED_PAGE_HEADER *) pgptr;
sptr = (SLOT *) ((char *) pgptr + DB_PAGESIZE - sizeof (SLOT));
sptr -= slotid;
if (slotid < 0 || slotid >= sphdr->nslots || sptr->roffset == NULL_OFFSET)
{
recdes->length = 0;
return S_DOESNT_EXIST;
}
/*
* If peeking, the address of the data in the descriptor is set to the
* address of the record in the buffer. Otherwise, the record is copied
* onto the area specified by the descriptor
*/
if (peek_p == PEEK)
{
recdes->area_size = -1;
recdes->data = (char *) pgptr + sptr->roffset;
}
else
{
/* copy the record */
if (sptr->rlength > recdes->area_size)
{
/*
* DOES NOT FIT
* Give a hint to the user of the needed length. Hint is given as a
* negative value
*/
recdes->length = -sptr->rlength;
return S_DOESNT_FIT;
}
memcpy (recdes->data, (char *) pgptr + sptr->roffset, sptr->rlength);
}
recdes->length = sptr->rlength;
recdes->type = sptr->rtype;
return S_SUCCESS;
}
#if defined(CUBRID_DEBUG)
/*
* sort_spage_dump_sptr () - Dump the slotted page array
* return: Total length of records
* sptr(in): Pointer to slotted page pointer array
* nslots(in): Number of slots
* alignment(in): Alignment for records
*
* Note: The content of the record is not dumped by this function.
* This function is used for debugging purposes.
*/
static INT16
sort_spage_dump_sptr (SLOT * sptr, INT16 nslots, INT16 alignment)
{
int i;
INT16 waste;
INT16 total_length_records = 0;
for (i = 0; i < nslots; sptr--, i++)
{
assert (sptr->rtype == REC_HOME || sptr->rtype == REC_BIGONE);
(void) fprintf (stdout, "\nSlot-id = %2d, offset = %4d, type = %s", i, sptr->roffset,
((sptr->rtype == REC_HOME) ? "HOME" : (sptr->rtype ==
REC_BIGONE) ? "BIGONE" : "UNKNOWN-BY-CURRENT-MODULE"));
if (sptr->roffset != NULL_OFFSET)
{
total_length_records += sptr->rlength;
waste = DB_WASTED_ALIGN (sptr->rlength, alignment);
(void) fprintf (stdout, ", length = %4d, waste = %d", sptr->rlength, waste);
}
(void) fprintf (stdout, "\n");
}
return total_length_records;
}
/*
* sort_spage_dump_hdr () - Dump an slotted page header
* return: void
* sphdr(in): Pointer to header of slotted page
*
* Note: This function is used for debugging purposes.
*/
static void
sort_spage_dump_hdr (SLOTTED_PAGE_HEADER * sphdr)
{
(void) fprintf (stdout, "NUM SLOTS = %d, NUM RECS = %d, TYPE OF SLOTS = %s,\n", sphdr->nslots, sphdr->nrecs,
spage_anchor_flag_string (sphdr->anchor_flag));
(void) fprintf (stdout, "ALIGNMENT-TO = %s, WASTED AREA FOR ALIGNMENT = %d,\n",
spage_alignment_string (sphdr->alignment), spage_waste_align);
(void) fprintf (stdout, "TOTAL FREE AREA = %d, CONTIGUOUS FREE AREA = %d, FREE SPACE OFFSET = %d,\n", sphdr->tfree,
sphdr->cfree, sphdr->foffset);
}
/*
* sort_spage_dump () - Dump an slotted page
* return: void
* pgptr(in): Pointer to slotted page
* rec_p(in): If true, records are printed in ascii format, otherwise, the
* records are not printed
*
* Note: The records are printed only when the value of rec_p is true.
* This function is used for debugging purposes.
*/
static void
sort_spage_dump (PAGE_PTR pgptr, int rec_p)
{
int i, j;
SLOTTED_PAGE_HEADER *sphdr;
SLOT *sptr;
char *rec;
int used_length = 0;
sphdr = (SLOTTED_PAGE_HEADER *) pgptr;
sort_spage_dump_hdr (sphdr);
sptr = (SLOT *) ((char *) pgptr + DB_PAGESIZE - sizeof (SLOT));
used_length = (sizeof (SLOTTED_PAGE_HEADER) + sphdr->waste_align + sizeof (SLOT) * sphdr->nslots);
used_length += sort_spage_dump_sptr (sptr, sphdr->nslots, sphdr->alignment);
if (rec_p)
{
(void) fprintf (stdout, "\nRecords in ascii follow ...\n");
for (i = 0; i < sphdr->nslots; sptr--, i++)
{
if (sptr->roffset != NULL_OFFSET)
{
(void) fprintf (stdout, "\nSlot-id = %2d\n", i);
rec = (char *) pgptr + sptr->roffset;
for (j = 0; j < sptr->rlength; j++)
{
(void) fputc (*rec++, stdout);
}
(void) fprintf (stdout, "\n");
}
else
{
(void) fprintf (stdout, "\nSlot-id = %2d has been deleted\n", i);
}
}
}
if (used_length + sphdr->tfree > DB_PAGESIZE)
{
(void) fprintf (stdout,
"sort_spage_dump: Inconsistent page \n (Used_space + tfree > DB_PAGESIZE\n (%d + %d) > %d \n "
" %d > %d\n", used_length, sphdr->tfree, DB_PAGESIZE, used_length + sphdr->tfree, DB_PAGESIZE);
}
if ((sphdr->cfree + sphdr->foffset + sizeof (SLOT) * sphdr->nslots) > DB_PAGESIZE)
{
(void) fprintf (stdout,
"sort_spage_dump: Inconsistent page\n (cfree + foffset + SIZEOF(SLOT) * nslots) > "
" DB_PAGESIZE\n (%d + %d + (%d * %d)) > %d\n %d > %d\n", sphdr->cfree, sphdr->foffset,
sizeof (SLOT), sphdr->nslots, DB_PAGESIZE,
(sphdr->cfree + sphdr->foffset + sizeof (SLOT) * sphdr->nslots), DB_PAGESIZE);
}
if (sphdr->cfree <= -(sphdr->alignment - 1))
{
(void) fprintf (stdout, "sort_spage_dump: Cfree %d is inconsistent in page. Cannot be < -%d\n", sphdr->cfree,
sphdr->alignment);
}
}
#endif /* CUBRID_DEBUG */
/* Sorting module functions */
/*
* sort_run_flip () - Flip a run in place
* return: void
* start(in):
* stop(in):
*
* Note: Odd runs will have middle pointer undisturbed.
*/
static void
sort_run_flip (char **start, char **stop)
{
char *temp;
while (start < stop)
{
temp = *start;
*start = *stop;
*stop = temp;
start++;
stop--;
}
return;
}
/*
* sort_append () -
* return: void
* pk0(in):
* pk1(in):
*/
static void
sort_append (const void *pk0, const void *pk1)
{
SORT_REC *node, *list;
node = *(SORT_REC **) pk0;
list = *(SORT_REC **) pk1;
while (list->next)
{
list = list->next;
}
list->next = node;
return;
}
/*
* sort_run_find () - Finds the longest ascending or descending run it can
* return:
* source(in):
* top(in):
* st_p(in):
* limit(in):
* compare(in):
* comp_arg(in):
* option(in):
*
* Note: Flip descending run, and assign RUN start and stop
*/
static void
sort_run_find (char **source, long *top, SORT_STACK * st_p, long limit, SORT_CMP_FUNC * compare, void *comp_arg,
SORT_DUP_OPTION option)
{
char **start;
char **stop;
char **next_stop;
char **limit_p;
SRUN *srun_p;
char **dup;
char **non_dup;
int dup_num;
int cmp;
bool increasing_order;
/* init new SRUN */
st_p->top++;
srun_p = &(st_p->srun[st_p->top]);
srun_p->tree_depth = 1;
srun_p->low_high = 'H';
srun_p->start = *top;
if (*top >= (limit - 1))
{
/* degenerate run length 1. Must go ahead and compare with length 2, because we may need to flip them */
srun_p->stop = limit - 1;
*top = limit;
return;
}
start = &source[*top];
stop = start + 1;
next_stop = stop + 1;
limit_p = &source[limit];
dup_num = 0;
/* have a non-trivial run of length 2 or more */
cmp = (*compare) (start, stop, comp_arg);
if (cmp > 0)
{
increasing_order = false; /* mark as non-increasing order run */
while (next_stop < limit_p && ((cmp = (*compare) (stop, next_stop, comp_arg)) >= 0))
{
/* mark duplicate as NULL */
SORT_CHECK_DUPLICATE (stop, next_stop); /* increase dup_num */
stop = next_stop;
next_stop = next_stop + 1;
}
}
else
{
increasing_order = true; /* mark as increasing order run */
/* mark duplicate as NULL */
SORT_CHECK_DUPLICATE (start, stop); /* increase dup_num */
/* build increasing order run */
while (next_stop < limit_p && ((cmp = (*compare) (stop, next_stop, comp_arg)) <= 0))
{
/* mark duplicate as NULL */
SORT_CHECK_DUPLICATE (stop, next_stop); /* increase dup_num */
stop = next_stop;
next_stop = next_stop + 1;
}
}
/* eliminate duplicates; right-shift slots */
if (dup_num)
{
dup = stop - 1;
for (non_dup = dup - 1; non_dup >= start; dup--, non_dup--)
{
/* find duplicated value slot */
if (*dup == NULL)
{
/* find previous non-duplicated value slot */
for (; non_dup >= start; non_dup--)
{
/* move non-duplicated value slot to duplicated value slot */
if (*non_dup != NULL)
{
*dup = *non_dup;
*non_dup = NULL;
break;
}
}
}
}
}
/* change non-increasing order run to increasing order run */
if (increasing_order != true)
{
sort_run_flip (start + dup_num, stop);
}
*top += CAST_BUFLEN (stop - start); /* advance to last visited */
srun_p->start += dup_num;
srun_p->stop = *top;
(*top)++; /* advance to next unvisited element */
return;
}
/*
* sort_run_merge () - Merges two runs from source to dest, updateing dest_top
* return:
* low(in):
* high(in):