文档章节

聊聊Elasticsearch的EvictingQueue

go4it
 go4it
发布于 06/05 20:54
字数 623
阅读 6
收藏 1

本文主要研究一下Elasticsearch的EvictingQueue

EvictingQueue

elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/common/collect/EvictingQueue.java

public class EvictingQueue<T> implements Queue<T> {
    private final int maximumSize;
    private final ArrayDeque<T> queue;

    /**
     * Construct a new {@code EvictingQueue} that holds {@code maximumSize} elements.
     *
     * @param maximumSize The maximum number of elements that the queue can hold
     * @throws IllegalArgumentException if {@code maximumSize} is less than zero
     */
    public EvictingQueue(int maximumSize) {
        if (maximumSize < 0) {
            throw new IllegalArgumentException("maximumSize < 0");
        }
        this.maximumSize = maximumSize;
        this.queue = new ArrayDeque<>(maximumSize);
    }

    /**
     * @return the number of additional elements that the queue can accommodate before evictions occur
     */
    public int remainingCapacity() {
        return this.maximumSize - this.size();
    }

    /**
     * Add the given element to the queue, possibly forcing an eviction from the head if {@link #remainingCapacity()} is
     * zero.
     *
     * @param t the element to add
     * @return true if the element was added (always the case for {@code EvictingQueue}
     */
    @Override
    public boolean add(T t) {
        if (maximumSize == 0) {
            return true;
        }
        if (queue.size() == maximumSize) {
            queue.remove();
        }
        queue.add(t);
        return true;
    }

    /**
     * @see #add(Object)
     */
    @Override
    public boolean offer(T t) {
        return add(t);
    }

    @Override
    public T remove() {
        return queue.remove();
    }


    @Override
    public T poll() {
        return queue.poll();
    }

    @Override
    public T element() {
        return queue.element();
    }

    @Override
    public T peek() {
        return queue.peek();
    }

    @Override
    public int size() {
        return queue.size();
    }

    @Override
    public boolean isEmpty() {
        return queue.isEmpty();
    }

    @Override
    public boolean contains(Object o) {
        return queue.contains(o);
    }

    @Override
    public Iterator<T> iterator() {
        return queue.iterator();
    }

    @Override
    public Object[] toArray() {
        return queue.toArray();
    }

    @Override
    public <T1> T1[] toArray(T1[] a) {
        return queue.toArray(a);
    }

    @Override
    public boolean remove(Object o) {
        return queue.remove(o);
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return queue.containsAll(c);
    }

    /**
     * Add the given elements to the queue, possibly forcing evictions from the head if {@link #remainingCapacity()} is
     * zero or becomes zero during the execution of this method.
     *
     * @param c the collection of elements to add
     * @return true if any elements were added to the queue
     */
    @Override
    public boolean addAll(Collection<? extends T> c) {
        boolean modified = false;
        for (T e : c)
            if (add(e))
                modified = true;
        return modified;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return queue.removeAll(c);
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return queue.retainAll(c);
    }

    @Override
    public void clear() {
        queue.clear();
    }
}
  • EvictingQueue实现了Queue接口,它的构造器要求输入maximumSize,然后根据maximumSize创建ArrayDeque;其add方法会判断当前队列大小是否等于maximumSize,等于则移除队首的元素然后再添加新元素

实例

elasticsearch-7.0.1/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/MovFnWhitelistedFunctionTests.java

    public void testWindowMax() {
        int numValues = randomIntBetween(1, 100);
        int windowSize = randomIntBetween(1, 50);

        EvictingQueue<Double> window = new EvictingQueue<>(windowSize);
        for (int i = 0; i < numValues; i++) {

            double randValue = randomDouble();
            double expected = -Double.MAX_VALUE;

            if (i == 0) {
                window.offer(randValue);
                continue;
            }

            for (double value : window) {
                expected = Math.max(expected, value);
            }

            double actual = MovingFunctions.max(window.stream().mapToDouble(Double::doubleValue).toArray());
            assertEquals(expected, actual, 0.01 * Math.abs(expected));
            window.offer(randValue);
        }
    }
  • 这里使用EvictingQueue作为一个window的数据,不断根据numValues来offer数据,同时计算window中的最大值

