File: mapping-row-set.c

package info (click to toggle)
sra-sdk 2.9.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 31,724 kB
  • sloc: ansic: 182,592; cpp: 33,717; sh: 5,385; perl: 4,969; makefile: 4,030; python: 3,560; java: 2,363; yacc: 786; lex: 416; lisp: 77; xml: 54
file content (496 lines) | stat: -rw-r--r-- 13,587 bytes parent folder | download | duplicates (8)
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
/*===========================================================================
 *
 *                            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 MappingRowSet MappingRowSet;
#define ROWSET_IMPL MappingRowSet

typedef struct MappingRowSetIterator MappingRowSetIterator;
#define ROWSET_ITER_IMPL MappingRowSetIterator

#include "row-set-priv.h"
#include "tbl-pair.h"
#include "idx-mapping.h"
#include "map-file.h"
#include "ctx.h"
#include "caps.h"
#include "except.h"
#include "status.h"
#include "mem.h"
#include "sra-sort.h"

#include <klib/rc.h>

FILE_ENTRY ( mapping-row-set );


/*--------------------------------------------------------------------------
 * MappingRowSetIterator
 *  interface to iterate RowSets
 */
struct MappingRowSetIterator
{
    RowSetIterator dad;

    /* base row-id */
    int64_t row_id;

    /* last row-id + 1 */
    int64_t last_excl;

    /* takes its input from new=>old index */
    const MapFile *idx;

    /* working data within map */
    IdxMapping *map;
    size_t max_elems;
    size_t num_elems;

    bool large;
};

/*--------------------------------------------------------------------------
 * MappingRowSet
 *  implementation of RowSet based upon auto-generated id pairs
 */
struct MappingRowSet
{
    RowSet dad;

    /* keep-alive reference */
    MappingRowSetIterator *iter;

    /* working data within map */
    IdxMapping *map;
    size_t num_elems;
    size_t cur_elem;
};

static
void MappingRowSetWhack ( MappingRowSet *self, const ctx_t *ctx )
{
    FUNC_ENTRY ( ctx );
    RowSetIteratorRelease ( & self -> iter -> dad, ctx );
    MemFree ( ctx, self, sizeof *self );
}

static
size_t MappingRowSetNextStat ( MappingRowSet *self, const ctx_t *ctx,
    int64_t *ids, size_t max_ids )
{
    FUNC_ENTRY ( ctx );

    /* the starting row-id is taken from row-set-iterator
       and offset by our current element counter */
    int64_t row_id = self -> iter -> row_id + self -> cur_elem;

    /* limit id generation to request */
    size_t i, to_set = self -> num_elems - self -> cur_elem;
    if ( to_set > max_ids )
        to_set = max_ids;

    /* fill request with serial numbers
       could just as easily be the same number
       the important thing is to limit the count
       and produce the correct range */
    for ( i = 0; i < to_set; ++ i )
        ids [ i ] = row_id + i;

    /* advance counter */
    self -> cur_elem += to_set;
    return to_set;
}

static
size_t MappingRowSetNextPhys ( MappingRowSet *self, const ctx_t *ctx,
    int64_t *ids, size_t max_ids )
{
    FUNC_ENTRY ( ctx );

    /* limit copy to request */
    size_t i, to_copy = self -> num_elems - self -> cur_elem;
    if ( to_copy > max_ids )
        to_copy = max_ids;

    /* copy out old-ids */
    for ( i = 0; i < to_copy; ++ i )
        ids [ i ] = self -> map [ self -> cur_elem + i ] . old_id;

    /* advance counter */
    self -> cur_elem += to_copy;
    return to_copy;
}

static
void MappingRowSetReset ( MappingRowSet *self, const ctx_t *ctx, bool for_static );

static RowSet_vt MappingRowSetPhys_vt =
{
    MappingRowSetWhack,
    MappingRowSetNextPhys,
    MappingRowSetReset
};

static RowSet_vt MappingRowSetStat_vt =
{
    MappingRowSetWhack,
    MappingRowSetNextStat,
    MappingRowSetReset
};

static
void MappingRowSetReset ( MappingRowSet *self, const ctx_t *ctx, bool for_static )
{
    FUNC_ENTRY ( ctx );

    if ( for_static )
    {
        /* go static */
        self -> dad . vt = & MappingRowSetStat_vt;
    }
    else
    {
        size_t i, max_elems;
        MappingRowSetIterator *iter = self -> iter;

        /* go physical */
        self -> dad . vt = & MappingRowSetPhys_vt;

        /* limit id generation */
        max_elems = iter -> max_elems;
        if ( iter -> row_id + max_elems >= iter -> last_excl )
            max_elems = ( size_t ) ( iter -> last_excl - iter -> row_id );

        STATUS ( 4, "auto-generating %,zu ( old_id, new_id ) pairs where new_id >= %ld",
                 max_elems, iter -> row_id );
        for ( i = 0; i < max_elems; ++ i )
            iter -> map [ i ] . old_id = iter -> map [ i ] . new_id = iter -> row_id + i;

        self -> num_elems = iter -> num_elems = max_elems;
    }

    /* ready */
    self -> cur_elem = 0;
}

static
void MapFileMappingRowSetReset ( MappingRowSet *self, const ctx_t *ctx, bool for_static );

static RowSet_vt MapFileMappingRowSetPhys_vt =
{
    MappingRowSetWhack,
    MappingRowSetNextPhys,
    MapFileMappingRowSetReset
};

static RowSet_vt MapFileMappingRowSetStat_vt =
{
    MappingRowSetWhack,
    MappingRowSetNextStat,
    MapFileMappingRowSetReset
};

static
void MapFileMappingRowSetReset ( MappingRowSet *self, const ctx_t *ctx, bool for_static )
{
    FUNC_ENTRY ( ctx );

    if ( for_static )
    {
        /* go static */
        self -> dad . vt = & MapFileMappingRowSetStat_vt;
    }
    else
    {
        MappingRowSetIterator *iter = self -> iter;

        /* go physical */
        self -> dad . vt = & MapFileMappingRowSetPhys_vt;

        /* read new-ids from file, selecting only those within range */
        STATUS ( 3, "selecting first %,zu ( old_id, new_id ) pairs from old=>new where new_id >= %,ld",
                 iter -> max_elems, iter -> row_id );
        ON_FAIL ( iter -> num_elems = MapFileSelectOldToNewPairs ( iter -> idx, ctx,
                      iter -> row_id, iter -> map, iter -> max_elems ) )
            return;

        self -> num_elems = iter -> num_elems;
        STATUS ( 3, "selected %,zu ( old_id, new_id ) pairs", self -> num_elems );
    }

    /* ready */
    self -> cur_elem = 0;
}


/*--------------------------------------------------------------------------
 * MappingRowSetIterator
 *  interface to iterate RowSets
 */

static
void MappingRowSetIteratorWhack ( MappingRowSetIterator *self, const ctx_t *ctx )
{
    FUNC_ENTRY ( ctx );

    if ( self -> map != NULL )
        MemFree ( ctx, self -> map, sizeof self -> map [ 0 ] * self -> max_elems );

    RowSetIteratorDestroy ( & self -> dad, ctx );
    MemFree ( ctx, self, sizeof * self );
}

static
void MapFileMappingRowSetIteratorWhack ( MappingRowSetIterator *self, const ctx_t *ctx )
{
    FUNC_ENTRY ( ctx );

    if ( self -> map != NULL )
        MemFree ( ctx, self -> map, sizeof self -> map [ 0 ] * self -> max_elems );

    MapFileRelease ( self -> idx, ctx );

    RowSetIteratorDestroy ( & self -> dad, ctx );
    MemFree ( ctx, self, sizeof * self );
}

static
RowSet *MappingRowSetIteratorMakeTheRowSet ( MappingRowSetIterator *self, const ctx_t *ctx, const RowSet_vt *vt )
{
    FUNC_ENTRY ( ctx );

    MappingRowSet *rs;
    TRY ( rs = MemAlloc ( ctx, sizeof * rs, false ) )
    {
        TRY ( RowSetInit ( & rs -> dad, ctx, vt ) )
        {
            /* initialize the row-set with map and self reference */
            rs -> map = self -> map;
            rs -> iter = ( MappingRowSetIterator* ) RowSetIteratorDuplicate ( & self -> dad, ctx );
            rs -> num_elems = self -> num_elems;
            rs -> cur_elem = 0;
            return & rs -> dad;
        }

        MemFree ( ctx, rs, sizeof * rs );
    }

    return NULL;
 }

static
struct IdxMapping *MappingRowSetIteratorGetIdxMapping ( MappingRowSetIterator *self, const ctx_t *ctx, size_t *num_items )
{
    FUNC_ENTRY ( ctx );

    if ( self -> map == NULL )
    {
        rc_t rc = RC ( rcExe, rcCursor, rcAccessing, rcSelf, rcInconsistent );
        ERROR ( rc, "IdxMapping array is NULL" );
        if ( num_items != NULL )
            * num_items = 0;
        return NULL;
    }

    if ( num_items != NULL )
        * num_items = self -> num_elems;
    return self -> map;
}

static
int64_t *MappingRowSetIteratorGetSourceIds ( MappingRowSetIterator *self, const ctx_t *ctx, uint32_t **opt_ord, size_t *num_items )
{
    FUNC_ENTRY ( ctx );

    ANNOTATE ( "CALLING GetSourceIds ON MAPPING ROWSET ITERATOR" );

    if ( opt_ord != NULL )
        * opt_ord = NULL;

    if ( num_items != NULL )
        * num_items = 0;

    return NULL;
}

