org.apache.cassandra.utils.CollectionSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cassandra-all Show documentation
Show all versions of cassandra-all Show documentation
The Apache Cassandra Project develops a highly scalable second-generation distributed database, bringing together Dynamo's fully distributed design and Bigtable's ColumnFamily-based data model.
/*
* 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.utils;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.RandomAccess;
import java.util.Set;
import java.util.function.IntFunction;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.cassandra.db.TypeSizes;
import org.apache.cassandra.io.IVersionedSerializer;
import org.apache.cassandra.io.util.DataInputPlus;
import org.apache.cassandra.io.util.DataOutputPlus;
public class CollectionSerializer
{
public static void serializeCollection(IVersionedSerializer valueSerializer, Collection values, DataOutputPlus out, int version) throws IOException
{
out.writeUnsignedVInt(values.size());
for (V value : values)
valueSerializer.serialize(value, out, version);
}
public static & RandomAccess> void serializeList(IVersionedSerializer valueSerializer, L values, DataOutputPlus out, int version) throws IOException
{
int size = values.size();
out.writeUnsignedVInt(size);
for (int i = 0 ; i < size ; ++i)
valueSerializer.serialize(values.get(i), out, version);
}
public static void serializeMap(IVersionedSerializer keySerializer, IVersionedSerializer valueSerializer, Map map, DataOutputPlus out, int version) throws IOException
{
out.writeUnsignedVInt(map.size());
for (Map.Entry e : map.entrySet())
{
keySerializer.serialize(e.getKey(), out, version);
valueSerializer.serialize(e.getValue(), out, version);
}
}
public static > C deserializeCollection(IVersionedSerializer serializer, IntFunction factory, DataInputPlus in, int version) throws IOException
{
int size = (int) in.readUnsignedVInt();
C result = factory.apply(size);
while (size-- > 0)
result.add(serializer.deserialize(in, version));
return result;
}
public static > M deserializeMap(IVersionedSerializer keySerializer, IVersionedSerializer valueSerializer, IntFunction factory, DataInputPlus in, int version) throws IOException
{
int size = (int) in.readUnsignedVInt();
M result = factory.apply(size);
while (size-- > 0)
{
K key = keySerializer.deserialize(in, version);
V value = valueSerializer.deserialize(in, version);
result.put(key, value);
}
return result;
}
public static long serializedSizeCollection(IVersionedSerializer valueSerializer, Collection values, int version)
{
long size = TypeSizes.sizeofUnsignedVInt(values.size());
for (V value : values)
size += valueSerializer.serializedSize(value, version);
return size;
}
public static & RandomAccess> long serializedSizeList(IVersionedSerializer valueSerializer, L values, int version) throws IOException
{
int items = values.size();
long size = TypeSizes.sizeofUnsignedVInt(items);
for (int i = 0 ; i < items ; ++i)
size += valueSerializer.serializedSize(values.get(i), version);
return size;
}
public static long serializedSizeMap(IVersionedSerializer keySerializer, IVersionedSerializer valueSerializer, Map map, int version)
{
long size = TypeSizes.sizeofUnsignedVInt(map.size());
for (Map.Entry e : map.entrySet())
size += keySerializer.serializedSize(e.getKey(), version)
+ valueSerializer.serializedSize(e.getValue(), version);
return size;
}
public static IntFunction> newHashSet()
{
return i -> i == 0 ? Collections.emptySet() : Sets.newHashSetWithExpectedSize(i);
}
public static IntFunction