queue.js
3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* @Author: limin
* @Date: 2019-12-06 14:58:35
* @Last Modified by: limin
* @Last Modified time: 2020-01-08 14:08:08
*/
// const items = new WeakMap(); //WeakMap对象是密钥/值对的集合,其中密钥被弱引用。键必须是对象,值可以是任意值。
// module.exports = class Queue {
// constructor() {
// items.set(this, []);
// }
// enqueue(...element) {
// //向队列尾部添加一个(或多个)新的项
// let q = items.get(this);
// q.push(...element);
// }
// dequeue() {
// //移除队列的第一个(排在队列最前面的)项,并返回被移除的元素。
// let q = items.get(this);
// let r = q.shift();
// return r;
// }
// front() {
// //返回队列中第一个元素——最先被添加,也将是最先被移除的元素。队列不做任何变动(不移除元素,只返回元素信息)
// let q = items.get(this);
// return q[0];
// }
// isEmpty() {
// //如果队列中不包含任何元素,返回true,否则返回false。
// return items.get(this).length == 0;
// }
// size() {
// //返回队列包含的元素个数,与数组的length属性类似。
// let q = items.get(this);
// return q.length;
// }
// clear() {
// //清空队列里面的元素。
// items.set(this, []);
// }
// print() {
// //打印队列为String到控制台
// console.log(this.toString());
// }
// toString() {
// //输出队列以String模式。
// return items.get(this).toString();
// }
// }
module.exports = class Queue {
constructor({size,data = [],cache,fetch,const_id}) {
this._elements = data;
this._size = size;
this._last_dequeue = data;
this._cache = cache;
this._fetch = fetch;
this._const_id = const_id;
cache.set(const_id,(data && data.length && data[data.length-1].id) || 0 )
}
isArray(arr) {
return Object.prototype.toString.call(arr) === "[object Array]";
}
isEmpty() {
return this._elements.length === 0;
}
q() {
return this._elements;
}
size() {
return this._size;
}
enqueue(...element) {
if (this.isArray(...element)) {
this._elements = this._elements.concat(...element);
return;
}
this._elements.push(...element);
}
/**
* 出列 前 N个对象 并返回 出列的对象
* @param {*} n 出列 前N 个
*/
dequeue(n) {
if (this.isEmpty()) throw new Error("Queue is empty");
if (n) {
var r = [];
this._elements = this._elements.filter((item, index) => {
index < n && r.push(item);
return index >= n;
});
return r;
}
return this._elements.shift();
}
peek() {
this._last_dequeue = this._elements;
this._elements = [];
this.fill()
return this._last_dequeue;
}
async fill() {
let _last_id = await this._cache.get(this._const_id); //获取缓存的最后一个ID
const result = await this._fetch.call(this._cache,{limit: this._size, last_id:_last_id}); //数据库获取数据
const _result_size = result.length;
if (!_result_size) {
this.enqueue(this._last_dequeue);
} else if (_result_size < this._size) {
// 从上一次返回的数据中 获取相差的几条数据 补充到最新数据的末尾 保证队列数据的数量 公式为: [data][_result_size]
const _last_data_list = this._last_dequeue.filter((item, index) => index >= _result_size);
this.enqueue(...result, ..._last_data_list);
} else {
this.enqueue(result);
}
_last_id = _result_size? result.reduce((pre, next) => Math.max(pre, next.id), 0): _last_id;
this._cache.set(this._const_id,_last_id) // redis 缓存最后一个ID
return _last_id
}
first() {
if (this.isEmpty()) throw new Error("Queue is empty");
return this._elements[0];
}
};