FileStore seems to be mostly working...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-07-30 19:43:32 +03:00
parent e8ea046652
commit 4cb65861e7

View File

@ -573,8 +573,10 @@ function(meth, drop_cache=false, post){
var res = var res =
store == null ? store == null ?
object.parentCall(MetaStore, meth, this, path, ...args) object.parentCall(MetaStore, meth, this, path, ...args)
//: this.data[store][meth](path.slice(store.length), ...args) : this.data[store][target](
: this.data[store][target](path.slice(store.length), ...args) // NOTE: we are normalizing for root/non-root paths...
path.slice(path.indexOf(store)+store.length),
...args)
if(drop_cache){ if(drop_cache){
delete this.__substores } delete this.__substores }
@ -601,9 +603,13 @@ module.MetaStore = {
return path })) }, return path })) },
substore: function(path){ substore: function(path){
path = module.path.normalize(path, 'string') path = module.path.normalize(path, 'string')
var root = path[0] == '/'
var store = this.substores var store = this.substores
.filter(function(p){ .filter(function(p){
return path.startsWith(p) }) return path.startsWith(
root ?
'/'+p
: p) })
.sort(function(a, b){ .sort(function(a, b){
return a.length - b.length }) return a.length - b.length })
.pop() .pop()
@ -628,14 +634,14 @@ module.MetaStore = {
.flat() }, .flat() },
// XXX revise... // XXX revise...
__exists__: metaProxy('__exists__', __exists__: metaProxy('__exists__',
// normalize path...
function(res, store, path){ function(res, store, path){
return (res && store) ? return (store && res) ?
// XXX which way should we go???
//module.path.join(store, res)
path path
: res }), : res }),
__get__: metaProxy('__get__'), __get__: metaProxy('__get__'),
__delete__: metaProxy('__delete__', true), __delete__: metaProxy('__delete__', true),
// XXX BUG: this does not create stuff in sub-store...
__update__: metaProxy('__update__', true), __update__: metaProxy('__update__', true),
json: function(asstring=false){ json: function(asstring=false){
@ -722,12 +728,14 @@ var exists =
module.exists = module.exists =
async function(base, sub, options={index: '.index'}){ async function(base, sub, options={index: '.index'}){
if(typeof(sub) != 'string'){ if(typeof(sub) != 'string'){
options = sub options = sub ?? options
sub = base sub = base
base = '' } base = null }
var {index} = options var {index} = options
var target = module.path.join(base, sub) var target = base ?
module.path.join(base, sub)
: sub
if(!fs.existsSync(target)){ if(!fs.existsSync(target)){
return false } return false }
var stat = await fs.promises.stat(target) var stat = await fs.promises.stat(target)
@ -738,12 +746,14 @@ var read =
module.read = module.read =
async function(base, sub, options={index: '.index'}){ async function(base, sub, options={index: '.index'}){
if(typeof(sub) != 'string'){ if(typeof(sub) != 'string'){
options = sub options = sub ?? options
sub = base sub = base
base = '' } base = null }
var {index} = options var {index} = options
var target = module.path.join(base, sub) var target = base ?
module.path.join(base, sub)
: sub
if(!fs.existsSync(target)){ if(!fs.existsSync(target)){
return undefined } return undefined }
// handle dir text... // handle dir text...
@ -759,14 +769,16 @@ var mkdir =
module.mkdir = module.mkdir =
async function(base, sub, options={index: '.index'}){ async function(base, sub, options={index: '.index'}){
if(typeof(sub) != 'string'){ if(typeof(sub) != 'string'){
options = sub options = sub ?? options
sub = base sub = base
base = '' } base = null }
var {index} = options var {index} = options
var levels = module.path.split(sub) var levels = module.path.split(sub)
for(var level of levels){ for(var level of levels){
base = module.path.join(base, level) base = base == null ?
level
: module.path.join(base, level)
// nothing exists -- create dir and continue... // nothing exists -- create dir and continue...
if(!fs.existsSync(base)){ if(!fs.existsSync(base)){
await fs.promises.mkdir(base, {recursive: true}) await fs.promises.mkdir(base, {recursive: true})
@ -787,25 +799,35 @@ async function(base, sub, options={index: '.index'}){
var update = var update =
module.update = module.update =
async function(base, sub, data, options={index: '.index'}){ async function(base, sub, data, options={index: '.index'}){
if(typeof(sub) != 'string'){ if(typeof(data) != 'string'){
options = sub options = data ?? options
data = sub
sub = base sub = base
base = '' } base = null }
var {index} = options var {index} = options
var target = base ?
module.path.join(base, sub)
: sub
// path already exists... // path already exists...
if(fs.existsSync(module.path.join(base, sub))){ if(fs.existsSync(target)){
var stat = await fs.promises.stat(base) var stat = await fs.promises.stat(target)
if(stat.isDirectory()){ if(stat.isDirectory()){
sub = module.path.join(sub, index) } target = module.path.join(target, index) }
// create path / parts of path... // create path / parts of path...
} else { } else {
var levels = module.path.split(sub) var levels = module.path.split(target)
var basename = levels.pop() levels.pop()
// ensure the parent dir exists... // ensure the parent dir exists...
await module.mkdir(base, levels, index) } await module.mkdir(
...(base ?
// NOTE: we are keeping this separate here to avoid creating
// anything above it...
[base]
: []),
levels,
options) }
// write the data... // write the data...
var target = module.path.join(base, sub)
var f = await fs.promises.open(target, 'w') var f = await fs.promises.open(target, 'w')
await f.writeFile(data) await f.writeFile(data)
f.close() f.close()
@ -814,13 +836,15 @@ var clear =
module.clear = module.clear =
async function(base, sub, options={index: '.index'}){ async function(base, sub, options={index: '.index'}){
if(typeof(sub) != 'string'){ if(typeof(sub) != 'string'){
options = sub options = sub ?? options
sub = base sub = base
base = '' } base = '' }
var {index} = options var {index} = options
// remove leaf... // remove leaf...
var target = module.path.join(base, sub) var target = base == '' ?
sub
: module.path.join(base, sub)
// dir... // dir...
if(fs.existsSync(target)){ if(fs.existsSync(target)){
var stat = await fs.promises.stat(target) var stat = await fs.promises.stat(target)
@ -853,6 +877,12 @@ async function(base, sub, options={index: '.index'}){
fs.promises.rmdir(cur) } } fs.promises.rmdir(cur) } }
levels.pop() } levels.pop() }
return target } return target }
// XXX cleanup all sub-paths...
var cleanup =
module.cleanup =
async function(base){
// XXX
}
// XXX add monitor API... // XXX add monitor API...
@ -882,7 +912,7 @@ module.FileStore = {
return path return path
.slice(that.__path__.length) })) }) }) }, .slice(that.__path__.length) })) }) }) },
__exists__: async function(path){ __exists__: async function(path){
return module.exists(this.__path__, path, {index: this.__directory_text__}) return await module.exists(this.__path__, path, {index: this.__directory_text__})
&& path }, && path },
__get__: async function(path){ __get__: async function(path){
var p = module.path.join(this.__path__, path) var p = module.path.join(this.__path__, path)
@ -891,8 +921,9 @@ module.FileStore = {
atime: atimeMs, atime: atimeMs,
mtime: mtimeMs, mtime: mtimeMs,
ctime: ctimeMs, ctime: ctimeMs,
text: module.read(p, {index: this.__directory_text__}) text: await module.read(p, {index: this.__directory_text__})
} }, } },
// XXX BUG: meta-store: writing to this creates a root path rather than a file...
// XXX do we write all the data or only the .text??? // XXX do we write all the data or only the .text???
__update__: async function(path, data, mode='update'){ __update__: async function(path, data, mode='update'){
return module.update( return module.update(