摘要:序本文主要研究一下的继承了接口提供静态方法用于更方便地使用实例在中使用关闭了小结提供静态方法用于更方便地使用
序
本文主要研究一下Elasticsearch的Releasables
Releasableelasticsearch-7.0.1/server/src/main/java/org/elasticsearch/common/lease/Releasable.java
public interface Releasable extends Closeable { @Override void close(); }
Releasable继承了java.io.Closeable接口
Releasableselasticsearch-7.0.1/server/src/main/java/org/elasticsearch/common/lease/Releasables.java
public enum Releasables { ; private static void close(Iterable extends Releasable> releasables, boolean ignoreException) { try { // this does the right thing with respect to add suppressed and not wrapping errors etc. IOUtils.close(releasables); } catch (IOException e) { if (ignoreException == false) { throw new UncheckedIOException(e); } } } /** Release the provided {@link Releasable}s. */ public static void close(Iterable extends Releasable> releasables) { close(releasables, false); } /** Release the provided {@link Releasable}s. */ public static void close(Releasable... releasables) { close(Arrays.asList(releasables)); } /** Release the provided {@link Releasable}s, ignoring exceptions. */ public static void closeWhileHandlingException(Iterablereleasables) { close(releasables, true); } /** Release the provided {@link Releasable}s, ignoring exceptions. */ public static void closeWhileHandlingException(Releasable... releasables) { closeWhileHandlingException(Arrays.asList(releasables)); } /** Release the provided {@link Releasable}s, ignoring exceptions if success
is {@code false}. */ public static void close(boolean success, Iterablereleasables) { if (success) { close(releasables); } else { closeWhileHandlingException(releasables); } } /** Release the provided {@link Releasable}s, ignoring exceptions if success
is {@code false}. */ public static void close(boolean success, Releasable... releasables) { close(success, Arrays.asList(releasables)); } /** Wrap several releasables into a single one. This is typically useful for use with try-with-resources: for example let"s assume * that you store in a list several resources that you would like to see released after execution of the try block: * ** List*/ public static Releasable wrap(final Iterableresources = ...; * try (Releasable releasable = Releasables.wrap(resources)) { * // do something * } * // the resources will be released when reaching here * releasables) { return () -> close(releasables); } /** @see #wrap(Iterable) */ public static Releasable wrap(final Releasable... releasables) { return () -> close(releasables); } /** * Equivalent to {@link #wrap(Releasable...)} but can be called multiple times without double releasing. */ public static Releasable releaseOnce(final Releasable... releasables) { final AtomicBoolean released = new AtomicBoolean(false); return () -> { if (released.compareAndSet(false, true)) { close(releasables); } }; } }
Releasables提供close、closeWhileHandlingException、wrap、releaseOnce静态方法用于更方便地使用Releasable
实例elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/index/translog/Translog.java
public class Translog extends AbstractIndexShardComponent implements IndexShardComponent, Closeable { //...... public static void writeOperations(StreamOutput outStream, ListtoWrite) throws IOException { final ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(BigArrays.NON_RECYCLING_INSTANCE); try { outStream.writeInt(toWrite.size()); final BufferedChecksumStreamOutput checksumStreamOutput = new BufferedChecksumStreamOutput(out); for (Operation op : toWrite) { out.reset(); final long start = out.position(); out.skip(Integer.BYTES); writeOperationNoSize(checksumStreamOutput, op); long end = out.position(); int operationSize = (int) (out.position() - Integer.BYTES - start); out.seek(start); out.writeInt(operationSize); out.seek(end); ReleasablePagedBytesReference bytes = out.bytes(); bytes.writeTo(outStream); } } finally { Releasables.close(out); } } //...... }
writeOperations在finally中使用Releasables.close关闭了ReleasableBytesStreamOutput
小结Releasables提供close、closeWhileHandlingException、wrap、releaseOnce静态方法用于更方便地使用Releasable
docReleasables
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/34508.html
摘要:序本文主要研究一下的实现了,它是线程安全的其构造器要求输入及越大表示新数据权重越大旧数据权重越小返回的是的值,不过它存储的是的形式,返回的时候使用转换会方法使用计算新值,然后使用方法来实现原子更新实例方法测试算法的计算逻辑测试 序 本文主要研究一下Elasticsearch的ExponentiallyWeightedMovingAverage ExponentiallyWeighted...
摘要:序本文主要研究一下的继承了接口,并指定类型为实现了接口,它内部使用实现提供了及两个静态方法用于创建其中方法创建为,为,为的小结继承了接口,并指定类型为实现了接口,它内部使用实现提供了及两个静态方法用于创建其中方法创建为,为,为的 序 本文主要研究一下Elasticsearch的ConcurrentMapLong ConcurrentMapLong elasticsearch-7.0.1...
摘要:序本文主要研究一下的接口定义了方法,该方法返回,另外还定义了一个方法,默认返回的方法返回了一系列,其中包括等要求不得小于要求是对开启的话要求是以后,避免小结接口定义了方法,该方法返回,另外还定义了一个方法,默认返回的方法返回了一 序 本文主要研究一下Elasticsearch的BootstrapCheck BootstrapCheck elasticsearch-7.0.1/serve...
摘要:序本文主要研究一下的实现了接口,其方法使用来选择数组的下标,然后返回该下标的值的构造器创建了两个,分别是及方法执行的是方法执行的是的及方法都接收参数,通过该来选取小结实现了接口,其方法使用来选择数组的下标,然后返回该下标的值的构造器创 序 本文主要研究一下Elasticsearch的RoundRobinSupplier RoundRobinSupplier elasticsearch-...
摘要:序本文主要研究一下的实现了接口,它的构造器要求输入,同时构造了变量方法会先使用将由设置为,如果成功则执行代理的的方法方法则返回值实例方法验证了顺序多次执行的场景方法则验证了并发多次执行的场景则验证了使用作为的场景小结实现了接口,它的构 序 本文主要研究一下Elasticsearch的RunOnce RunOnce elasticsearch-7.0.1/server/src/main/...
阅读 3308·2023-04-25 15:52
阅读 337·2021-11-19 09:40
阅读 1912·2021-09-26 09:47
阅读 815·2021-09-22 15:17
阅读 3376·2021-08-13 13:25
阅读 1906·2019-08-30 15:56
阅读 3350·2019-08-30 13:56
阅读 1885·2019-08-30 11:27