小结

EvictingQueue实现了Queue接口,它的构造器要求输入maximumSize,然后根据maximumSize创建ArrayDeque;其add方法会判断当前队列大小是否等于maximumSize,等于则移除队首的元素然后再添加新元素

doc

© 著作权归作者所有

go4it
粉丝 87
博文 1068
码字总数 1013111
作品 0
深圳
私信 提问
加载中

评论(1)

JPer
JPer
这个性能怎么样
聊聊springboot elasticsearch autoconfigure

序 本文主要研究一下springboot elasticsearch autoconfigure ElasticsearchAutoConfiguration spring-boot-autoconfigure-2.1.4.RELEASE-sources.jar!/org/springframework/boot/autoconfi......

go4it
04/17
47
0
聊聊Elasticsearch的Releasables

序 本文主要研究一下Elasticsearch的Releasables Releasable elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/common/lease/Releasable.java Releasable继承了java.io.Closeab......

go4it
06/14
22
0
聊聊Elasticsearch的ConcurrentMapLong

序 本文主要研究一下Elasticsearch的ConcurrentMapLong ConcurrentMapLong elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/common/util/concurrent/ConcurrentMapLong.java Co......

go4it
06/03
6
0
CentOS7 部署 ElasticSearch 集群

环境 主机名 IP 操作系统 ES 版本 es227 192.168.1.227 CentOS7.5 6.5.4 es228 192.168.1.228 CentOS7.5 6.5.4 es229 192.168.1.229 CentOS7.5 6.5.4 下载 elasticsearch-6.5.4.tar.gz --- 各......

俊赛潘安-才比管乐
2018/12/27
402
0
聊聊Elasticsearch的SizeBlockingQueue

序 本文主要研究一下Elasticsearch的SizeBlockingQueue SizeBlockingQueue elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/common/util/concurrent/SizeBlockingQueue.java Si......

go4it
06/01
14
0

没有更多内容

加载失败,请刷新页面

加载更多

Xss过滤器(Java)

问题 最近旧的系统,遇到Xss安全问题。这个系统采用用的是spring mvc的maven工程。 解决 maven依赖配置 <properties><easapi.version>2.2.0.0</easapi.version></properties><dependenci......

亚林瓜子
28分钟前
2
0
Navicat 快捷键

操作 结果 ctrl+q 打开查询窗口 ctrl+/ 注释sql语句 ctrl+shift +/ 解除注释 ctrl+r 运行查询窗口的sql语句 ctrl+shift+r 只运行选中的sql语句 F6 打开一个mysql命令行窗口 ctrl+l 删除一行 ...

低至一折起
今天
7
0
Set 和 Map

Set 1:基本概念 类数组对象, 内部元素唯一 let set = new Set([1, 2, 3, 2, 1]); console.log(set); // Set(3){ 1, 2, 3 } [...set]; // [1, 2, 3] 接收数组或迭代器对象 ...

凌兮洛
今天
1
0
PyTorch入门笔记一

张量 引入pytorch,生成一个随机的5x3张量 >>> from __future__ import print_function>>> import torch>>> x = torch.rand(5, 3)>>> print(x)tensor([[0.5555, 0.7301, 0.5655],......

仪山湖
今天
5
0
OSChina 周二乱弹 —— 开发语言和语言开发的能一样么

Osc乱弹歌单(2019)请戳(这里) 【今日歌曲】 @花间小酌:#今日歌曲推荐# 分享The Score的单曲《Revolution》 《Revolution》- The Score 手机党少年们想听歌,请使劲儿戳(这里) @批判派...

小小编辑
今天
2.8K
19

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部