Skip to content

Commit ca67071

Browse files
authored
QuickDB | Optimize
1 parent 9bad193 commit ca67071

File tree

1 file changed

+82
-87
lines changed

1 file changed

+82
-87
lines changed

scripts/quick-db/index.js

Lines changed: 82 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -6,97 +6,92 @@ import { world, World } from "@minecraft/server";
66
const DATABASE_PREFIX = "\u0235\u0235";
77

88
const {
9-
getDynamicProperty: GET,
10-
setDynamicProperty: SET,
11-
getDynamicPropertyIds: IDS
9+
getDynamicProperty: GET,
10+
setDynamicProperty: SET,
11+
getDynamicPropertyIds: IDS
1212
} = World.prototype;
1313

14-
//adapt code to JalyDev/scriptAPI
1514
class QuickDB {
16-
#identifier;
17-
constructor(id) {
18-
this.#identifier = `${DATABASE_PREFIX}${id}${DATABASE_PREFIX}`;
19-
}
20-
21-
get size() {
22-
return IDS.call(world).filter((id) => id.startsWith(this.#identifier))
23-
.length;
24-
}
25-
26-
has(key) {
27-
return !!(
28-
GET.call(world, `${this.#identifier}${key}`) &&
29-
GET.call(world, `${this.#identifier}${key}`) !== undefined
30-
);
31-
}
32-
33-
get(key) {
34-
return this.has(key)
35-
? JSON.parse(GET.call(world, `${this.#identifier}${key}`))
36-
: undefined;
37-
}
38-
39-
set(key, value) {
40-
if (typeof key !== "string") return false;
41-
SET.call(world, `${this.#identifier}${key}`, JSON.stringify(value));
42-
return true;
43-
}
44-
45-
delete(key) {
46-
if (!this.has(key)) return false;
47-
SET.call(world, `${this.#identifier}${key}`, undefined);
48-
return true;
49-
}
50-
51-
keys() {
52-
return Array.from(this.#UIDX("keys"));
53-
}
54-
55-
values() {
56-
return Array.from(this.#UIDX("values"));
57-
}
58-
59-
entries() {
60-
return Array.from(this.#UIDX("entries"));
61-
}
62-
63-
#UIDX(type) {
64-
const ids = this.getIds();
65-
let u_idx = 0;
66-
const len = ids.length;
67-
68-
return function* () {
69-
while (u_idx < len) {
70-
const id = ids[u_idx];
71-
const key = id.split(this.#identifier)[1];
72-
const value = this.get(key);
73-
switch (type) {
74-
case "key":
75-
yield key;
76-
break;
77-
case "value":
78-
yield this.has(key) ? JSON.parse(value) : undefined;
79-
break;
80-
case "entries":
81-
yield [key, JSON.parse(value)];
82-
break;
83-
}
84-
u_idx++;
85-
}
86-
}.bind(this)();
87-
}
88-
89-
getIds() {
90-
return world
91-
.getDynamicPropertyIds()
92-
.filter((id) => id.startsWith(this.#identifier));
93-
}
94-
95-
clear() {
96-
for (const id of this.getIds()) {
97-
this.delete(id.replace(this.#identifier,""));
98-
}
15+
#identifier;
16+
constructor(id) {
17+
this.#identifier = `${DATABASE_PREFIX}${id}${DATABASE_PREFIX}`;
18+
19+
const IDDS = this.getIds();
20+
let length_ = IDDS.length;
21+
this.__cache = {};
22+
while (length_--) {
23+
const key = IDDS[length_].replace(this.#identifier, "");
24+
let value = GET.call(world, this.#identifier + key);
25+
if (typeof value == "string" && value.startsWith("obj"))
26+
this.__cache[key] = JSON.parse(value.slice(3));
27+
this.__cache[key] = value;
9928
}
29+
}
30+
31+
get size() {
32+
return this.keys().length;
33+
}
34+
35+
keys() {
36+
return Object.keys(this.__cache);
37+
}
38+
39+
values() {
40+
return Object.values(this.__cache);
41+
}
42+
43+
entries() {
44+
return Object.entries(this.__cache);
45+
}
46+
47+
set(key, value) {
48+
if (!key) throw new Error("pls type the key!!");
49+
if (value.length > 0 || value !== undefined || value !== undefined)
50+
SET.call(world, this.#identifier + String(key), JSON.stringify(value));
51+
else SET.call(world, this.#identifier + String(key));
52+
return true;
53+
}
54+
55+
delete(key) {
56+
if (!this.has(key)) return false;
57+
SET.call(world, `${this.#identifier}${String(key)}`);
58+
return true;
59+
}
60+
61+
get(key) {
62+
if (!key) throw new Error("pls type the key!!");
63+
return this.__cache?.[key];
64+
}
65+
66+
has(key) {
67+
return !!this.get(key);
68+
}
69+
70+
static get ids() {
71+
return [
72+
...new Set(
73+
IDS.call(world)
74+
.filter((id) => id.startsWith(DATABASE_PREFIX))
75+
.map((k) => k.slice(DATABASE_PREFIX.length).split(DATABASE_PREFIX)[0])
76+
)
77+
];
78+
}
79+
getIds() {
80+
return IDS.call(world).filter((id) => id.startsWith(this.#identifier));
81+
}
82+
83+
clear() {
84+
let length_ = this.size;
85+
while (length_--) this.delete(this.keys()[length_]);
86+
this.__cache = {};
87+
}
88+
89+
static clearAll() {
90+
for (const real_id of IDS.call(world).filter((id) =>
91+
id.startsWith(DATABASE_PREFIX)
92+
))
93+
SET.call(world, real_id);
94+
}
10095
}
10196

10297
export default QuickDB;

0 commit comments

Comments
 (0)