mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
refactor: Message Unique
This commit is contained in:
@@ -1,20 +1,63 @@
|
|||||||
import crypto from 'crypto';
|
import crypto from 'crypto';
|
||||||
|
|
||||||
|
class CircularQueue<T> {
|
||||||
|
private items: T[] = [];
|
||||||
|
private front: number = 0;
|
||||||
|
private maxSize: number;
|
||||||
|
constructor(maxSize: number) {
|
||||||
|
this.maxSize = maxSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
enqueue(item: T): void {
|
||||||
|
if (this.items.length == this.maxSize) {
|
||||||
|
this.front = (this.front + 1) % this.maxSize;
|
||||||
|
}
|
||||||
|
this.items[(this.front + this.items.length) % this.maxSize] = item;
|
||||||
|
}
|
||||||
|
|
||||||
|
dequeue(): T | undefined {
|
||||||
|
return this.items.length == 0 ? undefined : this.items[(this.front + 1) % this.items.length];
|
||||||
|
}
|
||||||
|
|
||||||
|
size(): number {
|
||||||
|
return this.items.length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class LimitedHashTable<K, V> {
|
class LimitedHashTable<K, V> {
|
||||||
private keyToValue: Map<K, V> = new Map();
|
private keyToValue: Map<K, V> = new Map();
|
||||||
private valueToKey: Map<V, K> = new Map();
|
private valueToKey: Map<V, K> = new Map();
|
||||||
|
private keyQueue: CircularQueue<K> = new CircularQueue<K>(0);
|
||||||
|
private valueQueue: CircularQueue<V> = new CircularQueue<V>(0);
|
||||||
private maxSize: number;
|
private maxSize: number;
|
||||||
private KeyQueneList: K[] = [];
|
|
||||||
private ValueQueneList: V[] = [];
|
|
||||||
constructor(maxSize: number) {
|
constructor(maxSize: number) {
|
||||||
this.maxSize = maxSize;
|
this.maxSize = maxSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private removeFromQueues(key: K, value: V): void {
|
||||||
|
this.keyQueue.dequeue();
|
||||||
|
this.valueQueue.dequeue();
|
||||||
|
}
|
||||||
|
|
||||||
set(key: K, value: V): void {
|
set(key: K, value: V): void {
|
||||||
|
let isExist = this.keyToValue.get(key);
|
||||||
|
if (isExist && isExist === value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
this.keyToValue.set(key, value);
|
this.keyToValue.set(key, value);
|
||||||
this.valueToKey.set(value, key);
|
this.valueToKey.set(value, key);
|
||||||
if (this.KeyQueneList.length >= this.maxSize || this.ValueQueneList.length >= this.maxSize) {
|
this.keyQueue.enqueue(key);
|
||||||
this.KeyQueneList.shift();
|
this.valueQueue.enqueue(value);
|
||||||
this.ValueQueneList.shift();
|
|
||||||
|
if (this.keyQueue.size() > this.maxSize || this.valueQueue.size() > this.maxSize) {
|
||||||
|
const removedKey = this.keyQueue.dequeue();
|
||||||
|
const removedValue = this.valueQueue.dequeue();
|
||||||
|
if (removedKey !== undefined && removedValue !== undefined) {
|
||||||
|
this.keyToValue.delete(removedKey);
|
||||||
|
this.valueToKey.delete(removedValue);
|
||||||
|
this.removeFromQueues(removedKey, removedValue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,27 +69,54 @@ class LimitedHashTable<K, V> {
|
|||||||
return this.valueToKey.get(value);
|
return this.valueToKey.get(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
delete(key: K): void {
|
deleteByValue(value: V) {
|
||||||
|
const key = this.valueToKey.get(value);
|
||||||
|
if (key !== undefined) {
|
||||||
|
this.keyToValue.delete(key);
|
||||||
|
this.valueToKey.delete(value);
|
||||||
|
this.removeFromQueues(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteByKey(key: K): void {
|
||||||
const value = this.keyToValue.get(key);
|
const value = this.keyToValue.get(key);
|
||||||
if (value !== undefined) {
|
if (value !== undefined) {
|
||||||
this.keyToValue.delete(key);
|
this.keyToValue.delete(key);
|
||||||
this.valueToKey.delete(value);
|
this.valueToKey.delete(value);
|
||||||
|
this.removeFromQueues(key, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class MessageUniqueWrapper {
|
class MessageUniqueWrapper {
|
||||||
private msgIdMap: LimitedHashTable<number, string> = new LimitedHashTable(1000);
|
private msgIdMap: LimitedHashTable<string, number>;
|
||||||
|
constructor(MaxMap: number = 1000) {
|
||||||
|
this.msgIdMap = new LimitedHashTable<string, number>(MaxMap);
|
||||||
|
}
|
||||||
|
|
||||||
createMsg(MsgId: string) {
|
createMsg(MsgId: string) {
|
||||||
const ShortId = parseInt(crypto.createHash('sha1').update('2345').digest('hex').slice(0, 8), 16);
|
const hash = crypto.createHash('sha256');
|
||||||
this.msgIdMap.set(ShortId, MsgId);
|
hash.update(MsgId);
|
||||||
return ShortId;
|
const ShortId = parseInt(hash.digest('hex').slice(0, 8), 16);
|
||||||
|
let isExist = this.msgIdMap.getKey(ShortId)
|
||||||
|
if (isExist && isExist === MsgId) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return this.msgIdMap.set(MsgId, ShortId);
|
||||||
}
|
}
|
||||||
|
|
||||||
getMsgIdByShortId(ShortId: number) {
|
getMsgIdByShortId(ShortId: number) {
|
||||||
return this.msgIdMap.getValue(ShortId);
|
return this.msgIdMap.getKey(ShortId);
|
||||||
}
|
}
|
||||||
|
|
||||||
getShortIdByMsgId(MsgId: string) {
|
getShortIdByMsgId(MsgId: string) {
|
||||||
return this.msgIdMap.getKey(MsgId);
|
return this.msgIdMap.getValue(MsgId);
|
||||||
|
}
|
||||||
|
|
||||||
|
CreateMsgIdList(MsgIdList: string[]) {
|
||||||
|
return MsgIdList.map((MsgId) => {
|
||||||
|
return this.createMsg(MsgId);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user