Class: AerospikeNative::Batch

Inherits:
Object
  • Object
show all
Defined in:
ext/aerospike_native/batch.c

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vClient) ⇒ Object



8
9
10
11
12
13
# File 'ext/aerospike_native/batch.c', line 8

VALUE batch_initialize(VALUE vSelf, VALUE vClient)
{
    check_aerospike_client(vClient);
    rb_iv_set(vSelf, "@client", vClient);
    return vSelf;
}

Instance Attribute Details

#clientObject (readonly)

Instance Method Details

#exists(, vSelf) ⇒ Object



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
# File 'ext/aerospike_native/batch.c', line 154

VALUE batch_exists(int argc, VALUE* vArgs, VALUE vSelf)
{
    VALUE vKeys, vClient, vArray;

    as_batch batch;
    as_policy_batch policy;
    as_key* key;
    aerospike* ptr;
    as_error err;

    uint32_t n = 0, idx = 0;

    if (argc > 2 || argc < 1) {  // there should only be 1 or 2 arguments
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc);
    }

    vKeys = vArgs[0];
    Check_Type(vKeys, T_ARRAY);

    as_policy_batch_init(&policy);
    if (argc == 2 && TYPE(vArgs[1]) != T_NIL) {
        SET_BATCH_POLICY(policy, vArgs[1]);
    }

    idx = RARRAY_LEN(vKeys);
    as_batch_inita(&batch, idx);

    for(n = 0; n < idx; n++) {
        VALUE vKey = rb_ary_entry(vKeys, n);
        Data_Get_Struct(vKey, as_key, key);
        as_key_init_value(as_batch_keyat(&batch, n), key->ns, key->set, key->valuep);
    }

    vClient = rb_iv_get(vSelf, "@client");
    Data_Get_Struct(vClient, aerospike, ptr);

    vArray = rb_ary_new();
    if (aerospike_batch_exists(ptr, &err, &policy, &batch, batch_read_callback, &vArray) != AEROSPIKE_OK) {
        as_batch_destroy(&batch);
        raise_aerospike_exception(err.code, err.message);
    }
    as_batch_destroy(&batch);

    if ( rb_block_given_p() ) {
        return Qnil;
    }

    return vArray;
}

#get(, vSelf) ⇒ Object



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
# File 'ext/aerospike_native/batch.c', line 58

VALUE batch_get(int argc, VALUE* vArgs, VALUE vSelf)
{
    VALUE vKeys, vClient, vArray, vBins;

    as_batch batch;
    as_policy_batch policy;
    as_key* key;
    aerospike* ptr;
    as_error err;

    uint32_t n = 0, idx = 0, bins_idx = 0;

    if (argc > 3 || argc < 1) {  // there should only be 1, 2 or 3 arguments
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..3)", argc);
    }

    vKeys = vArgs[0];
    Check_Type(vKeys, T_ARRAY);
    as_policy_batch_init(&policy);

    if (argc == 3) {
        vBins = vArgs[1];
        Check_Type(vBins, T_ARRAY);
        bins_idx = RARRAY_LEN(vBins);

        if (TYPE(vArgs[2]) != T_NIL) {
            SET_BATCH_POLICY(policy, vArgs[2]);
        }
    } else {
        switch(TYPE(vArgs[1])) {
        case T_NIL:
            break;
        case T_ARRAY:
            vBins = vArgs[1];
            bins_idx = RARRAY_LEN(vBins);
            break;
        case T_HASH: {
            SET_POLICY(policy, vArgs[1]);
            break;
        }
        default:
            rb_raise(rb_eTypeError, "wrong argument type (expected Array or Hash)");
        }
    }

    idx = RARRAY_LEN(vKeys);
    as_batch_inita(&batch, idx);

    for(n = 0; n < idx; n++) {
        VALUE vKey = rb_ary_entry(vKeys, n);
        Data_Get_Struct(vKey, as_key, key);
        as_key_init_value(as_batch_keyat(&batch, n), key->ns, key->set, key->valuep);
    }

    vClient = rb_iv_get(vSelf, "@client");
    Data_Get_Struct(vClient, aerospike, ptr);

    vArray = rb_ary_new();
    if (bins_idx > 0) {
        char* sBins[bins_idx];
        for(n = 0; n < bins_idx; n++) {
            VALUE vEl = rb_ary_entry(vBins, n);
            GET_STRING(vEl);
            sBins[n] = StringValueCStr(vEl);
        }
        if (aerospike_batch_get_bins(ptr, &err, &policy, &batch, sBins, bins_idx, batch_read_callback, &vArray) != AEROSPIKE_OK) {
            as_batch_destroy(&batch);
            raise_aerospike_exception(err.code, err.message);
        }
    } else {
        if (aerospike_batch_get(ptr, &err, &policy, &batch, batch_read_callback, &vArray) != AEROSPIKE_OK) {
            as_batch_destroy(&batch);
            raise_aerospike_exception(err.code, err.message);
        }
    }

    as_batch_destroy(&batch);
    if ( rb_block_given_p() ) {
        return Qnil;
    }

    return vArray;
}