gpt4 book ai didi

org.apache.lucene.util.WeakIdentityMap类的使用及代码示例

转载 作者:知者 更新时间:2024-03-22 06:43:05 26 4
gpt4 key购买 nike

本文整理了Java中org.apache.lucene.util.WeakIdentityMap类的一些代码示例,展示了WeakIdentityMap类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WeakIdentityMap类的具体详情如下:
包路径:org.apache.lucene.util.WeakIdentityMap
类名称:WeakIdentityMap

WeakIdentityMap介绍

[英]Implements a combination of java.util.WeakHashMap and java.util.IdentityHashMap. Useful for caches that need to key off of a == comparison instead of a .equals.

This class is not a general-purpose java.util.Mapimplementation! It intentionally violates Map's general contract, which mandates the use of the equals method when comparing objects. This class is designed for use only in the rare cases wherein reference-equality semantics are required.

This implementation was forked from Apache CXF but modified to not implement the java.util.Map interface and without any set views on it, as those are error-prone and inefficient, if not implemented carefully. The map only contains Iterator implementations on the values and not-GCed keys. Lucene's implementation also supports nullkeys, but those are never weak!

The map supports two modes of operation:

  • reapOnRead = true: This behaves identical to a java.util.WeakHashMapwhere it also cleans up the reference queue on every read operation ( #get(Object), #containsKey(Object), #size(), #valueIterator()), freeing map entries of already GCed keys.
  • reapOnRead = false: This mode does not call #reap() on every read operation. In this case, the reference queue is only cleaned up on write operations (like #put(Object,Object)). This is ideal for maps with few entries where the keys are unlikely be garbage collected, but there are lots of #get(Object)operations. The code can still call #reap() to manually clean up the queue without doing a write operation.
    [中]实现了java的组合。util。WeakHashMap和java。util。身份地图。对于需要从==比较而不是从。等于。
    此类不是通用java。util。地图实现!它故意违反了Map的通用合同,该合同要求在比较对象时使用equals方法。该类仅在少数需要引用相等语义的情况下使用。
    这个实现是从Apache CXF派生出来的,但经过修改后没有实现java。util。映射接口,并且没有任何设置视图,因为如果不小心实现,这些容易出错且效率低下。映射只包含值的迭代器实现,而不包含GCed键。Lucene的实现也支持空键,但它们从来都不弱!
    地图支持两种操作模式:
    *reapOnRead=true:其行为与java相同。util。WeakhashMap,它还清理每个读取操作上的引用队列(#get(对象)、#containsKey(对象)、#size()、#valueIterator()),释放已GCed键的映射项。
    *reapOnRead=false:此模式不会在每次读取操作时调用#eaw()。在这种情况下,引用队列仅在写入操作(如#put(Object,Object))时清理。这非常适合于条目很少的地图,其中密钥不太可能被垃圾收集,但有很多#get(Object)操作。代码仍然可以调用#reaw()来手动清理队列,而无需执行写操作。

代码示例

代码示例来源:origin: org.apache.lucene/lucene-core

/**
 * Creates a new {@code WeakIdentityMap} based on a {@link ConcurrentHashMap}.
 * The map <a href="#reapInfo">cleans up the reference queue on every read operation</a>.
 */
public static <K,V> WeakIdentityMap<K,V> newConcurrentHashMap() {
 return newConcurrentHashMap(true);
}

代码示例来源:origin: org.apache.lucene/lucene-core

/**
 * Creates a new {@code WeakIdentityMap} based on a non-synchronized {@link HashMap}.
 * @param reapOnRead controls if the map <a href="#reapInfo">cleans up the reference queue on every read operation</a>.
 */
public static <K,V> WeakIdentityMap<K,V> newHashMap(boolean reapOnRead) {
 return new WeakIdentityMap<>(new HashMap<IdentityWeakReference,V>(), reapOnRead);
}

代码示例来源:origin: org.apache.lucene/lucene-core

/** Removes all of the mappings from this map. */
public void clear() {
 backingStore.clear();
 reap();
}

代码示例来源:origin: org.infinispan/infinispan-embedded-query

unsetBuffers();
if (clones != null) {
 clones.remove(this);
 for (Iterator<ByteBufferIndexInput> it = this.clones.keyIterator(); it.hasNext();) {
  final ByteBufferIndexInput clone = it.next();
  assert clone.isClone;
  clone.unsetBuffers();
 this.clones.clear();

代码示例来源:origin: gncloud/fastcatsearch

private static Class<? extends AttributeImpl> getClassForInterface(Class<? extends Attribute> attClass) {
  final WeakReference<Class<? extends AttributeImpl>> ref = attClassImplMap.get(attClass);
  Class<? extends AttributeImpl> clazz = (ref == null) ? null : ref.get();
  if (clazz == null) {
   // we have the slight chance that another thread may do the same, but who cares?
   try {
    attClassImplMap.put(attClass,
     new WeakReference<Class<? extends AttributeImpl>>(
      clazz = Class.forName(attClass.getName() + "Impl", true, attClass.getClassLoader())
      .asSubclass(AttributeImpl.class)
     )
    );
   } catch (ClassNotFoundException e) {
    throw new IllegalArgumentException("Could not find implementing class for " + attClass.getName());
   }
  }
  return clazz;
 }
}

代码示例来源:origin: org.apache.lucene/lucene-core

/** Returns {@code true} if this map contains no key-value mappings. */
public boolean isEmpty() {
 return size() == 0;
}

代码示例来源:origin: apache/jackrabbit-oak

@Override
public OakIndexInput clone() {
  // TODO : shouldn't we call super#clone ?
  OakIndexInput clonedIndexInput = new OakIndexInput(this);
  clonedIndexInput.isClone = true;
  if (clones != null) {
    clones.put(clonedIndexInput, Boolean.TRUE);
  }
  return clonedIndexInput;
}

代码示例来源:origin: org.apache.lucene/lucene-core

/** 
 * Creates a new {@code WeakIdentityMap} based on a non-synchronized {@link HashMap}.
 * The map <a href="#reapInfo">cleans up the reference queue on every read operation</a>.
 */
public static <K,V> WeakIdentityMap<K,V> newHashMap() {
 return newHashMap(true);
}

代码示例来源:origin: org.apache.jackrabbit/oak-lucene

@Override
public void close() {
  file.close();
  if (clones != null) {
    for (Iterator<OakIndexInput> it = clones.keyIterator(); it.hasNext();) {
      final OakIndexInput clone = it.next();
      assert clone.isClone;
      clone.close();
    }
  }
}

代码示例来源:origin: gncloud/fastcatsearch

unsetBuffers();
if (clones != null) {
 clones.remove(this);
 for (Iterator<ByteBufferIndexInput> it = this.clones.keyIterator(); it.hasNext();) {
  final ByteBufferIndexInput clone = it.next();
  assert clone.isClone;
  clone.unsetBuffers();
 this.clones.clear();

代码示例来源:origin: gncloud/fastcatsearch

static LinkedList<WeakReference<Class<? extends Attribute>>> getAttributeInterfaces(final Class<? extends AttributeImpl> clazz) {
 LinkedList<WeakReference<Class<? extends Attribute>>> foundInterfaces = knownImplClasses.get(clazz);
 if (foundInterfaces == null) {
  // we have the slight chance that another thread may do the same, but who cares?
  foundInterfaces = new LinkedList<WeakReference<Class<? extends Attribute>>>();
  // find all interfaces that this attribute instance implements
  // and that extend the Attribute interface
  Class<?> actClazz = clazz;
  do {
   for (Class<?> curInterface : actClazz.getInterfaces()) {
    if (curInterface != Attribute.class && Attribute.class.isAssignableFrom(curInterface)) {
     foundInterfaces.add(new WeakReference<Class<? extends Attribute>>(curInterface.asSubclass(Attribute.class)));
    }
   }
   actClazz = actClazz.getSuperclass();
  } while (actClazz != null);
  knownImplClasses.put(clazz, foundInterfaces);
 }
 return foundInterfaces;
}

代码示例来源:origin: harbby/presto-connectors

/** Returns {@code true} if this map contains no key-value mappings. */
public boolean isEmpty() {
 return size() == 0;
}

代码示例来源:origin: org.apache.jackrabbit/oak-lucene

@Override
public OakIndexInput clone() {
  // TODO : shouldn't we call super#clone ?
  OakIndexInput clonedIndexInput = new OakIndexInput(this);
  clonedIndexInput.isClone = true;
  if (clones != null) {
    clones.put(clonedIndexInput, Boolean.TRUE);
  }
  return clonedIndexInput;
}

代码示例来源:origin: org.infinispan/infinispan-embedded-query

/** 
 * Creates a new {@code WeakIdentityMap} based on a non-synchronized {@link HashMap}.
 * The map <a href="#reapInfo">cleans up the reference queue on every read operation</a>.
 */
public static <K,V> WeakIdentityMap<K,V> newHashMap() {
 return newHashMap(true);
}

代码示例来源:origin: apache/jackrabbit-oak

@Override
public void close() {
  file.close();
  if (clones != null) {
    for (Iterator<OakIndexInput> it = clones.keyIterator(); it.hasNext();) {
      final OakIndexInput clone = it.next();
      assert clone.isClone;
      clone.close();
    }
  }
}

代码示例来源:origin: org.apache.lucene/lucene-core

/** Returns an iterator over all values of this map.
 * This iterator may return values whose key is already
 * garbage collected while iterator is consumed,
 * especially if {@code reapOnRead} is {@code false}. */
public Iterator<V> valueIterator() {
 if (reapOnRead) reap();
 return backingStore.values().iterator();
}

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.lucene

/**
 * Creates a new {@code WeakIdentityMap} based on a {@link ConcurrentHashMap}.
 * The map <a href="#reapInfo">cleans up the reference queue on every read operation</a>.
 */
public static <K,V> WeakIdentityMap<K,V> newConcurrentHashMap() {
 return newConcurrentHashMap(true);
}

代码示例来源:origin: harbby/presto-connectors

unsetBuffers();
if (clones != null) {
 clones.remove(this);
 for (Iterator<ByteBufferIndexInput> it = this.clones.keyIterator(); it.hasNext();) {
  final ByteBufferIndexInput clone = it.next();
  assert clone.isClone;
  clone.unsetBuffers();
 this.clones.clear();

代码示例来源:origin: org.apache.lucene/lucene-core

/**
 * Creates a new {@code WeakIdentityMap} based on a {@link ConcurrentHashMap}.
 * @param reapOnRead controls if the map <a href="#reapInfo">cleans up the reference queue on every read operation</a>.
 */
public static <K,V> WeakIdentityMap<K,V> newConcurrentHashMap(boolean reapOnRead) {
 return new WeakIdentityMap<>(new ConcurrentHashMap<IdentityWeakReference,V>(), reapOnRead);
}

代码示例来源:origin: org.onehippo.cms7/hippo-repository-engine

private DocIdSet getIndexReaderDocIdSet(final IndexReader reader) throws IOException {
  OpenBitSet docIdSet = cache.get(reader);
  if (docIdSet != null) {
    log.debug("For userId '{}' return cached bitSet for reader with max doc '{}' and num docs '{}'",
        userId, reader.maxDoc(), reader.numDocs());
    return docIdSet;
  }
  synchronized (this) {
    // try again after obtaining the lock
    docIdSet = cache.get(reader);
    if (docIdSet != null) {
      log.debug("For userId '{}' return cached bitSet for reader with max doc '{}' and num docs '{}'",
          userId, reader.maxDoc(), reader.numDocs());
      return docIdSet;
    }
    log.debug("For userId '{}' could not find a cached bitSet for reader  with max doc '{}' and num docs '{}'",
        userId, reader.maxDoc(), reader.numDocs());
    docIdSet = createDocIdSet(reader);
    cache.put(reader, docIdSet);
    return docIdSet;
  }
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com