Ora il db funge più veramente e si possono inviare i problemi

This commit is contained in:
2022-11-08 03:34:41 +01:00
parent 1452c0c143
commit 484c21d606
16 changed files with 519 additions and 144 deletions
+126 -26
View File
@@ -1,15 +1,72 @@
import crypto from 'crypto'
import { readFile, writeFile, access, constants } from 'fs/promises'
export function createDatabase(path, initialValue) {
return { path, initialValue }
function once(fn, message) {
let flag = false
return (...args) => {
if (flag) {
throw new Error(message ?? `cannot run more than once`)
}
flag = true
return fn(...args)
}
}
async function withDatabase({ path, initialValue }, fn) {
function createMutex() {
let locked = false
const waiters = []
const unlock = () => {
if (waiters.length > 0) {
console.log(`[Mutex] Passing lock to next in queue (of size ${waiters.length})`)
const resolve = waiters.shift()
resolve(once(unlock, `lock already released`))
} else {
locked = false
console.log(`[Mutex] Releasing the lock`)
}
}
const lock = () => {
if (locked) {
console.log(`[Mutex] Putting into queue`)
return new Promise(resolve => {
waiters.push(resolve)
})
} else {
console.log(`[Mutex] Acquiring the lock`)
locked = true
return once(unlock, `lock already released`)
}
}
return { lock }
}
// l = createLock()
// unlock1 = await l.lock() // immediate
// unlock2 = await l.lock() // hangs...
// unlock1() // l2 restarts...
// unlock2() // no waiters so noop
export function createDatabase(path, initialValue) {
return {
path,
initialValue,
mu: createMutex(),
}
}
async function withDatabase({ path, initialValue, mu }, fn) {
const unlock = await mu.lock()
try {
await access(path, constants.R_OK)
} catch (e) {
console.log(`[Database] Creating empty database into "${path}"`)
await writeFile(path, JSON.stringify(initialValue, null, 2))
await writeFile(path, JSON.stringify(initialValue, null, 4))
}
console.log(`[Database] Loading database from "${path}"`)
@@ -18,40 +75,83 @@ async function withDatabase({ path, initialValue }, fn) {
const result = await fn(state)
console.log(`[Database] Saving database to "${path}"`)
await writeFile(path, JSON.stringify(state, null, 2))
await writeFile(path, JSON.stringify(state, null, 4))
unlock()
return result
}
function createTable(tableName) {
return {
create() {},
get() {},
update() {},
delete() {},
}
}
export const User = createTable('users')
export const Problem = createTable('problems')
export const getUsers = db =>
withDatabase(db, state => {
return Object.values(state.users)
})
export const getUser = (db, id) =>
withDatabase(db, state => {
return state.users[id]
return state.users[id] ?? null
})
export const createUser = (db, { email, username, password }) =>
// export const createUser = (db, { email, username }) =>
// withDatabase(db, state => {
// state.users[username] = {
// email,
// username,
// }
// })
// export const updateUser = (db, username, { email, password }) =>
// withDatabase(db, state => {
// state.users[username] = {
// email,
// password,
// }
// })
//
// Problems
//
export const getProblem = (db, id) =>
withDatabase(db, state => {
state.users[email] = {
username,
password,
return state.problems[id]
})
export const getProblems = db =>
withDatabase(db, state => {
return Object.values(state.problems)
})
//
// Solutions
//
export const createSolution = (db, { userId, problemId, content }) =>
withDatabase(db, state => {
const id = crypto.randomBytes(10).toString('hex')
state.solutions[id] = {
id,
userId,
problemId,
content,
}
})
export const updateUser = (db, username, { email, password }) =>
export const getSolution = (db, id) =>
withDatabase(db, state => {
state.users[username] = {
email,
password,
}
return state.solutions[id]
})
export const getSolutions = (db, { userId, problemId } = {}) =>
withDatabase(db, state => {
let solutions = Object.values(state.solutions)
if (userId) {
solutions = solutions.filter(s => s.userId === userId)
}
if (problemId) {
solutions = solutions.filter(s => s.problemId === problemId)
}
return solutions
})