File: Client.xs

package info (click to toggle)
libcassandra-client-perl 0.21-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 716 kB
  • sloc: perl: 3,898; ansic: 1,767; makefile: 3
file content (284 lines) | stat: -rw-r--r-- 8,726 bytes parent folder | download | duplicates (2)
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
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"

#include <stdint.h>
#include "define.h"
#include "type.h"
#include "proto.h"
#include "decode.h"
#include "encode.h"

typedef struct {
    int column_count;
    int uniq_column_count;
    struct cc_column *columns;
} Cassandra__Client__RowMeta;

MODULE = Cassandra::Client  PACKAGE = Cassandra::Client::Protocol
PROTOTYPES: DISABLE

void
unpack_metadata(protocol_version, is_result, data)
    int protocol_version
    int is_result
    SV *data
  PPCODE:
    STRLEN pos, size;
    unsigned char *ptr;
    int32_t flags, column_count, uniq_column_count;
    Cassandra__Client__RowMeta *row_meta;

    ST(0) = &PL_sv_undef; /* Will have our RowMeta instance */
    ST(1) = &PL_sv_undef; /* Will have our paging state */

    ptr = (unsigned char*)SvPV(data, size);
    pos = 0;

    if (UNLIKELY(!ptr))
        croak("Missing data argument to unpack_metadata");
    if (UNLIKELY(protocol_version != 3 && protocol_version != 4))
        croak("Invalid protocol version");

    flags = unpack_int(aTHX_ ptr, size, &pos);
    column_count = unpack_int(aTHX_ ptr, size, &pos);

    if (protocol_version >= 4 && !is_result) {
        int i, pk_count;

        pk_count = unpack_int(aTHX_ ptr, size, &pos);
        if (UNLIKELY(pk_count < 0))
            croak("Protocol error: pk_count<0");

        for (i = 0; i < pk_count; i++) {
            // Read the short, but throw it away for now.
            unpack_short(aTHX_ ptr, size, &pos);
        }
    }

    if (UNLIKELY(flags < 0 || flags > 7))
        croak("Invalid protocol data passed to unpack_metadata (reason: invalid flags)");
    if (UNLIKELY(column_count < 0))
        croak("Invalid protocol data passed to unpack_metadata (reason: invalid column count)");

    if (flags & CC_METADATA_FLAG_HAS_MORE_PAGES) {
        ST(1) = unpack_bytes_sv(aTHX_ ptr, size, &pos);
        sv_2mortal(ST(1));
    }

    if (!(flags & CC_METADATA_FLAG_NO_METADATA)) {
        int i, have_global_spec;
        SV *global_keyspace, *global_table;
        HV *name_hash;

        have_global_spec = flags & CC_METADATA_FLAG_GLOBAL_TABLES_SPEC;

        if (have_global_spec) {
            global_keyspace = unpack_string_sv(aTHX_ ptr, size, &pos);
            sv_2mortal(global_keyspace);
            global_table = unpack_string_sv(aTHX_ ptr, size, &pos);
            sv_2mortal(global_table);
        }

        Newxz(row_meta, 1, Cassandra__Client__RowMeta);
        ST(0) = sv_newmortal();
        sv_setref_pv(ST(0), "Cassandra::Client::RowMetaPtr", (void*)row_meta);

        if (UNLIKELY(column_count > size))
            croak("Invalid protocol data passed to unpack_metadata (reason: column count unlikely)");

        row_meta->column_count = column_count;
        Newxz(row_meta->columns, column_count, struct cc_column);

        name_hash = (HV*)sv_2mortal( (SV*)newHV() );
        uniq_column_count = 0;

        for (i = 0; i < column_count; i++) {
            struct cc_column *column = &(row_meta->columns[i]);
            if (have_global_spec) {
                column->keyspace = global_keyspace;
                SvREFCNT_inc(column->keyspace);
                column->table = global_table;
                SvREFCNT_inc(column->table);
            } else {
                column->keyspace = unpack_string_sv(aTHX_ ptr, size, &pos);
                column->table = unpack_string_sv(aTHX_ ptr, size, &pos);
            }

            column->name = unpack_string_sv_hash(aTHX_ ptr, size, &pos, &column->name_hash);
            unpack_type(aTHX_ ptr, size, &pos, &column->type);
            if (!hv_exists_ent(name_hash, column->name, column->name_hash)) {
                uniq_column_count++;
                hv_store_ent(name_hash, column->name, &PL_sv_undef, column->name_hash);
            }
        }

        row_meta->uniq_column_count = uniq_column_count;
    }

    sv_chop(data, (char*)ptr+pos);

    XSRETURN(2);

MODULE = Cassandra::Client  PACKAGE = Cassandra::Client::RowMetaPtr

