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.
164 lines (138 loc) • 4.72 kB
JavaScript
import Queries from "./Queries.js";
class Config extends Queries {
  constructor(folder) {
    super();
    this.folder = folder;
    this.address = folder.address;
    this.manager = this.folder.manager;
    this.servers = folder.servers;
  }
  sync = async () => {
    this.config_address = `${this.folder.path}/.config`;
    this.fs = this.folder.fs;
    this.config = await this.read_file(this.config_address);
    if (this.config) {
      if (this.config.dropped) this.config = null;
      else {
        if (!this.config.is_index) this.config.is_index = [];
        if (!this.config.indexes) this.config.indexes = [];
      }
    }
    if (!this.config) {
      let is_index = [];
      this.folder.options.index && is_index.push(this.folder.options.index);
      this.config = {
        name: this.folder.name,
        address: this.folder.address,
        indexes: this.folder.indexes,
        blocks: this.folder.options.blocks,
        joins: this.folder.joins,
        is_index,
        files: 0,
        recent_file: "",
        total_size: 0,
        created: Date.now(),
        updated: Date.now(),
      };
      this.folder.is_new = true;
      await this.persist();
    } else {
      if (this.folder.options.index) {
        if (!this.config.is_index.includes(this.folder.options.index)) {
          this.config.is_index.push(this.folder.options.index);
          await this.persist();
        }
      }
    }
    this.config.indexes = this.config.indexes || [];
    this.config.joins = this.config.joins || {};
    let indexes = this.folder.indexes.concat(this.config.indexes),
      persist;
    indexes = Array.from(new Set(indexes));
    for (let i = 0; i < this.folder.indexes.length; i++) {
      let index = this.folder.indexes[i];
      if (!this.config.indexes.includes(index)) {
        persist = true;
        break;
      }
    }
    let join_props = Object.keys(this.config.joins)
    for (let j in this.folder.joins){
      if (!join_props.includes(j)){
        this.config.joins[j] = this.folder.joins[j]
        persist = true
      }
    }
    if (persist) {
      this.config.indexes = indexes;
      await this.persist();
    }
  };
  persist = async (op) => {
    this.config.updated = Date.now();
    if(!op) return;
    await this.write_file(this.config_address, JSON.stringify(this.config), {write_bulks: op});
    
    this.folder.is_new = false
  };
  update = async (bfr, aft, prior_values, persist) => {
    this.config.total_size -= bfr.length;
    this.config.total_size += JSON.stringify(aft).length;
    for (let i = 0; i < this.config.indexes.length; i++) {
      let index = this.config.indexes[i];
      if (prior_values[index]) {
        if (prior_values[index] !== aft[index]) {
          let index_folder = await this.folder.index(prior_values[index]);
          await index_folder.remove_data({ _id: aft._id });
        }
      }
      if (aft[index]) {
        if (prior_values[index] !== aft[index]) {
          let fold = await this.folder.folder(
            `index-${await this.folder.manager.hash(aft[index])}`,
            {
              index,
              account: this.folder.account,
            }
          );
          await fold.write({ _id: aft._id }, { allow_index_write: true, write_bulks: persist === 'update'?undefined: persist });
        }
      }
    }
    await this.persist(persist);
  };
  write = async (data, persist) => {
    this.config.recent_file = data._id;
    this.config.files++;
    this.config.total_size += JSON.stringify(data).length;
    for (let i = 0; i < this.config.indexes.length; i++) {
      let index = this.config.indexes[i],
        name = data[index];
      if (!name) continue;
      let fold = await this.folder.folder(
        `index-${await this.folder.manager.hash(name)}`,
        {
          index,
          account: this.folder.account,
        }
      );
      await fold.write({ _id: data._id }, { allow_index_write: true, write_bulks: persist === 'write'? undefined: persist });
    }
    await this.persist(persist);
  };
  remove = async (data, persist) => {
    this.config.files--;
    this.config.total_size -= JSON.stringify(data.content).length;
    if (data.content._id === this.config.recent_file) {
      this.config.recent_file = data.previous;
    }
    for (let i = 0; i < this.config.indexes.length; i++) {
      let index = this.config.indexes[i],
        name = data.content[index];
      if (!name) continue;
      let index_folder = await this.folder.index(name);
      await index_folder.remove_data({ _id: data.content._id }, {write_bulks: persist === 'remove'? undefined: persist});
    }
     await this.persist(persist);
  };
}
export default Config;