File: run.c

package info (click to toggle)
sra-sdk 2.8.1-2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 18,984 kB
  • ctags: 21,087
  • sloc: ansic: 164,474; cpp: 20,802; sh: 4,885; perl: 3,708; makefile: 3,280; python: 2,817; yacc: 471; lex: 412; lisp: 77; xml: 28
file content (388 lines) | stat: -rw-r--r-- 12,159 bytes parent folder | download | duplicates (4)
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
/*===========================================================================
 *
 *                            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.
 *
 * ===========================================================================
 *
 */

#include "sra-sort.h"
#include "db-pair.h"
#include "ctx.h"
#include "caps.h"
#include "mem.h"
#include "except.h"
#include "status.h"

#include <sra/sraschema.h>
#include <vdb/manager.h>
#include <vdb/schema.h>
#include <vdb/database.h>
#include <vdb/table.h>
#include <kfg/config.h>
#include <klib/text.h>
#include <klib/rc.h>

#include <string.h>


FILE_ENTRY ( run );

static
void typename_to_config_path ( const char *in, char *out )
{
    bool end;
    uint32_t i;

    for ( end = false, i = 0; ! end; ++ i )
    {
        switch ( out [ i ] = in [ i ] )
        {
        case ':':
            out [ i ] = '/';
            break;
        case ' ':
        case '#':
            out [ i ] = 0;
        case 0:
            end = true;
            return;
        }
    }
}

static
bool map_typename_builtin ( const ctx_t *ctx, const char *sub_node, const char *in, char *out, char *schema_src, size_t size )
{
    FUNC_ENTRY ( ctx );

    size_t copied;

#define ALIGN_EVIDENCE_MAP "NCBI:align:db:alignment_evidence_sorted"
#define ALIGN_UNSORTED_MAP "NCBI:align:db:alignment_sorted"
#define ALIGN_SRC          "/usr/lib/ncbi-vdb/align/align.vschema"

    if ( strcmp ( out, "NCBI/align/db/alignment_evidence" ) == 0 )
        copied = string_copy ( out, size, ALIGN_EVIDENCE_MAP, sizeof ALIGN_EVIDENCE_MAP - 1 );
    else if ( strcmp ( out, "NCBI/align/db/alignment_unsorted" ) == 0 )
        copied = string_copy ( out, size, ALIGN_UNSORTED_MAP, sizeof ALIGN_UNSORTED_MAP - 1 );
    else
    {
        strcpy ( out, in );
        schema_src [ 0 ] = 0;
        return false;
    }

    if ( copied != size )
    {
        out [ copied ] = 0;
        copied = string_copy ( schema_src, size, ALIGN_SRC, sizeof ALIGN_SRC - 1 );
    }

    if ( copied == size )
    {
        rc_t rc = RC ( rcExe, rcType, rcCopying, rcBuffer, rcInsufficient );
        INTERNAL_ERROR ( rc, "failed to copy a built-in constant" );
        strcpy ( out, in );
        schema_src [ 0 ] = 0;
        return false;
    }

#undef ALIGN_EVIDENCE_MAP
#undef ALIGN_UNSORTED_MAP
#undef ALIGN_SRC

    schema_src [ copied ] = 0;
    return true;
}

