Debian Codegraph @

extensions/VisualEditor/lib/ve/rebaser/src/dm/ve.dm.DocumentStore.js

raw symbols SCIP
1 /*!
2 * VisualEditor document store class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 'use strict';
8
9 /**
10 * @constructor
11 * @param {Object} storageClient MongoClient-like object; passed as a parameter for testing purposes
12 * @param {string} dbName Database name
13 * @param {Object} logger Logger class
14 * @param {Function} logger.logServerEvent Stringify object argument to log, adding timestamp and server ID properties
15 */
16 ve.dm.DocumentStore = function VeDmDocumentStore( storageClient, dbName, logger ) {
17 this.storageClient = storageClient;
18 this.dbName = dbName;
19 this.logger = logger;
20 this.db = null;
21 this.collection = null;
22 this.startForDoc = new Map();
23 this.serverId = null;
24 };
25
26 /**
27 * @return {Promise} Resolves when connected
28 */
29 ve.dm.DocumentStore.prototype.connect = function () {
30 const documentStore = this;
31 return this.storageClient.connect().then( ( client ) => {
32 const db = client.db( documentStore.dbName );
33 documentStore.logger.logServerEvent( { type: 'DocumentStore#connected', dbName: documentStore.dbName }, 'info' );
34 documentStore.db = db;
35 documentStore.collection = db.collection( 'vedocstore' );
36 return documentStore.collection.findOneAndUpdate(
37 { config: 'options' },
38 { $setOnInsert: { serverId: Math.random().toString( 36 ).slice( 2 ) } },
39 { upsert: true, returnDocument: 'after' }
40 );
41 } ).then( ( result ) => {
42 documentStore.serverId = result.value.serverId;
43 } );
44 };
45
46 /**
47 * @return {Promise} Drops the entire database
48 */
49 ve.dm.DocumentStore.prototype.dropDatabase = function () {
50 this.logger.logServerEvent( { type: 'DocumentStore#dropDatabase', dbName: this.dbName }, 'info' );
51 return this.db.dropDatabase();
52 };
53
54 /**
55 * Load a document from storage (creating as empty if absent)
56 *
57 * @param {string} docName Name of the document
58 * @return {Promise} Confirmed document history as a ve.dm.Change
59 */
60 ve.dm.DocumentStore.prototype.load = function ( docName ) {
61 const documentStore = this;
62 return this.collection.findOneAndUpdate(
63 { docName: docName },
64 { $setOnInsert: { start: 0, transactions: [], stores: [] } },
65 { upsert: true, returnDocument: 'after' }
66 ).then( ( result ) => {
67 const length = result.value.transactions.length || 0;
68 documentStore.logger.logServerEvent( { type: 'DocumentStore#loaded', docName: docName, length: length } );
69 documentStore.startForDoc.set( docName, result.value.start + length );
70 return ve.dm.Change.static.deserialize( {
71 start: 0,
72 transactions: result.value.transactions,
73 stores: result.value.stores,
74 selections: {}
75 }, true );
76 } );
77 };
78
79 /**
80 * Save a new change to storage
81 *
82 * @param {string} docName Name of the document
83 * @param {ve.dm.Change} change The new change
84 * @return {Promise} Resolves when saved
85 */
86 ve.dm.DocumentStore.prototype.onNewChange = function ( docName, change ) {
87 const serializedChange = change.serialize( true ),
88 expectedStart = this.startForDoc.get( docName ) || 0;
89
90 if ( expectedStart !== serializedChange.start ) {
91 return Promise.reject( 'Unmatched starts:', expectedStart, serializedChange.start );
92 }
93 this.startForDoc.set( docName, serializedChange.start + serializedChange.transactions.length );
94 return this.collection.updateOne(
95 { docName: docName },
96 {
97 $push: {
98 transactions: { $each: serializedChange.transactions },
99 stores: { $each: serializedChange.stores || serializedChange.transactions.map( () => null ) }
100 }
101 }
102 ).then( () => {
103 this.logger.logServerEvent( {
104 type: 'DocumentStore#onNewChange',
105 docName: docName,
106 start: serializedChange.start,
107 length: serializedChange.transactions.length
108 } );
109 } );
110 };
111
112 ve.dm.DocumentStore.prototype.onClose = function () {
113 this.logger.logServerEvent( { type: 'DocumentStore#onClose' }, 'info' );
114 this.storageClient.close();
115 };