File: col-pair.c

package info (click to toggle)
sra-sdk 3.2.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 296,076 kB
  • sloc: ansic: 532,876; cpp: 243,000; perl: 9,649; python: 8,978; sh: 7,888; java: 6,253; makefile: 1,148; yacc: 703; xml: 310; lex: 236
file content (818 lines) | stat: -rw-r--r-- 22,683 bytes parent folder | download | duplicates (7)
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
/*===========================================================================
 *
 *                            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 SimpleColumnReader SimpleColumnReader;
#define COLREADER_IMPL SimpleColumnReader

typedef struct SimpleColumnWriter SimpleColumnWriter;
#define COLWRITER_IMPL SimpleColumnWriter

#include "col-pair.h"
#include "tbl-pair.h"
#include "row-set.h"
#include "ctx.h"
#include "caps.h"
#include "except.h"
#include "status.h"
#include "mem.h"

#include <vdb/cursor.h>
#include <vdb/vdb-priv.h>
#include <kapp/main.h>
#include <kfs/defs.h>
#include <klib/printf.h>
#include <klib/text.h>
#include <klib/rc.h>

#include <string.h>

FILE_ENTRY ( col-pair );


/*--------------------------------------------------------------------------
 * SimpleColumnReader
 *  implementation of ColumnReader based upon VCursor direct read
 */
struct SimpleColumnReader
{
    ColumnReader dad;

    const VCursor *curs;
    uint32_t idx;

    uint32_t full_spec_size;
    char full_spec [ 1 ];
};


/* Whack
 */
static
void SimpleColumnReaderWhack ( SimpleColumnReader *self, const ctx_t *ctx )
{
    FUNC_ENTRY ( ctx );

    rc_t rc;
    size_t bytes = sizeof * self + self -> full_spec_size;

    rc = VCursorRelease ( self -> curs );
    if ( rc != 0 )
        WARN ( "VCursorRelease failed on '%s'", self -> full_spec );

    self -> curs = NULL;
    MemFree ( ctx, self, bytes );
}

/* FullSpec
 */
static
const char *SimpleColumnReaderFullSpec ( const SimpleColumnReader *self, const ctx_t *ctx )
{
    return self -> full_spec;
}


/* IdRange
 *  returns the number of ids available
 *  and optionally the first id
 */
static
uint64_t SimpleColumnReaderIdRange ( const SimpleColumnReader *self,
    const ctx_t *ctx, int64_t *opt_first )
{
    FUNC_ENTRY ( ctx );

    rc_t rc;
    uint64_t count;

    rc = VCursorIdRange ( self -> curs, self -> idx, opt_first, & count );
    if ( rc != 0 )
        INTERNAL_ERROR ( rc, "VCursorIdRange failed for column '%s'", self -> full_spec );

    return count;
}


/* DummyStub
 *  useful for pre/post-copy
 */
static
void SimpleColumnReaderDummyStub ( SimpleColumnReader *self, const ctx_t *ctx )
{
}


/* Read
 *  read next row of data
 *  returns NULL if no rows are available
 */
static
const void *SimpleColumnReaderRead ( SimpleColumnReader *self, const ctx_t *ctx,
    int64_t row_id, uint32_t *elem_bits, uint32_t *boff, uint32_t *row_len )
{
    FUNC_ENTRY ( ctx );

    rc_t rc;
    const void *base;

    rc = VCursorCellDataDirect ( self -> curs, row_id, self -> idx, elem_bits, & base, boff, row_len );
    if ( rc != 0 )
        INTERNAL_ERROR ( rc, "VCursorCellDataDirect ( %ld ) failed for column '%s'", row_id, self -> full_spec );

    return base;
}

static ColumnReader_vt SimpleColumnReader_vt =
{
    SimpleColumnReaderWhack,
    SimpleColumnReaderFullSpec,
    SimpleColumnReaderIdRange,
    SimpleColumnReaderDummyStub,
    SimpleColumnReaderDummyStub,
    SimpleColumnReaderRead
};


/*--------------------------------------------------------------------------
 * ColumnReader
 *  interface to read column in serial order
 */


/* MakeColumnReader
 *  make simple column reader
 */
ColumnReader *TablePairMakeColumnReader ( TablePair *self, const ctx_t *ctx,
    const VCursor *opt_curs, const char *colspec, bool required )
{
    FUNC_ENTRY ( ctx );

    rc_t rc;
    const VCursor *curs;

    if ( opt_curs != NULL )
        rc = VCursorAddRef ( curs = opt_curs );
    else
        rc = VTableCreateCursorRead ( self -> stbl, & curs );
    if ( rc != 0 )
        ERROR ( rc, "failed to create cursor on column 'src.%s.%s'", self -> full_spec, colspec );
    else
    {
        uint32_t idx;
        rc = VCursorAddColumn ( curs, & idx, "%s", colspec );
        if ( rc != 0 && GetRCState ( rc ) != rcExists )
        {
            if ( required )
                ERROR ( rc, "failed to add column 'src.%s.%s' to cursor", self -> full_spec, colspec );
        }
        else
        {
            rc = VCursorOpen ( curs );
            if ( rc != 0 )
            {
                if ( required )
                    ERROR ( rc, "failed to open cursor on column 'src.%s.%s'", self -> full_spec, colspec );
            }
            else
            {
                SimpleColumnReader *col;
                size_t full_spec_size = self -> full_spec_size + string_size ( colspec ) + sizeof "src.." - 1;

                TRY ( col = MemAlloc ( ctx, sizeof * col + full_spec_size, false ) )
                {
                    ColumnReaderInit ( & col -> dad, ctx, & SimpleColumnReader_vt );
                    col -> curs = curs;
                    col -> idx = idx;
                    col -> full_spec_size = ( uint32_t ) full_spec_size;

                    rc = string_printf ( col -> full_spec, full_spec_size + 1, NULL,
                        "src.%s.%s", self -> full_spec, colspec );
                    if ( rc == 0 )
                        return & col -> dad;

                    ABORT ( rc, "miscalculated string size" );
                }
                CATCH_ALL ()
                {
                    ANNOTATE ( "failed to allocate %zu bytes for SimpleColumnReader", sizeof * col + full_spec_size );
                }
            }
        }

        VCursorRelease ( curs );
    }

    return NULL;
}


/* Release
 *  releases reference
 */
void ColumnReaderRelease ( const ColumnReader *self, const ctx_t *ctx )
{
    rc_t rc;
    FUNC_ENTRY ( ctx );

    if ( self != NULL )
    {
        switch ( KRefcountDrop ( & self -> refcount, "ColumnReader" ) )
        {
        case krefOkay:
            break;
        case krefWhack:
            ( * self -> vt -> whack ) ( ( void* ) self, ctx );
            break;
        case krefZero:
        case krefLimit:
        case krefNegative:
            rc = RC ( rcExe, rcCursor, rcDestroying, rcRefcount, rcInvalid );
            INTERNAL_ERROR ( rc, "failed to release ColumnReader" );
        }
    }
}

/* Duplicate
 */
ColumnReader *ColumnReaderDuplicate ( const ColumnReader *self, const ctx_t *ctx )
{
    rc_t rc;
    FUNC_ENTRY ( ctx );

    if ( self != NULL )
    {
        switch ( KRefcountAdd ( & self -> refcount, "ColumnReader" ) )
        {
        case krefOkay:
        case krefWhack:
        case krefZero:
            break;
        case krefLimit:
        case krefNegative:
            rc = RC ( rcExe, rcCursor, rcAttaching, rcRefcount, rcInvalid );
            INTERNAL_ERROR ( rc, "failed to duplicate ColumnReader" );
            return NULL;
        }
    }

    return ( ColumnReader* ) self;
}


/* Init
 */
void ColumnReaderInit ( ColumnReader *self, const ctx_t *ctx, const ColumnReader_vt *vt )
{
    if ( self == NULL )
    {
        rc_t rc = RC ( rcExe, rcCursor, rcConstructing, rcSelf, rcNull );
        INTERNAL_ERROR ( rc, "bad ColumnReader" );
    }
    else
    {
        self -> vt = vt;
        KRefcountInit ( & self -> refcount, 1, "ColumnReader", "init", "" );
        self -> presorted = false;
        memset ( self -> align, 0, sizeof self -> align );
    }
}


/*--------------------------------------------------------------------------
 * SimpleColumnWriter
 *  implementation of ColumnWriter based upon VCursor
 */
struct SimpleColumnWriter
{
    ColumnWriter dad;

    VCursor *curs;
    uint32_t idx;

    uint32_t full_spec_size;
    char full_spec [ 1 ];
};

static
void SimpleColumnWriterWhack ( SimpleColumnWriter *self, const ctx_t *ctx )
{
    FUNC_ENTRY ( ctx );

    rc_t rc;

    rc = VCursorRelease ( self -> curs );
    if ( rc != 0 )
        ERROR ( rc, "VCursorRelease failed on column '%s'", self -> full_spec );

    MemFree ( ctx, self, sizeof * self + self -> full_spec_size );
}


static
const char *SimpleColumnWriterFullSpec ( const SimpleColumnWriter *self, const ctx_t *ctx )
{
    return self -> full_spec;
}

static
void SimpleColumnWriterDummyStub ( SimpleColumnWriter *self, const ctx_t *ctx )
{
}

static
void SimpleColumnWriterWrite ( SimpleColumnWriter *self, const ctx_t *ctx,
    uint32_t elem_bits, const void *data, uint32_t boff, uint32_t row_len )
{
    FUNC_ENTRY ( ctx );

    rc_t rc;

    rc = VCursorOpenRow ( self -> curs );
    if ( rc != 0 )
        INTERNAL_ERROR ( rc, "VCursorOpenRow failed on column '%s'", self -> full_spec );
    else
    {
        rc = VCursorWrite ( self -> curs, self -> idx, elem_bits, data, boff, row_len );
        if ( rc != 0 )
            SYSTEM_ERROR ( rc, "VCursorWrite failed for column '%s'", self -> full_spec );
        else
        {
            rc = VCursorCommitRow ( self -> curs );
            if ( rc != 0 )
                SYSTEM_ERROR ( rc, "VCursorCommit failed for column '%s'", self -> full_spec );
        }

        rc = VCursorCloseRow ( self -> curs );
        if ( rc != 0 )
            INTERNAL_ERROR ( rc, "VCursorCloseRow failed on column '%s'", self -> full_spec );
    }
}

static
void SimpleColumnWriterWriteStatic ( SimpleColumnWriter *self, const ctx_t *ctx,
    uint32_t elem_bits, const void *data, uint32_t boff, uint32_t row_len, uint64_t count )
{
    FUNC_ENTRY ( ctx );

    for ( ; ! FAILED () && count > 0; -- count )
    {
        rc_t rc = VCursorOpenRow ( self -> curs );
        if ( rc != 0 )
            INTERNAL_ERROR ( rc, "VCursorOpenRow failed on column '%s'", self -> full_spec );
        else
        {
            rc = VCursorWrite ( self -> curs, self -> idx, elem_bits, data, boff, row_len );
            if ( rc != 0 )
                SYSTEM_ERROR ( rc, "VCursorWrite failed for column '%s'", self -> full_spec );
            else
            {
                rc = VCursorCommitRow ( self -> curs );
                if ( rc != 0 )
                    SYSTEM_ERROR ( rc, "VCursorCommitRow failed for column '%s'", self -> full_spec );
                else if ( count > 1 )
                {
                    /* setting it small since threshold is detected in CommitRow, but execused on CloseRow */
                    uint64_t cnt = ( count < 0x10000000U ) ? count : 0x10000000U; 
                    rc = VCursorRepeatRow ( self -> curs, cnt - 1 );
                    if ( rc != 0 )
                        SYSTEM_ERROR ( rc, "VCursorRepeatRow failed for column '%s'", self -> full_spec );
                    else
                        count -= cnt - 1;
                }
            }

            rc = VCursorCloseRow ( self -> curs );
            if ( rc != 0 )
                INTERNAL_ERROR ( rc, "VCursorCloseRow failed on column '%s'", self -> full_spec );
        }
    }
}

static
void SimpleColumnWriterCommit ( SimpleColumnWriter *self, const ctx_t *ctx )
{
    FUNC_ENTRY ( ctx );

    rc_t rc;

    rc = VCursorCommit ( self -> curs );
    if ( rc != 0 )
        ERROR ( rc, "VCursorCommit failed on column '%s'", self -> full_spec );
    else
    {
        rc = VCursorRelease ( self -> curs );
        if ( rc != 0 )
            ERROR ( rc, "VCursorRelease failed on column '%s'", self -> full_spec );
        else
        {
            self -> curs = NULL;
        }
    }
}


static ColumnWriter_vt SimpleColumnWriter_vt =
{
    SimpleColumnWriterWhack,
    SimpleColumnWriterFullSpec,
    SimpleColumnWriterDummyStub,
    SimpleColumnWriterDummyStub,
    SimpleColumnWriterWrite,
    SimpleColumnWriterWriteStatic,
    SimpleColumnWriterCommit
};



/*--------------------------------------------------------------------------
 * ColumnWriter
 *  interface to write column in serial order
 */


/* MakeColumnWriter
 *  make simple column writer
 */
ColumnWriter *TablePairMakeColumnWriter ( TablePair *self, const ctx_t *ctx,
   VCursor *opt_curs, const char *colspec )
{
    FUNC_ENTRY ( ctx );

    rc_t rc;
    VCursor *curs;

    if ( opt_curs != NULL )
        rc = VCursorAddRef ( curs = opt_curs );
    else
        rc = VTableCreateCursorWrite ( self -> dtbl, & curs, kcmInsert );
    if ( rc != 0 )
        ERROR ( rc, "failed to create cursor on column 'dst.%s.%s'", self -> full_spec, colspec );
    else
    {
        uint32_t idx;
        rc = VCursorAddColumn ( curs, & idx, "%s", colspec );
        if ( rc != 0 )
            ERROR ( rc, "failed to add column 'dst.%s.%s' to cursor", self -> full_spec, colspec );
        else
        {
            VCursorSuspendTriggers ( curs );

            rc = VCursorOpen ( curs );
            if ( rc != 0 )
                ERROR ( rc, "failed to open cursor on column 'dst.%s.%s'", self -> full_spec, colspec );
            else
            {
                SimpleColumnWriter *col;
                size_t full_spec_size = self -> full_spec_size + string_size ( colspec ) + sizeof "dst.." - 1;

                TRY ( col = MemAlloc ( ctx, sizeof * col + full_spec_size, false ) )
                {
                    ColumnWriterInit ( & col -> dad, ctx, & SimpleColumnWriter_vt, false );
                    col -> curs = curs;
                    col -> idx = idx;

                    col -> full_spec_size = ( uint32_t ) full_spec_size;
                    rc = string_printf ( col -> full_spec, full_spec_size + 1, NULL,
                        "dst.%s.%s", self -> full_spec, colspec );
                    if ( rc == 0 )
                        return & col -> dad;

                    ABORT ( rc, "miscalculated string size" );
                }
                CATCH_ALL ()
                {
                    ANNOTATE ( "failed to allocate %zu bytes for SimpleColumnWriter", sizeof * col + full_spec_size );
                }
            }
        }

        VCursorRelease ( curs );
    }

    return NULL;
}


/* Release
 *  releases reference
 */
void ColumnWriterRelease ( ColumnWriter *self, const ctx_t *ctx )
{
    rc_t rc;
    FUNC_ENTRY ( ctx );

    if ( self != NULL )
    {
        switch ( KRefcountDrop ( & self -> refcount, "ColumnWriter" ) )
        {
        case krefOkay:
            break;
        case krefWhack:
            ( * self -> vt -> whack ) ( ( void* ) self, ctx );
            break;
        case krefZero:
        case krefLimit:
        case krefNegative:
            rc = RC ( rcExe, rcCursor, rcDestroying, rcRefcount, rcInvalid );
            INTERNAL_ERROR ( rc, "failed to release ColumnWriter" );
        }
    }
}

/* Duplicate
 */
