1
1
var Fiber = Npm . require ( 'fibers' ) ;
2
2
3
3
// server only, but used on the client in msgfmt:ui
4
- mfPkg . mfRevisions = new Mongo . Collection ( 'mfRevisions' ) ;
4
+ msgfmt . mfRevisions = new Mongo . Collection ( 'mfRevisions' ) ;
5
5
6
6
/*
7
7
* New entry point for startup (native) and any language modification. Check that all
@@ -41,7 +41,8 @@ mfPkg.langUpdate = function(lang, strings, meta, lastSync) {
41
41
*/
42
42
43
43
var str , existing , revisionId , obj , updating , dbInsert , result , query ,
44
- optional = [ '_id' , 'file' , 'line' , 'template' , 'func' , 'removed' , 'fuzzy' ] ;
44
+ // removed in core.16; _id
45
+ optional = [ /*'_id',*/ 'file' , 'line' , 'template' , 'func' , 'removed' , 'fuzzy' ] ;
45
46
for ( key in strings ) {
46
47
str = strings [ key ] ;
47
48
existing = this . strings [ lang ] [ key ] ;
@@ -81,6 +82,8 @@ mfPkg.langUpdate = function(lang, strings, meta, lastSync) {
81
82
if ( str [ optional [ i ] ] )
82
83
obj [ optional [ i ] ] = str [ optional [ i ] ] ;
83
84
85
+ /*
86
+ - since core.16 we no longer worry about _id's
84
87
// insert unfound, or re-insert on wrong _id, otherwise update
85
88
if (existing) {
86
89
if (str._id && existing._id === str._id) {
@@ -93,6 +96,7 @@ mfPkg.langUpdate = function(lang, strings, meta, lastSync) {
93
96
} else {
94
97
dbInsert = true;
95
98
}
99
+ */
96
100
97
101
/* meteor upsert does allow _id even for insert upsert
98
102
if (this.strings[lang][key] && str._id
@@ -108,11 +112,16 @@ mfPkg.langUpdate = function(lang, strings, meta, lastSync) {
108
112
this.strings[lang][key] = obj;
109
113
*/
110
114
115
+ /*
111
116
if (dbInsert) {
112
- obj . _id = this . mfStrings . insert ( obj )
117
+ // obj._id = this.mfStrings.insert(obj) - v16 deprecates use of _id
118
+ obj._id = this.mfStrings.insert(obj)
113
119
} else {
114
- this . mfStrings . update ( obj . _id , obj ) ;
120
+ // this.mfStrings.update(obj._id, obj); - v16 deprecates use of _id
121
+ this.mfStrings.update({ key: obj.key, lang: obj.lang }, obj);
115
122
}
123
+ */
124
+ this . mfStrings . upsert ( { key : obj . key , lang : obj . lang } , obj ) ;
116
125
117
126
if ( updating ) {
118
127
// does this update affect translations?
@@ -377,13 +386,51 @@ WebApp.connectHandlers.use(function(req, res, next) {
377
386
378
387
msgfmt . strings = { } ;
379
388
389
+ /*
390
+ * As of preview.16, we no longer rely on `_id` and properly set a unique compound
391
+ * index on `key` and `lang`. In case the database has previous dups, let's clean
392
+ * them up.
393
+ */
394
+
395
+ var checkForDupes = ( msgfmt . meta . all && ! msgfmt . meta . all . _dupeFree ) ;
396
+ if ( checkForDupes ) {
397
+ msgfmt . mfMeta . update ( 'all' , { $set : { _dupeFree : 1 } } ) ;
398
+ msgfmt . meta . all . _dupeFree = true ;
399
+ log . warn ( 'Checking for dupes in mfStrings... (once off on upgrade from < core.15)' ) ;
400
+ }
401
+
380
402
// Yes, we absolutely do want this to block, to be fully loaded before mfAll.js etc
403
+ // But in future we could see what else needs this and queue in a Fiber
404
+ // e.g. make sure when requesting lang data that we're fully loaded, etc.
405
+
381
406
log . trace ( 'Retrieving strings from database...' ) ;
382
407
var startTime = Date . now ( ) ;
383
408
var allStrings = msgfmt . mfStrings . find ( ) . fetch ( ) ;
384
409
log . trace ( 'Finished retrieval in ' + ( Date . now ( ) - startTime ) + ' ms' ) ;
385
- _ . each ( allStrings , function ( str ) {
386
- checkLocaleMetaExists ( str . lang ) ;
387
- msgfmt . strings [ str . lang ] [ str . key ] = str ;
388
- } ) ;
410
+ if ( checkForDupes ) {
411
+ _ . each ( allStrings , function ( str ) {
412
+ checkLocaleMetaExists ( str . lang ) ;
413
+ var existing = msgfmt . strings [ str . lang ] [ str . key ] ;
414
+ if ( existing ) {
415
+ log . warn ( 'Duplicate "' + str . key + '" (' + str . lang + '), keeping latest.' ) ;
416
+ if ( str . mtime > existing . mtime ) {
417
+ msgfmt . strings [ str . lang ] [ str . key ] = str ; // overwrite with newer
418
+ msgfmt . mfStrings . remove ( { key : existing . key , lang : existing . lang } ) ;
419
+ } else {
420
+ // current str is older, remove it, don't overwrite in msgfmt.strings
421
+ msgfmt . mfStrings . remove ( { key : str . key , lang : str . lang } ) ;
422
+ }
423
+ } else
424
+ msgfmt . strings [ str . lang ] [ str . key ] = str ;
425
+ } ) ;
426
+ } else {
427
+ _ . each ( allStrings , function ( str ) {
428
+ checkLocaleMetaExists ( str . lang ) ;
429
+ msgfmt . strings [ str . lang ] [ str . key ] = str ;
430
+ } ) ;
431
+ }
389
432
delete allStrings ;
433
+
434
+ // ensure compound indexe on server only (collection exists in both)
435
+ // move to top of file when we're pretty sure everyone is _dupeFree
436
+ msgfmt . mfStrings . _ensureIndex ( { key :1 , lang :1 } , { unique : true } ) ;
0 commit comments