com.bigdata.btree.RangeCheckUtil Maven / Gradle / Ivy
/**
Copyright (C) SYSTAP, LLC DBA Blazegraph 2006-2016. All rights reserved.
Contact:
SYSTAP, LLC DBA Blazegraph
2501 Calvert ST NW #106
Washington, DC 20008
[email protected]
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Created on Jan 28, 2012
*/
package com.bigdata.btree;
import com.bigdata.mdi.ISeparatorKeys;
import com.bigdata.util.BytesUtil;
/**
* Utility class to verify that a key lies within a key range.
*
* Note: In order to be a useful check on the mapping of predicates across
* shards, the check needs to verify that the as-bound predicate either lies
* within or spans the shard, i.e., that the interaction of the as-bound
* predicate and the shard is not empty.
*
* @see
* KeyAfterPartitionException
*
* @author Bryan Thompson
* @version $Id$
*/
public class RangeCheckUtil {
/**
* Verify that the key lies within the key range of an index partition.
*
* @param key
* The key.
*
* @param allowUpperBound
* true
iff the key represents an inclusive
* upper bound and thus must be allowed to be LTE to the right
* separator key for the index partition. For example, this would
* be true
for the toKey parameter on
* rangeCount or rangeIterator methods.
*
* @return true
always.
*
* @throws IllegalArgumentException
* if the key is null
* @throws KeyOutOfRangeException
* if the key does not lie within the index partition.
*
* @see BytesUtil#rangeCheck(byte[], byte[], byte[])
*/
static public boolean rangeCheck(final ISeparatorKeys pmd,
final byte[] key, final boolean allowUpperBound) {
final byte[] leftSeparatorKey = pmd.getLeftSeparatorKey();
final byte[] rightSeparatorKey = pmd.getRightSeparatorKey();
if (BytesUtil.compareBytes(key, leftSeparatorKey) < 0) {
throw new KeyBeforePartitionException(key, allowUpperBound, pmd);
}
if (rightSeparatorKey != null) {
final int ret = BytesUtil.compareBytes(key, rightSeparatorKey);
if (allowUpperBound) {
if (ret <= 0) {
// key less than or equal to the exclusive upper bound.
} else {
throw new KeyAfterPartitionException(key, allowUpperBound,
pmd);
}
} else {
if (ret < 0) {
// key strictly less than the exclusive upper bound.
} else {
throw new KeyAfterPartitionException(key, allowUpperBound,
pmd);
}
}
}
// key lies within the index partition.
return true;
}
}