ColumnWriter *ColumnWriterDuplicate ( const ColumnWriter *self, const ctx_t *ctx )
{
    rc_t rc;
    FUNC_ENTRY ( ctx );

    if ( self != NULL )
    {
        switch ( KRefcountAdd ( & self -> refcount, "ColumnWriter" ) )
        {
        case krefOkay:
        case krefWhack:
        case krefZero:
            break;
        case krefLimit:
        case krefNegative:
            rc = RC ( rcExe, rcCursor, rcAttaching, rcRefcount, rcInvalid );
            INTERNAL_ERROR ( rc, "failed to duplicate ColumnWriter" );
            return NULL;
        }
    }

    return ( ColumnWriter* ) self;
}


/* Init
 */
void ColumnWriterInit ( ColumnWriter *self, const ctx_t *ctx, const ColumnWriter_vt *vt, bool mapped )
{
    if ( self == NULL )
    {
        rc_t rc = RC ( rcExe, rcCursor, rcConstructing, rcSelf, rcNull );
        INTERNAL_ERROR ( rc, "bad ColumnWriter" );
    }
    else
    {
        self -> vt = vt;
        KRefcountInit ( & self -> refcount, 1, "ColumnWriter", "init", "" );
        self -> mapped = mapped;
        memset ( self -> align, 0, sizeof self -> align );
    }
}



/*--------------------------------------------------------------------------
 * ColumnPair
 *  interface to pairing of source and destination columns
 */

/* Whack
 */
static
void ColumnPairWhack ( ColumnPair *self, const ctx_t *ctx )
{
    FUNC_ENTRY ( ctx );

    ColumnReaderRelease ( self -> reader, ctx );
    ColumnWriterRelease ( self -> writer, ctx );
    MemFree ( ctx, self, sizeof * self + self -> full_spec_size );
}

/* MakeColumnPair
 *  make a simple ColumnPair
 *  takes an optional source VCursor
 *  and a colspec
 */
ColumnPair *TablePairMakeColumnPair ( TablePair *self, const ctx_t *ctx,
    ColumnReader *reader, ColumnWriter *writer, const char *colspec, bool large )
{
    FUNC_ENTRY ( ctx );

    ColumnPair *col;
    size_t full_spec_size = self -> full_spec_size + string_size ( colspec ) + 1;

    TRY ( col = MemAlloc ( ctx, sizeof * col + full_spec_size, false ) )
    {
        TRY ( col -> reader = ColumnReaderDuplicate ( reader, ctx ) )
        {
            TRY ( col -> writer = ColumnWriterDuplicate ( writer, ctx ) )
            {
                rc_t rc;

                col -> full_spec_size = full_spec_size;
                KRefcountInit ( & col -> refcount, 1, "ColumnPair", "make", colspec );
                col -> is_static = false;
                col -> is_mapped = writer -> mapped;
                col -> presorted = reader -> presorted;
                col -> large = large;

                rc = string_printf ( col -> full_spec, full_spec_size + 1, NULL,
                    "%s.%s", self -> full_spec, colspec );
                if ( rc == 0 )
                {
                    col -> colspec = & col -> full_spec [ self -> full_spec_size + 1 ];
                    return col;
                }

                ABORT ( rc, "miscalculated string size" );
            }

            ColumnReaderRelease ( col -> reader, ctx );
        }

        MemFree ( ctx, col, sizeof * col + full_spec_size );
    }

    return NULL;
}

ColumnPair *TablePairMakeStaticColumnPair ( TablePair *self, const ctx_t *ctx,
    ColumnReader *reader, ColumnWriter *writer, const char *colspec )
{
    FUNC_ENTRY ( ctx );

    ColumnPair *col;

    TRY ( col = TablePairMakeColumnPair ( self, ctx, reader, writer, colspec, false ) )
    {
        if ( col != NULL )
            col -> is_static = true;
    }

    return col;
}


/* Release
 *  called by table at end of copy
 */
