Skip to content
This repository was archived by the owner on Feb 22, 2019. It is now read-only.

Support for reading collections in results is missing #96

Closed
micahnz opened this issue Mar 19, 2013 · 10 comments
Closed

Support for reading collections in results is missing #96

micahnz opened this issue Mar 19, 2013 · 10 comments

Comments

@micahnz
Copy link

micahnz commented Mar 19, 2013

I notice there is some code in there for ListType, SetType and MapType but no actual code to de-serialize them.

This works

function collectionDeserializer(deserializers) {
    return function(str) {
        if(str === null || str === undefined) {
            return null;
        }

        var buf = new Buffer(str, 'binary'),
            pos = 3, len, keys = [], vals = [], i = 0, result;

        while(pos < buf.length){
            if(i > deserializers.length - 1) {
                i = 0;
            }

            len = buf.readUInt16BE(pos);
            pos += 2;

            if(i == 0 && deserializers.length == 2) {
                keys.push(deserializers[i](buf.slice(pos, len + pos)));
            }
            else if(i == 0 && deserializers.length == 1 || i == 1 && deserializers.length == 2) {
                vals.push(deserializers[i](buf.slice(pos, len + pos)));
            }

            i += 1;

            pos += len;
        }

        if(keys.length === vals.length) {
            result = {}
            for(var i = 0, len = keys.length; i < len; i++) {
                result[keys[i]] = vals[i];
            }
        }
        else {
            result = vals;
        }

        return result;
    };
}
@devdazed
Copy link
Contributor

Interesting, I was under the impression that those were only available via the Binary protocol. If they work via the thrift protocol that would be great. Can you turn this into a pull request with some tests maybe?

@devdazed
Copy link
Contributor

Also, it seems that this will only de-serialize the objects. What about serialization? How do we differentiate between a collection and a CompositeType when entering data into the column?

@micahnz
Copy link
Author

micahnz commented Mar 27, 2013

Yes the only problem I face at this time was to get stuff out since inserting/updating seems to work fine at the moment if used like this..

INSERT INTO users (id, permissions) VALUES (e988aea0-9682-11e2-9e96-0800200c9a66, {'a': ?, 'b': ?});
UPDATE users SET permissions = permissions + {'b': ?} WHERE id = e988aea0-9682-11e2-9e96-0800200c9a66;

Not 100% sure how everything works yet and have a deadline so just need something that works for now.

@chris-rock
Copy link

Inserting was already done with my implementation, only deserialization was missing.

@industral
Copy link

@michaelmitchell can you please create pull request for that?

@srlm-io
Copy link

srlm-io commented Sep 24, 2013

@industral Does your 7b26591 fix actually work? For me it seems that it returns the function instead of the data.

@industral
Copy link

@srlmproductions you right, it was bad commit.

@srlm-io
Copy link

srlm-io commented Sep 25, 2013

It looks like it would be possible to have some sort of automatic algorithm, at least based on the output of row._schema:

row._schema:  { name_types: { data: 'UTF8Type' },
  value_types: { data: 'org.apache.cassandra.db.marshal.ListType(org.apache.cassandra.db.marshal.FloatType)' },

Here's my attempt at making a simple list(float) parser. You need to pass it the binary data that the helenus protocol returns:

DecodeRawFloatData = function(buffer){
    var offset = 0;
    var num_elements = buffer.readUInt16BE(offset);
    offset += 2;

    result = [];
    for(var i = 0; i < num_elements; i++){
        var element_size = buffer.readUInt16BE(offset);
        offset += 2;

        result.push(buffer.readFloatBE(offset));
        offset += 4;
    }
    return result;
};

@devdazed
Copy link
Contributor

Wouldn't that only work for types that have a predictable length? Would this work for say, DECIMAL which is an arbitrary precision integer, or a BLOB ?

@zmarcantel
Copy link

I'm still getting the issue @srlmproductions ran into with the deserializer simply returning the function rather than the value.

When inspecting the entire column, the value key maps to the exact function at the start of this issue.

Has the commit stated as bad not made it into npm registry?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants