You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
176 lines
4.3 KiB
JavaScript
176 lines
4.3 KiB
JavaScript
import crypto from 'crypto'
|
|
|
|
import { readFile, writeFile, access, constants } from 'fs/promises'
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
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, 4))
|
|
}
|
|
|
|
console.log(`[Database] Loading database from "${path}"`)
|
|
const state = JSON.parse(await readFile(path, 'utf-8'))
|
|
|
|
const result = await fn(state)
|
|
|
|
console.log(`[Database] Saving database to "${path}"`)
|
|
await writeFile(path, JSON.stringify(state, null, 4))
|
|
|
|
unlock()
|
|
|
|
return result
|
|
}
|
|
|
|
export const getUsers = db =>
|
|
withDatabase(db, state => {
|
|
return Object.values(state.users)
|
|
})
|
|
|
|
export const getUser = (db, id) =>
|
|
withDatabase(db, state => {
|
|
return state.users[id] ?? null
|
|
})
|
|
|
|
// 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 createProblem = (db, { content, createdBy }) =>
|
|
withDatabase(db, state => {
|
|
const nextId = (
|
|
Object.keys(state.problems)
|
|
.map(k => parseInt(k))
|
|
.reduce((acc, id) => Math.max(acc, id)) + 1
|
|
).toString()
|
|
|
|
state.problems[nextId] = {
|
|
id: nextId,
|
|
content,
|
|
createdBy,
|
|
createdAt: new Date().toJSON(),
|
|
}
|
|
|
|
return nextId
|
|
})
|
|
|
|
export const getProblem = (db, id) =>
|
|
withDatabase(db, state => {
|
|
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 getSolution = (db, id) =>
|
|
withDatabase(db, state => {
|
|
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
|
|
})
|