下面列出了java.util.IdentityHashMap#size ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Writes a <code>IdentityHashMap</code> to a <code>DataOutput</code>. Note
* that even though <code>map</code> may be an instance of a
* subclass of <code>IdentityHashMap</code>, <code>readIdentityHashMap</code> will
* always return an instance of <code>IdentityHashMap</code>, <B>not</B> an
* instance of the subclass. To preserve the class type of
* <code>map</code>, {@link #writeObject(Object, DataOutput)} should be used for data
* serialization.
*
* @throws IOException
* A problem occurs while writing to <code>out</code>
*
* @see #readIdentityHashMap
*/
public static void writeIdentityHashMap(IdentityHashMap<?,?> map, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
int size;
if (map == null) {
size = -1;
} else {
size = map.size();
}
InternalDataSerializer.writeArrayLength(size, out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing IdentityHashMap with " + size + " elements: " + map);
}
if (size > 0) {
for (Map.Entry<?,?> entry: map.entrySet()){
writeObject(entry.getKey(), out);
writeObject(entry.getValue(), out);
}
}
}
@Override
@SuppressWarnings("unchecked")
Iterator<Annotation> iteratorRecursive(SpanNode node) {
IdentityHashMap<SpanNode, SpanNode> nodes = new IdentityHashMap<SpanNode, SpanNode>();
nodes.put(node, node);
{
Iterator<SpanNode> childrenIt = node.childIteratorRecursive();
while (childrenIt.hasNext()) {
SpanNode child = childrenIt.next();
nodes.put(child, child);
}
}
List<Collection<Annotation>> annotationLists = new ArrayList<Collection<Annotation>>(nodes.size());
for (SpanNode includedNode : nodes.keySet()) {
Collection<Annotation> includedAnnotations = spanNode2Annotation.get(includedNode);
if (includedAnnotations != null) {
annotationLists.add(includedAnnotations);
}
}
return new AnnotationCollectionIterator(annotationLists);
}
/**
* Writes a <code>IdentityHashMap</code> to a <code>DataOutput</code>. Note
* that even though <code>map</code> may be an instance of a
* subclass of <code>IdentityHashMap</code>, <code>readIdentityHashMap</code> will
* always return an instance of <code>IdentityHashMap</code>, <B>not</B> an
* instance of the subclass. To preserve the class type of
* <code>map</code>, {@link #writeObject(Object, DataOutput)} should be used for data
* serialization.
*
* @throws IOException
* A problem occurs while writing to <code>out</code>
*
* @see #readIdentityHashMap
*/
public static void writeIdentityHashMap(IdentityHashMap<?,?> map, DataOutput out)
throws IOException {
InternalDataSerializer.checkOut(out);
int size;
if (map == null) {
size = -1;
} else {
size = map.size();
}
InternalDataSerializer.writeArrayLength(size, out);
if (DEBUG) {
InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing IdentityHashMap with " + size + " elements: " + map);
}
if (size > 0) {
for (Map.Entry<?,?> entry: map.entrySet()){
writeObject(entry.getKey(), out);
writeObject(entry.getValue(), out);
}
}
}
@VisibleForTesting
int numWatchersForTest(String service) {
synchronized (watchLock) {
IdentityHashMap<StreamObserver<HealthCheckResponse>, Boolean> serviceWatchers =
watchers.get(service);
if (serviceWatchers == null) {
return 0;
}
return serviceWatchers.size();
}
}
/**
* Given the expected size, computes such a number N of items that
* inserting (N+1) items will trigger resizing of the internal storage
*/
static int threshold(int size) throws Throwable {
IdentityHashMap<Object,Object> m = new IdentityHashMap<>(size);
int initialCapacity = capacity(m);
while (capacity(m) == initialCapacity)
growUsingPut(m, 1);
return m.size() - 1;
}
@Override
@SuppressWarnings("unchecked")
public boolean retainAll(Collection<?> elements) {
IdentityHashMap<E, Object> newMap = new IdentityHashMap<E, Object>();
for (Object element : elements) {
if (map.containsKey(element))
newMap.put((E) element, null);
}
boolean anyChanges = newMap.size() != map.size();
map = newMap;
return anyChanges;
}
/**
* Given the expected size, computes such a number N of items that
* inserting (N+1) items will trigger resizing of the internal storage
*/
static int threshold(int size) throws Throwable {
IdentityHashMap<Object,Object> m = new IdentityHashMap<>(size);
int initialCapacity = capacity(m);
while (capacity(m) == initialCapacity)
growUsingPut(m, 1);
return m.size() - 1;
}
/**
* Given the expected size, computes such a number N of items that
* inserting (N+1) items will trigger resizing of the internal storage
*/
static int threshold(int size) throws Throwable {
IdentityHashMap<Object,Object> m = new IdentityHashMap<>(size);
int initialCapacity = capacity(m);
while (capacity(m) == initialCapacity)
growUsingPut(m, 1);
return m.size() - 1;
}
/**
* Given the expected size, computes such a number N of items that
* inserting (N+1) items will trigger resizing of the internal storage
*/
static int threshold(int size) throws Throwable {
IdentityHashMap<Object,Object> m = new IdentityHashMap<>(size);
int initialCapacity = capacity(m);
while (capacity(m) == initialCapacity)
growUsingPut(m, 1);
return m.size() - 1;
}
/**
* Given the expected size, computes such a number N of items that
* inserting (N+1) items will trigger resizing of the internal storage
*/
static int threshold(int size) throws Throwable {
IdentityHashMap<Object,Object> m = new IdentityHashMap<>(size);
int initialCapacity = capacity(m);
while (capacity(m) == initialCapacity)
growUsingPut(m, 1);
return m.size() - 1;
}
/**
* Given the expected size, computes such a number N of items that
* inserting (N+1) items will trigger resizing of the internal storage
*/
static int threshold(int size) throws Throwable {
IdentityHashMap<Object,Object> m = new IdentityHashMap<>(size);
int initialCapacity = capacity(m);
while (capacity(m) == initialCapacity)
growUsingPut(m, 1);
return m.size() - 1;
}
/**
* Given the expected size, computes such a number N of items that
* inserting (N+1) items will trigger resizing of the internal storage
*/
static int threshold(int size) throws Throwable {
IdentityHashMap<Object,Object> m = new IdentityHashMap<>(size);
int initialCapacity = capacity(m);
while (capacity(m) == initialCapacity)
growUsingPut(m, 1);
return m.size() - 1;
}
/**
* Given the expected size, computes such a number N of items that
* inserting (N+1) items will trigger resizing of the internal storage
*/
static int threshold(int size) throws Throwable {
IdentityHashMap<Object,Object> m = new IdentityHashMap<>(size);
int initialCapacity = capacity(m);
while (capacity(m) == initialCapacity)
growUsingPut(m, 1);
return m.size() - 1;
}
/**
* Given the expected size, computes such a number N of items that
* inserting (N+1) items will trigger resizing of the internal storage
*/
static int threshold(int size) throws Throwable {
IdentityHashMap<Object,Object> m = new IdentityHashMap<>(size);
int initialCapacity = capacity(m);
while (capacity(m) == initialCapacity)
growUsingPut(m, 1);
return m.size() - 1;
}
/**
* Given the expected size, computes such a number N of items that
* inserting (N+1) items will trigger resizing of the internal storage
*/
static int threshold(int size) throws Throwable {
IdentityHashMap<Object,Object> m = new IdentityHashMap<>(size);
int initialCapacity = capacity(m);
while (capacity(m) == initialCapacity)
growUsingPut(m, 1);
return m.size() - 1;
}
/**
* Given the expected size, computes such a number N of items that
* inserting (N+1) items will trigger resizing of the internal storage
*/
static int threshold(int size) throws Throwable {
IdentityHashMap<Object,Object> m = new IdentityHashMap<>(size);
int initialCapacity = capacity(m);
while (capacity(m) == initialCapacity)
growUsingPut(m, 1);
return m.size() - 1;
}
@VisibleForTesting
int numWatchersForTest(String service) {
synchronized (watchLock) {
IdentityHashMap<StreamObserver<HealthCheckResponse>, Boolean> serviceWatchers =
watchers.get(service);
if (serviceWatchers == null) {
return 0;
}
return serviceWatchers.size();
}
}
/**
* Checks adequacy of the expected maximum size of a static field
* of type {@code IdentityHashMap}.
*
* Having
* <pre>
* class XClass {
* static IdentityHashMap theMap = new IdentityHashMap(M);
* }
* </pre>
*
* you should call from the test
*
* <pre>
* OptimalCapacity.ofIdentityHashMap(XClass.class, "theMap", M);
* </pre>
*/
public static void ofIdentityHashMap(Class<?> clazz, String fieldName,
int expectedMaxSize)
{
try {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
Object obj = field.get(null);
if (!IdentityHashMap.class.equals(obj.getClass())) {
throw new RuntimeException("'" + field +
"' expected to be of type IdentityHashMap");
}
IdentityHashMap<?,?> map = (IdentityHashMap<?,?>)obj;
// Check that size of map is what was expected
if (map.size() != expectedMaxSize) {
throw new RuntimeException("Size of '" + field +
"' is " + map.size() +
", which differs from expected " + expectedMaxSize);
}
// Check that the map allocated only necessary amount of memory
IdentityHashMap<Object, Object> tmp = new IdentityHashMap<>(map);
if (internalArraySize(map) != internalArraySize(tmp)) {
throw new RuntimeException("Final capacity of '" + field +
"' is " + internalArraySize(map) +
", which exceeds necessary minimum " + internalArraySize(tmp));
}
// Check that map was initially properly sized
tmp = new IdentityHashMap<>(expectedMaxSize);
tmp.put(new Object(), new Object()); // trigger storage init
if (internalArraySize(map) != internalArraySize(tmp)) {
throw new RuntimeException("Requested number of elements in '" + field +
"' was " + expectedMaxSize +
", which resulted in final capacity " + internalArraySize(tmp) +
", which differs from necessary minimum " + internalArraySize(map));
}
} catch (ReflectiveOperationException roe) {
throw new RuntimeException(roe);
}
}
private void handleFilterExclusions() throws IOException {
List<String> excludeTags = freq.domain.excludeTags;
if (excludeTags == null || excludeTags.size() == 0) {
return;
}
@SuppressWarnings({"rawtypes"})
Map tagMap = (Map) fcontext.req.getContext().get("tags");
if (tagMap == null) {
// no filters were tagged
return;
}
IdentityHashMap<Query,Boolean> excludeSet = new IdentityHashMap<>();
for (String excludeTag : excludeTags) {
Object olst = tagMap.get(excludeTag);
// tagMap has entries of List<String,List<QParser>>, but subject to change in the future
if (!(olst instanceof Collection)) continue;
for (Object o : (Collection<?>)olst) {
if (!(o instanceof QParser)) continue;
QParser qp = (QParser)o;
try {
excludeSet.put(qp.getQuery(), Boolean.TRUE);
} catch (SyntaxError syntaxError) {
// This should not happen since we should only be retrieving a previously parsed query
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, syntaxError);
}
}
}
if (excludeSet.size() == 0) return;
List<Query> qlist = new ArrayList<>();
// TODO: somehow remove responsebuilder dependency
ResponseBuilder rb = SolrRequestInfo.getRequestInfo().getResponseBuilder();
// add the base query
if (!excludeSet.containsKey(rb.getQuery())) {
qlist.add(rb.getQuery());
}
// add the filters
if (rb.getFilters() != null) {
for (Query q : rb.getFilters()) {
if (!excludeSet.containsKey(q)) {
qlist.add(q);
}
}
}
// now walk back up the context tree
// TODO: we lose parent exclusions...
for (FacetContext curr = fcontext; curr != null; curr = curr.parent) {
if (curr.filter != null) {
qlist.add( curr.filter );
}
}
// recompute the base domain
fcontext.base = fcontext.searcher.getDocSet(qlist);
}
/**
* Computes a base {@link DocSet} for the current request to be used
* when computing global stats for the local index.
*
* This is typically the same as the main DocSet for the {@link ResponseBuilder}
* unless {@link CommonParams#TAG tag}ged filter queries have been excluded using
* the {@link CommonParams#EXCLUDE ex} local param
*/
public DocSet computeBaseDocSet() throws IOException {
DocSet docs = rb.getResults().docSet;
Map<?,?> tagMap = (Map<?,?>) rb.req.getContext().get("tags");
if (excludeTagList.isEmpty() || null == tagMap) {
// either the exclude list is empty, or there
// aren't any tagged filters to exclude anyway.
return docs;
}
IdentityHashMap<Query,Boolean> excludeSet = new IdentityHashMap<Query,Boolean>();
for (String excludeTag : excludeTagList) {
Object olst = tagMap.get(excludeTag);
// tagMap has entries of List<String,List<QParser>>, but subject to change in the future
if (!(olst instanceof Collection)) continue;
for (Object o : (Collection<?>)olst) {
if (!(o instanceof QParser)) continue;
QParser qp = (QParser)o;
try {
excludeSet.put(qp.getQuery(), Boolean.TRUE);
} catch (SyntaxError e) {
// this shouldn't be possible since the request should have already
// failed when attempting to execute the query, but just in case...
throw new SolrException(ErrorCode.BAD_REQUEST, "Excluded query can't be parsed: " +
originalParam + " due to: " + e.getMessage(), e);
}
}
}
if (excludeSet.size() == 0) return docs;
List<Query> qlist = new ArrayList<Query>();
// add the base query
if (!excludeSet.containsKey(rb.getQuery())) {
qlist.add(rb.getQuery());
}
// add the filters
if (rb.getFilters() != null) {
for (Query q : rb.getFilters()) {
if (!excludeSet.containsKey(q)) {
qlist.add(q);
}
}
}
// get the new base docset for this facet
return searcher.getDocSet(qlist);
}