Skip to content

Instantly share code, notes, and snippets.

@kylebernhardy
Created June 21, 2021 15:12
Show Gist options
  • Save kylebernhardy/c8ca325452a9fb47114f5db2725036eb to your computer and use it in GitHub Desktop.
Save kylebernhardy/c8ca325452a9fb47114f5db2725036eb to your computer and use it in GitHub Desktop.
'use strict';
const fs = require('fs');
const path = require('path');
const lmdb = require('lmdb-store');
const cluster = require('cluster');
const ENV_NUM = 1;
const FORK_NUM = 64;
function openEnv(name, create = true) {
let environment_path = path.join(__dirname, 'data', name);
if(create) {
try {
fs.rmdirSync(environment_path, {recursive: true})
fs.mkdirSync(environment_path);
} catch (e) {
console.error(e);
}
}
let env_init = {
"path": environment_path,
"mapSize": 1024 * 1024 * 1024,
"maxReaders": 100,
"maxDbs":20,
sharedStructuresKey: Symbol.for('structures')
};
let env = lmdb.open(env_init);
env.dbis = {};
env.dbis['id'] = env.openDB('id', {
create: create,
dupSort: false,
useVersions: true
});
env.dbis['str'] = env.openDB('str', {
create: create,
dupSort: true,
useVersions: false
});
env.dbis['str2'] = env.openDB('str2', {
create: create,
dupSort: true,
useVersions: false
});
env.dbis['str3'] = env.openDB('str3', {
create: create,
dupSort: true,
useVersions: false
});
env.dbis['str4'] = env.openDB('str4', {
create: create,
dupSort: true,
useVersions: false
});
env.dbis['str5'] = env.openDB('str5', {
create: create,
dupSort: true,
useVersions: false
});
env.dbis['str5'] = env.openDB('str5', {
create: create,
dupSort: true,
useVersions: false
});
env.dbis['str6'] = env.openDB('str6', {
create: create,
dupSort: true,
useVersions: false
});
env.dbis['str7'] = env.openDB('str7', {
create: create,
dupSort: true,
useVersions: false
});
env.dbis['str8'] = env.openDB('str8', {
create: create,
dupSort: true,
useVersions: false
});
env.dbis['str9'] = env.openDB('str9', {
create: create,
dupSort: true,
useVersions: false
});
global.envs[name] = env;
}
if(cluster.isMaster){
global.envs = {};
for(let x = 0; x < ENV_NUM; x++){
openEnv(`env${x}`);
}
for(let x = 0; x < FORK_NUM; x++){
cluster.fork();
}
} else {
const {v4: uuidv4} = require('uuid');
const fastify = require('fastify')({
logger: false
})
global.envs = {};
for(let x = 0; x < ENV_NUM; x++){
openEnv(`env${x}`, false);
}
fastify.get('/:num', async function (request, reply) {
let num = request.params.num;
let env = global.envs[`env${num}`];
const id = uuidv4();
try {
await env.dbis['id'].transactionAsync(() => {
env.dbis['id'].put(id, {id: id, str: 'hello'});
env.dbis['str'].put('hello', id);
env.dbis['str2'].put('hello2', id);
env.dbis['str3'].put('hello3', id);
env.dbis['str4'].put('hello4', id);
env.dbis['str5'].put('hello5', id);
env.dbis['str6'].put('hello6', id);
env.dbis['str7'].put('hello7', id);
env.dbis['str8'].put('hello8', id);
env.dbis['str9'].put('hello9', id);
});
reply.send({success: true});
}catch(e){
reply.send({success: false});
}
})
fastify.listen(3000, '::', function (err, address) {
if (err) {
fastify.log.error(err)
process.exit(1)
}
fastify.log.info(`server listening on ${address}`)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment