Skip to content

Commit

Permalink
Merge pull request #27 from bolorundurovj/optimizations
Browse files Browse the repository at this point in the history
Optimizations
  • Loading branch information
bolorundurovj authored May 16, 2022
2 parents 17dce22 + 91ba335 commit c6a3776
Show file tree
Hide file tree
Showing 15 changed files with 1,787 additions and 1,126 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</div>
<div align="center">
<a href="/LICENSE">
<img alt="License GPLv3" src="https://img.shields.io/badge/license-BSD-blue.svg" />
<img alt="License GPLv3" src="https://img.shields.io/badge/license-BSD--3--Clause-blue.svg" />
</a>
<a href="https://www.npmjs.com/package/akivadb">
<img alt="NPM" src="https://img.shields.io/npm/v/akivadb?label=npm">
Expand Down
69 changes: 60 additions & 9 deletions lib/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/main.js.map

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
"build": "npx tsc",
"repl": "node repl/index.js repl",
"test": "node tests/test.js",
"benchmark:time": "node tests/benchmarks/time.js",
"bench:ops": "tsc && node tests/benchmarks/benchmarks.js",
"bench:rps": "tsc && node tests/benchmarks/benchmark.js"
"benchmark:time": "tsc && node tests/benchmarks/time.js",
"bench:ops": "tsc && node --max-old-space-size=7192 tests/benchmarks/benchmarks.js",
"bench:rps": "tsc && node --max-old-space-size=7192 tests/benchmarks/benchmark.js",
"bench:op": "tsc && node --max-old-space-size=7192 tests/benchmarks/benchmarksn.js"
},
"repository": {
"type": "git",
Expand All @@ -23,15 +24,18 @@
},
"homepage": "https://github.com/bolorundurovj/akivaDB",
"devDependencies": {
"@types/lodash": "^4.14.179",
"@types/node": "^17.0.18",
"benchmark": "^2.1.4",
"benchmarkify": "^3.0.0",
"jest": "27.4.5",
"nodemark": "^0.3.0",
"standard": "^16.0.4",
"typescript": "^4.5.5"
},
"dependencies": {
"@chronocide/dot-obj": "^1.3.1",
"fast-deep-equal": "^3.1.3"
"fast-deep-equal": "^3.1.3",
"lodash.chunk": "^4.2.0"
}
}
20 changes: 20 additions & 0 deletions pages/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>AkivaDB Login</title>
</head>
<style>
h1 {
font-weight: lighter;
}
</style>
<body>
<h1>Login</h1>
</body>
<script>
console.log(1);
</script>
</html>
80 changes: 69 additions & 11 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
} from "./errors";
import { generateUIDByTimestamp } from "./idGenerator";
import { AkivaDBError } from "./errorHandler";
import * as _ from "lodash";

