Skip to content
Snippets Groups Projects
Commit 14be1e66 authored by mcmlxxix's avatar mcmlxxix
Browse files

add modopts.js support

some minor updates
parent 3a85f064
Branches
Tags
No related merge requests found
...@@ -42,6 +42,9 @@ function JSONdb (fileName) { ...@@ -42,6 +42,9 @@ function JSONdb (fileName) {
else else
this.file=undefined; this.file=undefined;
/* load modopts */
this.options = load("modopts.js","jsondb");
/* master database object */ /* master database object */
this.data={}; this.data={};
...@@ -63,8 +66,6 @@ function JSONdb (fileName) { ...@@ -63,8 +66,6 @@ function JSONdb (fileName) {
/* file read buffer */ /* file read buffer */
FILE_BUFFER:65535, FILE_BUFFER:65535,
/* autosave interval */
AUTO_SAVE: 300 /*seconds*/ *1000,
LAST_SAVE:-1, LAST_SAVE:-1,
UPDATES:false, UPDATES:false,
...@@ -411,13 +412,16 @@ function JSONdb (fileName) { ...@@ -411,13 +412,16 @@ function JSONdb (fileName) {
if(!timestamp) if(!timestamp)
timestamp = Date.now(); timestamp = Date.now();
/* if we are due for a data update, save everything to file */ /* if we are due for a data update, save everything to file */
if(timestamp - this.settings.LAST_SAVE >= this.settings.AUTO_SAVE) { if(timestamp - this.settings.LAST_SAVE >= (this.options.save_interval * 1000)) {
//TODO: create n backups before overwriting data file //TODO: create n backups before overwriting data file
this.file.open("w"); this.file.open("w");
// This function gets called every 30 seconds or so // This function gets called every 30 seconds or so
// And flushes all objects to disk in case of crash // And flushes all objects to disk in case of crash
// Also, this is called on clean exit. // Also, this is called on clean exit.
this.file.write(JSON.stringify(this.data,undefined,4)); if(this.options.readable)
this.file.write(JSON.stringify(this.data,undefined,'\t'));
else
this.file.write(JSON.stringify(this.data));
this.file.close(); this.file.close();
this.settings.LAST_SAVE=timestamp; this.settings.LAST_SAVE=timestamp;
this.settings.UPDATES=false; this.settings.UPDATES=false;
...@@ -445,7 +449,7 @@ function JSONdb (fileName) { ...@@ -445,7 +449,7 @@ function JSONdb (fileName) {
/* initialize autosave timer */ /* initialize autosave timer */
this.settings.LAST_SAVE = Date.now(); this.settings.LAST_SAVE = Date.now();
log(LOG_INFO,"JSON database initialized (v" + this.VERSION + ")"); log(LOG_INFO,"database initialized (v" + this.VERSION + ")");
} }
/* release any locks or subscriptions held by a disconnected client */ /* release any locks or subscriptions held by a disconnected client */
...@@ -642,11 +646,11 @@ function JSONdb (fileName) { ...@@ -642,11 +646,11 @@ function JSONdb (fileName) {
/* if the requested child object does not exist, create it */ /* if the requested child object does not exist, create it */
function verify(data,shadow,child_name) { function verify(data,shadow,child_name) {
if(!data[child_name]) { if(data[child_name] === undefined) {
log(LOG_DEBUG,"creating new data: " + child_name); log(LOG_DEBUG,"creating new data: " + child_name);
data[child_name]={}; data[child_name]={};
} }
if(!shadow[child_name]) { if(shadow[child_name] === undefined) {
log(LOG_DEBUG,"creating new shadow: " + child_name); log(LOG_DEBUG,"creating new shadow: " + child_name);
shadow[child_name]=new Shadow(); shadow[child_name]=new Shadow();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment