All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.cassandra.serializers.MapSerializer Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.cassandra.serializers;

import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.util.*;

import org.apache.cassandra.utils.ByteBufferUtil;
import org.apache.cassandra.utils.Pair;

public class MapSerializer extends CollectionSerializer>
{
    // interning instances
    private static final Map, TypeSerializer>, MapSerializer> instances = new HashMap, TypeSerializer>, MapSerializer>();

    public final TypeSerializer keys;
    public final TypeSerializer values;

    public static synchronized  MapSerializer getInstance(TypeSerializer keys, TypeSerializer values)
    {
        Pair, TypeSerializer> p = Pair., TypeSerializer>create(keys, values);
        MapSerializer t = instances.get(p);
        if (t == null)
        {
            t = new MapSerializer(keys, values);
            instances.put(p, t);
        }
        return t;
    }

    private MapSerializer(TypeSerializer keys, TypeSerializer values)
    {
        this.keys = keys;
        this.values = values;
    }

    public Map deserialize(ByteBuffer bytes)
    {
        try
        {
            ByteBuffer input = bytes.duplicate();
            int n = ByteBufferUtil.readShortLength(input);
            Map m = new LinkedHashMap(n);
            for (int i = 0; i < n; i++)
            {
                ByteBuffer kbb = ByteBufferUtil.readBytesWithShortLength(input);
                keys.validate(kbb);

                ByteBuffer vbb = ByteBufferUtil.readBytesWithShortLength(input);
                values.validate(vbb);

                m.put(keys.deserialize(kbb), values.deserialize(vbb));
            }
            return m;
        }
        catch (BufferUnderflowException e)
        {
            throw new MarshalException("Not enough bytes to read a map");
        }
    }

    /**
     * Layout is: {@code ... }
     * where:
     *   n is the number of elements
     *   sk_i is the number of bytes composing the ith key k_i
     *   k_i is the sk_i bytes composing the ith key
     *   sv_i is the number of bytes composing the ith value v_i
     *   v_i is the sv_i bytes composing the ith value
     */
    public ByteBuffer serialize(Map value)
    {
        List bbs = new ArrayList(2 * value.size());
        int size = 0;
        for (Map.Entry entry : value.entrySet())
        {
            ByteBuffer bbk = keys.serialize(entry.getKey());
            ByteBuffer bbv = values.serialize(entry.getValue());
            bbs.add(bbk);
            bbs.add(bbv);
            size += 4 + bbk.remaining() + bbv.remaining();
        }
        return pack(bbs, value.size(), size);
    }

    public String toString(Map value)
    {
        StringBuilder sb = new StringBuilder();
        boolean isFirst = true;
        for (Map.Entry element : value.entrySet())
        {
            if (isFirst)
            {
                isFirst = false;
            }
            else
            {
                sb.append("; ");
            }
            sb.append('(');
            sb.append(keys.toString(element.getKey()));
            sb.append(", ");
            sb.append(values.toString(element.getValue()));
            sb.append(')');
        }
        return sb.toString();
    }

    public Class> getType()
    {
        return (Class)Map.class;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy