-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayDatabase.ts
More file actions
166 lines (142 loc) · 4.2 KB
/
ArrayDatabase.ts
File metadata and controls
166 lines (142 loc) · 4.2 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { world } from '@minecraft/server';
export class ArrayDatabase<T = any> {
public static readonly PROPERTY_MAX_SIZE = 12000;
public static readonly PREFIX = 'array';
private readonly cache: T[][] = [];
private cacheLoaded = false;
private currentKeyIndex = 0;
constructor(public readonly id: string) {}
public getAll(): T[] {
if (!this.cacheLoaded) this.load();
const result: T[] = [];
for (const value of this.cache) result.push(...value);
return result;
}
public add(value: T): this {
if (!this.cacheLoaded) this.load();
const baseData = this.cache[this.currentKeyIndex] ?? [];
baseData.push(value);
this.trySave(baseData);
return this;
}
public has(value: T): boolean {
const locations = this.getAll();
return locations.includes(value);
}
public clear(): void {
const keys = world.getDynamicPropertyIds();
const prefix = this.keyPrefix;
for (const key of keys) {
if (key.startsWith(prefix)) world.setDynamicProperty(key);
}
this.currentKeyIndex = 0;
world.setDynamicProperty(this.indexKey);
this.cache.length = 0;
}
public *values(): IterableIterator<T> {
if (!this.cacheLoaded) this.load();
for (const values of this.cache) {
for (const value of values) {
yield value;
}
}
}
public find(callbackfn: (value: T) => boolean): T | undefined {
for (const value of this.values()) {
if (callbackfn(value)) return value;
}
return undefined;
}
public filter(callbackfn: (value: T) => boolean): T[] {
const result: T[] = [];
for (const value of this.values()) {
if (callbackfn(value)) result.push(value);
}
return result;
}
public map<U>(callbackfn: (value: T) => U): U[] {
const result: U[] = [];
for (const value of this.values()) {
result.push(callbackfn(value));
}
return result;
}
public forEach(callbackfn: (value: T) => void): void {
for (const value of this.values()) {
callbackfn(value);
}
}
public some(callbackfn: (value: T) => boolean): boolean {
for (const value of this.values()) {
if (callbackfn(value)) return true;
}
return false;
}
public every(callbackfn: (value: T) => boolean): boolean {
for (const value of this.values()) {
if (!callbackfn(value)) return false;
}
return true;
}
public [Symbol.iterator](): IterableIterator<T> {
return this.values();
}
public get size(): number {
return this.cache.reduce((acc, arr) => acc + arr.length, 0);
}
private trySave(values: T[], swap: T[] = []) {
let sizeOK = true;
const stringified = JSON.stringify(values);
if (stringified.length > ArrayDatabase.PROPERTY_MAX_SIZE) {
sizeOK = false;
} else {
try {
world.setDynamicProperty(this.currentKey, stringified);
this.cache[this.currentKeyIndex] = values;
} catch (e) {
sizeOK = false;
}
}
if (!sizeOK) {
if (values.length > 0) swap.push(values.shift()!);
this.trySave(values, swap);
this.currentKeyIndex++;
world.setDynamicProperty(this.indexKey, this.currentKeyIndex);
this.trySave(swap);
}
}
private load() {
this.currentKeyIndex = world.getDynamicProperty(this.indexKey) as number ?? 0;
const keys = world.getDynamicPropertyIds();
const prefix = this.keyPrefix;
for (const key of keys) {
if (!key.startsWith(prefix)) continue;
const index = Number(key.slice(prefix.length));
if (Number.isNaN(index)) {
console.error(`Found invalid key: ${key}`);
continue;
}
this.cache[index] = JSON.parse(
world.getDynamicProperty(key) as string ?? '[]'
);
}
this.cacheLoaded = true;
}
private unload(): void {
this.cache.length = 0;
this.cacheLoaded = false;
this.currentKeyIndex = 0;
}
private get keyPrefix(): string {
return `${ArrayDatabase.PREFIX}:${this.id}` as string;
}
private get currentKey(): string {
return `${this.keyPrefix}${this.currentKeyIndex}`;
}
private get indexKey(): string {
return `${ArrayDatabase.PREFIX}:index_${this.id}` as string;
}
public get [Symbol.toStringTag](): string {
return this.id;
}
}