File: id-mapper-col.c

package info (click to toggle)
sra-sdk 3.0.3%2Bdfsg-6~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 165,852 kB
  • sloc: ansic: 374,775; cpp: 232,734; perl: 8,959; java: 6,253; sh: 6,032; python: 3,890; makefile: 1,046; yacc: 703; xml: 310; lex: 235
file content (260 lines) | stat: -rw-r--r-- 7,470 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
/*===========================================================================
 *
 *                            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 MapRowIdColWriter MapRowIdColWriter;
#define COLWRITER_IMPL MapRowIdColWriter

#include "id-mapper-col.h"
#include "map-file.h"
#include "csra-tbl.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 ( id-mapper-col );


/*--------------------------------------------------------------------------
 * MapRowIdColWriter
 */

struct MapRowIdColWriter
{
    ColumnWriter dad;

    ColumnWriter *cw;

    MapFile *idx;

    int64_t *row;
    uint32_t max_row_len;

    bool assign_ids;
};

/* called by Release */
static
void MapRowIdColWriterDestroy ( MapRowIdColWriter *self, const ctx_t *ctx )
{
    FUNC_ENTRY ( ctx );

    ColumnWriterRelease ( self -> cw, ctx );
    self -> cw = NULL;

    MapFileRelease ( self -> idx, ctx );
    self -> idx = NULL;

    if ( self -> row != NULL )
    {
        MemFree ( ctx, self -> row, sizeof * self -> row * self -> max_row_len );
        self -> row = NULL;
        self -> max_row_len = 0;
    }
}

static
void MapRowIdColWriterWhack ( MapRowIdColWriter *self, const ctx_t *ctx )
{
    FUNC_ENTRY ( ctx );
    MapRowIdColWriterDestroy ( self, ctx );
    ColumnWriterDestroy ( & self -> dad, ctx );
    MemFree ( ctx, self, sizeof * self );
}

/* full spec */
static
const char* MapRowIdColWriterFullSpec ( const MapRowIdColWriter *self, const ctx_t *ctx )
{
    FUNC_ENTRY ( ctx );
    return ColumnWriterFullSpec ( self -> cw, ctx );
}

static
void MapRowIdColWriterPreCopy ( MapRowIdColWriter *self, const ctx_t *ctx )
{
    FUNC_ENTRY ( ctx );

    ColumnWriterPreCopy ( self -> cw, ctx );
}

static
void MapRowIdColWriterPostCopy ( MapRowIdColWriter *self, const ctx_t *ctx )
{
    FUNC_ENTRY ( ctx );

    if ( self -> max_row_len > 16 )
    {
        void *row;
        TRY ( row = MemAlloc ( ctx, sizeof * self -> row * 16, false ) )
        {
            MemFree ( ctx, self -> row, sizeof * self -> row * self -> max_row_len );
            self -> row = row;
            self -> max_row_len = 16;
        }
        CATCH_ALL ()
        {
            CLEAR ();
        }
    }

    ColumnWriterPostCopy ( self -> cw, ctx );
}

/* write row to destination */
static
void MapRowIdColWriterWrite ( MapRowIdColWriter *self, const ctx_t *ctx,
    uint32_t elem_bits, const void *data, uint32_t boff, uint32_t row_len )
{
    FUNC_ENTRY ( ctx );

    uint32_t i;

    /* here is where the VALUE coming in, which is a row-id,
       gets mapped to its new value before being written out */
    assert ( elem_bits == sizeof * self -> row * 8 );
    assert ( boff == 0 );

    /* resize row if needed */
    if ( self -> max_row_len < row_len )
    {
        MemFree ( ctx, self -> row, sizeof * self -> row * self -> max_row_len );

        self -> max_row_len = ( row_len + 15 ) & ~ 15U;
        ON_FAIL ( self -> row = MemAlloc ( ctx, sizeof * self -> row * self -> max_row_len, false ) )
        {
            ANNOTATE ( "failed to allocate memory for %u row-ids", self -> max_row_len );
            return;
        }
    }

    /* copy row */
    memmove ( self -> row, data, row_len * sizeof * self -> row );

    /* map ids */
    for ( i = 0; i < row_len; ++ i )
    {
        if ( self -> row [ i ] != 0 )
        {
            int64_t new_id;

            /* map old row-id to new row-id */
            ON_FAIL ( new_id = MapFileMapSingleOldToNew ( self -> idx, ctx, self -> row [ i ], self -> assign_ids ) )
            {
                ANNOTATE ( "failed to map source row-id %ld", self -> row [ i ] );
                return;
            }
            if ( new_id == 0 )
            {
                rc_t rc = RC ( rcExe, rcColumn, rcWriting, rcId, rcNotFound );
                ERROR ( rc, "failed to map source row-id %ld", self -> row [ i ] );
                return;
            }

            self -> row [ i ] = new_id;
        }
    }

    /* now write the translated ids */
    ColumnWriterWrite ( self -> cw, ctx, sizeof * self -> row * 8, self -> row, 0, row_len );
}

static
void MapRowIdColWriterWriteStatic ( MapRowIdColWriter *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 );
    rc_t rc = RC ( rcExe, rcColumn, rcWriting, rcType, rcIncorrect );
    INTERNAL_ERROR ( rc, "writing to a non-static column" );
}

/* commit all writes */
static
void MapRowIdColWriterCommit ( MapRowIdColWriter *self, const ctx_t *ctx )
{
    FUNC_ENTRY ( ctx );
    ColumnWriterCommit ( self -> cw, ctx );
    MapRowIdColWriterDestroy ( self, ctx );
}

static ColumnWriter_vt MapRowIdColWriter_vt =
{
    MapRowIdColWriterWhack,
    MapRowIdColWriterFullSpec,
    MapRowIdColWriterPreCopy,
    MapRowIdColWriterPostCopy,
    MapRowIdColWriterWrite,
    MapRowIdColWriterWriteStatic,
    MapRowIdColWriterCommit
};


/* MakeMapRowIdWriter
 *  if "assign_ids" is true, the MapFile will be asked to
 *  map an old=>new id or assign (and record) a new id.
 */
ColumnWriter *TablePairMakeMapRowIdWriter ( TablePair *self,
    const ctx_t *ctx, ColumnWriter *cw, MapFile *idx, bool assign_ids )
{
    FUNC_ENTRY ( ctx );

    MapRowIdColWriter *writer;

    TRY ( writer = MemAlloc ( ctx, sizeof * writer, false ) )
    {
        TRY ( ColumnWriterInit ( & writer -> dad, ctx, & MapRowIdColWriter_vt, true ) )
        {
            writer -> max_row_len = 16;
            TRY ( writer -> row = MemAlloc ( ctx, sizeof * writer -> row * writer -> max_row_len, false ) )
            {
                TRY ( writer -> idx = MapFileDuplicate ( idx, ctx ) )
                {
                    TRY ( writer -> cw = ColumnWriterDuplicate ( cw, ctx ) )
                    {
                        writer -> assign_ids = assign_ids;
                        return & writer -> dad;
                    }

                    MapFileRelease ( writer -> idx, ctx );
                }

                ColumnWriterDestroy ( & writer -> dad, ctx );
            }

            MemFree ( ctx, writer -> row, sizeof * writer -> row * writer -> max_row_len );
        }

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

    return NULL;
}