void ColumnPairRelease ( ColumnPair *self, const ctx_t *ctx )
{
    rc_t rc;
    FUNC_ENTRY ( ctx );

    if ( self != NULL )
    {
        switch ( KRefcountDrop ( & self -> refcount, "ColumnPair" ) )
        {
        case krefOkay:
            break;
        case krefWhack:
            ColumnPairWhack ( self, ctx );
            break;
        case krefZero:
        case krefLimit:
        case krefNegative:
            rc = RC ( rcExe, rcColumn, rcDestroying, rcRefcount, rcInvalid );
            INTERNAL_ERROR ( rc, "failed to release ColumnPair" );
        }
    }
}

/* Duplicate
 */
ColumnPair *ColumnPairDuplicate ( const ColumnPair *self, const ctx_t *ctx )
{
    rc_t rc;
    FUNC_ENTRY ( ctx );

    if ( self != NULL )
    {
        switch ( KRefcountAdd ( & self -> refcount, "ColumnPair" ) )
        {
        case krefOkay:
        case krefWhack:
        case krefZero:
            break;
        case krefLimit:
        case krefNegative:
            rc = RC ( rcExe, rcColumn, rcAttaching, rcRefcount, rcInvalid );
            INTERNAL_ERROR ( rc, "failed to duplicate ColumnPair" );
            return NULL;
        }
    }

    return ( ColumnPair* ) self;
}


/* PreCopy
 * PostCopy
 */
void ColumnPairPreCopy ( const ColumnPair *self, const ctx_t *ctx )
{
    FUNC_ENTRY ( ctx );
    ColumnReaderPreCopy ( self -> reader, ctx );
    ColumnWriterPreCopy ( self -> writer, ctx );
}

void ColumnPairPostCopy ( const ColumnPair *self, const ctx_t *ctx )
{
    FUNC_ENTRY ( ctx );
    ColumnReaderPostCopy ( self -> reader, ctx );
    ColumnWriterPostCopy ( self -> writer, ctx );
}


/* Copy
 *  copy from source to destination column
 */
void ColumnPairCopy ( ColumnPair *self, const ctx_t *ctx, RowSet *rs )
{
    FUNC_ENTRY ( ctx );

    STATUS ( 3, "copying column '%s'", self -> full_spec );

    TRY ( RowSetReset ( rs, ctx, self -> is_static ) )
    {
        TRY ( ColumnPairPreCopy ( self, ctx ) )
        {
            while ( ! FAILED () )
            {
                rc_t rc;
                size_t i, count;
                int64_t row_ids [ 8 * 1024 ];

                ON_FAIL ( count = RowSetNext ( rs, ctx, row_ids, sizeof row_ids / sizeof row_ids [ 0 ] ) )
                    break;
                if ( count == 0 )
                    break;

                rc = Quitting ();
                if ( rc != 0 )
                {
                    INFO_ERROR ( rc, "quitting" );
                    break;
                }

                for ( i = 0; ! FAILED () && i < count; ++ i )
                {
                    const void *base;
                    uint32_t elem_bits, boff, row_len;
                    
                    TRY ( base = ColumnReaderRead ( self -> reader, ctx, row_ids [ i ], & elem_bits, & boff, & row_len ) )
                    {
                        ColumnWriterWrite ( self -> writer, ctx, elem_bits, base, boff, row_len );
                    }
                }
            }

            ColumnPairPostCopy ( self, ctx );
        }
    }
}


/* CopyStatic
 *  copy static column from source to destination
 */
void ColumnPairCopyStatic ( ColumnPair *self, const ctx_t *ctx, int64_t first_id, uint64_t count )
{
    FUNC_ENTRY ( ctx );

    rc_t rc;
    const void *base;
    uint32_t elem_bits, boff, row_len;

    if ( ! self -> is_static )
    {
        rc = RC ( rcExe, rcColumn, rcCopying, rcType, rcIncorrect );
        INTERNAL_ERROR ( rc, "'%s' IS NOT A STATIC COLUMN", self -> full_spec );
        return;
    }

    STATUS ( 3, "copying static column '%s'", self -> full_spec );
                    
    TRY ( base = ColumnReaderRead ( self -> reader, ctx, first_id, & elem_bits, & boff, & row_len ) )
    {
        ColumnWriterWriteStatic ( self -> writer, ctx, elem_bits, base, boff, row_len, count );
    }
}