File: sorting-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 (405 lines) | stat: -rw-r--r-- 11,499 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
/*===========================================================================
 *
 *                            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 SortingRowSet SortingRowSet;
#define ROWSET_IMPL SortingRowSet

typedef struct SortingRowSetIterator SortingRowSetIterator;
#define ROWSET_ITER_IMPL SortingRowSetIterator

#include "row-set-priv.h"
#include "tbl-pair.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>

#include <string.h>

FILE_ENTRY ( sorting-row-set );


/*--------------------------------------------------------------------------
 * SortingRowSetIterator
 *  interface to iterate RowSets
 */
struct SortingRowSetIterator
{
    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 */
    int64_t *src_ids;
    uint32_t *new_ord;
    size_t max_elems;
    size_t num_elems;

    /* if true, use setting for large-columns */
    bool large;

    /* if true, no need to regenerate new_ord */
    bool new_ord_valid;
};

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

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

    /* working data within map */
    int64_t *src_ids;
    size_t num_elems;
    size_t cur_elem;
};

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

static
size_t SortingRowSetNextStat ( SortingRowSet *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 SortingRowSetNextPhys ( SortingRowSet *self, const ctx_t *ctx,
    int64_t *ids, size_t max_ids )
{
    FUNC_ENTRY ( ctx );

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

    /* copy out old-ids */
    memmove ( ids, & self -> src_ids [ self -> cur_elem ], to_copy * sizeof ids [ 0 ] );

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

static
void SortingRowSetReset ( SortingRowSet *self, const ctx_t *ctx, bool for_static );

static RowSet_vt SortingRowSetPhys_vt =
{
    SortingRowSetWhack,
    SortingRowSetNextPhys,
    SortingRowSetReset
};

static RowSet_vt SortingRowSetStat_vt =
{
    SortingRowSetWhack,
    SortingRowSetNextStat,
    SortingRowSetReset
};

static
void SortingRowSetReset ( SortingRowSet *self, const ctx_t *ctx, bool for_static )
{
    FUNC_ENTRY ( ctx );

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

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

        if ( iter -> new_ord_valid )
        {
            /* read new-ids from file, selecting old-ids within range */
            STATUS ( 3, "selecting first %,zu ids from old=>new where new_id >= %,ld",
                     iter -> max_elems, iter -> row_id );
            iter -> num_elems = MapFileSelectOldToNewSingle ( iter -> idx, ctx,
                iter -> row_id, iter -> src_ids, NULL, iter -> max_elems );
        }
        else
        {
            /* read new-ids from file, init new-ord, selecting old-ids within range */
            STATUS ( 3, "selecting first %,zu ids, new_ord from old=>new where new_id >= %,ld",
                     iter -> max_elems, iter -> row_id );
            TRY ( iter -> num_elems = MapFileSelectOldToNewSingle ( iter -> idx, ctx,
                      iter -> row_id, iter -> src_ids, iter -> new_ord, iter -> max_elems ) )
            {
                iter -> new_ord_valid = true;
            }
        }

        if ( FAILED () )
            return;

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

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


/*--------------------------------------------------------------------------
 * SortingRowSetIterator
 *  interface to iterate RowSets
 */

static
void SortingRowSetIteratorWhack ( SortingRowSetIterator *self, const ctx_t *ctx )
{
    FUNC_ENTRY ( ctx );

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

    MapFileRelease ( self -> idx, ctx );

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

static
RowSet *SortingRowSetIteratorMakeTheRowSet ( SortingRowSetIterator *self, const ctx_t *ctx, const RowSet_vt *vt )
{
    FUNC_ENTRY ( ctx );

    SortingRowSet *rs;
    TRY ( rs = MemAlloc ( ctx, sizeof * rs, false ) )
    {
        TRY ( RowSetInit ( & rs -> dad, ctx, vt ) )
        {
            /* initialize the row-set with map and self reference */
            rs -> src_ids = self -> src_ids;
            rs -> iter = ( SortingRowSetIterator* ) 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 *SortingRowSetIteratorGetIdxMapping ( SortingRowSetIterator *self, const ctx_t *ctx, size_t *num_items )
{
    FUNC_ENTRY ( ctx );

    ANNOTATE ( "CALLING GetIdxMapping ON SORTING ROWSET ITERATOR" );

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

    return NULL;
}

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

    if ( opt_ord != NULL )
        * opt_ord = self -> new_ord;

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

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

static
void SortingRowSetIteratorAllocMap ( SortingRowSetIterator *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 -> src_ids = MemAlloc ( ctx, sizeof self -> src_ids [ 0 ] * self -> max_elems, false ) )
        {
            TRY ( self -> new_ord = MemAlloc ( ctx, sizeof self -> new_ord [ 0 ] * self -> max_elems, false ) )
            {
                STATUS ( 4, "allocated mapping array of %,u elements", self -> max_elems );
                return;
            }

            MemFree ( ctx, self -> src_ids, sizeof self -> src_ids [ 0 ] * self -> max_elems );
            self -> src_ids = NULL;
        }

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

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

static
RowSet *SortingRowSetIteratorNext ( SortingRowSetIterator *self, const ctx_t *ctx )
{
    FUNC_ENTRY ( ctx );

    RowSet *rs = NULL;

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

        assert ( self -> src_ids != NULL );
        assert ( self -> new_ord != NULL );
    }

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

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

    return rs;
}


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

static RowSetIterator_vt SortingRowSetIterator_vt =
{
    SortingRowSetIteratorWhack,
    SortingRowSetIteratorGetIdxMapping,
    SortingRowSetIteratorGetSourceIds,
    SortingRowSetIteratorNext
};

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

    SortingRowSetIterator *rsi;

    /* create the iterator */
    TRY ( rsi = MemAlloc ( ctx, sizeof * rsi, true ) )
    {
        TRY ( RowSetIteratorInit ( & rsi -> dad, ctx, & SortingRowSetIterator_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;
}