UNPKG

godprotocol

Version:

A distributed computing environment for Web 4.0 — integrating AI, decentralisation, and virtual computation.

365 lines (281 loc) 10.2 kB
import Storage from "./functions/storage.js"; class Twa extends Storage { constructor(config, account) { super(account); this.config = config this.type = 'twain' this.methods = { ...this.methods, extend: {args: [['obj', 'twain']], ret: ['thisobj', 'twain']}, zip: {args: [['prop', 'array'], ['value', 'array']], ret: ['obj', 'twain']}, unzip: {args: [], ret: ['array_pair', 'array[[], []]']}, copy: {args: [], ret: ['obj', 'twain']}, clear: {args: [], ret: ['obj', 'twain']}, is_empty: {args: [], ret: ['yes', 'boolean']}, has: {args: [['prop', 'object']], ret: ['yes', 'boolean']}, unset: {args: [['prop', 'object']], ret: ['yes', 'boolean']}, keys: {args: [], ret: ['keys', 'array']}, values: {args: [], ret: ['values', 'array']}, entries: {args: [], ret: ['[[key, value],...pair]', 'array']}, get: {args: [['prop', 'string']], ret: ['value', 'object|void']}, set: {args: [['prop', 'string'], ['value', 'object']], ret: ['done', 'void']}, } } __or__ = async(other)=>{ if (!this.config.value){ return await other.to_address() } return await this.to_address() } __and__ = async(other)=>{ if (!this.config.value){ return await this.to_address() } return await other.to_address() } get_ = async(prop)=>{ let key = await this.key(prop.literal? await prop.literal(): prop) return await (await this.chain()).get({key}) } set_ = async(prop, args)=>{ let value = args[1] let key = await this.key(await prop.literal()) prop = await prop.to_address(); value = await value.to_address() let chain = await this.chain() await chain.set({twa: this.config, prop, value, key}) } retrieve = async(entries)=>{ delete this.config.entries let ds = this.account.manager.ds await (await ds.folder(this.config.location)).write(this.config, {replace: true}) for (let i=0; i< entries.length; i++){ let entry = entries[i] let prop = await (await ds.folder(entry.prop.value, {account: this.account.physical_address})).readone({_id: entry.prop._id}) let val; if (entry.value.type === 'function'){ let res = await this.account.vm.cloth(entry.value, {just_content: true}) if (!res){ await this.account.reload({address: entry.value.module}) } } else val = await (await ds.folder(entry.value.value, {account: this.account.physical_address})).readone({_id: entry.value._id}) if(!val && entry.value.type !== 'function' && entry.value.value.endsWith('/instances')){ let adr = entry.value.value.split('/').slice(0,-1).join('/') val = await (await ds.folder(adr, {account: this.account.physical_address})).readone({_id: entry.value._id}) } if (val){ if (['array', 'twain'].includes(val.type)){ await (await this.account.vm.cloth_content(val)).retrieve(val.items || val.entries) } else if(val.type === 'instance'){ await this.account.vm.retrieve(val) } else if (val.type === 'function'){ } else { await (await ds.folder(entry.value.value)).write(val, {replace: true}) } } prop && await (await ds.folder(entry.prop.value)).write(prop, {replace: true}) let pair_folder = await ds.folder(entry.location) await (await pair_folder.folder('__prop__')).write(entry.prop, {replace: true}) delete entry.prop delete entry.location await pair_folder.write(entry, {replace: true}) } } store = async({store, thread})=>{ let ds = this.account.manager.ds, vm = this.account.vm; let addr = this.config.value; let data = {...this.config, entries: []} while(addr){ let o = this._split_datapath(addr) let val = await ds.folder(o[0]) let pair_value = await val.readone({_id: o[1]}) let datum = await store({object: await vm.cloth(pair_value.value)}, {vm, thread}) if (datum && datum.config){ process.exit() } let prop = await this.get_prop(val) let prop_obj = await vm.cloth(prop) await store({object: prop_obj}, {vm, thread}) pair_value.prop = prop; pair_value.location = o[0] data.entries.push(pair_value) addr = pair_value.prev } return data; } extend = async(obj)=>{ let {vm, manager} = this.account; let addr = obj.config.value while(addr){ let o = this._split_datapath(addr) o = {value: o[0], _id: o[1], type: 'address'} let val = await vm.cloth(o, {just_content: true}) let prop_folder = await manager.ds.folder(o.value) let conf = await this.get_prop(prop_folder) let prop_obj = await vm.cloth(conf) await this.set_(prop_obj, [null, await vm.cloth_content(val.value)]) addr = val.prev } return await this.to_address() } zip = async(prop, args)=>{ let {vm} = this.account; for (let i=0; i< prop.config.length; i++){ let item = await prop.index(i) let val = await args[1].index(i) await this.set_(await vm.cloth_content(item), [null, await vm.cloth_content(val)]) } return await this.to_address() } unzip = async()=>{ let addr = this.config.value, vals = new Array(), props = new Array(); let {vm, chain, manager} = this.account; while (addr){ let o = this._split_datapath(addr) o = {value: o[0], _id: o[1], type: 'address'} let val = await vm.cloth(o, {just_content: true}) let prop_folder = await manager.ds.folder(o.value) let conf = await this.get_prop(prop_folder) vals.push(val.value) props.push(conf) addr= val.prev } return await vm.instantiate({ type: 'array', value: [ await vm.instantiate({value: props, type:'array', location:this.spawn_address()}, {chain}), await vm.instantiate({value: vals, type:'array', location:this.spawn_address()}, {chain})] , location: this.spawn_address() }, {chain}) } copy = async()=>{ let addr = this.config.value, tries = new Object(); let {vm, chain, manager} = this.account; while (addr){ let o = this._split_datapath(addr) o = {value: o[0], _id: o[1], type: 'address'} let val = await vm.cloth(o, {just_content: true}) let prop_folder = await manager.ds.folder(o.value) let conf = await this.get_prop(prop_folder) let prop_obj = await vm.cloth(conf) let pair = [conf, val.value] tries[await this.key(await prop_obj.literal())] = pair addr= val.prev } return await vm.instantiate({ type: 'twain', value: tries, location: this.spawn_address() }, {chain}) } clear = async()=>{ this.config.value = '' let fold = await this._folder() await fold.write(this.config, {replace: true}) } is_empty = async()=>{ return !this.config.value } has = async(prop)=>{ let folder = await this._folder() let child = await folder.folder(await this.key(await prop.literal())) let v = (await child.readone()) return (v && !v.deleted) || false } key = async lit=>{ return await this.account.vm.hash(lit, 'sha1') } unset = async(prop)=>{ let key = await this.key(await prop.literal()) let chain = await this.chain() return await chain.remove({twa: this.config, key}) } get_prop = async(folder)=>{ return await (await folder.folder('__prop__')).readone() } keys = async(func)=>{ let addr = this.config.value, kys = new Array(); let {vm, chain, manager} = this.account; while (addr){ let o = this._split_datapath(addr) o = {value: o[0], _id: o[1], type: 'address'} let val = await vm.cloth(o, {just_content: true}) let prop_folder = await manager.ds.folder(o.value) let conf = await this.get_prop(prop_folder) kys.push(conf) addr = val.prev } return await vm.instantiate({ type: 'array', value: kys, location: this.spawn_address() }, {chain}) } values = async(func)=>{ let addr = this.config.value, tries = new Array(); let {vm, chain} = this.account; while (addr){ let o = this._split_datapath(addr) o = {value: o[0], _id: o[1], type: 'address'} let val = await vm.cloth(o, {just_content: true}) tries.push(val.value) addr= val.prev } return await vm.instantiate({ type: 'array', value: tries, location: this.spawn_address() }, {chain}) } entries = async(func)=>{ let addr = this.config.value, tries = new Array(); let {vm, chain, manager} = this.account; while (addr){ let o = this._split_datapath(addr) o = {value: o[0], _id: o[1], type: 'address'} let val = await vm.cloth(o, {just_content: true}) let prop_folder = await manager.ds.folder(o.value) let conf = await (await prop_folder.folder('__prop__')).readone() let pair = [conf, val.value] pair = await vm.instantiate({ type: 'array', value: pair, location: this.spawn_address() }, {chain}) tries.push(pair) addr= val.prev } return await vm.instantiate({ type: 'array', value: tries, location: this.spawn_address() }, {chain}) } literal = async(options={})=>{ if (this.value)return this.value; let {vm, manager} = this.account; this.value = {} let addr = this.config.value, val; while (addr){ let o = this._split_datapath(addr) o = {value: o[0], _id: o[1], type: 'address'} val = await vm.cloth(o, {just_content: true, }) let value = await vm.cloth(val.value) let prop_folder = await manager.ds.folder(o.value) let conf = await this.get_prop(prop_folder) let prop = await vm.cloth(conf) if(!value){ console.log(val) return } this.value[await prop.literal()] = await value.literal() addr= val.prev } return this.value } sync = ()=>{ return this } } export default Twa;