Package org.apache.cassandra.db.composites

Source Code of org.apache.cassandra.db.composites.AbstractComposite

/*
* 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.db.composites;

import java.nio.ByteBuffer;

import org.apache.cassandra.db.filter.ColumnSlice;
import org.apache.cassandra.db.marshal.AbstractCompositeType;

public abstract class AbstractComposite implements Composite
{
    public boolean isEmpty()
    {
        return size() == 0;
    }

    public EOC eoc()
    {
        return EOC.NONE;
    }

    public Composite start()
    {
        return withEOC(EOC.START);
    }

    public Composite end()
    {
        return withEOC(EOC.END);
    }

    public Composite withEOC(EOC newEoc)
    {
        // Note: CompositeBound overwrite this so we assume the EOC of this is NONE
        switch (newEoc)
        {
            case START:
                return BoundedComposite.startOf(this);
            case END:
                return BoundedComposite.endOf(this);
            default:
                return this;
        }
    }

    public ColumnSlice slice()
    {
        return new ColumnSlice(start(), end());
    }

    public ByteBuffer toByteBuffer()
    {
        // This is the legacy format of composites.
        // See org.apache.cassandra.db.marshal.CompositeType for details.
        ByteBuffer result = ByteBuffer.allocate(dataSize() + 3 * size());
        for (int i = 0; i < size(); i++)
        {
            ByteBuffer bb = get(i);
            AbstractCompositeType.putShortLength(result, bb.remaining());
            result.put(bb.duplicate());
            result.put((byte)0);
        }
        result.flip();
        return result;
    }

    public int dataSize()
    {
        int size = 0;
        for (int i = 0; i < size(); i++)
            size += get(i).remaining();
        return size;
    }

    public boolean isPrefixOf(Composite c)
    {
        if (size() > c.size())
            return false;

        for (int i = 0; i < size(); i++)
        {
            if (!get(i).equals(c.get(i)))
                return false;
        }
        return true;
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o)
            return true;

        if(!(o instanceof Composite))
            return false;

        Composite c = (Composite)o;
        if (size() != c.size())
            return false;

        for (int i = 0; i < size(); i++)
        {
            if (!get(i).equals(c.get(i)))
                return false;
        }
        return eoc() == c.eoc();
    }

    @Override
    public int hashCode()
    {
        int h = 31;
        for (int i = 0; i < size(); i++)
            h += get(i).hashCode();
        return h + eoc().hashCode();
    }
}
TOP

Related Classes of org.apache.cassandra.db.composites.AbstractComposite

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.