| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
'use strict'; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 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 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 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 |
}; |