// Bunch of functions to make accessing rocksdb easier import RocksDB from "rocksdb"; let db: RocksDB; export type RocksDBBatch = ReturnType; export async function initDB(path: string) { db = new RocksDB(path); await new Promise((resolve, reject) => { db.open( { createIfMissing: true, }, (err) => { if (err) { reject(err); process.exit(1); } else { resolve(undefined); } } ); }); } export function getDB() { return db; } export function cleanDB() { db.close(console.info); } export function startBatch() { return db.batch(); } interface Category { id: number; name: string; parentId: number | null; url: string | null; // null if api used to scrape } export function insertCategory(category: Category, batch: RocksDBBatch) { return batch.put(`category:${category.id}`, JSON.stringify(category)); } interface Forum { id: number; name: string; categoryId: number | null; url: string | null; // null if api used to scrape } export function insertForum(forum: Forum, batch: RocksDBBatch) { return batch.put(`forum:${forum.id}`, JSON.stringify(forum)); } interface Thread { id: number; title: string; url: string; date: string; lastEditDate: string | null; // null if api used to scrape pages: number; categoryId: number | null; forumId: number | null; authorId: string | null; // null if api used to scrape and author is anon tags: { url: string | null; name: string }[] | null; // null if android api used to scrape otherwise url is null if web api } export function insertThread(thread: Thread, batch: RocksDBBatch) { return batch.put(`thread:meta:${thread.id}`, JSON.stringify(thread)); } interface Page { url: string; posts: number[]; pageNumber: number; } export function insertPage(page: Page, threadId: number, batch: RocksDBBatch) { return batch.put( `thread:page:${threadId}:${page.pageNumber}`, JSON.stringify(page) ); } interface Post { id: number; content: string; content_markdown?: string; // only used if api used to scrape creationDate: string; lastEditDate: string | null; // null if api used to scrape authorId: string | null; // null if api used to scrape and post author is anon pageNumber: number; } export function insertPost(post: Post, batch: RocksDBBatch) { return batch.put(`thread:post:${post.id}`, JSON.stringify(post)); } interface User { id: number | null; username: string; url: string | null; anonymous: boolean; anonPFPStyles: string | null; } export function insertUser(user: User, batch: RocksDBBatch) { let userKey; if (user.anonymous) { userKey = `user:anon:${[user.username, user.anonPFPStyles].join("|")}`; } else { userKey = `user:auth:${user.username}`; } return batch.put(userKey, JSON.stringify(user)); }