Compare commits

...

3 commits

Author SHA1 Message Date
499a1eb440
Merge branch 'main' of ssh://localhost:3322/9pfs/countify
All checks were successful
/ node-check (push) Successful in 3s
/ prettier (push) Successful in 21s
/ tests (push) Successful in 26s
/ boot (push) Successful in 26s
2024-06-01 22:42:17 +00:00
0b67b3e3e8
Cache namespace IDs in namespace_id_cache
All checks were successful
/ node-check (pull_request) Successful in 3s
/ boot (pull_request) Successful in 10s
/ prettier (pull_request) Successful in 23s
/ tests (pull_request) Successful in 26s
2024-06-01 21:08:35 +00:00
b9f41165e0
Rename methods for consistency
All checks were successful
/ prettier (pull_request) Successful in 16s
/ node-check (pull_request) Successful in 3s
/ tests (pull_request) Successful in 25s
/ boot (pull_request) Successful in 9s
/ node-check (push) Successful in 3s
/ prettier (push) Successful in 15s
/ boot (push) Successful in 14s
/ tests (push) Successful in 24s
2024-06-01 20:13:58 +00:00

View file

@ -4,6 +4,7 @@ import { unlink } from "fs/promises";
import { randomUUID } from "crypto";
const app = express();
const valid_regex = /^[A-Za-z0-9_\-\.]{3,64}$/;
var namespace_id_cache = {};
var stats = {
keys_created: 0,
keys_updated: 0,
@ -18,6 +19,7 @@ app.use((req, res, next) => {
async function get_namespace_id(name, create = true) {
return await sql.begin(async (sql) => {
if (!valid_regex.test(name)) return null; // For sanity.
if (namespace_id_cache[name] != undefined) return namespace_id_cache[name];
if (create) {
var potential_res = await sql`
INSERT INTO namespaces(name) VALUES (${name}) ON CONFLICT DO NOTHING RETURNING id;
@ -26,13 +28,15 @@ async function get_namespace_id(name, create = true) {
if (!create || potential_res?.length == 0) {
var e = await sql`SELECT id FROM namespaces WHERE name = ${name} LIMIT 1`;
if (e.length == 0) return null;
namespace_id_cache[name] = e[0].id;
return e[0].id;
} else {
namespace_id_cache[name] = potential_res[0].id;
return potential_res[0].id;
}
});
}
async function add_counter(opts) {
async function counter_create(opts) {
const {
key,
value,
@ -68,7 +72,7 @@ async function add_counter(opts) {
}
}
async function get_counter_value(ns_name, key, return_id = false) {
async function counter_get(ns_name, key, return_id = false) {
const nsid = await get_namespace_id(ns_name, false);
if (nsid == null) {
if (!return_id) return null;
@ -98,7 +102,7 @@ async function counter_increment(ns_name, key, offset, create = false) {
await sql`SELECT value FROM keys WHERE name = ${key} AND namespace_id = ${nsid}`;
if (existence_res.length == 0) {
if (
!(await add_counter({
!(await counter_create({
namespace: ns_name,
key: key,
value: 1,
@ -137,7 +141,7 @@ async function counter_update(ns_name, key, amount) {
}
async function counter_set(ns_name, key, value) {
var { old_val: value, nsid } = await get_counter_value(ns_name, key, true);
var { old_val: value, nsid } = await counter_get(ns_name, key, true);
if (old_val == null) return { old_value: null, value: null, failed: true };
return await sql.begin(async (sql) => {
var changeable = (
@ -208,7 +212,7 @@ app.get("/hit/:key", async function (req, res) {
.json({ value: counter_res });
});
app.get("/update/:ns/:key", async function (req, res) {
var old_val = await get_counter_value(req.params.ns, req.params.key);
var old_val = await counter_get(req.params.ns, req.params.key);
var counter_res = await counter_increment(
req.params.ns,
req.params.key,
@ -221,7 +225,7 @@ app.get("/update/:ns/:key", async function (req, res) {
.json({ value: counter_res });
});
app.get("/update/:key", async function (req, res) {
var old_val = await get_counter_value("default", req.params.key);
var old_val = await counter_get("default", req.params.key);
var counter_res = await counter_increment(
"default",
req.params.key,
@ -274,11 +278,11 @@ app.get("/info/:key", async function (req, res) {
return res.status(e.value == null ? 404 : 200).json(e);
});
app.get("/get/:ns/:key", async function (req, res) {
var e = await get_counter_value(req.params.ns, req.params.key);
var e = await counter_get(req.params.ns, req.params.key);
return res.status(e == null ? 404 : 200).json({ value: e });
});
app.get("/get/:key", async function (req, res) {
var e = await get_counter_value("default", req.params.key);
var e = await counter_get("default", req.params.key);
return res.status(e == null ? 404 : 200).json({ value: e });
});
app.get("/create", async function (req, res) {
@ -292,8 +296,8 @@ app.get("/create", async function (req, res) {
if (!sane_query?.update_lowerbound) sane_query.update_lowerbound = "-1";
if (!sane_query?.update_upperbound) sane_query.update_upperbound = "1";
if (!sane_query?.value) sane_query.value = "0";
const add_counter_res = await add_counter(sane_query);
if (add_counter_res == true) {
const counter_create_res = await counter_create(sane_query);
if (counter_create_res == true) {
return res.json({
namespace: sane_query.namespace,
key: sane_query.key,