-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayDatabase.js
More file actions
155 lines (155 loc) · 4.29 KB
/
ArrayDatabase.js
File metadata and controls
155 lines (155 loc) · 4.29 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
import { world } from '@minecraft/server';
export class ArrayDatabase {
constructor(id) {
this.id = id;
this.cache = [];
this.cacheLoaded = false;
this.currentKeyIndex = 0;
}
getAll() {
if (!this.cacheLoaded)
this.load();
const result = [];
for (const value of this.cache)
result.push(...value);
return result;
}
add(value) {
if (!this.cacheLoaded)
this.load();
const baseData = this.cache[this.currentKeyIndex] ?? [];
baseData.push(value);
this.trySave(baseData);
return this;
}
has(value) {
const locations = this.getAll();
return locations.includes(value);
}
clear() {
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;
}
*values() {
if (!this.cacheLoaded)
this.load();
for (const values of this.cache) {
for (const value of values) {
yield value;
}
}
}
find(callbackfn) {
for (const value of this.values()) {
if (callbackfn(value))
return value;
}
return undefined;
}
filter(callbackfn) {
const result = [];
for (const value of this.values()) {
if (callbackfn(value))
result.push(value);
}
return result;
}
map(callbackfn) {
const result = [];
for (const value of this.values()) {
result.push(callbackfn(value));
}
return result;
}
forEach(callbackfn) {
for (const value of this.values()) {
callbackfn(value);
}
}
some(callbackfn) {
for (const value of this.values()) {
if (callbackfn(value))
return true;
}
return false;
}
every(callbackfn) {
for (const value of this.values()) {
if (!callbackfn(value))
return false;
}
return true;
}
[Symbol.iterator]() {
return this.values();
}
get size() {
return this.cache.reduce((acc, arr) => acc + arr.length, 0);
}
trySave(values, swap = []) {
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);
}
}
load() {
this.currentKeyIndex = world.getDynamicProperty(this.indexKey) ?? 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) ?? '[]');
}
this.cacheLoaded = true;
}
unload() {
this.cache.length = 0;
this.cacheLoaded = false;
this.currentKeyIndex = 0;
}
get keyPrefix() {
return `${ArrayDatabase.PREFIX}:${this.id}`;
}
get currentKey() {
return `${this.keyPrefix}${this.currentKeyIndex}`;
}
get indexKey() {
return `${ArrayDatabase.PREFIX}:index_${this.id}`;
}
get [Symbol.toStringTag]() {
return this.id;
}
}
ArrayDatabase.PROPERTY_MAX_SIZE = 12000;
ArrayDatabase.PREFIX = 'array';