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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
|
/*===========================================================================
*
* PUBLIC DOMAIN NOTICE
* National Center for Biotechnology Information
*
* This software/database is a "United States Government Work" under the
* terms of the United States Copyright Act. It was written as part of
* the author's official duties as a United States Government employee and
* thus cannot be copyrighted. This software/database is freely available
* to the public for use. The National Library of Medicine and the U.S.
* Government have not placed any restriction on its use or reproduction.
*
* Although all reasonable efforts have been taken to ensure the accuracy
* and reliability of the software and data, the NLM and the U.S.
* Government do not and cannot warrant the performance or results that
* may be obtained by using this software or data. The NLM and the U.S.
* Government disclaim all warranties, express or implied, including
* warranties of performance, merchantability or fitness for any particular
* purpose.
*
* Please cite the author in any work or product based on this material.
*
* ===========================================================================
*
*/
typedef struct TablePair StdTblPair;
#define TBLPAIR_IMPL StdTblPair
#include "tbl-pair.h"
#include "col-pair.h"
#include "db-pair.h"
#include "meta-pair.h"
#include "row-set-priv.h"
#include "ctx.h"
#include "caps.h"
#include "except.h"
#include "status.h"
#include "mem.h"
#include "sra-sort.h"
#include <vdb/table.h>
#include <vdb/cursor.h>
#include <vdb/vdb-priv.h>
#include <kdb/meta.h>
#include <klib/printf.h>
#include <klib/text.h>
#include <klib/namelist.h>
#include <klib/rc.h>
#include <kproc/thread.h> /* KThreadWait */
#include <string.h>
FILE_ENTRY ( tbl-pair );
/*--------------------------------------------------------------------------
* StdTblPair
* generic database object pair
*/
static
void StdTblPairWhack ( StdTblPair *self, const ctx_t *ctx )
{
FUNC_ENTRY ( ctx );
TablePairDestroy ( self, ctx );
MemFree ( ctx, self, sizeof * self );
}
static
void StdTblPairDummyStub ( StdTblPair *self, const ctx_t *ctx )
{
}
static
ColumnPair *StdTblPairMakeColumnPair ( StdTblPair *self, const ctx_t *ctx,
struct ColumnReader *reader, struct ColumnWriter *writer, const char *colspec, bool large )
{
FUNC_ENTRY ( ctx );
return TablePairMakeColumnPair ( self, ctx, reader, writer, colspec, large );
}
static
RowSetIterator *StdTblPairGetRowSetIterator ( StdTblPair *self, const ctx_t *ctx, bool mapped, bool large )
{
FUNC_ENTRY ( ctx );
return TablePairMakeRowSetIterator ( self, ctx, NULL, mapped, large );
}
static TablePair_vt StdTblPair_vt =
{
StdTblPairWhack,
StdTblPairDummyStub,
StdTblPairDummyStub,
StdTblPairMakeColumnPair,
StdTblPairDummyStub,
StdTblPairDummyStub,
StdTblPairGetRowSetIterator
};
/*--------------------------------------------------------------------------
* TablePair
* interface code
*/
/* Make
* makes an object based upon open source and destination VDatabase objects
*/
TablePair *TablePairMake ( const ctx_t *ctx,
const VTable *src, VTable *dst, const char *name, const char *opt_full_spec, bool reorder )
{
FUNC_ENTRY ( ctx );
StdTblPair *tbl;
TRY ( tbl = MemAlloc ( ctx, sizeof * tbl, false ) )
{
TRY ( TablePairInit ( tbl, ctx, & StdTblPair_vt, src, dst, name, opt_full_spec, reorder ) )
{
return tbl;
}
MemFree ( ctx, tbl, sizeof * tbl );
}
return NULL;
}
TablePair *DbPairMakeStdTblPair ( DbPair *self, const ctx_t *ctx,
const VTable *src, VTable *dst, const char *name, bool reorder )
{
FUNC_ENTRY ( ctx );
return TablePairMake ( ctx, src, dst, name, self -> full_spec, reorder );
}
/* Release
*/
void TablePairRelease ( TablePair *self, const ctx_t *ctx )
{
rc_t rc;
FUNC_ENTRY ( ctx );
if ( self != NULL )
{
switch ( KRefcountDrop ( & self -> refcount, "TablePair" ) )
{
case krefOkay:
break;
case krefWhack:
( * self -> vt -> whack ) ( ( void* ) self, ctx );
break;
case krefZero:
case krefLimit:
case krefNegative:
rc = RC ( rcExe, rcTable, rcDestroying, rcRefcount, rcInvalid );
INTERNAL_ERROR ( rc, "failed to release TablePair" );
}
}
}
/* Duplicate
*/
TablePair *TablePairDuplicate ( TablePair *self, const ctx_t *ctx )
{
rc_t rc;
FUNC_ENTRY ( ctx );
if ( self != NULL )
{
switch ( KRefcountAdd ( & self -> refcount, "TablePair" ) )
{
case krefOkay:
case krefWhack:
case krefZero:
break;
case krefLimit:
case krefNegative:
rc = RC ( rcExe, rcTable, rcAttaching, rcRefcount, rcInvalid );
INTERNAL_ERROR ( rc, "failed to duplicate TablePair" );
return NULL;
}
}
return ( TablePair* ) self;
}
/* CommitColumnPair
*/
static
bool CC TablePairCommitColumnPair ( void *item, void *data )
{
const ctx_t *ctx = ( const void* ) data;
FUNC_ENTRY ( ctx );
ColumnPair *col = item;
TRY ( ColumnWriterCommit ( col -> writer, ctx ) )
{
return false;
}
return true;
}
/* ReleaseColumnPair
*/
static
void CC TablePairReleaseColumnPair ( void *item, void *data )
{
const ctx_t *ctx = ( const void* ) data;
FUNC_ENTRY ( ctx );
ColumnPairRelease ( item, ctx );
}
/* Copy
* the table has to obtain a RowSetIterator
* which it walks vertically
*
* for each RowSet, it walks its columns horizontally,
* resetting it between columns
*/
static
void TablePairCopyStaticColumns ( TablePair *self, const ctx_t *ctx )
{
FUNC_ENTRY ( ctx );
/* copy static columns */
uint32_t count = VectorLength ( & self -> static_cols );
if ( count != 0 )
{
#if OLD_STATIC_WRITE
RowSetIterator *rsi;
TRY ( rsi = TablePairMakeRowSetIterator ( self, ctx, NULL, false ) )
{
#endif
STATUS ( 2, "copying '%s' static columns", self -> full_spec );
while ( ! FAILED () )
{
uint32_t i;
#if OLD_STATIC_WRITE
RowSet *rs;
ON_FAIL ( rs = RowSetIteratorNext ( rsi, ctx ) )
break;
if ( rs == NULL )
break;
#endif
for ( i = 0; i < count; ++ i )
{
ColumnPair *col = VectorGet ( & self -> static_cols, i );
assert ( col != NULL );
#if OLD_STATIC_WRITE
ON_FAIL ( ColumnPairCopy ( col, ctx, rs ) )
break;
#else
ON_FAIL ( ColumnPairCopyStatic ( col, ctx, self -> first_id, self -> last_excl - self -> first_id ) )
break;
#endif
}
#if OLD_STATIC_WRITE
RowSetRelease ( rs, ctx );
#else
break;
#endif
}
#if OLD_STATIC_WRITE
RowSetIteratorRelease ( rsi, ctx );
}
#endif
/* commit columns */
if ( ! FAILED () )
{
STATUS ( 3, "committing static columns" );
VectorDoUntil ( & self -> static_cols, false, TablePairCommitColumnPair, ( void* ) ctx );
}
/* douse columns */
STATUS ( 3, "releasing static columns" );
VectorWhack ( & self -> static_cols, TablePairReleaseColumnPair, ( void* ) ctx );
}
}
static
void TablePairCopyPresortColumns ( TablePair *self, const ctx_t *ctx )
{
FUNC_ENTRY ( ctx );
/* copy remaining columns */
uint32_t count = VectorLength ( & self -> presort_cols );
if ( count != 0 )
{
RowSetIterator *rsi;
TRY ( rsi = TablePairMakeSimpleRowSetIterator ( self, ctx ) )
{
STATUS ( 2, "copying '%s' presorted columns", self -> full_spec );
while ( ! FAILED () )
{
uint32_t i;
RowSet *rs;
ON_FAIL ( rs = RowSetIteratorNext ( rsi, ctx ) )
break;
if ( rs == NULL )
break;
for ( i = 0; i < count; ++ i )
{
ColumnPair *col = VectorGet ( & self -> presort_cols, i );
assert ( col != NULL );
ON_FAIL ( ColumnPairCopy ( col, ctx, rs ) )
break;
}
RowSetRelease ( rs, ctx );
}
RowSetIteratorRelease ( rsi, ctx );
}
/* commit columns */
if ( ! FAILED () )
{
STATUS ( 3, "committing presorted columns" );
VectorDoUntil ( & self -> presort_cols, false, TablePairCommitColumnPair, ( void* ) ctx );
}
/* douse columns */
STATUS ( 3, "releasing presorted columns" );
VectorWhack ( & self -> presort_cols, TablePairReleaseColumnPair, ( void* ) ctx );
}
}
static
void TablePairCopyMappedColumns ( TablePair *self, const ctx_t *ctx )
{
FUNC_ENTRY ( ctx );
/* copy mapped columns */
uint32_t count = VectorLength ( & self -> mapped_cols );
if ( count != 0 )
{
RowSetIterator *rsi;
const bool is_mapped = true;
const bool is_large = false;
TRY ( rsi = TablePairGetRowSetIterator ( self, ctx, is_mapped, is_large ) )
{
STATUS ( 2, "copying '%s' mapped columns", self -> full_spec );
while ( ! FAILED () )
{
uint32_t i;
RowSet *rs;
ON_FAIL ( rs = RowSetIteratorNext ( rsi, ctx ) )
break;
if ( rs == NULL )
break;
for ( i = 0; i < count; ++ i )
{
ColumnPair *col = VectorGet ( & self -> mapped_cols, i );
assert ( col != NULL );
ON_FAIL ( ColumnPairCopy ( col, ctx, rs ) )
break;
}
RowSetRelease ( rs, ctx );
}
RowSetIteratorRelease ( rsi, ctx );
}
/* commit columns */
if ( ! FAILED () )
{
STATUS ( 3, "committing mapped columns" );
VectorDoUntil ( & self -> mapped_cols, false, TablePairCommitColumnPair, ( void* ) ctx );
}
/* douse columns */
STATUS ( 3, "releasing mapped columns" );
VectorWhack ( & self -> mapped_cols, TablePairReleaseColumnPair, ( void* ) ctx );
}
}
static
void TablePairCopyLargeColumns ( TablePair *self, const ctx_t *ctx )
{
FUNC_ENTRY ( ctx );
/* copy large columns */
uint32_t count = VectorLength ( & self -> large_cols );
if ( count != 0 )
{
RowSetIterator *rsi;
const bool is_mapped = false;
const bool is_large = true;
TRY ( rsi = TablePairGetRowSetIterator ( self, ctx, is_mapped, is_large ) )
{
STATUS ( 2, "copying '%s' large columns", self -> full_spec );
while ( ! FAILED () )
{
uint32_t i;
RowSet *rs;
ON_FAIL ( rs = RowSetIteratorNext ( rsi, ctx ) )
break;
if ( rs == NULL )
break;
for ( i = 0; i < count; ++ i )
{
ColumnPair *col = VectorGet ( & self -> large_cols, i );
assert ( col != NULL );
ON_FAIL ( ColumnPairCopy ( col, ctx, rs ) )
break;
}
RowSetRelease ( rs, ctx );
}
RowSetIteratorRelease ( rsi, ctx );
}
/* commit columns */
if ( ! FAILED () )
{
STATUS ( 3, "committing large columns" );
VectorDoUntil ( & self -> large_cols, false, TablePairCommitColumnPair, ( void* ) ctx );
}
/* douse columns */
STATUS ( 3, "releasing large columns" );
VectorWhack ( & self -> large_cols, TablePairReleaseColumnPair, ( void* ) ctx );
}
}
static
void TablePairCopyLargeMappedColumns ( TablePair *self, const ctx_t *ctx )
{
FUNC_ENTRY ( ctx );
/* copy large mapped columns */
uint32_t count = VectorLength ( & self -> large_mapped_cols );
if ( count != 0 )
{
RowSetIterator *rsi;
const bool is_mapped = true;
const bool is_large = true;
TRY ( rsi = TablePairGetRowSetIterator ( self, ctx, is_mapped, is_large ) )
{
STATUS ( 2, "copying '%s' large mapped columns", self -> full_spec );
while ( ! FAILED () )
{
uint32_t i;
RowSet *rs;
ON_FAIL ( rs = RowSetIteratorNext ( rsi, ctx ) )
break;
if ( rs == NULL )
break;
for ( i = 0; i < count; ++ i )
{
ColumnPair *col = VectorGet ( & self -> large_mapped_cols, i );
assert ( col != NULL );
ON_FAIL ( ColumnPairCopy ( col, ctx, rs ) )
break;
}
RowSetRelease ( rs, ctx );
}
RowSetIteratorRelease ( rsi, ctx );
}
/* commit columns */
if ( ! FAILED () )
{
STATUS ( 3, "committing large mapped columns" );
VectorDoUntil ( & self -> large_mapped_cols, false, TablePairCommitColumnPair, ( void* ) ctx );
}
/* douse columns */
STATUS ( 3, "releasing large mapped columns" );
VectorWhack ( & self -> large_mapped_cols, TablePairReleaseColumnPair, ( void* ) ctx );
}
}
static
void TablePairCopyNormalColumns ( TablePair *self, const ctx_t *ctx )
{
FUNC_ENTRY ( ctx );
/* copy remaining columns */
uint32_t count = VectorLength ( & self -> normal_cols );
if ( count != 0 )
{
RowSetIterator *rsi;
const bool is_mapped = false;
const bool is_large = false;
TRY ( rsi = TablePairGetRowSetIterator ( self, ctx, is_mapped, is_large ) )
{
STATUS ( 2, "copying '%s' columns", self -> full_spec );
while ( ! FAILED () )
{
uint32_t i;
RowSet *rs;
ON_FAIL ( rs = RowSetIteratorNext ( rsi, ctx ) )
break;
if ( rs == NULL )
break;
for ( i = 0; i < count; ++ i )
{
ColumnPair *col = VectorGet ( & self -> normal_cols, i );
assert ( col != NULL );
ON_FAIL ( ColumnPairCopy ( col, ctx, rs ) )
break;
}
RowSetRelease ( rs, ctx );
}
RowSetIteratorRelease ( rsi, ctx );
}
/* commit columns */
if ( ! FAILED () )
{
STATUS ( 3, "committing columns" );
VectorDoUntil ( & self -> normal_cols, false, TablePairCommitColumnPair, ( void* ) ctx );
}
/* douse columns */
STATUS ( 3, "releasing columns" );
VectorWhack ( & self -> normal_cols, TablePairReleaseColumnPair, ( void* ) ctx );
}
}
void TablePairCopy ( TablePair *self, const ctx_t *ctx )
{
FUNC_ENTRY ( ctx );
size_t in_use, quota;
/* copy columns */
ON_FAIL ( TablePairExplode ( self, ctx ) )
return;
STATUS ( 2, "copying table '%s'", self -> full_spec );
in_use = MemInUse ( ctx, & quota );
if ( ( quota + 1 ) != 0 )
STATUS ( 4, "MEMORY: %,zu bytes used out of %,zu total ( %u%% )", in_use, quota, ( uint32_t ) ( in_use * 100 ) / quota );
else
STATUS ( 4, "MEMORY: %,zu bytes used", in_use );
TRY ( TablePairPreCopy ( self, ctx ) )
{
TRY ( TablePairCopyStaticColumns ( self, ctx ) )
{
TRY ( TablePairCopyPresortColumns ( self, ctx ) )
{
TRY ( TablePairCopyMappedColumns ( self, ctx ) )
{
TRY ( TablePairCopyLargeColumns ( self, ctx ) )
{
TRY ( TablePairCopyLargeMappedColumns ( self, ctx ) )
{
TablePairCopyNormalColumns ( self, ctx );
}
}
}
}
}
}
/* cleanup */
if ( ! FAILED () )
TablePairPostCopy ( self, ctx );
in_use = MemInUse ( ctx, & quota );
if ( ( quota + 1 ) != 0 )
STATUS ( 4, "MEMORY: %,zu bytes used out of %,zu total ( %u%% )", in_use, quota, ( uint32_t ) ( in_use * 100 ) / quota );
else
STATUS ( 4, "MEMORY: %,zu bytes used", in_use );
STATUS ( 2, "finished with table '%s'", self -> full_spec );
}
/* Explode
* probably a bad name, but it is intended to mean
* register all enclosed columns
* and create a proper pair of KMetadata
*/
static
MetaPair *TablePairExplodeMetaPair ( TablePair *self, const ctx_t *ctx )
{
FUNC_ENTRY ( ctx );
rc_t rc;
MetaPair *meta = NULL;
const KMetadata *smeta;
rc = VTableOpenMetadataRead ( self -> stbl, & smeta );
if ( rc != 0 )
ERROR ( rc, "VTableOpenMetadataRead failed on 'src.%s'", self -> full_spec );
else
{
KMetadata *dmeta;
rc = VTableOpenMetadataUpdate ( self -> dtbl, & dmeta );
if ( rc != 0 )
ERROR ( rc, "VTableOpenMetadataUpdate failed on 'dst.%s'", self -> full_spec );
else
{
meta = MetaPairMake ( ctx, smeta, dmeta, self -> full_spec );
KMetadataRelease ( dmeta );
}
KMetadataRelease ( smeta );
}
return meta;
}
static
ColumnPair *TablePairMatchColumnPair ( TablePair *self, const ctx_t *ctx, const char *name, bool required )
{
FUNC_ENTRY ( ctx );
rc_t rc;
KNamelist *types;
ColumnPair *col = NULL;
rc = VTableListWritableDatatypes ( self -> dtbl, name, & types );
if ( rc != 0 )
ERROR ( rc, "VTableListWritableDatatypes failed listing 'dst.%s.%s' datatypes", self -> full_spec, name );
else
{
uint32_t count;
rc = KNamelistCount ( types, & count );
if ( rc != 0 )
ERROR ( rc, "KNamelistCount failed listing 'dst.%s.%s' datatypes", self -> full_spec, name );
else
{
const VCursor *scurs;
rc = VTableCreateCursorRead ( self -> stbl, & scurs );
if ( rc != 0 )
ERROR ( rc, "VTableCreateCursorRead failed on 'src.%s'", self -> full_spec, name );
else
{
rc = VCursorPermitPostOpenAdd ( scurs );
if ( rc != 0 )
ERROR ( rc, "VCursorPermitPostOpenAdd failed on 'src.%s'", self -> full_spec, name );
else
{
rc = VCursorOpen ( scurs );
if ( rc != 0 )
ERROR ( rc, "VCursorOpen failed with no contents on 'src.%s'", self -> full_spec, name );
else
{
uint32_t i;
for ( i = 0; i < count; ++ i )
{
uint32_t idx;
char colspec [ 256 ];
const char *td;
rc = KNamelistGet ( types, i, & td );
if ( rc != 0 )
{
ERROR ( rc, "KNamelistGet ( %u ) failed listing 'dst.%s.%s' datatypes", i, self -> full_spec, name );
break;
}
rc = string_printf ( colspec, sizeof colspec, NULL, "(%s)%s", td, name );
if ( rc != 0 )
{
ABORT ( rc, "failed creating colspec for 'dst.%s.%s'", self -> full_spec, name );
break;
}
rc = VCursorAddColumn ( scurs, & idx, "%s", colspec );
if ( rc == 0 )
{
ColumnReader *reader;
TRY ( reader = TablePairMakeColumnReader ( self, ctx, scurs, colspec, true ) )
{
ColumnWriter *writer;
TRY ( writer = TablePairMakeColumnWriter ( self, ctx, NULL, colspec ) )
{
uint32_t j;
bool large = false;
bool is_static = VTableHasStaticColumn ( self -> stbl, name );
if ( is_static )
{
if ( self -> nonstatic_col_names != NULL )
{
for ( j = 0; self -> nonstatic_col_names [ j ] != NULL; ++ j )
{
if ( strcmp ( name, self -> nonstatic_col_names [ j ] ) == 0 )
{
is_static = false;
break;
}
}
}
}
else if ( self -> large_col_names != NULL )
{
for ( j = 0; self -> large_col_names [ j ] != NULL; ++ j )
{
if ( strcmp ( name, self -> large_col_names [ j ] ) == 0 )
{
large = true;
break;
}
}
}
/* at this point, if the column is static, then avoid
any rigorous treatment and create an otherwise normal
column pair. may want to special case it for reporting */
col = is_static
? TablePairMakeStaticColumnPair ( self, ctx, reader, writer, colspec )
: ( * self -> vt -> make_column_pair ) ( ( TBLPAIR_IMPL* ) self, ctx, reader, writer, colspec, large );
ColumnWriterRelease ( writer, ctx );
}
ColumnReaderRelease ( reader, ctx );
}
break;
}
}
if ( col == NULL && ! FAILED () )
ANNOTATE ( "column '%s' cannot be written", name );
}
}
VCursorRelease ( scurs );
}
}
KNamelistRelease ( types );
}
return col;
}
static
void TablePairDefaultExplode ( TablePair *self, const ctx_t *ctx )
{
FUNC_ENTRY ( ctx );
rc_t rc;
KNamelist *pnames;
/* look at the physical columns in source */
rc = VTableListPhysColumns ( self -> stbl, & pnames );
if ( rc != 0 )
ERROR ( rc, "VTableListWritableColumns failed listing 'src.%s' physical columns", self -> full_spec );
else
{
KNamelist *dnames;
/* this gives us the columns that could potentially take input */
rc = VTableListSeededWritableColumns ( self -> dtbl, & dnames, pnames );
if ( rc != 0 )
ERROR ( rc, "VTableListWritableColumns failed listing 'dst.%s' columns", self -> full_spec );
else
{
uint32_t count;
rc = KNamelistCount ( dnames, & count );
if ( rc != 0 )
ERROR ( rc, "KNamelistCount failed listing 'dst.%s' columns", self -> full_spec );
else if ( count > 0 )
{
uint32_t i;
for ( i = 0; ! FAILED () && i < count; ++ i )
{
ColumnPair *col;
const char *name;
rc = KNamelistGet ( dnames, i, & name );
if ( rc != 0 )
{
ERROR ( rc, "KNamelistGet ( %u ) failed listing 'dst.%s' columns", i, self -> full_spec );
break;
}
/* filter out the column names that are specifically excluded */
if ( self -> exclude_col_names != NULL )
{
uint32_t j;
for ( j = 0; self -> exclude_col_names [ j ] != NULL; ++ j )
{
if ( strcmp ( name, self -> exclude_col_names [ j ] ) == 0 )
{
name = NULL;
break;
}
}
if ( name == NULL )
continue;
}
TRY ( col = TablePairMatchColumnPair ( self, ctx, name, true ) )
{
if ( col != NULL )
{
ON_FAIL ( TablePairAddColumnPair ( self, ctx, col ) )
{
ColumnPairRelease ( col, ctx );
}
}
}
}
}
KNamelistRelease ( dnames );
}
KNamelistRelease ( pnames );
}
}
void TablePairExplode ( TablePair *self, const ctx_t *ctx )
{
if ( ! self -> exploded )
{
FUNC_ENTRY ( ctx );
STATUS ( 2, "exploding table '%s'", self -> full_spec );
/* first, ask subclass to perform explicit explode */
TRY ( ( * self -> vt -> explode ) ( self, ctx ) )
{
if ( self -> meta == NULL )
{
TRY ( self -> meta = TablePairExplodeMetaPair ( self, ctx ) )
{
MetaPairCopy ( self -> meta, ctx, self -> full_spec, self -> exclude_meta );
STATUS ( 3, "releasing metadata" );
MetaPairRelease ( self -> meta, ctx );
self -> meta = NULL;
}
}
if ( ! FAILED () )
{
/* next, perform default self-explode for all columns not excluded */
TRY ( TablePairDefaultExplode ( self, ctx ) )
{
/* now create metadata pair */
if ( self -> meta == NULL )
self -> meta = TablePairExplodeMetaPair ( self, ctx );
if ( ! FAILED () )
self -> exploded = true;
}
}
}
}
}
/* AddColumnPair
* called from implementations
*/
void TablePairAddColumnPair ( TablePair *self, const ctx_t *ctx, ColumnPair *col )
{
if ( col != NULL )
{
FUNC_ENTRY ( ctx );
rc_t rc;
Vector *v = & self -> normal_cols;
const char *col_type = "";
const char *col_size = "";
if ( col -> large )
{
col_size = "large ";
v = & self -> large_cols;
}
if ( col -> is_static )
{
col_type = "static ";
v = & self -> static_cols;
}
else if ( col -> presorted )
{
col_type = "presorted ";
v = & self -> presort_cols;
}
else if ( col -> is_mapped )
{
col_type = "mapped ";
v = col -> large ? & self -> large_mapped_cols : & self -> mapped_cols;
}
STATUS ( 4, "adding %s%scolumn pair '%s'", col_size, col_type, col -> full_spec );
rc = VectorAppend ( v, NULL, ( const void* ) col );
if ( rc != 0 )
SYSTEM_ERROR ( rc, "failed to add column pair '%s'", col -> full_spec );
else
{
int64_t first;
uint64_t count;
TRY ( count = ColumnReaderIdRange ( col -> reader, ctx, & first ) )
{
int64_t last_excl = first + count;
if ( first < self -> first_id )
self -> first_id = first;
/* Static columns may have incorrect range in legacy data */
if ( last_excl > self -> last_excl && !col->is_static)
self -> last_excl = last_excl;
}
}
}
}
/* Init
*/
void TablePairInit ( TablePair *self, const ctx_t *ctx, const TablePair_vt *vt,
const VTable *src, VTable *dst, const char *name, const char *opt_full_spec, bool reorder )
{
FUNC_ENTRY ( ctx );
rc_t rc;
memset ( self, 0, sizeof * self );
self -> vt = vt;
VectorInit ( & self -> static_cols, 0, 32 );
VectorInit ( & self -> presort_cols, 0, 4 );
VectorInit ( & self -> large_cols, 0, 32 );
VectorInit ( & self -> mapped_cols, 0, 32 );
VectorInit ( & self -> large_mapped_cols, 0, 32 );
VectorInit ( & self -> normal_cols, 0, 32 );
if ( opt_full_spec == NULL )
opt_full_spec = "";
rc = VTableAddRef ( self -> stbl = src );
if ( rc != 0 )
ERROR ( rc, "failed to duplicate tbl 'src.%s%s%s'", opt_full_spec, opt_full_spec [ 0 ] ? "." : "", name );
else
{
rc = VTableAddRef ( self -> dtbl = dst );
if ( rc != 0 )
ERROR ( rc, "failed to duplicate tbl 'dst.%s%s%s'", opt_full_spec, opt_full_spec [ 0 ] ? "." : "", name );
else
{
char *full_spec;
self -> full_spec_size = string_size ( opt_full_spec ) + string_size ( name );
if ( opt_full_spec [ 0 ] != 0 )
++ self -> full_spec_size;
TRY ( full_spec = MemAlloc ( ctx, self -> full_spec_size + 1, false ) )
{
if ( opt_full_spec [ 0 ] != 0 )
rc = string_printf ( full_spec, self -> full_spec_size + 1, NULL, "%s.%s", opt_full_spec, name );
else
strcpy ( full_spec, name );
if ( rc != 0 )
ABORT ( rc, "miscalculated string size" );
else
{
const Tool *tp = ctx -> caps -> tool;
self -> full_spec = full_spec;
self -> name = full_spec;
if ( opt_full_spec [ 0 ] != 0 )
self -> name += string_size ( opt_full_spec ) + 1;
/* start with an invalid range */
self -> first_id = ( int64_t ) ( ~ ( uint64_t ) 0 >> 1 );
self -> last_excl = - self -> first_id;
/* row-set id ranges */
self -> max_idx_ids = tp -> max_idx_ids;
self -> min_idx_ids = tp -> min_idx_ids;
/* record reordering */
self -> reorder = reorder;
KRefcountInit ( & self -> refcount, 1, "TablePair", "init", name );
return;
}
}
VTableRelease ( self -> dtbl );
self -> dtbl = NULL;
}
VTableRelease ( self -> stbl );
self -> stbl = NULL;
}
}
/* Destroy
* destroys superclass items
*/
void TablePairDestroy ( TablePair *self, const ctx_t *ctx )
{
FUNC_ENTRY ( ctx );
rc_t rc;
VectorWhack ( & self -> static_cols, TablePairReleaseColumnPair, ( void* ) ctx );
VectorWhack ( & self -> presort_cols, TablePairReleaseColumnPair, ( void* ) ctx );
VectorWhack ( & self -> large_cols, TablePairReleaseColumnPair, ( void* ) ctx );
VectorWhack ( & self -> mapped_cols, TablePairReleaseColumnPair, ( void* ) ctx );
VectorWhack ( & self -> large_mapped_cols, TablePairReleaseColumnPair, ( void* ) ctx );
VectorWhack ( & self -> normal_cols, TablePairReleaseColumnPair, ( void* ) ctx );
MetaPairRelease ( self -> meta, ctx );
rc = VTableReindex ( self -> dtbl );
if ( rc != 0 )
WARN ( "VTableReindex failed on 'dst.%s'", self -> full_spec );
rc = VTableRelease ( self -> dtbl );
if ( rc != 0 )
WARN ( "VTableRelease failed on 'dst.%s'", self -> full_spec );
VTableRelease ( self -> stbl );
MemFree ( ctx, ( void* ) self -> full_spec, self -> full_spec_size + 1 );
if ( self -> thread != NULL ) {
rc_t rc = 0;
rc_t status = 0;
STATUS ( 2, "waiting for background thread 0x%p to finish...",
self -> thread );
rc = KThreadWait ( self -> thread, & status );
if ( rc != 0 )
ERROR ( rc, "failed to wait for background thread 0x%p",
self -> thread );
else if ( status == 0 )
STATUS ( 2, "...background thread 0x%p succeed", self -> thread );
else {
ERROR ( status, "background thread 0x%p failed", self -> thread );
rc = status;
}
{
rc_t r2 = KThreadRelease ( self -> thread );
if ( r2 != 0 ) {
ERROR ( r2, "failed to release background thread 0x%p",
self -> thread );
if ( rc == 0 )
rc = r2;
}
}
if ( rc != 0 && ctx != NULL ) {
ctx_t * mctx = NULL;
for ( mctx = ( ctx_t * ) ctx; mctx != NULL && mctx -> rc == 0;
mctx = ( ctx_t* ) mctx -> caller )
{
mctx -> rc = rc;
}
}
}
memset ( self, 0, sizeof * self );
}
|