All files / src lossless.ts

90.32% Statements 84/93
87.5% Branches 21/24
93.75% Functions 15/16
92.04% Lines 81/88

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197      5x 5x     5x   5x                                         14x   14x     25x 78x 25x 25x   25x 25x 25x 20x 20x     25x 25x                               5x 10x   10x 10x   10x 10x 10x 3x 3x 3x 3x 6x           15x 40x 25x 25x   15x 25x   25x 14x 14x     25x     15x 40x 25x 25x 25x 15x 15x   25x         15x   15x   6x 10x     15x       11x 21x 4x 4x   17x     21x 11x       15x 15x   15x 17x 17x     17x     17x   17x 23x   23x 30x 6x         24x 12x             24x   24x 74x 74x 24x       24x             17x             15x          
// Deltas target entities.
// We can maintain a record of all the targeted entities, and the deltas that targeted them
 
import Debug from 'debug';
import EventEmitter from 'events';
import {Delta, DeltaFilter, DeltaID, DeltaNetworkImageV1} from './delta';
import {RhizomeNode} from './node';
import {Transactions} from './transactions';
import {DomainEntityID, PropertyID, PropertyTypes, TransactionID, ViewMany} from "./types";
const debug = Debug('rz:lossless');
 
export type CollapsedPointer = {[key: PropertyID]: PropertyTypes};
 
export type CollapsedDelta = Omit<DeltaNetworkImageV1, 'pointers'> & {
  pointers: CollapsedPointer[];
};
 
export type LosslessViewOne = {
  id: DomainEntityID,
  referencedAs: string[];
  propertyDeltas: {
    [key: PropertyID]: CollapsedDelta[]
  }
};
 
export type LosslessViewMany = ViewMany<LosslessViewOne>;
 
class LosslessEntityMap extends Map<DomainEntityID, LosslessEntity> {};
 
class LosslessEntity {
  properties = new Map<PropertyID, Set<Delta>>();
 
  constructor(readonly lossless: Lossless, readonly id: DomainEntityID) {}
 
  addDelta(delta: Delta) {
    const targetContexts = delta.pointers
      .filter(({target}) => target === this.id)
      .map(({targetContext}) => targetContext)
      .filter((targetContext) => typeof targetContext === 'string');
 
    for (const targetContext of targetContexts) {
      let propertyDeltas = this.properties.get(targetContext);
      if (!propertyDeltas) {
        propertyDeltas = new Set<Delta>();
        this.properties.set(targetContext, propertyDeltas);
      }
 
      propertyDeltas.add(delta);
      debug(`[${this.lossless.rhizomeNode.config.peerId}]`, `entity ${this.id} added delta:`, JSON.stringify(delta));
    }
  }
 
  toJSON() {
    const properties: {[key: PropertyID]: number} = {};
    for (const [key, deltas] of this.properties.entries()) {
      properties[key] = deltas.size;
    }
    return {
      id: this.id,
      properties
    };
  }
}
 
export class Lossless {
  domainEntities = new LosslessEntityMap();
  transactions: Transactions;
  referencedAs = new Map<string, Set<DomainEntityID>>();
  eventStream = new EventEmitter();
 
  constructor(readonly rhizomeNode: RhizomeNode) {
    this.transactions = new Transactions(this);
    this.transactions.eventStream.on("completed", (transactionId, deltaIds) => {
      debug(`[${this.rhizomeNode.config.peerId}]`, `Completed transaction ${transactionId}`);
      const transaction = this.transactions.get(transactionId);
      Iif (!transaction) return;
      for (const id of transaction.entityIds) {
        this.eventStream.emit("updated", id, deltaIds);
      }
    });
  }
 
  ingestDelta(delta: Delta): TransactionID | undefined {
    const targets = delta.pointers
      .filter(({targetContext}) => !!targetContext)
      .map(({target}) => target)
      .filter((target) => typeof target === 'string')
 
    for (const target of targets) {
      let ent = this.domainEntities.get(target);
 
      if (!ent) {
        ent = new LosslessEntity(this, target);
        this.domainEntities.set(target, ent);
      }
 
      ent.addDelta(delta);
    }
 
    for (const {target, localContext} of delta.pointers) {
      if (typeof target === "string" && this.domainEntities.has(target)) {
        if (this.domainEntities.has(target)) {
          let referencedAs = this.referencedAs.get(localContext);
          if (!referencedAs) {
            referencedAs = new Set<string>();
            this.referencedAs.set(localContext, referencedAs);
          }
          referencedAs.add(target);
        }
      }
    }
 
    const transactionId = this.transactions.ingestDelta(delta, targets);
 
    if (!transactionId) {
      // No transaction -- we can issue an update event immediately
      for (const id of targets) {
        this.eventStream.emit("updated", id, [delta.id]);
      }
    }
    return transactionId;
  }
 
  viewSpecific(entityId: DomainEntityID, deltaIds: DeltaID[], deltaFilter?: DeltaFilter): LosslessViewOne | undefined {
    const combinedFilter = (delta: Delta) => {
      if (!deltaIds.includes(delta.id)) {
        debug(`[${this.rhizomeNode.config.peerId}]`, `Excluding delta ${delta.id} because it's not in the requested list of deltas`);
        return false;
      }
      if (!deltaFilter) return true;
      return deltaFilter(delta);
    };
    const res = this.view([entityId], (delta) => combinedFilter(delta));
    return res[entityId];
  }
 
  view(entityIds?: DomainEntityID[], deltaFilter?: DeltaFilter): LosslessViewMany {
    const view: LosslessViewMany = {};
    entityIds = entityIds ?? Array.from(this.domainEntities.keys());
 
    for (const id of entityIds) {
      const ent = this.domainEntities.get(id);
      Iif (!ent) continue;
 
 
      const referencedAs = new Set<string>();
      const propertyDeltas: {
        [key: PropertyID]: CollapsedDelta[]
      } = {};
 
      for (const [key, deltas] of ent.properties.entries()) {
        propertyDeltas[key] = propertyDeltas[key] || [];
 
        for (const delta of deltas) {
          if (deltaFilter && !deltaFilter(delta)) {
            continue;
          }
 
          // If this delta is part of a transaction,
          // we need to be able to wait for the whole transaction.
          if (delta.transactionId) {
            Iif (!this.transactions.isComplete(delta.transactionId)) {
              // TODO: Test this condition
              debug(`[${this.rhizomeNode.config.peerId}]`, `Excluding delta ${delta.id} because transaction ${delta.transactionId} is not completed`);
              continue;
            }
          }
 
          const pointers: CollapsedPointer[] = [];
 
          for (const {localContext, target} of delta.pointers) {
            pointers.push({[localContext]: target});
            if (target === ent.id) {
              referencedAs.add(localContext);
            }
          }
 
          propertyDeltas[key].push({
            ...delta,
            pointers
          });
        }
      }
 
      view[ent.id] = {
        id: ent.id,
        referencedAs: Array.from(referencedAs.values()),
        propertyDeltas
      };
    }
 
    return view;
  }
 
  // TODO: point-in-time queries
}