-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayDatabase.js
More file actions
163 lines (163 loc) · 4.76 KB
/
Copy pathArrayDatabase.js
File metadata and controls
163 lines (163 loc) · 4.76 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
import { world } from '@minecraft/server';
/**
* T: The type of the value used in the database
* U: (optional) The type of the value stored in the world
*/
export class ArrayDatabase {
constructor(id, options = {}) {
this.id = id;
this.PROPERTY_MAX_SIZE = 12000;
this.PREFIX = 'array';
this.cache = [];
this.cacheLoaded = false;
this.currentKeyIndex = 0;
if (options.PROPERTY_MAX_SIZE)
this.PROPERTY_MAX_SIZE = options.PROPERTY_MAX_SIZE;
if (options.PREFIX)
this.PREFIX = options.PREFIX;
if (options.transformer)
this.transformer = options.transformer;
}
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 = [], index = this.currentKeyIndex) {
let sizeOK = true;
const stringified = JSON.stringify(this.transformer ? values.map(this.transformer.onWrite) : values);
if (stringified.length > this.PROPERTY_MAX_SIZE) {
sizeOK = false;
}
else {
try {
world.setDynamicProperty(`${this.keyPrefix}${index}`, stringified);
this.cache[index] = 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(`[ArrayDatabase:${this.id}] Found invalid key: ${key}`);
continue;
}
const parsed = JSON.parse(world.getDynamicProperty(key) ?? '[]');
this.cache[index] = this.transformer ? parsed.map(this.transformer.onRead) : parsed;
}
this.cacheLoaded = true;
}
unload() {
this.cache.length = 0;
this.cacheLoaded = false;
this.currentKeyIndex = 0;
}
get keyPrefix() {
return `${this.PREFIX}:${this.id}`;
}
get indexKey() {
return `${this.PREFIX}:index_${this.id}`;
}
get [Symbol.toStringTag]() {
return this.id;
}
}