generalised-datastore
Version:
Generalised Datastore (GDS) — a decentralized data engine and RPC layer built for Web 4.0 applications, enabling distributed joins, caching, and remote program execution across nodes.
93 lines (72 loc) • 2.63 kB
JavaScript
import Folder_query from "./Folder_query.js";
import Response from "./Response.js";
import Config from './Config.js'
import Queries from "./Queries.js";
class Group extends Queries{
constructor(address, filter){
super()
this.address = address;
this.filter = filter
this.folders = new Array()
}
get_price = async(query)=>{
if (query){
this.path = `${this.path}/${query}`
}
if (this.config){
if (this.config.path === this.path) return this.config.price
}
this.config = await this.ds.manager.repository.read(`${this.path}/.config`)
if (this.config) this.config = JSON.parse(this.config)
else this.config = {}
return this.config.price;
}
match = async(query, data)=>{
return await this.pass(data, query)
}
set_key = async key=>{
this.filter = {key}
this.keyhash = await this.ds.manager.initiator.vm.hash(key);
this.path = `${this.address}/${this.keyhash}`
this.key_config = `${this.path}/.config`
this.config = await this.manager.repository.read(this.key_config)
}
respond = async (data, options={})=>{
let {method} = options;
let res = new Response(data, this)
return res;
}
addressing = async()=>{
let add_split = this.address.split('/')
add_split = [`${add_split.slice(0,-1).join('/')}`, add_split.slice(-1)[0]]
this.name = add_split[1]
this.parent = add_split[0]
let hsh = this.manager.initiator.vm.hash
this.hash = await hsh(this.address)
this.path = `${this.ds.group_path}${add_split[0]? `/${add_split[0]}`: ''}/${this.hash}`
if (!this.filter)return;
this.key = this.filter.key
this.keyhash = await hsh(this.key)
this.path = `${this.path}/${this.key}`
this.key_config = `${this.path}/.config`
let query_hash = await hsh(this.filter.query), now = Date.now()
if (await this.manager.repository.exists(this.key_config)){
let key_conf = await this.manager.repository.read(this.key_config)
if (!key_conf.queries[query_hash]){
key_conf.queries[query_hash] = this.filter.query
key_conf.updated = Date.now()
await this.manager.repository.write(this.key_config, JSON.stringify(key_conf))
}
}else await this.manager.repository.write(this.key_config, JSON.stringify({key: this.key, queries: {[query_hash]:query}, updated:now, created: now}))
this.path = `${this.path}/${query_hash}`
}
sync =async ds=>{
this.ds = ds
this.manager = ds.manager
await this.addressing()
this.config = new Config(this)
await this.config.sync()
return this;
}
}
export default Group