export default class AkivaDB<T extends object> extends EventEmitter {
readonly root?: string;
Expand Down Expand Up @@ -99,6 +100,15 @@ export default class AkivaDB<T extends object> extends EventEmitter {
return doc;
}

private addMany(docs: Array<DocPrivate<T>>) {
_.forEach(docs, (doc) => {
this.indexes.add(doc._id);
_.set(this.map, doc._id, doc);
});

return docs;
}

private get(_id: string): DocPrivate<T> | null {
const doc = this.map[_id];
return !doc?.$deleted ? doc : null;
Expand Down Expand Up @@ -182,6 +192,21 @@ export default class AkivaDB<T extends object> extends EventEmitter {
if (!this.file) throw new AkivaDBError(MEMORY_MODE("persist"), 3);

const data: string[] = [];
// _.forEach(this.indexes, (_id: string) => {
// try {
// const doc = this.get(_id);
// if (doc) {
// data.push(JSON.stringify(doc));
// }
// } catch (err) {
// this.remove(_id);

// if (strict) {
// throw err;
// }
// }
// });

this.indexes.forEach((_id) => {
try {
const doc = this.get(_id);
Expand Down Expand Up @@ -251,17 +276,8 @@ export default class AkivaDB<T extends object> extends EventEmitter {
*/
insertMany(docs: OneOrMore<Doc<T>>, options?: { strict?: boolean }) {
{
return Promise.all(
toArray(docs).map((newDoc) => this.insert(newDoc, options))
).then((docs) => {
const arr = docs.reduce<DocPrivate<T>[]>((acc, doc) => {
if (doc !== null) {
acc.push(doc);
}
return acc;
}, []);
return arr;
});
docs = toArray(docs);
return Promise.resolve(this._addManyAndEmit(docs));
}
}

Expand Down Expand Up @@ -550,6 +566,26 @@ export default class AkivaDB<T extends object> extends EventEmitter {
return x;
};

/**
* Add documents to database and emit `insert`
* @param {Array<Doc<T>>} docs Documents
* @returns {Array<DocPrivate<T>>} docs
*/
private _addManyAndEmit = (docs: Array<Doc<T>>): Array<DocPrivate<T>> => {
const arr = _.map(docs, (x) => {
return _.assign(x, {
_id: !!x._id ? x._id.toString() : generateUIDByTimestamp(),
});
});
this.addMany(arr);

this.emit("insertMany", arr);
if (this.inMemory == false) {
this.persist();
}
return arr;
};

/**
* Mark document as deleted against persistence.
* @param doc document
Expand Down Expand Up @@ -590,6 +626,7 @@ export default class AkivaDB<T extends object> extends EventEmitter {
* @param {DocPrivate<T>} doc Document
* @param {Update<T>} update
* @returns {DocPrivate<T>} document
* @todo update only passed fields not entire document
*/
private _updateDoc(doc: DocPrivate<T>, update: Update<T>) {
const newDoc = isModifier(update)
Expand All @@ -605,3 +642,24 @@ export default class AkivaDB<T extends object> extends EventEmitter {
return newDoc;
}
}

export const mid = (req, res, next) => {
if (
/^\/akivadb/i.test(String(req.path)) &&
/^\/akivadb/i.test(String(req.originalUrl))
) {
if (
req.method == "GET" &&
/^\/akivadb\/login/i.test(String(req.path)) &&
/^\/akivadb\/login/i.test(String(req.originalUrl))
) {
res.send("post login");
} else {
const file = path.join(__dirname, "..", "pages", "login.html");
const content = fs.readFileSync(file, { encoding: "utf8", flag: "r" });
res.send(content);
}
}

next();
};
19 changes: 11 additions & 8 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ classes.on("insert", (x) => {
updateObj = x;
console.log(x._id, "insert");
});
classes.on("insertMany", (x) => {
console.log(x, "insertMany");
});
classes.on("delete", (x) => {
console.log(x._id, "delete");
});
classes.on("update", (x) => {
console.log(x._id, "update");
});

let y,
x = 10;
let x = 10;
while (x > 0) {
classes
.insert({
Expand All @@ -41,6 +43,7 @@ while (x > 0) {
x--;
}

let y =10;
let arr = Array.from({ length: y }, (x, i) => ({
name: `Class ${y}`,
students: i + 1,
Expand All @@ -66,12 +69,12 @@ classes
// })

updateObj.name = `Jane Doe - new`;
classes.updateById(updateObj._id, updateObj).then((a) => {
classes.updateById(updateObj._id, {name: `Jane Doe - new`}).then((a) => {
// console.log(a);
});

updateObj.students = 1e4;
classes.updateOne({students: updateObj.students}, updateObj).then((a) => {
classes.updateOne({students: updateObj.students}, {name: `Jane Doe - x`}).then((a) => {
// console.log(a);
});

Expand All @@ -95,13 +98,13 @@ classes.deleteOne({ name: "Class 10" }).then((x) => {
console.log(x);
});

// classes.deleteMany().then(x => {
// // console.log(x);
// })
classes.deleteMany().then(x => {
console.log(x);
})

// classes.drop()
classes.updateMany(
{ name: "Jane Doe - 1" },
{ name: "Jane Doe - new" },
{ name: "AkivaDB is awesome" }
).then((x) => {
console.log(x);
Expand Down
7 changes: 3 additions & 4 deletions tests/benchmarks/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ const Benchmarkify = require("benchmarkify");
const benchmark = new Benchmarkify("Benchmarks").printHeader();

const suite = benchmark.createSuite("Benchmark OPS");
const db = new AkivaDB({
name: "benchmark",
root: "testdbs",
});
const db = new AkivaDB();

console.log("Memory Mode: ", db.memoryMode);

console.group("\nBenchmark");
suite
Expand Down
Loading

0 comments on commit c6a3776

Please sign in to comment.