org.apache.cassandra.utils.btree.BTreeSet 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.btree;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.NavigableSet;
import java.util.SortedSet;
public class BTreeSet implements NavigableSet
{
protected final Comparator comparator;
protected final Object[] tree;
public BTreeSet(Object[] tree, Comparator comparator)
{
this.tree = tree;
this.comparator = comparator;
}
public BTreeSet update(Collection updateWith, boolean isSorted)
{
return new BTreeSet<>(BTree.update(tree, comparator, updateWith, isSorted, UpdateFunction.NoOp.instance()), comparator);
}
@Override
public Comparator super V> comparator()
{
return comparator;
}
protected Cursor slice(boolean forwards, boolean permitInversion)
{
return BTree.slice(tree, forwards);
}
@Override
public int size()
{
return slice(true, false).count();
}
@Override
public boolean isEmpty()
{
return slice(true, false).hasNext();
}
@Override
public Iterator iterator()
{
return slice(true, true);
}
@Override
public Iterator descendingIterator()
{
return slice(false, true);
}
@Override
public Object[] toArray()
{
return toArray(new Object[0]);
}
@Override
public T[] toArray(T[] a)
{
int size = size();
if (a.length < size)
a = Arrays.copyOf(a, size);
int i = 0;
for (V v : this)
a[i++] = (T) v;
return a;
}
@Override
public NavigableSet subSet(V fromElement, boolean fromInclusive, V toElement, boolean toInclusive)
{
return new BTreeRange<>(tree, comparator, fromElement, fromInclusive, toElement, toInclusive);
}
@Override
public NavigableSet headSet(V toElement, boolean inclusive)
{
return new BTreeRange<>(tree, comparator, null, true, toElement, inclusive);
}
@Override
public NavigableSet tailSet(V fromElement, boolean inclusive)
{
return new BTreeRange<>(tree, comparator, fromElement, inclusive, null, true);
}
@Override
public SortedSet subSet(V fromElement, V toElement)
{
return subSet(fromElement, true, toElement, false);
}
@Override
public SortedSet headSet(V toElement)
{
return headSet(toElement, false);
}
@Override
public SortedSet tailSet(V fromElement)
{
return tailSet(fromElement, true);
}
@Override
public V first()
{
throw new UnsupportedOperationException();
}
@Override
public V last()
{
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(Collection extends V> c)
{
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection> c)
{
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(Collection> c)
{
throw new UnsupportedOperationException();
}
@Override
public void clear()
{
throw new UnsupportedOperationException();
}
@Override
public V pollFirst()
{
throw new UnsupportedOperationException();
}
@Override
public V pollLast()
{
throw new UnsupportedOperationException();
}
@Override
public boolean add(V v)
{
throw new UnsupportedOperationException();
}
@Override
public boolean remove(Object o)
{
throw new UnsupportedOperationException();
}
@Override
public V lower(V v)
{
throw new UnsupportedOperationException();
}
@Override
public V floor(V v)
{
throw new UnsupportedOperationException();
}
@Override
public V ceiling(V v)
{
throw new UnsupportedOperationException();
}
@Override
public V higher(V v)
{
throw new UnsupportedOperationException();
}
@Override
public boolean contains(Object o)
{
throw new UnsupportedOperationException();
}
@Override
public boolean containsAll(Collection> c)
{
throw new UnsupportedOperationException();
}
@Override
public NavigableSet descendingSet()
{
return new BTreeRange<>(this.tree, this.comparator).descendingSet();
}
public static class BTreeRange extends BTreeSet implements NavigableSet
{
protected final V lowerBound, upperBound;
protected final boolean inclusiveLowerBound, inclusiveUpperBound;
BTreeRange(Object[] tree, Comparator comparator)
{
this(tree, comparator, null, true, null, true);
}
BTreeRange(BTreeRange from)
{
this(from.tree, from.comparator, from.lowerBound, from.inclusiveLowerBound, from.upperBound, from.inclusiveUpperBound);
}
BTreeRange(Object[] tree, Comparator comparator, V lowerBound, boolean inclusiveLowerBound, V upperBound, boolean inclusiveUpperBound)
{
super(tree, comparator);
this.lowerBound = lowerBound;
this.upperBound = upperBound;
this.inclusiveLowerBound = inclusiveLowerBound;
this.inclusiveUpperBound = inclusiveUpperBound;
}
// narrowing range constructor - makes this the intersection of the two ranges over the same tree b
BTreeRange(BTreeRange a, BTreeRange b)
{
super(a.tree, a.comparator);
assert a.tree == b.tree;
final BTreeRange lb, ub;
if (a.lowerBound == null)
{
lb = b;
}
else if (b.lowerBound == null)
{
lb = a;
}
else
{
int c = comparator.compare(a.lowerBound, b.lowerBound);
if (c < 0)
lb = b;
else if (c > 0)
lb = a;
else if (!a.inclusiveLowerBound)
lb = a;
else
lb = b;
}
if (a.upperBound == null)
{
ub = b;
}
else if (b.upperBound == null)
{
ub = a;
}
else
{
int c = comparator.compare(b.upperBound, a.upperBound);
if (c < 0)
ub = b;
else if (c > 0)
ub = a;
else if (!a.inclusiveUpperBound)
ub = a;
else
ub = b;
}
lowerBound = lb.lowerBound;
inclusiveLowerBound = lb.inclusiveLowerBound;
upperBound = ub.upperBound;
inclusiveUpperBound = ub.inclusiveUpperBound;
}
@Override
protected Cursor slice(boolean forwards, boolean permitInversion)
{
return BTree.slice(tree, comparator, lowerBound, inclusiveLowerBound, upperBound, inclusiveUpperBound, forwards);
}
@Override
public NavigableSet subSet(V fromElement, boolean fromInclusive, V toElement, boolean toInclusive)
{
return new BTreeRange<>(this, new BTreeRange<>(tree, comparator, fromElement, fromInclusive, toElement, toInclusive));
}
@Override
public NavigableSet headSet(V toElement, boolean inclusive)
{
return new BTreeRange<>(this, new BTreeRange<>(tree, comparator, lowerBound, true, toElement, inclusive));
}
@Override
public NavigableSet tailSet(V fromElement, boolean inclusive)
{
return new BTreeRange<>(this, new BTreeRange<>(tree, comparator, fromElement, inclusive, null, true));
}
@Override
public NavigableSet descendingSet()
{
return new BTreeDescRange<>(this);
}
}
public static class BTreeDescRange extends BTreeRange
{
BTreeDescRange(BTreeRange from)
{
super(from.tree, from.comparator, from.lowerBound, from.inclusiveLowerBound, from.upperBound, from.inclusiveUpperBound);
}
@Override
protected Cursor slice(boolean forwards, boolean permitInversion)
{
return super.slice(permitInversion ? !forwards : forwards, false);
}
@Override
public NavigableSet subSet(V fromElement, boolean fromInclusive, V toElement, boolean toInclusive)
{
return super.subSet(toElement, toInclusive, fromElement, fromInclusive).descendingSet();
}
@Override
public NavigableSet headSet(V toElement, boolean inclusive)
{
return super.tailSet(toElement, inclusive).descendingSet();
}
@Override
public NavigableSet tailSet(V fromElement, boolean inclusive)
{
return super.headSet(fromElement, inclusive).descendingSet();
}
@Override
public NavigableSet descendingSet()
{
return new BTreeRange<>(this);
}
}
}