Skip to content

Commit

Permalink
add rave-level support #77
Browse files Browse the repository at this point in the history
  • Loading branch information
mfornos committed May 28, 2024
1 parent 344dd31 commit 4079380
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"level": "^8.0.1",
"lru-cache": "^10.2.2",
"prom-client": "^15.1.2",
"rave-level": "^1.0.0",
"redis": "^4.6.14",
"rfc6902": "^5.1.1",
"toml": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from './services/index.js'
import version from './version.js'

import { toCorsOpts } from './args.js'
import { toCorsOpts } from './cli/args.js'
import {
$BaseServerOptions,
$ConfigServerOptions,
Expand Down
30 changes: 23 additions & 7 deletions packages/server/src/services/persistence/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { FastifyPluginAsync } from 'fastify'
import { FastifyInstance, FastifyPluginAsync } from 'fastify'
import fp from 'fastify-plugin'

import { Level } from 'level'
import { MemoryLevel } from 'memory-level'
import { RaveLevel } from 'rave-level'

import { DB } from '../types.js'
import { DB, LevelEngine } from '../types.js'
import { Janitor, JanitorOptions } from './janitor.js'
import { Scheduler, SchedulerOptions } from './scheduler.js'

Expand All @@ -17,7 +20,24 @@ declare module 'fastify' {
type DBOptions = JanitorOptions &
SchedulerOptions & {
data: string
levelEngine: LevelEngine
}

function createLevel({ log }: FastifyInstance, { data, levelEngine }: DBOptions): Level {
const dbPath = data || './.db'

log.info('Level engine %s', levelEngine)
log.info('Open database at %s', dbPath)

switch (levelEngine) {
case LevelEngine.mem:
return new MemoryLevel() as Level
case LevelEngine.rave:
return new RaveLevel(dbPath) as Level
default:
return new Level(dbPath)
}
}

/**
* Persistence related services.
Expand All @@ -26,11 +46,7 @@ type DBOptions = JanitorOptions &
* @param options - The persistence options
*/
const persistencePlugin: FastifyPluginAsync<DBOptions> = async (fastify, options) => {
const dbPath = options.data || './db'

fastify.log.info(`Open database at ${dbPath}`)

const root = new Level(dbPath)
const root = createLevel(fastify, options)
const scheduler = new Scheduler(fastify.log, root, options)
const janitor = new Janitor(fastify.log, root, scheduler, options)

Expand Down
9 changes: 9 additions & 0 deletions packages/server/src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ export type DB<F = Buffer | Uint8Array | string, K = string, V = any> = Abstract
export type Family<F = Buffer | Uint8Array | string, K = string, V = any> = AbstractSublevel<DB, F, K, V>
export type BatchOperation<K = string, V = any> = AbstractBatchOperation<DB, K, V>

/**
* Supported Abstract Level engines.
*/
export enum LevelEngine {
classic = 'classic',
rave = 'rave',
mem = 'mem',
}

/**
* Sublevel prefixes.
*/
Expand Down
3 changes: 3 additions & 0 deletions packages/server/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { z } from 'zod'

import { LevelEngine } from './services/types.js'

export const $BaseServerOptions = z.object({
port: z.number().min(0),
address: z.string().min(1),
Expand All @@ -17,6 +19,7 @@ export const $LevelServerOptions = z.object({
required_error: 'Database directory path is required',
})
.min(1),
levelEngine: z.nativeEnum(LevelEngine).default(LevelEngine.classic),
scheduler: z.boolean().default(true),
schedulerFrequency: z.number().min(1000),
sweepExpiry: z.number().min(20000),
Expand Down
85 changes: 82 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2554,6 +2554,7 @@ __metadata:
patch-package: "npm:^8.0.0"
pino-pretty: "npm:^11.1.0"
prom-client: "npm:^15.1.2"
rave-level: "npm:^1.0.0"
redis: "npm:^4.6.14"
rfc6902: "npm:^5.1.1"
toml: "npm:^3.0.0"
Expand Down Expand Up @@ -2974,7 +2975,7 @@ __metadata:
languageName: node
linkType: hard

"abstract-level@npm:^1.0.0, abstract-level@npm:^1.0.2, abstract-level@npm:^1.0.4":
"abstract-level@npm:^1.0.0, abstract-level@npm:^1.0.2, abstract-level@npm:^1.0.3, abstract-level@npm:^1.0.4":
version: 1.0.4
resolution: "abstract-level@npm:1.0.4"
dependencies:
Expand Down Expand Up @@ -3240,6 +3241,13 @@ __metadata:
languageName: node
linkType: hard

"b4a@npm:^1.6.0":
version: 1.6.6
resolution: "b4a@npm:1.6.6"
checksum: 10c0/56f30277666cb511a15829e38d369b114df7dc8cec4cedc09cc5d685bc0f27cb63c7bcfb58e09a19a1b3c4f2541069ab078b5328542e85d74a39620327709a38
languageName: node
linkType: hard

"babel-jest@npm:^29.7.0":
version: 29.7.0
resolution: "babel-jest@npm:29.7.0"
Expand Down Expand Up @@ -3962,7 +3970,7 @@ __metadata:
languageName: node
linkType: hard

"duplexify@npm:^4.1.2":
"duplexify@npm:^4.1.1, duplexify@npm:^4.1.2":
version: 4.1.3
resolution: "duplexify@npm:4.1.3"
dependencies:
Expand Down Expand Up @@ -5973,6 +5981,17 @@ __metadata:
languageName: node
linkType: hard

"length-prefixed-stream@npm:^2.0.0":
version: 2.0.0
resolution: "length-prefixed-stream@npm:2.0.0"
dependencies:
inherits: "npm:^2.0.3"
readable-stream: "npm:^3.1.1"
varint: "npm:^5.0.0"
checksum: 10c0/c8cc4124439b2c58d8ab3b892edc76e2e7dafa5ea27e29d47236df271ae07e8d94b2b94b6b6bfcc212be27984a4940c8daf91c6b76e3bd4cbe6ac0d9fc0118e1
languageName: node
linkType: hard

"level-supports@npm:^4.0.0":
version: 4.0.1
resolution: "level-supports@npm:4.0.1"
Expand Down Expand Up @@ -6160,6 +6179,20 @@ __metadata:
languageName: node
linkType: hard

"many-level@npm:^1.0.1":
version: 1.0.1
resolution: "many-level@npm:1.0.1"
dependencies:
abstract-level: "npm:^1.0.3"
duplexify: "npm:^4.1.1"
end-of-stream: "npm:^1.1.0"
length-prefixed-stream: "npm:^2.0.0"
module-error: "npm:^1.0.2"
protocol-buffers-encodings: "npm:^1.1.0"
checksum: 10c0/135e98aa0b48885a3cc640aafcb0b456b39ecdc86a48584e8ce7b59d565e2a52dec9f95301cca3a554f6f20501bd9588339d0536a82bd746799ca79f1bb6f1ea
languageName: node
linkType: hard

"marked@npm:^4.3.0":
version: 4.3.0
resolution: "marked@npm:4.3.0"
Expand Down Expand Up @@ -7036,6 +7069,17 @@ __metadata:
languageName: node
linkType: hard

"protocol-buffers-encodings@npm:^1.1.0":
version: 1.2.0
resolution: "protocol-buffers-encodings@npm:1.2.0"
dependencies:
b4a: "npm:^1.6.0"
signed-varint: "npm:^2.0.1"
varint: "npm:5.0.0"
checksum: 10c0/c7098aec3b8125a05911d841d58bdd5c6039570efa9f90721c58b314a508839a7445a7e8fe3ddb780081595ab20af5b2bf4c93646c0e442c47dfa93421b29b12
languageName: node
linkType: hard

"proxy-addr@npm:^2.0.7":
version: 2.0.7
resolution: "proxy-addr@npm:2.0.7"
Expand Down Expand Up @@ -7098,14 +7142,26 @@ __metadata:
languageName: node
linkType: hard

"rave-level@npm:^1.0.0":
version: 1.0.0
resolution: "rave-level@npm:1.0.0"
dependencies:
classic-level: "npm:^1.2.0"
many-level: "npm:^1.0.1"
module-error: "npm:^1.0.2"
readable-stream: "npm:^3.6.0"
checksum: 10c0/f08244618f1e31c0ecc12428b037fd7f542a25cb6e406dd878757c4fbb021946ebc63f91b1b97706536940158a8108e6964a9c254d6f38263f3d7fd53004e7b3
languageName: node
linkType: hard

"react-is@npm:^18.0.0":
version: 18.2.0
resolution: "react-is@npm:18.2.0"
checksum: 10c0/6eb5e4b28028c23e2bfcf73371e72cd4162e4ac7ab445ddae2afe24e347a37d6dc22fae6e1748632cd43c6d4f9b8f86dcf26bf9275e1874f436d129952528ae0
languageName: node
linkType: hard

"readable-stream@npm:^3.1.1":
"readable-stream@npm:^3.1.1, readable-stream@npm:^3.6.0":
version: 3.6.2
resolution: "readable-stream@npm:3.6.2"
dependencies:
Expand Down Expand Up @@ -7537,6 +7593,15 @@ __metadata:
languageName: node
linkType: hard

"signed-varint@npm:^2.0.1":
version: 2.0.1
resolution: "signed-varint@npm:2.0.1"
dependencies:
varint: "npm:~5.0.0"
checksum: 10c0/3cca068d6c97ffc487021ac7efd0192d5b0fc683cd6d9e92c8b9f182044b57fe121a1eff3e357ef040a84ecb74ce48e1c4148a7a15752ad1d429ba12714eaabb
languageName: node
linkType: hard

"simple-update-notifier@npm:^2.0.0":
version: 2.0.0
resolution: "simple-update-notifier@npm:2.0.0"
Expand Down Expand Up @@ -8274,6 +8339,20 @@ __metadata:
languageName: node
linkType: hard

"varint@npm:5.0.0":
version: 5.0.0
resolution: "varint@npm:5.0.0"
checksum: 10c0/d7621f24de3513f54d60f8826ca9f299cbe496ec18cca79bf8816932a2808a21c055f4f129851b37c28792e3e376ec2d795be92a22c5cf6ed471312c1ed4a7bb
languageName: node
linkType: hard

"varint@npm:^5.0.0, varint@npm:~5.0.0":
version: 5.0.2
resolution: "varint@npm:5.0.2"
checksum: 10c0/a8e6c304cb140389cc56f14c808cd2ad4764d81f8afdaf4e49e9804231f2a62d9443098dba6b1249b0bd160b823fc7886d51e1cb0fca54209f842310d1d2591d
languageName: node
linkType: hard

"vscode-oniguruma@npm:^1.7.0":
version: 1.7.0
resolution: "vscode-oniguruma@npm:1.7.0"
Expand Down

0 comments on commit 4079380

Please sign in to comment.