AV*
decode(self, data, use_hashes)
    Cassandra::Client::RowMeta *self
    SV *data
    int use_hashes
  CODE:
    STRLEN size, pos;
    unsigned char *ptr;
    int32_t row_count;
    int i, j, col_count;
    struct cc_column *columns;

    RETVAL = newAV();
    sv_2mortal((SV*)RETVAL); /* work around a bug in perl */

    ptr = (unsigned char*)SvPV(data, size);
    pos = 0;

    if (UNLIKELY(!ptr))
        croak("Invalid input to decode()");

    col_count = self->column_count;
    columns = self->columns;

    row_count = unpack_int(aTHX_ ptr, size, &pos);

    /* This came up while fuzzing: when we have 1000000 rows but no columns, we
     * just flood the memory with empty arrays/hashes. Let's just reject this
     * corner case. If you need this, please contact the author! */
    if (UNLIKELY(row_count > 1000 && !col_count))
        croak("Refusing to decode %d rows without known column information", row_count);

    for (i = 0; i < row_count; i++) {
        if (use_hashes) {
            HV *this_row = newHV();
            av_push(RETVAL, newRV_noinc((SV*)this_row));

            for (j = 0; j < col_count; j++) {
                SV *decoded = newSV(0);
                hv_store_ent(this_row, columns[j].name, decoded, columns[j].name_hash);

                decode_cell(aTHX_ ptr, size, &pos, &columns[j].type, decoded);
            }

        } else {
            AV *this_row = newAV();
            av_push(RETVAL, newRV_noinc((SV*)this_row));

            for (j = 0; j < col_count; j++) {
                SV *decoded = newSV(0);
                av_push(this_row, decoded);

                decode_cell(aTHX_ ptr, size, &pos, &columns[j].type, decoded);
            }
        }
    }

  OUTPUT:
    RETVAL

SV*
encode(self, row)
    Cassandra::Client::RowMeta *self
    SV* row
  CODE:
    int column_count, i, use_hash;
    STRLEN size_estimate;
    AV *row_a;
    HV *row_h;

    if (UNLIKELY(row == NULL))
        croak("row must be passed");
    if (UNLIKELY(!SvROK(row)))
        croak("encode: argument must be a reference");

    column_count = self->column_count;

    if (SvTYPE(SvRV(row)) == SVt_PVAV) {
        row_a = (AV*)SvRV(row);
        use_hash = 0;
        if (UNLIKELY((av_len(row_a)+1) != column_count))
            croak("row encoder expected %d column(s), but got %d", column_count, ((int)av_len(row_a))+1);

    } else if (SvTYPE(SvRV(row)) == SVt_PVHV) {
        row_h = (HV*)SvRV(row);
        use_hash = 1;
        if (UNLIKELY(HvUSEDKEYS(row_h) != self->uniq_column_count))
            croak("row encoder expected %d column(s), but got %d", self->uniq_column_count, (int)HvUSEDKEYS(row_h));

    } else {
        croak("encode: argument must be an ARRAY or HASH reference");
    }

    /* Rough estimate. We only use it to predict Sv size, we don't rely on it being accurate.
       If we overshoot, we waste some memory, and if we undershoot we copy a bit too often. */
    size_estimate = 2 + (column_count * 12);
    if (size_estimate <= 0) /* overflows aren't impossible, I guess */
        size_estimate = 0; /* wing it */

    RETVAL = newSV(size_estimate);
    sv_setpvn(RETVAL, "", 0);
    pack_short(aTHX_ RETVAL, column_count);

    if (!use_hash) {
        for (i = 0; i < column_count; i++) {
            SV **maybe_cell = av_fetch(row_a, i, 0);
            if (UNLIKELY(maybe_cell == NULL))
                croak("row encoder error. bailing out");
            encode_cell(aTHX_ RETVAL, *maybe_cell, &self->columns[i].type);
        }

    } else {
        for (i = 0; i < column_count; i++) {
            struct cc_column *column;
            HE *ent;

            column = &self->columns[i];
            ent = hv_fetch_ent(row_h, column->name, 0, column->name_hash);
            if (UNLIKELY(!ent)) {
                croak("missing value for required entry <%s>", SvPV_nolen(column->name));
            }

            encode_cell(aTHX_ RETVAL, HeVAL(ent), &column->type);
        }
    }

  OUTPUT:
    RETVAL

AV*
column_names(self)
    Cassandra::Client::RowMeta *self
  CODE:
    int i;

    RETVAL = newAV();
    sv_2mortal((SV*)RETVAL); /* work around a bug in perl */

    for (i = 0; i < self->column_count; i++) {
        av_push(RETVAL, SvREFCNT_inc(self->columns[i].name));
    }
  OUTPUT:
    RETVAL

void
DESTROY(self)
    Cassandra::Client::RowMeta *self
  CODE:
    int i;
    for (i = 0; i < self->column_count; i++) {
        struct cc_column *column = &(self->columns[i]);
        SvREFCNT_dec(column->keyspace);
        SvREFCNT_dec(column->table);
        SvREFCNT_dec(column->name);
        cc_type_destroy(aTHX_ &column->type);
    }
    Safefree(self->columns);
    Safefree(self);