static
void MappingRowSetIteratorAllocMap ( MappingRowSetIterator *self, const ctx_t *ctx )
{
    FUNC_ENTRY ( ctx );

    /* determine the maximum number of elements */
    const Tool *tp = ctx -> caps -> tool;
    uint64_t count = self -> last_excl - self -> row_id;
    self -> max_elems = self -> large ? tp -> max_large_idx_ids : tp -> max_idx_ids;
    if ( ( uint64_t ) self -> max_elems > count )
        self -> max_elems = ( size_t ) count;

    /* try to allocate the memory
       this may be limited by the MemBank */
    do
    {
        CLEAR ();

        TRY ( self -> map = MemAlloc ( ctx, sizeof self -> map [ 0 ] * self -> max_elems, false ) )
        {
            STATUS ( 4, "allocated mapping array of %,u elements", self -> max_elems );
            return;
        }

        self -> max_elems >>= 1;
    }
    while ( self -> max_elems >= ctx -> caps -> tool -> min_idx_ids );

    ANNOTATE ( "failed to allocate map for row-set" );
}

static
RowSet *MappingRowSetIteratorNextInt ( MappingRowSetIterator *self, const ctx_t *ctx, const RowSet_vt *vt )
{
    /* no FUNC_ENTRY */

    RowSet *rs = NULL;

    /* on first invocation, allocate the map */
    if ( self -> map == NULL )
    {
        ON_FAIL ( MappingRowSetIteratorAllocMap ( self, ctx ) )
            return NULL;
    }

    /* advance row_id */
    self -> row_id += self -> num_elems;
    self -> num_elems = 0;

    /* create the empty row-set */
    if ( self -> row_id < self -> last_excl )
        rs = MappingRowSetIteratorMakeTheRowSet ( self, ctx, vt );

    return rs;
}

static
RowSet *MappingRowSetIteratorNext ( MappingRowSetIterator *self, const ctx_t *ctx )
{
    FUNC_ENTRY ( ctx );
    return MappingRowSetIteratorNextInt ( self, ctx, & MappingRowSetPhys_vt );
}

static
RowSet *MapFileMappingRowSetIteratorNext ( MappingRowSetIterator *self, const ctx_t *ctx )
{
    FUNC_ENTRY ( ctx );
    return MappingRowSetIteratorNextInt ( self, ctx, & MapFileMappingRowSetPhys_vt );
}


/*--------------------------------------------------------------------------
 * TablePair
 */

static RowSetIterator_vt MappingRowSetIterator_vt =
{
    MappingRowSetIteratorWhack,
    MappingRowSetIteratorGetIdxMapping,
    MappingRowSetIteratorGetSourceIds,
    MappingRowSetIteratorNext
};

/* MakeMappingRowSetIterator
 *  runs from first to last id in cursor
 */
RowSetIterator *TablePairMakeMappingRowSetIterator ( TablePair *self, const ctx_t *ctx, bool large )
{
    FUNC_ENTRY ( ctx );

    if ( self -> first_id > self -> last_excl )
    {
        rc_t rc = RC ( rcExe, rcCursor, rcConstructing, rcRange, rcInvalid );
        INTERNAL_ERROR ( rc, "union of all column ranges improperly set" );
    }
    else
    {
        MappingRowSetIterator *rsi;

        TRY ( rsi = MemAlloc ( ctx, sizeof * rsi, true ) )
        {
            TRY ( RowSetIteratorInit ( & rsi -> dad, ctx, & MappingRowSetIterator_vt ) )
            {
                rsi -> row_id = self -> first_id;
                rsi -> last_excl = self -> last_excl;
                rsi -> large = large;
                return & rsi -> dad;
            }

            MemFree ( ctx, rsi, sizeof * rsi );
        }
    }

    return NULL;
}


/*--------------------------------------------------------------------------
 * MapFile
 */

static RowSetIterator_vt MapFileMappingRowSetIterator_vt =
{
    MapFileMappingRowSetIteratorWhack,
    MappingRowSetIteratorGetIdxMapping,
    MappingRowSetIteratorGetSourceIds,
    MapFileMappingRowSetIteratorNext
};

RowSetIterator * MapFileMakeMappingRowSetIterator ( const MapFile *self, const ctx_t *ctx, bool large )
{
    FUNC_ENTRY ( ctx );

    MappingRowSetIterator *rsi;

    /* create the iterator */
    TRY ( rsi = MemAlloc ( ctx, sizeof * rsi, true ) )
    {
        TRY ( RowSetIteratorInit ( & rsi -> dad, ctx, & MapFileMappingRowSetIterator_vt ) )
        {
            /* initialize the row-set with map and self reference */
            TRY ( rsi -> row_id = MapFileFirst ( self, ctx ) )
            {
                TRY ( rsi -> last_excl = rsi -> row_id + MapFileCount ( self, ctx ) )
                {
                    TRY ( rsi -> idx = MapFileDuplicate ( self, ctx ) )
                    {
                        rsi -> large = large;
                        return & rsi -> dad;
                    }
                }
            }
        }

        MemFree ( ctx, rsi, sizeof * rsi );
    }

    return NULL;
}