-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #19.
- Loading branch information
Tom Ashworth
committed
Jan 19, 2014
1 parent
4b53ec0
commit 8f43b44
Showing
6 changed files
with
118 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require('es6-shim'); | ||
var fs = require('fs'); | ||
var pathlib = require('path'); | ||
var crypto = require('crypto') | ||
var Promise = require('rsvp').Promise; | ||
|
||
module.exports = _Storage; | ||
|
||
_Storage.cache = {}; | ||
|
||
_Storage.id = function () { | ||
var tuple = process.hrtime(); | ||
var time = tuple[0] * 1e9 + tuple[1]; | ||
var shasum = crypto.createHash('sha1'); | ||
shasum.update(''+time); | ||
return shasum.digest('hex'); | ||
}; | ||
|
||
_Storage.heapPrefix = 'heap' + String.fromCharCode(0); | ||
|
||
/** | ||
* _Storage | ||
* Note: non-heap objects cannot reference each other. | ||
* TODO: save to files | ||
*/ | ||
|
||
function _Storage(namespace) { | ||
this.heap = !namespace; | ||
this.namespace = namespace || _Storage.id(); | ||
this.readyPromise = Promise.resolve(); | ||
if (this.heap) { | ||
_Storage.cache[this.reference({ _storage: this })] = this; | ||
} | ||
} | ||
|
||
_Storage.prototype.init = function(data) { | ||
this.readyPromise = Promise.resolve(data); | ||
return this.readyPromise; | ||
}; | ||
|
||
_Storage.prototype.persist = function (data) { | ||
return Promise.resolve(); | ||
}; | ||
|
||
_Storage.prototype.reference = function (storedObject) { | ||
if (!storedObject._storage || !storedObject._storage.heap) { | ||
return storedObject; | ||
} | ||
return _Storage.heapPrefix + storedObject._storage.namespace; | ||
}; | ||
|
||
_Storage.prototype.dereference = function (storedObjectReference) { | ||
if (!storedObjectReference.startsWith(_Storage.heapPrefix)) { | ||
throw Error('Reference is not valid: ' + storedObjectReference) | ||
} | ||
// TODO: get from storage | ||
if (_Storage.cache[namespace]) { | ||
return Promise.resolve(_Storage.cache[namespace]); | ||
} else { | ||
return Promise.reject(new Error('NullPointerException: could not find that thing.')); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters