Internal: remove unused code.
This commit is contained in:
parent
9d5e789508
commit
098c01d86c
|
@ -33,7 +33,6 @@ import org.elasticsearch.cluster.routing.allocation.StartedRerouteAllocation;
|
|||
import org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders;
|
||||
import org.elasticsearch.cluster.routing.allocation.decider.Decision;
|
||||
import org.elasticsearch.cluster.routing.allocation.decider.Decision.Type;
|
||||
import org.elasticsearch.common.collect.IdentityHashSet;
|
||||
import org.elasticsearch.common.component.AbstractComponent;
|
||||
import org.elasticsearch.common.inject.Inject;
|
||||
import org.elasticsearch.common.logging.ESLogger;
|
||||
|
@ -597,7 +596,7 @@ public class BalancedShardsAllocator extends AbstractComponent implements Shards
|
|||
int secondaryLength = 0;
|
||||
int primaryLength = primary.length;
|
||||
ArrayUtil.timSort(primary, comparator);
|
||||
final Set<ModelNode> throttledNodes = new IdentityHashSet<>();
|
||||
final Set<ModelNode> throttledNodes = Collections.newSetFromMap(new IdentityHashMap<ModelNode, Boolean>());
|
||||
do {
|
||||
for (int i = 0; i < primaryLength; i++) {
|
||||
MutableShardRouting shard = primary[i];
|
||||
|
|
|
@ -1,63 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch 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.elasticsearch.common.collect;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.TreeSet;
|
||||
|
||||
/**
|
||||
* A {@link TreeSet} that is bounded by size.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class BoundedTreeSet<E> extends TreeSet<E> {
|
||||
|
||||
private final int size;
|
||||
|
||||
public BoundedTreeSet(int size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public BoundedTreeSet(Comparator<? super E> comparator, int size) {
|
||||
super(comparator);
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(E e) {
|
||||
boolean result = super.add(e);
|
||||
rebound();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(Collection<? extends E> c) {
|
||||
boolean result = super.addAll(c);
|
||||
rebound();
|
||||
return result;
|
||||
}
|
||||
|
||||
private void rebound() {
|
||||
while (size() > size) {
|
||||
remove(last());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,201 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch 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.elasticsearch.common.collect;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class IdentityHashSet<E>
|
||||
extends AbstractSet<E>
|
||||
implements Set<E>, Cloneable, java.io.Serializable {
|
||||
|
||||
static final long serialVersionUID = -5024744406713321677L;
|
||||
|
||||
private transient IdentityHashMap<E, Object> map;
|
||||
|
||||
// Dummy value to associate with an Object in the backing Map
|
||||
private static final Object PRESENT = new Object();
|
||||
|
||||
public IdentityHashSet() {
|
||||
map = new IdentityHashMap<>();
|
||||
}
|
||||
|
||||
public IdentityHashSet(Collection<? extends E> c) {
|
||||
map = new IdentityHashMap<>(Math.max((int) (c.size() / .75f) + 1, 16));
|
||||
addAll(c);
|
||||
}
|
||||
|
||||
public IdentityHashSet(int expectedSize) {
|
||||
map = new IdentityHashMap<>(expectedSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an iterator over the elements in this set. The elements
|
||||
* are returned in no particular order.
|
||||
*
|
||||
* @return an Iterator over the elements in this set
|
||||
* @see ConcurrentModificationException
|
||||
*/
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
return map.keySet().iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of elements in this set (its cardinality).
|
||||
*
|
||||
* @return the number of elements in this set (its cardinality)
|
||||
*/
|
||||
@Override
|
||||
public int size() {
|
||||
return map.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <tt>true</tt> if this set contains no elements.
|
||||
*
|
||||
* @return <tt>true</tt> if this set contains no elements
|
||||
*/
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return map.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <tt>true</tt> if this set contains the specified element.
|
||||
* More formally, returns <tt>true</tt> if and only if this set
|
||||
* contains an element <tt>e</tt> such that
|
||||
* <tt>(o==e)</tt>.
|
||||
*
|
||||
* @param o element whose presence in this set is to be tested
|
||||
* @return <tt>true</tt> if this set contains the specified element
|
||||
*/
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
return map.containsKey(o);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the specified element to this set if it is not already present.
|
||||
* More formally, adds the specified element <tt>e</tt> to this set if
|
||||
* this set contains no element <tt>e2</tt> such that
|
||||
* <tt>(e==e2)</tt>.
|
||||
* If this set already contains the element, the call leaves the set
|
||||
* unchanged and returns <tt>false</tt>.
|
||||
*
|
||||
* @param e element to be added to this set
|
||||
* @return <tt>true</tt> if this set did not already contain the specified
|
||||
* element
|
||||
*/
|
||||
@Override
|
||||
public boolean add(E e) {
|
||||
return map.put(e, PRESENT) == null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the specified element from this set if it is present.
|
||||
* More formally, removes an element <tt>e</tt> such that
|
||||
* <tt>(o==e)</tt>,
|
||||
* if this set contains such an element. Returns <tt>true</tt> if
|
||||
* this set contained the element (or equivalently, if this set
|
||||
* changed as a result of the call). (This set will not contain the
|
||||
* element once the call returns.)
|
||||
*
|
||||
* @param o object to be removed from this set, if present
|
||||
* @return <tt>true</tt> if the set contained the specified element
|
||||
*/
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
return map.remove(o) == PRESENT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all of the elements from this set.
|
||||
* The set will be empty after this call returns.
|
||||
*/
|
||||
@Override
|
||||
public void clear() {
|
||||
map.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a shallow copy of this <tt>HashSet</tt> instance: the elements
|
||||
* themselves are not cloned.
|
||||
*
|
||||
* @return a shallow copy of this set
|
||||
*/
|
||||
@Override
|
||||
public Object clone() {
|
||||
try {
|
||||
IdentityHashSet<E> newSet = (IdentityHashSet<E>) super.clone();
|
||||
newSet.map = (IdentityHashMap<E, Object>) map.clone();
|
||||
return newSet;
|
||||
} catch (CloneNotSupportedException e) {
|
||||
throw new InternalError();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Index the state of this <tt>HashSet</tt> instance to a stream (that is,
|
||||
* serialize it).
|
||||
*
|
||||
* @serialData The capacity of the backing <tt>HashMap</tt> instance
|
||||
* (int), and its load factor (float) are emitted, followed by
|
||||
* the size of the set (the number of elements it contains)
|
||||
* (int), followed by all of its elements (each an Object) in
|
||||
* no particular order.
|
||||
*/
|
||||
private void writeObject(java.io.ObjectOutputStream s)
|
||||
throws java.io.IOException {
|
||||
// Write out any hidden serialization magic
|
||||
s.defaultWriteObject();
|
||||
|
||||
// Write out size
|
||||
s.writeInt(map.size());
|
||||
|
||||
// Write out all elements in the proper order.
|
||||
for (Iterator i = map.keySet().iterator(); i.hasNext(); )
|
||||
s.writeObject(i.next());
|
||||
}
|
||||
|
||||
/**
|
||||
* Reconstitute the <tt>HashSet</tt> instance from a stream (that is,
|
||||
* deserialize it).
|
||||
*/
|
||||
private void readObject(java.io.ObjectInputStream s)
|
||||
throws java.io.IOException, ClassNotFoundException {
|
||||
// Read in any hidden serialization magic
|
||||
s.defaultReadObject();
|
||||
|
||||
// Read in size
|
||||
int size = s.readInt();
|
||||
|
||||
map = new IdentityHashMap<>(size);
|
||||
|
||||
// Read in all elements in the proper order.
|
||||
for (int i = 0; i < size; i++) {
|
||||
E e = (E) s.readObject();
|
||||
map.put(e, PRESENT);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,376 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch 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.elasticsearch.common.collect;
|
||||
|
||||
import com.carrotsearch.hppc.*;
|
||||
import com.carrotsearch.hppc.cursors.LongCursor;
|
||||
import com.carrotsearch.hppc.cursors.LongObjectCursor;
|
||||
import com.carrotsearch.hppc.cursors.ObjectCursor;
|
||||
import com.carrotsearch.hppc.predicates.IntObjectPredicate;
|
||||
import com.carrotsearch.hppc.predicates.LongObjectPredicate;
|
||||
import com.carrotsearch.hppc.predicates.LongPredicate;
|
||||
import com.carrotsearch.hppc.procedures.LongObjectProcedure;
|
||||
import com.google.common.collect.UnmodifiableIterator;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* An immutable map implementation based on open hash map.
|
||||
* <p/>
|
||||
* Can be constructed using a {@link #builder()}, or using {@link #builder(org.elasticsearch.common.collect.ImmutableOpenLongMap)} (which is an optimized
|
||||
* option to copy over existing content and modify it).
|
||||
*/
|
||||
public final class ImmutableOpenLongMap<VType> implements Iterable<LongObjectCursor<VType>> {
|
||||
|
||||
private final LongObjectHashMap<VType> map;
|
||||
|
||||
private ImmutableOpenLongMap(LongObjectHashMap<VType> map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Returns the value associated with the given key or the default value
|
||||
* for the key type, if the key is not associated with any value.
|
||||
* <p/>
|
||||
* <b>Important note:</b> For primitive type values, the value returned for a non-existing
|
||||
* key may not be the default value of the primitive type (it may be any value previously
|
||||
* assigned to that slot).
|
||||
*/
|
||||
public VType get(long key) {
|
||||
return map.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <code>true</code> if this container has an association to a value for
|
||||
* the given key.
|
||||
*/
|
||||
public boolean containsKey(long key) {
|
||||
return map.containsKey(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Returns the current size (number of assigned keys) in the container.
|
||||
*/
|
||||
public int size() {
|
||||
return map.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Return <code>true</code> if this hash map contains no assigned keys.
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return map.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a cursor over the entries (key-value pairs) in this map. The iterator is
|
||||
* implemented as a cursor and it returns <b>the same cursor instance</b> on every
|
||||
* call to {@link java.util.Iterator#next()}. To read the current key and value use the cursor's
|
||||
* public fields. An example is shown below.
|
||||
* <pre>
|
||||
* for (IntShortCursor c : intShortMap)
|
||||
* {
|
||||
* System.out.println("index=" + c.index
|
||||
* + " key=" + c.key
|
||||
* + " value=" + c.value);
|
||||
* }
|
||||
* </pre>
|
||||
* <p/>
|
||||
* <p>The <code>index</code> field inside the cursor gives the internal index inside
|
||||
* the container's implementation. The interpretation of this index depends on
|
||||
* to the container.
|
||||
*/
|
||||
@Override
|
||||
public Iterator<LongObjectCursor<VType>> iterator() {
|
||||
return map.iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a specialized view of the keys of this associated container.
|
||||
* The view additionally implements {@link com.carrotsearch.hppc.ObjectLookupContainer}.
|
||||
*/
|
||||
public LongLookupContainer keys() {
|
||||
return map.keys();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a direct iterator over the keys.
|
||||
*/
|
||||
public UnmodifiableIterator<Long> keysIt() {
|
||||
final Iterator<LongCursor> iterator = map.keys().iterator();
|
||||
return new UnmodifiableIterator<Long>() {
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return iterator.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long next() {
|
||||
return iterator.next().value;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Returns a container with all values stored in this map.
|
||||
*/
|
||||
public ObjectContainer<VType> values() {
|
||||
return map.values();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a direct iterator over the keys.
|
||||
*/
|
||||
public UnmodifiableIterator<VType> valuesIt() {
|
||||
final Iterator<ObjectCursor<VType>> iterator = map.values().iterator();
|
||||
return new UnmodifiableIterator<VType>() {
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return iterator.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public VType next() {
|
||||
return iterator.next().value;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return map.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
ImmutableOpenLongMap that = (ImmutableOpenLongMap) o;
|
||||
|
||||
if (!map.equals(that.map)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return map.hashCode();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static final ImmutableOpenLongMap EMPTY = new ImmutableOpenLongMap(new LongObjectHashMap());
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <VType> ImmutableOpenLongMap<VType> of() {
|
||||
return EMPTY;
|
||||
}
|
||||
|
||||
public static <VType> Builder<VType> builder() {
|
||||
return new Builder<>();
|
||||
}
|
||||
|
||||
public static <VType> Builder<VType> builder(int size) {
|
||||
return new Builder<>(size);
|
||||
}
|
||||
|
||||
public static <VType> Builder<VType> builder(ImmutableOpenLongMap<VType> map) {
|
||||
return new Builder<>(map);
|
||||
}
|
||||
|
||||
public static class Builder<VType> implements LongObjectMap<VType> {
|
||||
|
||||
private LongObjectHashMap<VType> map;
|
||||
|
||||
public Builder() {
|
||||
//noinspection unchecked
|
||||
this(EMPTY);
|
||||
}
|
||||
|
||||
public Builder(int size) {
|
||||
this.map = new LongObjectHashMap<>(size);
|
||||
}
|
||||
|
||||
public Builder(ImmutableOpenLongMap<VType> map) {
|
||||
this.map = map.map.clone();
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a new instance of the
|
||||
*/
|
||||
public ImmutableOpenLongMap<VType> build() {
|
||||
LongObjectHashMap<VType> map = this.map;
|
||||
this.map = null; // nullify the map, so any operation post build will fail! (hackish, but safest)
|
||||
return new ImmutableOpenLongMap<>(map);
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts all the entries in the map to the builder.
|
||||
*/
|
||||
public Builder<VType> putAll(Map<Long, VType> map) {
|
||||
for (Map.Entry<Long, VType> entry : map.entrySet()) {
|
||||
this.map.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* A put operation that can be used in the fluent pattern.
|
||||
*/
|
||||
public Builder<VType> fPut(long key, VType value) {
|
||||
map.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public VType put(long key, VType value) {
|
||||
return map.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public VType get(long key) {
|
||||
return map.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public VType getOrDefault(long kType, VType vType) {
|
||||
return map.getOrDefault(kType, vType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove that can be used in the fluent pattern.
|
||||
*/
|
||||
public Builder<VType> fRemove(long key) {
|
||||
map.remove(key);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public VType remove(long key) {
|
||||
return map.remove(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<LongObjectCursor<VType>> iterator() {
|
||||
return map.iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsKey(long key) {
|
||||
return map.containsKey(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return map.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return map.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
map.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int putAll(LongObjectAssociativeContainer<? extends VType> container) {
|
||||
return map.putAll(container);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int putAll(Iterable<? extends LongObjectCursor<? extends VType>> iterable) {
|
||||
return map.putAll(iterable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int removeAll(LongContainer container) {
|
||||
return map.removeAll(container);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int removeAll(LongPredicate predicate) {
|
||||
return map.removeAll(predicate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LongCollection keys() {
|
||||
return map.keys();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ObjectContainer<VType> values() {
|
||||
return map.values();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends LongObjectProcedure<? super VType>> T forEach(T procedure) {
|
||||
return map.forEach(procedure);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int indexOf(long key) {
|
||||
return map.indexOf(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean indexExists(int index) {
|
||||
return map.indexExists(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public VType indexGet(int index) {
|
||||
return map.indexGet(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public VType indexReplace(int index, VType newValue) {
|
||||
return map.indexReplace(index, newValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void indexInsert(int index, long key, VType value) {
|
||||
map.indexInsert(index, key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void release() {
|
||||
map.release();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visualizeKeyDistribution(int characters) {
|
||||
return map.visualizeKeyDistribution(characters);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int removeAll(LongObjectPredicate<? super VType> predicate) {
|
||||
return map.removeAll(predicate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends LongObjectPredicate<? super VType>> T forEach(T predicate) {
|
||||
return map.forEach(predicate);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,65 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch 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.elasticsearch.common.collect;
|
||||
|
||||
import com.google.common.collect.Iterators;
|
||||
import com.google.common.collect.PeekingIterator;
|
||||
import com.google.common.collect.UnmodifiableIterator;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
|
||||
public enum Iterators2 {
|
||||
;
|
||||
|
||||
/** Remove duplicated elements from an iterator over sorted content. */
|
||||
public static <T> Iterator<T> deduplicateSorted(Iterator<? extends T> iterator, final Comparator<? super T> comparator) {
|
||||
// TODO: infer type once JI-9019884 is fixed
|
||||
final PeekingIterator<T> it = Iterators.<T>peekingIterator(iterator);
|
||||
return new UnmodifiableIterator<T>() {
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return it.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T next() {
|
||||
final T ret = it.next();
|
||||
while (it.hasNext() && comparator.compare(ret, it.peek()) == 0) {
|
||||
it.next();
|
||||
}
|
||||
assert !it.hasNext() || comparator.compare(ret, it.peek()) < 0 : "iterator is not sorted: " + ret + " > " + it.peek();
|
||||
return ret;
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
/** Return a merged view over several iterators, optionally deduplicating equivalent entries. */
|
||||
public static <T> Iterator<T> mergeSorted(Iterable<Iterator<? extends T>> iterators, Comparator<? super T> comparator, boolean deduplicate) {
|
||||
Iterator<T> it = Iterators.mergeSorted(iterators, comparator);
|
||||
if (deduplicate) {
|
||||
it = deduplicateSorted(it, comparator);
|
||||
}
|
||||
return it;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,85 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch 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.elasticsearch.common.lucene;
|
||||
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
|
||||
/**
|
||||
* A wrapped to {@link BytesRef} that also caches the hashCode for it.
|
||||
*/
|
||||
public class HashedBytesRef {
|
||||
|
||||
public BytesRef bytes;
|
||||
public int hash;
|
||||
|
||||
public HashedBytesRef() {
|
||||
}
|
||||
|
||||
public HashedBytesRef(String bytes) {
|
||||
this(new BytesRef(bytes));
|
||||
}
|
||||
|
||||
public HashedBytesRef(BytesRef bytes) {
|
||||
this(bytes, bytes.hashCode());
|
||||
}
|
||||
|
||||
public HashedBytesRef(BytesRef bytes, int hash) {
|
||||
this.bytes = bytes;
|
||||
this.hash = hash;
|
||||
}
|
||||
|
||||
public HashedBytesRef resetHashCode() {
|
||||
this.hash = bytes.hashCode();
|
||||
return this;
|
||||
}
|
||||
|
||||
public HashedBytesRef reset(BytesRef bytes, int hash) {
|
||||
this.bytes = bytes;
|
||||
this.hash = hash;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return hash;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (other instanceof HashedBytesRef) {
|
||||
return bytes.equals(((HashedBytesRef) other).bytes);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return bytes.toString();
|
||||
}
|
||||
|
||||
public HashedBytesRef deepCopy() {
|
||||
return deepCopyOf(this);
|
||||
}
|
||||
|
||||
public static HashedBytesRef deepCopyOf(HashedBytesRef other) {
|
||||
BytesRef copy = BytesRef.deepCopyOf(other.bytes);
|
||||
return new HashedBytesRef(copy, other.hash);
|
||||
}
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch 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.elasticsearch.common.lucene.store;
|
||||
|
||||
import org.apache.lucene.store.IndexInput;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class ThreadSafeInputStreamIndexInput extends InputStreamIndexInput {
|
||||
|
||||
public ThreadSafeInputStreamIndexInput(IndexInput indexInput, long limit) {
|
||||
super(indexInput, limit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized int read(byte[] b, int off, int len) throws IOException {
|
||||
return super.read(b, off, len);
|
||||
}
|
||||
}
|
|
@ -1,64 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch 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.elasticsearch.common.unit;
|
||||
|
||||
import org.elasticsearch.common.io.stream.StreamInput;
|
||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||
import org.elasticsearch.common.io.stream.Streamable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class Percent implements Streamable, Serializable {
|
||||
|
||||
private double value;
|
||||
|
||||
public Percent(double value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public double value() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return format(value);
|
||||
}
|
||||
|
||||
public static String format(double value) {
|
||||
String p = String.valueOf(value * 100.0);
|
||||
int ix = p.indexOf(".") + 1;
|
||||
return p.substring(0, ix) + p.substring(ix, ix + 1) + "%";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFrom(StreamInput in) throws IOException {
|
||||
value = in.readDouble();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(StreamOutput out) throws IOException {
|
||||
out.writeDouble(value);
|
||||
}
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch 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.elasticsearch.common.collect;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Ordering;
|
||||
import com.google.common.collect.Sets;
|
||||
import org.apache.lucene.util.CollectionUtil;
|
||||
import org.elasticsearch.test.ElasticsearchTestCase;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class Iterators2Tests extends ElasticsearchTestCase {
|
||||
|
||||
public void testDeduplicateSorted() {
|
||||
final List<String> list = Lists.newArrayList();
|
||||
for (int i = randomInt(100); i >= 0; --i) {
|
||||
final int frequency = randomIntBetween(1, 10);
|
||||
final String s = randomAsciiOfLength(randomIntBetween(2, 20));
|
||||
for (int j = 0; j < frequency; ++j) {
|
||||
list.add(s);
|
||||
}
|
||||
}
|
||||
CollectionUtil.introSort(list);
|
||||
final List<String> deduplicated = Lists.newArrayList();
|
||||
for (Iterator<String> it = Iterators2.deduplicateSorted(list.iterator(), Ordering.natural()); it.hasNext(); ) {
|
||||
deduplicated.add(it.next());
|
||||
}
|
||||
assertEquals(Lists.newArrayList(Sets.newTreeSet(list)), deduplicated);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue