Divec has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/334995 )
Change subject: WIPissimo: RebaseServer persistent storage ...................................................................... WIPissimo: RebaseServer persistent storage Change-Id: I06cb8a1264ec74ae96b90d525c8328237001e99c --- A rebaser/MongoStateStore.js M rebaser/server.js A rebaser/test-MongoStateStore.js M src/dm/ve.dm.RebaseDocState.js 4 files changed, 140 insertions(+), 4 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor refs/changes/95/334995/1 diff --git a/rebaser/MongoStateStore.js b/rebaser/MongoStateStore.js new file mode 100644 index 0000000..249d895 --- /dev/null +++ b/rebaser/MongoStateStore.js @@ -0,0 +1,87 @@ +/*! + * VisualEditor DataModel mongo state store class. + * + * @copyright 2011-2017 VisualEditor Team and others; see http://ve.mit-license.org + */ + +/* eslint-env node, es6 */ +/* eslint-disable no-console */ + +var ve = require( '../dist/ve-rebaser.js' ), + MongoClient = require( 'mongodb' ).MongoClient; + +/** + * DataModel mongo state store + * + * @class + * + * @constructor + * @param {Object} db MongoClient db connection + */ +function MongoStateStore( db ) { + this.db = db; + this.collection = db.collection( 'foo' ); +} + +/* Initialization */ + +OO.inheritClass( MongoStateStore, ve.dm.RebaseServer ); + +/* Static methods */ + +MongoStateStore.static.connect = ve.async( function* connect( url ) { + var db = yield MongoClient.connect( url ); + // yield db.dropDatabase(); + return new MongoStateStore( db ); +} ); + +/* Methods */ + +MongoStateStore.prototype.getDocState = ve.async( function* getDocState( doc ) { + var result = ( yield Promise.resolve( this.collection.find( { name: doc } ).toArray() ) )[ 0 ]; + console.log( 'MongoStateStore#getDocState', result ); + if ( !result ) { + result = { + name: doc, + transactions: [], + stores: [], + selections: {}, + continueBases: {}, + rejections: {} + }; + yield this.collection.insert( result ); + } + return ve.dm.RebaseDocState.static.deserialize( { + history: { + start: 0, + transactions: result.transactions, + stores: result.stores, + selections: result.selections + }, + continueBases: result.continueBases, + rejections: result.rejections + } ); +} ); + +MongoStateStore.prototype.updateDocState = ve.async( function* updateDocState( doc, author, newHistory, continueBase, rejections ) { + var update; + newHistory = newHistory.serialize(); + continueBase = continueBase ? continueBase.serialize() : null; + update = { + $push: { + transactions: { $each: newHistory.transactions }, + stores: { $each: newHistory.stores } + }, + $set: { + selections: newHistory.selections + } + }; + update.$set[ 'rejections.' + author ] = rejections; + if ( continueBase ) { + update.$set[ 'continueBases.' + author ] = continueBase; + } + console.log( 'MongoStateStore#updateDocStore', update ); + yield this.collection.update( { name: doc }, update ); +} ); + +module.exports = { MongoStateStore: MongoStateStore }; diff --git a/rebaser/server.js b/rebaser/server.js index 66cb48d..bc070f6 100644 --- a/rebaser/server.js +++ b/rebaser/server.js @@ -14,7 +14,8 @@ url = require( 'url' ), http = require( 'http' ).Server( app ), io = require( 'socket.io' )( http ), - ve = require( '../dist/ve-rebaser.js' ); + ve = require( '../dist/ve-rebaser.js' ), + MongoStateStore = require( './MongoStateStore.js' ).MongoStateStore; function summarize( author, backtrack, change ) { var storeCount = 0, @@ -49,7 +50,7 @@ console.log( err.stack ); } -rebaseServer = new ve.dm.RebaseServer(); +rebaseServer = null; docNamespaces = new Map(); lastAuthorForDoc = new Map(); pendingForDoc = new Map(); @@ -131,5 +132,11 @@ } } ); -http.listen( port ); -console.log( 'Listening on ' + port + ' (artificial delay ' + artificialDelay + ' ms)' ); +MongoStateStore.static.connect( 'mongodb://localhost:27017/test' ).then( function ( store ) { + rebaseServer = store; + http.listen( port ); + console.log( 'Listening on ' + port + ' (artificial delay ' + artificialDelay + ' ms)' ); +} ).catch( function ( err ) { + console.error( err.stack ); + rebaseServer.db.close(); +} ); diff --git a/rebaser/test-MongoStateStore.js b/rebaser/test-MongoStateStore.js new file mode 100644 index 0000000..2437865 --- /dev/null +++ b/rebaser/test-MongoStateStore.js @@ -0,0 +1,28 @@ +/* eslint-env node, es6 */ +/* eslint-disable no-console */ + +var store, + ve = require( '../dist/ve-rebaser.js' ), + MongoStateStore = require( './MongoStateStore.js' ).MongoStateStore; + +ve.spawn( function* main() { + var state; + store = yield MongoStateStore.static.connect( 'mongodb://localhost:27017/test' ); + yield store.db.dropDatabase(); + console.log( 'connected' ); + state = yield store.getDocState( 'foo' ); + console.log( state ); + yield store.updateDocState( + 'foo', + 'fred', + new ve.dm.Change( 0, [], [], {} ), + new ve.dm.Change( 0, [], [], {} ), + 1 + ( state.rejections.get( 'fred' ) || 0 ) + ); +}() ).catch( function ( err ) { + console.error( err.stack ); +} ).then( function () { + if ( store ) { + store.db.close(); + } +} ); diff --git a/src/dm/ve.dm.RebaseDocState.js b/src/dm/ve.dm.RebaseDocState.js index e8d6c96..aae80ef 100644 --- a/src/dm/ve.dm.RebaseDocState.js +++ b/src/dm/ve.dm.RebaseDocState.js @@ -36,3 +36,17 @@ /* Inheritance */ OO.initClass( ve.dm.RebaseDocState ); + +ve.dm.RebaseDocState.static.deserialize = function ( data ) { + var history, continueBases, author, rejections; + history = ve.dm.Change.static.deserialize( data.history ); + continueBases = new Map(); + for ( author in data.continueBases ) { + continueBases.set( author, ve.dm.Change.static.deserialize( data.continueBases[ author ] ) ); + } + rejections = new Map(); + for ( author in data.rejections ) { + rejections.set( author, data.rejections[ author ] ); + } + return new ve.dm.RebaseDocState( history, continueBases, rejections ); +}; -- To view, visit https://gerrit.wikimedia.org/r/334995 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I06cb8a1264ec74ae96b90d525c8328237001e99c Gerrit-PatchSet: 1 Gerrit-Project: VisualEditor/VisualEditor Gerrit-Branch: master Gerrit-Owner: Divec <da...@troi.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits