Skip to content

Commit 8eaa354

Browse files
committed
core.16; Don't rely on _id; compound index on key,lang; remove dupes (#184)
1 parent 5ec9b33 commit 8eaa354

File tree

7 files changed

+73
-14
lines changed

7 files changed

+73
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Current versions of each package (requires manual, explicit updates until
1616
the stable release):
1717

1818
```
19-
meteor add msgfmt:core@2.0.0-preview.15 (2015-12-23)
19+
meteor add msgfmt:core@2.0.0-preview.16 # 2016-01-11
2020
meteor add msgfmt:extract@2.0.0-preview.12
2121
meteor add msgfmt:ui@2.0.0-preview.5
2222
```

msgfmt:core/.versions

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ http@1.1.1
3131
id-map@1.0.4
3232
jag:pince@0.0.6
3333
jquery@1.11.4
34-
local-test:msgfmt:core@2.0.0-preview.15
34+
local-test:msgfmt:core@2.0.0-preview.16
3535
logging@1.0.8
3636
meteor@1.1.10
3737
meteorhacks:inject-initial@1.0.2
3838
minifiers@1.1.7
3939
minimongo@1.0.10
4040
mongo@1.1.3
4141
mongo-id@1.0.1
42-
msgfmt:core@2.0.0-preview.15
42+
msgfmt:core@2.0.0-preview.16
4343
npm-mongo@1.4.39_1
4444
observe-sequence@1.0.7
4545
ordered-dict@1.0.4

msgfmt:core/History.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## vNEXT
22

3+
## 2.0.0-preview.16
4+
5+
* Don't rely on `_id` in translation data. Set compound index in mongo
6+
on (key, lang). Remove possible duplicates in database when upgrading.
7+
38
## 2.0.0-preview.15
49

510
* Run `syncAll` in it's own Fiber to avoid blocking on load

msgfmt:core/lib/mfPkg/messageformat-server.js

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var Fiber = Npm.require('fibers');
22

33
// server only, but used on the client in msgfmt:ui
4-
mfPkg.mfRevisions = new Mongo.Collection('mfRevisions');
4+
msgfmt.mfRevisions = new Mongo.Collection('mfRevisions');
55

66
/*
77
* New entry point for startup (native) and any language modification. Check that all
@@ -41,7 +41,8 @@ mfPkg.langUpdate = function(lang, strings, meta, lastSync) {
4141
*/
4242

4343
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'];
4546
for (key in strings) {
4647
str = strings[key];
4748
existing = this.strings[lang][key];
@@ -81,6 +82,8 @@ mfPkg.langUpdate = function(lang, strings, meta, lastSync) {
8182
if (str[optional[i]])
8283
obj[optional[i]] = str[optional[i]];
8384

85+
/*
86+
- since core.16 we no longer worry about _id's
8487
// insert unfound, or re-insert on wrong _id, otherwise update
8588
if (existing) {
8689
if (str._id && existing._id === str._id) {
@@ -93,6 +96,7 @@ mfPkg.langUpdate = function(lang, strings, meta, lastSync) {
9396
} else {
9497
dbInsert = true;
9598
}
99+
*/
96100

97101
/* meteor upsert does allow _id even for insert upsert
98102
if (this.strings[lang][key] && str._id
@@ -108,11 +112,16 @@ mfPkg.langUpdate = function(lang, strings, meta, lastSync) {
108112
this.strings[lang][key] = obj;
109113
*/
110114

115+
/*
111116
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)
113119
} 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);
115122
}
123+
*/
124+
this.mfStrings.upsert({ key: obj.key, lang: obj.lang }, obj);
116125

117126
if (updating) {
118127
// does this update affect translations?
@@ -377,13 +386,51 @@ WebApp.connectHandlers.use(function(req, res, next) {
377386

378387
msgfmt.strings = {};
379388

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+
380402
// 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+
381406
log.trace('Retrieving strings from database...');
382407
var startTime = Date.now();
383408
var allStrings = msgfmt.mfStrings.find().fetch();
384409
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+
}
389432
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 } );

msgfmt:core/package.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package.describe({
22
name: "msgfmt:core",
3-
version: "2.0.0-preview.15",
3+
version: "2.0.0-preview.16",
44
summary: "MessageFormat support, the Meteor way",
55
git: "https://github.com/gadicc/meteor-messageformat.git",
66
});

msgfmt:extract/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,10 @@
44
// for now, like this
55
msgfmt.extractLogLevel = 'trace'; // debug, warn, info
66
```
7+
8+
**Force rescan**:
9+
10+
```bash
11+
meteor mongo
12+
db.mfExtractFiles.remove({});
13+
```

website/.meteor/versions

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ mobile-status-bar@1.0.3
5757
momentjs:moment@2.10.6
5858
mongo@1.1.0
5959
mrt:sp-marked@0.0.2
60-
msgfmt:core@2.0.0-preview.14
60+
msgfmt:core@2.0.0-preview.16
6161
msgfmt:extract@2.0.0-preview.12
62-
msgfmt:ui@2.0.0-preview.5
62+
msgfmt:ui@2.0.0-preview.6
6363
nicolaslopezj:router-layer@0.0.11
6464
npm-bcrypt@0.7.8_2
6565
oauth@1.1.4

0 commit comments

Comments
 (0)