UNPKG

10.8 kBJavaScriptView Raw
1"use strict";var __extends=this&&this.__extends||function(){var n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r])};return function(t,e){function r(){this.constructor=t}n(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)}}(),__decorate=this&&this.__decorate||function(t,e,r,n){var o,i=arguments.length,s=i<3?e:null===n?n=Object.getOwnPropertyDescriptor(e,r):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(t,e,r,n);else for(var a=t.length-1;0<=a;a--)(o=t[a])&&(s=(i<3?o(s):3<i?o(e,r,s):o(e,r))||s);return 3<i&&s&&Object.defineProperty(e,r,s),s},__metadata=this&&this.__metadata||function(t,e){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(t,e)},__param=this&&this.__param||function(r,n){return function(t,e){n(t,e,r)}};Object.defineProperty(exports,"__esModule",{value:!0});var data_structure_1=require("./data_structure"),Collections=require("typescript-collections"),T=require("ts-promise");require("reflect-metadata");var injection_js_1=require("injection-js"),jayeson_lib_record_1=require("@jayesol/jayeson.lib.record"),message_class_1=require("./message_class"),mutable_1=require("./mutable"),merge_1=require("./merge"),data_structure_2=require("./data_structure");exports.FSREPO_IMPL=new injection_js_1.InjectionToken("FSRepoInstance");var AbstractFSRepo=function(){function t(){this._handlers=new Collections.Set}return t.prototype.handlers=function(){return this._handlers},t.prototype.appendSnapshot=function(t,e){throw new Error("appendSnapshot is not implemented")},t.prototype.getSnapshot=function(t){throw new Error("getSnapshot is not implemented")},t.prototype.registerSnapshotHandler=function(t){this.handlers().add(t)},t.prototype.deRegisterSnapshotHandler=function(t){this.handlers().remove(t)},t.prototype.getRegisteredHandlers=function(){return this.handlers().toArray()},t.prototype.push=function(t){throw new Error("push is not implemented")},t.prototype.isReady=function(t){return null},t=__decorate([injection_js_1.Injectable(),__metadata("design:paramtypes",[])],t)}(),FSRepoImpl=function(o){function t(t,e){var r=o.call(this)||this;r.outputStreamName=t,r.sportsGroup=e,r._head=null;var n=r;return r.fssEndReceived=new T.Promise(function(t,e){n.promiseResolver=t,n.promiseRejector=e}),r}var e;return __extends(t,o),(e=t).prototype.head=function(){return this._head},t.prototype.appendSnapshot=function(t,e){return null==this._head?this.result=e.apply(null):this.result=e.apply(this._head),this._head=this.result.getAfter(),this.result.getDeltaOut()},t.prototype.getSnapshot=function(t){return void 0===t&&(t=""),null==this._head?data_structure_1.IndexedSnapshotImpl.EMPTY_SNAPSHOT:this._head},t.prototype.getTtlRemoveSnapshot=function(){return null==this._head?[]:[new TTLRemoveCheck(null,this._head.getPartitions().toArray(),this.outputStreamName)]},t.prototype.push=function(e){if(e.msgType()==this.sportsGroup.FULLSNAPSHOT_START());else if(e.msgType()==this.sportsGroup.FULLSNAPSHOT_END())this.promiseResolver(!0);else{var r=this;this._handlers.forEach(function(t){t.process(r.outputStreamName,e)})}},t.prototype.isReady=function(t){return this.fssEndReceived},t.outputStream=new injection_js_1.InjectionToken("aggregatedStreamName"),t=e=__decorate([injection_js_1.Injectable(),__param(0,injection_js_1.Inject(e.outputStream)),__metadata("design:paramtypes",[String,message_class_1.SportsFeedMessageGroup])],t)}(exports.AbstractFSRepo=AbstractFSRepo);exports.FSRepoImpl=FSRepoImpl;var Delta=function(){function t(t,e,r){this._incoming=t,this._after=e,this._before=r}return t.prototype.incoming=function(){return this._incoming},t.prototype.msgType=function(){return this._incoming.msgType()},t.prototype.after=function(){return this._after},t.prototype.delta=function(){return this._incoming.data()},t.prototype.before=function(){return this._before},t}();exports.Delta=Delta;var TTLType,TTLConfig=function(){function t(){this.livettl=18e4,this.todayttl=18e4,this.earlyttl=18e4,this.enableTtl=!0}return t.prototype.getLivettl=function(){return this.livettl},t.prototype.setLivettl=function(t){this.livettl=t},t.prototype.getTodayttl=function(){return this.todayttl},t.prototype.setTodayttl=function(t){this.todayttl=t},t.prototype.getEarlyttl=function(){return this.earlyttl},t.prototype.setEarlyttl=function(t){this.earlyttl=t},t.prototype.getRunInterval=function(){if(!this.enableTtl)return-1;var t=this.getLivettl(),e=this.getTodayttl(),r=this.getEarlyttl(),n=Math.min(t,e,r)/2;return n<1e3?1e4:n},t.prototype.isEnableTtl=function(){return this.enableTtl},t.prototype.setEnableTtl=function(t){this.enableTtl=t},t=__decorate([injection_js_1.Injectable()],t)}();exports.TTLConfig=TTLConfig,function(t){t[t.REMOVE=0]="REMOVE",t[t.RESTORE=1]="RESTORE"}(TTLType=exports.TTLType||(exports.TTLType={}));var TTLOutgoing=function(i){function t(t,e,r,n){var o=i.call(this,e,r,n)||this;return o.ttlType=t,o}return __extends(t,i),t.prototype.getTtlType=function(){return this.ttlType},t}(Delta);exports.TTLOutgoing=TTLOutgoing;var TTLCheck=function(){function t(t,e,r,n){this.ttlType=t,this.recycleBin=e,this.keys=r,this.stream=n}return t.prototype.getKeys=function(){return this.keys},t.prototype.getKeysMapping=function(){var e=new Collections.Dictionary;return this.keys.forEach(function(t){e.setValue(t,(new Date).getTime())}),e},t.prototype.getTtlType=function(){return this.ttlType},t.prototype.getRecycleBin=function(){return this.recycleBin},t.prototype.setRecycleBin=function(t){this.recycleBin=t},t.prototype.getStream=function(){return this.stream},t}(),TTLRestoreCheck=function(n){function t(t,e,r){return n.call(this,TTLType.RESTORE,t,e,r)||this}return __extends(t,n),t.prototype.apply=function(t){var n=this,o=t,i=!1,s=new Collections.Dictionary,a={},p=[];this.getKeys().forEach(function(t,e){if(n.getRecycleBin().containData(t)){i=!0;var r=(new Date).getTime();s.setValue(t,r),a=n.getRecycleBin().copyData(t,a),n.ttlRestoreWrapper=n.getRecycleBin().restoreData(new data_structure_2.TTLWrapper,o,t,r),o=n.ttlRestoreWrapper.getRemainingSs(),p.push(n.ttlRestoreWrapper.getRestoredSs())}});var e=[],r=new data_structure_2.MergeableWrapper;if(r.setAfter(o),i)return console.log("restore -----\x3e"+JSON.stringify(s.keys())),merge_1.DeltaTransformingLogicImpl.transformTTLRestore(p,this.getRecycleBin().getGrp(),this.getStream(),t,o,a,s);var c=new data_structure_1.Incoming(this.getRecycleBin().getGrp().ADMIN_REFRESH(),this.getStream(),new data_structure_1.IndexedSnapshotImpl(a,this.getKeysMapping()));return e.push(new TTLOutgoing(this.getTtlType(),c,o,t)),r.setDeltaOut(e),r},t}(exports.TTLCheck=TTLCheck);exports.TTLRestoreCheck=TTLRestoreCheck;var TTLRemoveCheck=function(n){function t(t,e,r){return n.call(this,TTLType.REMOVE,t,e,r)||this}return __extends(t,n),t.prototype.apply=function(t){for(var e=t.getPartitionMap(),r=new Collections.Dictionary,n=t,o=[],i=this.getRecycleBin().getTtlConfig(),s=!1,a=0,p=this.getKeys();a<p.length;a++){var c=p[a],u=0;if(e.containsKey(c)){switch(c.oddType()){case jayeson_lib_record_1.OddType.LIVE:u=e.getValue(c)+i.getLivettl();break;case jayeson_lib_record_1.OddType.TODAY:u=e.getValue(c)+i.getTodayttl();break;case jayeson_lib_record_1.OddType.EARLY:u=e.getValue(c)+i.getEarlyttl()}u<(new Date).getTime()&&(s=!0,r.setValue(c,e.getValue(c)),this.ttlRemoveWrapper=this.getRecycleBin().removeData(new data_structure_2.TTLWrapper,n,c),n=this.ttlRemoveWrapper.getRemainingSs(),o.push(this.ttlRemoveWrapper.getRemovedSs()))}}var l=[];if(s)return console.log("reseting -----\x3e"+JSON.stringify(r.keys())),merge_1.DeltaTransformingLogicImpl.transformTTLRemove(o,this.getRecycleBin().getGrp(),this.getStream(),t,n,r);var h=new data_structure_2.MergeableWrapper,g=new data_structure_1.Incoming(this.getRecycleBin().getGrp().ADMIN_REFRESH(),this.getStream(),new data_structure_1.IndexedSnapshotImpl({},this.getKeysMapping()));return l.push(new TTLOutgoing(this.getTtlType(),g,n,t)),h.setDeltaOut(l),h.setAfter(n),h},t}(TTLCheck);exports.TTLRemoveCheck=TTLRemoveCheck;var RecycleBin=function(){function t(t,e,r){this.ttlConfig=t,this.grp=e,this.fsRepo=r,this.expiredMatches=new Collections.Dictionary,this.ttlStatus=new Collections.Set}return t.prototype.removeData=function(t,e,r){if(!e.getPartitions().contains(r))return t.setRemainingSs(e),t.setRemovedSs(null),t;var n=mutable_1.BuilderProvider.getSnapshotBuilder(e),o=n.reset(r).build(),i=n.build();return this.expiredMatches.setValue(r,o),t.setRemovedSs(o),t.setRemainingSs(i),t},t.prototype.restoreData=function(t,e,r,n){var o=this.expiredMatches.getValue(r);if(null==o)return t.setRemainingSs(e),t.setRestoredSs(null),t;(new Collections.Dictionary).setValue(r,n);var i=mutable_1.BuilderProvider.getSnapshotBuilder(o);return i.markPartitionAsUpdated(r,n),o=i.build(),e=merge_1.SnapshotUtil.combineSnapshots(this.getGrp().DATA_INSERT_MATCH(),o,e).getAfterSs(),e=merge_1.SnapshotUtil.combineSnapshots(this.getGrp().DATA_INSERT_EVENT(),o,e).getAfterSs(),e=merge_1.SnapshotUtil.combineSnapshots(this.getGrp().DATA_INSERT_ODD(),o,e).getAfterSs(),t.setRestoredSs(o),t.setRemainingSs(e),this.expiredMatches.remove(r),t},t.prototype.clearBin=function(t){this.expiredMatches.remove(t),this.ttlStatus.remove(t)},t.prototype.containData=function(t){return this.expiredMatches.containsKey(t)},t.prototype.getTtlConfig=function(){return this.ttlConfig},t.prototype.getFsRepo=function(){return this.fsRepo},t.prototype.getTtlRestoreSnapshot=function(n){var o=this,i=null;return this.ttlStatus.isEmpty()||(this.getGrp().DATA_RESET().id()==n.msgType().id()?n.data().getPartitions().forEach(function(t){console.log("Dropping partition "+t),o.expiredMatches.remove(t)}):n.data().getPartitionMap().forEach(function(t,e){var r=[];o.ttlStatus.contains(t)&&(o.ttlStatus.remove(t),r.push(t)),0<r.length&&(i=new TTLRestoreCheck(o,r,n.stream()))})),i},t.prototype.getTtlRemoveSnapshot=function(){var e=this,t=this.getFsRepo().getTtlRemoveSnapshot();return t.forEach(function(t){t.setRecycleBin(e),t.getKeys().forEach(function(t){e.ttlStatus.add(t)})}),t},t.prototype.copyData=function(t,e){var r=this.expiredMatches.getValue(t);if(void 0===r)return e;var n=new data_structure_1.IndexedSnapshotImpl(e);return n=merge_1.SnapshotUtil.combineSnapshots(this.getGrp().DATA_INSERT_MATCH(),r,n).getAfterSs(),n=merge_1.SnapshotUtil.combineSnapshots(this.getGrp().DATA_INSERT_EVENT(),r,n).getAfterSs(),(n=merge_1.SnapshotUtil.combineSnapshots(this.getGrp().DATA_INSERT_ODD(),r,n).getAfterSs()).matchesSport},t.prototype.getGrp=function(){return this.grp},t=__decorate([injection_js_1.Injectable(),__metadata("design:paramtypes",[TTLConfig,message_class_1.SportsFeedMessageGroup,AbstractFSRepo])],t)}();exports.RecycleBin=RecycleBin;
\No newline at end of file