@@ -7,6 +7,7 @@ var MemoryPubSub = require('./pubsub/memory');
77var ot = require ( './ot' ) ;
88var projections = require ( './projections' ) ;
99var QueryEmitter = require ( './query-emitter' ) ;
10+ var Snapshot = require ( './snapshot' ) ;
1011var StreamSocket = require ( './stream-socket' ) ;
1112var SubmitRequest = require ( './submit-request' ) ;
1213var types = require ( './types' ) ;
@@ -583,35 +584,39 @@ Backend.prototype.getChannels = function(collection, id) {
583584} ;
584585
585586Backend . prototype . fetchSnapshot = function ( agent , index , id , version , callback ) {
587+ var start = Date . now ( ) ;
586588 var backend = this ;
587- this . _fetchSnapshot ( agent , index , id , version , function ( error , snapshot ) {
588- if ( error ) return callback ( error ) ;
589-
590- var request = {
591- collection : index ,
592- id : id ,
593- v : snapshot . v ,
594- snapshots : snapshot . data ? [ snapshot . data ] : [ ] ,
595- type : snapshot . type
596- } ;
589+ var projection = this . projections [ index ] ;
590+ var collection = projection ? projection . target : index ;
591+ var request = {
592+ agent : agent ,
593+ index : index ,
594+ collection : collection ,
595+ id : id ,
596+ version : version
597+ } ;
597598
598- backend . trigger ( backend . MIDDLEWARE_ACTIONS . readSnapshots , agent , request , function ( error ) {
599+ this . _fetchSnapshot ( collection , id , version , function ( error , snapshot ) {
600+ if ( error ) return callback ( error ) ;
601+ var snapshotProjection = backend . _getSnapshotProjection ( backend . db , projection ) ;
602+ var snapshots = [ snapshot ] ;
603+ backend . _sanitizeSnapshots ( agent , snapshotProjection , collection , snapshots , function ( error ) {
599604 if ( error ) return callback ( error ) ;
600- callback ( null , {
601- data : request . snapshots [ 0 ] ,
602- v : request . v ,
603- type : request . type
604- } ) ;
605+ backend . emit ( 'timing' , 'fetchSnapshot' , Date . now ( ) - start , request ) ;
606+ callback ( null , snapshot ) ;
605607 } ) ;
606608 } ) ;
607609} ;
608610
609- Backend . prototype . _fetchSnapshot = function ( agent , index , id , version , callback ) {
610- this . getOps ( agent , index , id , 0 , version , function ( error , ops ) {
611+ Backend . prototype . _fetchSnapshot = function ( collection , id , version , callback ) {
612+ // Bypass backend.getOps so that we don't call _sanitizeOps. We want to avoid this, because:
613+ // - we want to avoid the 'op' middleware, because we later use the 'readSnapshots' middleware in _sanitizeSnapshots
614+ // - we handle the projection in _sanitizeSnapshots
615+ this . db . getOps ( collection , id , 0 , version , null , function ( error , ops ) {
611616 if ( error ) return callback ( error ) ;
612617
613618 var type = null ;
614- var snapshot ;
619+ var data ;
615620 var fetchedVersion = 0 ;
616621
617622 for ( var index = 0 ; index < ops . length ; index ++ ) {
@@ -621,12 +626,12 @@ Backend.prototype._fetchSnapshot = function (agent, index, id, version, callback
621626 if ( op . create ) {
622627 type = types . map [ op . create . type ] ;
623628 if ( ! type ) return callback ( { code : 4008 , message : 'Unknown type' } ) ;
624- snapshot = type . create ( op . create . data ) ;
629+ data = type . create ( op . create . data ) ;
625630 } else if ( op . del ) {
626- snapshot = undefined ;
631+ data = undefined ;
627632 type = null ;
628633 } else {
629- snapshot = type . apply ( snapshot , op . op ) ;
634+ data = type . apply ( data , op . op ) ;
630635 }
631636 }
632637
@@ -636,11 +641,8 @@ Backend.prototype._fetchSnapshot = function (agent, index, id, version, callback
636641 return callback ( { code : 4024 , message : 'Requested version exceeds latest snapshot version' } ) ;
637642 }
638643
639- callback ( null , {
640- data : snapshot ,
641- v : fetchedVersion ,
642- type : type
643- } ) ;
644+ var snapshot = new Snapshot ( id , fetchedVersion , type , data , null ) ;
645+ callback ( null , snapshot ) ;
644646 } ) ;
645647} ;
646648
0 commit comments