1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Message DB Cache manager
*/
/* :::::::: Constants and Helpers ::::::::::::::: */
this.EXPORTED_SYMBOLS = ["msgDBCacheManager"];
var Cc = Components.classes;
var Ci = Components.interfaces;
var Cu = Components.utils;
Cu.import("resource:///modules/mailServices.js");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource:///modules/gloda/log4moz.js");
var log = Log4Moz.getConfiguredLogger("mailnews.database.dbcache");
/**
*/
var DBCACHE_INTERVAL_DEFAULT_MS = 60000; // 1 minute
/* :::::::: The Module ::::::::::::::: */
var msgDBCacheManager =
{
_initialized: false,
_msgDBCacheTimer: null,
_msgDBCacheTimerIntervalMS: DBCACHE_INTERVAL_DEFAULT_MS,
_dbService: null,
/**
* This is called on startup
*/
init: function dbcachemgr_init()
{
if (this._initialized)
return;
this._dbService = Cc["@mozilla.org/msgDatabase/msgDBService;1"]
.getService(Ci.nsIMsgDBService);
// we listen for "quit-application-granted" instead of
// "quit-application-requested" because other observers of the
// latter can cancel the shutdown.
Services.obs.addObserver(this, "quit-application-granted", false);
this.startPeriodicCheck();
this._initialized = true;
},
/* ........ Timer Callback ................*/
_dbCacheCheckTimerCallback: function dbCache_CheckTimerCallback()
{
msgDBCacheManager.checkCachedDBs();
},
/* ........ Observer Notification Handler ................*/
observe: function dbCache_observe(aSubject, aTopic, aData) {
switch (aTopic) {
// This is observed before any windows start unloading if something other
// than the last 3pane window closing requested the application be
// shutdown. For example, when the user quits via the file menu.
case "quit-application-granted":
Services.obs.removeObserver(this, "quit-application-granted");
this.stopPeriodicCheck();
break;
}
},
/* ........ Public API ................*/
/**
* Stops db cache check
*/
stopPeriodicCheck: function dbcache_stopPeriodicCheck()
{
if (this._dbCacheCheckTimer) {
this._dbCacheCheckTimer.cancel();
delete this._dbCacheCheckTimer;
this._dbCacheCheckTimer = null;
}
},
/**
* Starts periodic db cache check
*/
startPeriodicCheck: function dbcache_startPeriodicCheck()
{
if (!this._dbCacheCheckTimer) {
this._dbCacheCheckTimer = Cc["@mozilla.org/timer;1"]
.createInstance(Ci.nsITimer);
this._dbCacheCheckTimer.initWithCallback(
this._dbCacheCheckTimerCallback,
this._msgDBCacheTimerIntervalMS,
Ci.nsITimer.TYPE_REPEATING_SLACK);
}
},
/**
* Checks if any DBs need to be closed due to inactivity or too many of them open.
*/
checkCachedDBs: function()
{
let idleLimit = Services.prefs.getIntPref("mail.db.idle_limit");
let maxOpenDBs = Services.prefs.getIntPref("mail.db.max_open");
// db.lastUseTime below is in microseconds while Date.now and idleLimit pref
// is in milliseconds.
let closeThreshold = (Date.now() - idleLimit) * 1000;
let cachedDBs = this._dbService.openDBs;
log.info("Periodic check of cached folder databases (DBs), count=" + cachedDBs.length);
// Count databases that are already closed or get closed now due to inactivity.
let numClosing = 0;
// Count databases whose folder is open in a window.
let numOpenInWindow = 0;
let dbs = [];
for (let i = 0; i < cachedDBs.length; i++) {
let db = cachedDBs.queryElementAt(i, Ci.nsIMsgDatabase);
if (!db.folder.databaseOpen) {
// The DB isn't really open anymore.
log.debug("Skipping, DB not open for folder: " + db.folder.name);
numClosing++;
continue;
}
if (MailServices.mailSession.IsFolderOpenInWindow(db.folder)) {
// The folder is open in a window so this DB must not be closed.
log.debug("Skipping, DB open in window for folder: " + db.folder.name);
numOpenInWindow++;
continue;
}
if (db.lastUseTime < closeThreshold)
{
// DB open too log without activity.
log.debug("Closing expired DB for folder: " + db.folder.name);
db.folder.msgDatabase = null;
numClosing++;
continue;
}
// Database eligible for closing.
dbs.push(db);
}
log.info("DBs open in a window: " + numOpenInWindow + ", DBs open: " + dbs.length + ", DBs already closing: " + numClosing);
let dbsToClose = Math.max(dbs.length - Math.max(maxOpenDBs - numOpenInWindow, 0), 0);
if (dbsToClose > 0) {
// Close some DBs so that we do not have more than maxOpenDBs.
// However, we skipped DBs for folders that are open in a window
// so if there are so many windows open, it may be possible for
// more than maxOpenDBs folders to stay open after this loop.
log.info("Need to close " + dbsToClose + " more DBs");
// Order databases by lowest lastUseTime (oldest) at the end.
dbs.sort((a, b) => b.lastUseTime - a.lastUseTime);
while (dbsToClose > 0) {
let db = dbs.pop();
log.debug("Closing DB for folder: " + db.folder.name);
db.folder.msgDatabase = null;
dbsToClose--;
}
}
},
};
|