static
bool map_typename ( const ctx_t *ctx, const char *sub_node, const char *in, char *out, char *schema_src, size_t size )
{
    FUNC_ENTRY ( ctx );

    rc_t rc;
    const KConfigNode *n;
    size_t num_read, remaining;

    typename_to_config_path ( in, out );

    rc = KConfigOpenNodeRead ( ctx -> caps -> cfg, & n, "sra-sort/%s/%s", sub_node, out );
    if ( rc != 0 )
        return map_typename_builtin ( ctx, sub_node, in, out, schema_src, size );

    rc = KConfigNodeRead ( n, 0, out, size - 1, & num_read, & remaining );
    if ( rc != 0 )
        ERROR ( rc, "KConfigNodeRead failed" );
    else if ( remaining != 0 )
    {
        rc = RC ( rcExe, rcNode, rcReading, rcBuffer, rcInsufficient );
        ERROR ( rc, "type map of '%s' is too long", in );
    }
    else
    {
        out [ num_read ] = 0;
        typename_to_config_path ( out, schema_src );

        KConfigNodeRelease ( n );
        rc = KConfigOpenNodeRead ( ctx -> caps -> cfg, & n, "sra-sort/schema-src/%s", schema_src );
        if ( rc != 0 )
            ERROR ( rc, "KConfigOpenNodeRead - failed to find entry 'sra-sort/schema-src/%s'", schema_src );
        else
        {
            rc = KConfigNodeRead ( n, 0, schema_src, size - 1, & num_read, & remaining );
            if ( rc != 0 )
                ERROR ( rc, "KConfigNodeRead failed" );
            else if ( remaining != 0 )
            {
                rc = RC ( rcExe, rcNode, rcReading, rcBuffer, rcInsufficient );
                ERROR ( rc, "type map of '%s' is too long", in );
            }
            else
            {
                schema_src [ num_read ] = 0;
            }
        }
    }

    KConfigNodeRelease ( n );
    return true;
}

static
VSchema *map_schema_types ( TypeParams *type, const ctx_t *ctx, const VSchema *src_schema )
{
    FUNC_ENTRY ( ctx );

    bool mapped;
    char schema_src [ 256 ];
    assert ( sizeof schema_src == sizeof type -> dst_type );

    TRY ( mapped = map_typename ( ctx, "out-map", type -> src_type, type -> dst_type, schema_src, sizeof schema_src ) )
    {
        rc_t rc;
        VSchema *dst_schema;

        if ( ! mapped )
        {
            type -> view_type [ 0 ] = 0;
            rc = VSchemaAddRef ( src_schema );
            if ( rc != 0 )
                ERROR ( rc, "VSchemaAddRef failed" );
            return ( VSchema* ) src_schema;
        }

        rc = VDBManagerMakeSchema ( ctx -> caps -> vdb, & dst_schema );
        if ( rc != 0 )
            ERROR ( rc, "VDBManagerMakeSchema failed" );
        else
        {
            rc = VSchemaParseFile ( dst_schema, "%s", schema_src );
            if ( rc != 0 )
                ERROR ( rc, "VSchemaParseFile failed adding file '%s' for destination", src_schema );
            else
            {
                TRY ( mapped = map_typename ( ctx, "view-map", type -> src_type, type -> view_type, schema_src, sizeof schema_src ) )
                {
                    if ( ! mapped )
                    {
                        type -> view_type [ 0 ] = 0;
                        return dst_schema;
                    }

                    rc = VSchemaParseFile ( dst_schema, "%s", schema_src );
                    if ( rc == 0 )
                        return dst_schema;

                    ERROR ( rc, "VSchemaParseFile failed adding file '%s' for view", src_schema );
                }
            }

            VSchemaRelease ( dst_schema );
        }
    }

    return NULL;
}

/* open_db
 *  called from run
 *  determines the type of schema
 *  opens output object
 *  dispatches to the appropriate handler
 */
static
void open_db ( const ctx_t *ctx, const VDatabase **srcp )
{
    FUNC_ENTRY ( ctx );

    const VDatabase *src = *srcp;

    const VSchema *src_schema;
    rc_t rc = VDatabaseOpenSchema ( src, & src_schema );
    if ( rc != 0 )
        ERROR ( rc, "VDatabaseOpenSchema failed" );
    else
    {
        TypeParams type;
        rc = VDatabaseTypespec ( src, type . src_type, sizeof type . src_type );
        if ( rc != 0 )
            ERROR ( rc, "failed to obtain database typespec" );
        else
        {
            VSchema *dst_schema;

            /* map input type to output type */
            TRY ( dst_schema = map_schema_types ( & type, ctx, src_schema ) )
            {
                const Tool *tp = ctx -> caps -> tool;

                /* if view was remapped, reopen the database */
                if ( type . view_type [ 0 ] != 0 )
                {
                    VSchemaRelease ( src_schema );
                    rc = VSchemaAddRef ( dst_schema );
                    if ( rc != 0 )
                    {
                        ERROR ( rc, "VSchemaAddRef failed" );
                        src_schema = NULL;
                    }
                    else
                    {
                        src_schema = dst_schema;
                        VDatabaseRelease ( src );
                        rc = VDBManagerOpenDBRead ( ctx -> caps -> vdb, srcp, src_schema, "%s", tp -> src_path );
                        if ( rc != 0 )
                            ERROR ( rc, "VDBManagerOpenDBRead failed reopening db '%s'", tp -> src_path );
                        src = *srcp;
                    }
                }

                if ( ! FAILED () )
                {
                    VDatabase *dst;
                    rc = VDBManagerCreateDB ( ctx -> caps -> vdb, & dst, dst_schema,
                                              type . dst_type, tp -> db . cmode, "%s", tp -> dst_path );
                    if ( rc != 0 )
                        ERROR ( rc, "VDBManagerCreateDB failed to create '%s' with type '%s'", tp -> dst_path, type . dst_type );
                    else
                    {
                        rc = VDatabaseColumnCreateParams ( dst, tp -> col . cmode, tp -> col . checksum, tp -> col . pgsize );
                        if ( rc != 0 )
                            ERROR ( rc, "VDatabaseColumnCreateParams: failed to set column create params on db '%s'", tp -> dst_path );
                        else
                        {
                            DbPair *pb;

                            /* TBD - this has to be fixed to use proper stuff */
                            const char *name = strrchr ( tp -> src_path, '/' );
                            if ( name ++ == NULL )
                                name = tp -> src_path;

                            TRY ( pb = DbPairMake ( ctx, src, dst, name ) )
                            {
                                DbPairRun ( pb, ctx );
                                DbPairRelease ( pb, ctx );
                            }
                        }

                        rc = VDatabaseRelease ( dst );
                        if ( rc != 0 )
                            ERROR ( rc, "VDatabaseRelease failed on '%s'", tp -> dst_path );
                    }
                }

                VSchemaRelease ( dst_schema );
            }
        }

        VSchemaRelease ( src_schema );
    }
}


/* open_tbl
 *  called from run
 *  determines the type of schema
 *  opens output object
 *  dispatches to the appropriate handler
 */
static
void open_tbl ( const ctx_t *ctx, const VTable **tblp )
{
    FUNC_ENTRY ( ctx );

    rc_t rc = RC ( rcExe, rcFunction, rcExecuting, rcFunction, rcUnsupported );
    ERROR ( rc, "unimplemented function" );
}

/* run
 *  called from KMain
 *  determines the type of object being copied/sorted
 *  opens input object
 *  dispatches to the appropriate handler
 */
void run ( const ctx_t *ctx )
{
    FUNC_ENTRY ( ctx );

    VDBManager *mgr = ctx -> caps -> vdb;
    const Tool *tp = ctx -> caps -> tool;

    const VDatabase *db;
    rc_t rc = VDBManagerOpenDBRead ( mgr, & db, NULL, "%s", tp -> src_path );
    if ( rc == 0 )
    {
        open_db ( ctx, & db );
        VDatabaseRelease ( db );
    }
    else
    {
        const VTable *tbl;
        rc_t rc2 = VDBManagerOpenTableRead ( mgr, & tbl, NULL, "%s", tp -> src_path );
        if ( rc2 == 0 )
        {
            rc = 0;
            open_tbl ( ctx, & tbl );
            VTableRelease ( tbl );
        }
        else
        {
            VSchema *sra_dflt;
            rc2 = VDBManagerMakeSRASchema ( mgr, & sra_dflt );
            if ( rc2 == 0 )
            {
                rc2 = VDBManagerOpenTableRead ( mgr, & tbl, sra_dflt, "%s", tp -> src_path );
                if ( rc2 == 0 )
                {
                    rc = 0;
                    open_tbl ( ctx, & tbl );
                    VTableRelease ( tbl );
                }
                
                VSchemaRelease ( sra_dflt );
            }
        }
    }

    if ( rc != 0 )
        ERROR ( rc, "failed to open object '%s'", tp -> src_path );
}