js-options: implementing housekeeping tasks (almost done)

This commit is contained in:
Eric van der Vlist 2021-05-01 00:34:15 +02:00
parent 429f44f57b
commit 1fee5c0083
2 changed files with 63 additions and 19 deletions

View File

@ -2,8 +2,10 @@ hello = function () {
include('options.js');
// Instanciation
var options = new Options(authorAccess, 'hello', 10);
/*
Packages.java.lang.System.out.println("<new Options(authorAccess, 'hello', 3)>");
var options = new Options(authorAccess, 'hello', 3);
Packages.java.lang.System.out.println("</new Options(authorAccess, 'hello', 3)");
// Set up a couple of options
options.setOption('Hello', 'World');
options.setOption('See you later', 'aligator');
@ -16,7 +18,9 @@ hello = function () {
}
// Do the same with file specific options
options.setSystemId(undefined, 2);
Packages.java.lang.System.out.println("<options.setSystemId(undefined, 1)>");
options.setSystemId(undefined, 1);
Packages.java.lang.System.out.println("</options.setSystemId(undefined, 1)>");
options.setSystemIdOption('Hello', 'aligator');
options.setSystemIdOption('See you later', 'World');
var myOptions = options.getSystemIdOptions();
@ -28,4 +32,4 @@ hello = function () {
// Display some file specific properties
Packages.java.lang.System.out.println('options.systemId: ' + options.systemId);
Packages.java.lang.System.out.println('options.systemIdDigest: ' + options.systemIdDigest);
*/}
}

View File

@ -12,11 +12,11 @@ Options = function (authorAccess, namespace, lifetime, __system__) {
// Consider namespaces as a SYSTEM option
if ( __system__ !== true ) {
this.systemOptions = new Options(authorAccess, Options.SYSTEM, null, true);
this.__cleanup__();
var namespaces = JSON.parse(this.systemOptions.getOption(Options.NAMESPACES, '{}'));
namespaces[ this.namespace] = this.expiration;
Packages.java.lang.System.out.println('Namespaces update for ' + this.namespace );
this.systemOptions.setOption(Options.NAMESPACES, JSON.stringify(namespaces));
this.__cleanup__();
}
}
@ -26,7 +26,7 @@ Options.HOUSEKEEPING = '__housekeeping__';
Options.HOUSEKEEPING_PERIOD = 1;
Options.getExpiration = function (lifetime) {
if (lifetime === undefined) {
if (lifetime == undefined) {
return null;
} else {
var d = new Date();
@ -50,6 +50,19 @@ Options.prototype.setOption = function (key, value) {
this.optionsStorage.setOption(key, value);
}
Options.prototype.deleteOption = function (key) {
this.optionsStorage.setOptionsDoctypePrefix(Options.SYSTEM);
var options = JSON.parse(this.optionsStorage.getOption(this.namespace, '{}'));
delete options[key];
if (Object.keys(options).length > 0 ){
this.optionsStorage.setOption(this.namespace, JSON.stringify(options));
} else {
this.optionsStorage.setOption(this.namespace, null);
}
this.optionsStorage.setOptionsDoctypePrefix(this.namespace);
this.optionsStorage.setOption(key, null);
}
Options.prototype.getOptionKeys = function () {
this.optionsStorage.setOptionsDoctypePrefix(Options.SYSTEM);
return JSON.parse(this.optionsStorage.getOption(this.namespace, '{}'));
@ -103,35 +116,42 @@ Options.prototype.setSystemId = function (systemID, lifetime) {
}
Options.prototype.setSystemIdOption = function (key, value) {
if (this.systemIdOptions === undefined) {
if (this.systemIdOptions == undefined) {
this.setSystemId();
}
this.systemIdOptions.setOption(key, value);
}
Options.prototype.deleteSystemIdOption = function (key) {
if (this.systemIdOptions == undefined) {
this.setSystemId();
}
this.systemIdOptions.deleteOption(key);
}
Options.prototype.getSystemIdOption = function (key, defaultValue) {
if (this.systemIdOptions === undefined) {
if (this.systemIdOptions == undefined) {
this.setSystemId();
}
return this.systemIdOptions.getOption(key, defaultValue);
}
Options.prototype.getSystemIdOptions = function () {
if (this.systemIdOptions === undefined) {
if (this.systemIdOptions == undefined) {
this.setSystemId();
}
return this.systemIdOptions.getOptions();
}
Options.prototype.getSystemOptionKeys = function () {
if (this.systemIdOptions === undefined) {
if (this.systemIdOptions == undefined) {
this.setSystemId();
}
return this.systemIdOptions.getSystemOptionKeys();
}
Options.prototype.getSystemOptions = function () {
if (this.systemIdOptions === undefined) {
if (this.systemIdOptions == undefined) {
this.setSystemId();
}
return this.systemIdOptions.getSystemOptions();
@ -145,31 +165,51 @@ Options.prototype.getSystemOptions = function () {
Options.prototype.__cleanup__ = function () {
// TODO: delete systemIds
var nextCleanup = this.systemOptions.getOption(Options.HOUSEKEEPING, null);
Packages.java.lang.System.out.println('nextCleanup: ' + nextCleanup);
if (nextCleanup === null) {
if (nextCleanup == null) {
this.systemOptions.setOption(Options.HOUSEKEEPING, Options.getExpiration(Options.HOUSEKEEPING_PERIOD));
} else {
var currentDate = (new Date()).toISOString();
if (nextCleanup <= currentDate || true) {
this.currentDate = (new Date()).toISOString();
//this.currentDate = Options.getExpiration(10);
Packages.java.lang.System.out.println('currentDate: ' + this.currentDate);
if (nextCleanup <= this.currentDate) {
Packages.java.lang.System.out.println('Time to clean !');
var namespaces = JSON.parse(this.systemOptions.getOption(Options.NAMESPACES, '{}'));
for (var name in namespaces) {
this.__cleanupNamespace__(name, namespaces[name], currentDate); // TODO: return value to say if the NS needsto be deleted
if ( ! this.__cleanupNamespace__(name, namespaces[name])) {
delete namespaces[name];
Packages.java.lang.System.out.println('deleting empty namespace ' + name);
};
}
this.systemOptions.setOption(Options.NAMESPACES, JSON.stringify(namespaces));
this.systemOptions.setOption(Options.HOUSEKEEPING, Options.getExpiration(Options.HOUSEKEEPING_PERIOD));
}
}
}
Options.prototype.__cleanupNamespace__ = function (name, expiration, currentDate) {
Packages.java.lang.System.out.println('Cleanup for namespace ' + name + ' / ' + expiration);
/*
*
* Cleanup a namespace, return true if there are still keys in the namespace
*
*/
Options.prototype.__cleanupNamespace__ = function (name, namespaceExpiration, currentDate) {
Packages.java.lang.System.out.println('Cleanup for namespace ' + name + ' / ' + namespaceExpiration);
var namespaceExpired = namespaceExpiration != null && namespaceExpiration <= this.currentDate;
var options = new Options(this.authorAccess, name, undefined, true);
var nsOptions = options.getOptionKeys();
for (var key in nsOptions) {
var value = nsOptions[key];
Packages.java.lang.System.out.println(' Cleanup for option ' + key + ' / ' + value);
var keyExpiration = nsOptions[key];
Packages.java.lang.System.out.println(' Cleanup for option ' + key + ' / ' + keyExpiration);
if ( namespaceExpired || (keyExpiration != null && keyExpiration <= this.currentDate) ) {
options.deleteOption(key);
Packages.java.lang.System.out.println(' deleting key ' + key);
}
}
nsOptions = options.getOptionKeys();
return Object.keys(nsOptions).length > 0;
}