Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.lang.reflect.Field;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.security.AccessController;
import java.security.PrivilegedAction;
import com.google.common.primitives.*;
import net.nicoulaj.compilecommand.annotations.Inline;
import sun.misc.Unsafe;
/**
* Utility code to do optimized byte-array comparison.
* This is borrowed and slightly modified from Guava's {@link UnsignedBytes}
* class to be able to compare arrays that start at non-zero offsets.
*/
public class FastByteOperations
{
/**
* Lexicographically compare two byte arrays.
*/
public static int compareUnsigned(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2)
{
return BestHolder.BEST.compare(b1, s1, l1, b2, s2, l2);
}
public static int compareUnsigned(ByteBuffer b1, byte[] b2, int s2, int l2)
{
return BestHolder.BEST.compare(b1, b2, s2, l2);
}
public static int compareUnsigned(byte[] b1, int s1, int l1, ByteBuffer b2)
{
return -BestHolder.BEST.compare(b2, b1, s1, l1);
}
public static int compareUnsigned(ByteBuffer b1, ByteBuffer b2)
{
return BestHolder.BEST.compare(b1, b2);
}
public static void copy(ByteBuffer src, int srcPosition, byte[] trg, int trgPosition, int length)
{
BestHolder.BEST.copy(src, srcPosition, trg, trgPosition, length);
}
public static void copy(ByteBuffer src, int srcPosition, ByteBuffer trg, int trgPosition, int length)
{
BestHolder.BEST.copy(src, srcPosition, trg, trgPosition, length);
}
public interface ByteOperations
{
abstract public int compare(byte[] buffer1, int offset1, int length1,
byte[] buffer2, int offset2, int length2);
abstract public int compare(ByteBuffer buffer1, byte[] buffer2, int offset2, int length2);
abstract public int compare(ByteBuffer buffer1, ByteBuffer buffer2);
abstract public void copy(ByteBuffer src, int srcPosition, byte[] trg, int trgPosition, int length);
abstract public void copy(ByteBuffer src, int srcPosition, ByteBuffer trg, int trgPosition, int length);
}
/**
* Provides a lexicographical comparer implementation; either a Java
* implementation or a faster implementation based on {@link Unsafe}.
*
*
Uses reflection to gracefully fall back to the Java implementation if
* {@code Unsafe} isn't available.
*/
private static class BestHolder
{
static final String UNSAFE_COMPARER_NAME = FastByteOperations.class.getName() + "$UnsafeOperations";
static final ByteOperations BEST = getBest();
/**
* Returns the Unsafe-using Comparer, or falls back to the pure-Java
* implementation if unable to do so.
*/
static ByteOperations getBest()
{
if (!Architecture.IS_UNALIGNED)
return new PureJavaOperations();
try
{
Class> theClass = Class.forName(UNSAFE_COMPARER_NAME);
// yes, UnsafeComparer does implement Comparer
@SuppressWarnings("unchecked")
ByteOperations comparer = (ByteOperations) theClass.getConstructor().newInstance();
return comparer;
}
catch (Throwable t)
{
JVMStabilityInspector.inspectThrowable(t);
// ensure we really catch *everything*
return new PureJavaOperations();
}
}
}
@SuppressWarnings("unused") // used via reflection
public static final class UnsafeOperations implements ByteOperations
{
static final Unsafe theUnsafe;
/**
* The offset to the first element in a byte array.
*/
static final long BYTE_ARRAY_BASE_OFFSET;
static final long DIRECT_BUFFER_ADDRESS_OFFSET;
static
{
theUnsafe = (Unsafe) AccessController.doPrivileged(
new PrivilegedAction