Example #1
0
/*
 * call-seq:
 *   new(namespace, set, value) -> AerospikeNative::Key
 *   new(namespace, set, value, digest) -> AerospikeNative::Key
 *
 * initialize new key
 */
VALUE key_initialize(int argc, VALUE* vArgs, VALUE vSelf)
{
    VALUE vNamespace, vSet, vValue, vDigest = Qnil;
    as_key *ptr;
    as_digest* digest = NULL;

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

    vNamespace = vArgs[0];
    Check_Type(vNamespace, T_STRING);

    vSet = vArgs[1];
    Check_Type(vSet, T_STRING);

    vValue = vArgs[2];

    if (argc == 4) {
        vDigest = vArgs[3];
    }

    Data_Get_Struct(vSelf, as_key, ptr);

    if(TYPE(vValue) != T_NIL) {
        switch(TYPE(vValue)) {
        case T_FIXNUM:
            as_key_init_int64(ptr, StringValueCStr( vNamespace ), StringValueCStr( vSet ), FIX2LONG( vValue ));
            break;
        case T_STRING:
            as_key_init_str(ptr, StringValueCStr( vNamespace ), StringValueCStr( vSet ), StringValueCStr( vValue ));
            break;
        default: {
            VALUE vBytes = rb_funcall(vValue, rb_intern("to_msgpack"), 0);
            as_key_init_raw(ptr, StringValueCStr( vNamespace ), StringValueCStr( vSet ), StringValuePtr(vBytes), RSTRING_LEN(vBytes));
        }
        }
    } else {
        Check_Type(vValue, T_NIL);
        Check_Type(vDigest, T_STRING);
        as_key_init_digest(ptr, StringValueCStr( vNamespace ), StringValueCStr( vSet ), StringValuePtr( vDigest ));
    }

    if (digest == NULL) {
        digest = as_key_digest(ptr);
    }

    rb_iv_set(vSelf, "@namespace", vNamespace);
    rb_iv_set(vSelf, "@set", vSet);
    rb_iv_set(vSelf, "@value", vValue);
    rb_iv_set(vSelf, "@digest", rb_str_new( digest->value, AS_DIGEST_VALUE_SIZE));

    return vSelf;
}
Example #2
0
void askey_from_clkey(as_key * key, const as_namespace ns, const as_set set, cl_object * clkey)
{
	if (! (key && clkey)) {
		return;
	}

	switch (clkey->type) {
		case CL_NULL:
			as_key_init_value(key, ns, set, NULL);
			break;
		case CL_INT:
			as_key_init_int64(key, ns, set, clkey->u.i64);
			break;
		case CL_STR: {
			// Must null-terminate here.
			char* s = (char*)malloc(clkey->sz + 1);
			memcpy(s, clkey->u.str, clkey->sz);
			s[clkey->sz] = 0;
			as_key_init_strp(key, ns, set, s, true);
			break;
		}
		case CL_BLOB:
		case CL_JAVA_BLOB:
		case CL_CSHARP_BLOB:
		case CL_PYTHON_BLOB:
		case CL_RUBY_BLOB:
		case CL_ERLANG_BLOB: {
			// obj value points into recv buf - don't free it.
			as_key_init_raw(key, ns, set, (const uint8_t*)clkey->u.blob, (uint32_t)clkey->sz);
			break;
		}
		// Unsupported as key types for now:
		case CL_LIST:
		case CL_MAP:
		default:
			as_key_init_value(key, ns, set, NULL);
			break;
	}
}