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

org.apache.cassandra.db.IndexExpression Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 5.0.0
Show newest version
/*
 * 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;

import java.io.DataInput;
import java.io.IOException;
import java.nio.ByteBuffer;

import com.google.common.base.Objects;

import org.apache.cassandra.cql3.Operator;
import org.apache.cassandra.io.util.DataOutputPlus;
import org.apache.cassandra.utils.ByteBufferUtil;

public final class IndexExpression
{
    public final ByteBuffer column;
    public final Operator operator;
    public final ByteBuffer value;

    public IndexExpression(ByteBuffer column, Operator operator, ByteBuffer value)
    {
        this.column = column;
        this.operator = operator;
        this.value = value;
    }

    /**
     * Checks if the operator of this IndexExpression is a CONTAINS operator.
     *
     * @return true if the operator of this IndexExpression is a CONTAINS
     * operator, false otherwise.
     */
    public boolean isContains()
    {
        return Operator.CONTAINS == operator;
    }

    /**
     * Checks if the operator of this IndexExpression is a CONTAINS_KEY operator.
     *
     * @return true if the operator of this IndexExpression is a CONTAINS_KEY
     * operator, false otherwise.
     */
    public boolean isContainsKey()
    {
        return Operator.CONTAINS_KEY == operator;
    }

    @Override
    public String toString()
    {
        return String.format("%s %s %s", ByteBufferUtil.bytesToHex(column), operator, ByteBufferUtil.bytesToHex(value));
    }

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

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

        IndexExpression ie = (IndexExpression) o;

        return Objects.equal(this.column, ie.column)
            && Objects.equal(this.operator, ie.operator)
            && Objects.equal(this.value, ie.value);
    }

    @Override
    public int hashCode()
    {
        return Objects.hashCode(column, operator, value);
    }

    /**
     * Write the serialized version of this IndexExpression to the specified output.
     *
     * @param output the output to write to
     * @throws IOException if an I/O problem occurs while writing to the specified output
     */
    public void writeTo(DataOutputPlus output) throws IOException
    {
        ByteBufferUtil.writeWithShortLength(column, output);
        operator.writeTo(output);
        ByteBufferUtil.writeWithShortLength(value, output);
    }

    /**
     * Deserializes an IndexExpression instance from the specified input. 
     *
     * @param input the input to read from 
     * @return the IndexExpression instance deserialized
     * @throws IOException if a problem occurs while deserializing the IndexExpression instance.
     */
    public static IndexExpression readFrom(DataInput input) throws IOException
    {
        return new IndexExpression(ByteBufferUtil.readWithShortLength(input),
                                   Operator.readFrom(input),
                                   ByteBufferUtil.readWithShortLength(input));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy