home *** CD-ROM | disk | FTP | other *** search
- var gYaStorage = {
- init: function() {
- this._initStorage();
- OBSERVER_SERVICE.addObserver(this, "quit-application-granted", false);
- },
-
- _uninit: function() {
- this.shutdown();
- OBSERVER_SERVICE.removeObserver(this, "quit-application-granted");
- },
-
- shutdown: function() {
- try {
- this._dbClearStatementsCache();
-
- if (this._dbConnection)
- this._dbConnection.close();
- } catch(e) {}
- },
-
- observe: function(aSubject, aTopic, aData) {
- switch (aTopic) {
- case "quit-application-granted":
- this._uninit();
- break;
-
- default:
- break;
- }
- },
-
- get _storageService() {
- delete this._storageService;
- return this._storageService = Cc["@mozilla.org/storage/service;1"].getService(Ci.mozIStorageService);
- },
-
- DB_VERSION: 1,
-
- _dbSchema: {
- tables: {
- page_snapshots: "id INTEGER PRIMARY KEY,\
- hostRevert TEXT,\
- url TEXT UNIQUE ON CONFLICT REPLACE NOT NULL,\
- urlReal TEXT,\
- title TEXT,\
- checkTime INTEGER NOT NULL,\
- httpStatus INTEGER NOT NULL,\
- faviconUrl TEXT,\
- img BLOB"
- }
- },
-
- _storageInitialized: false,
-
- _dbConnection: null,
- _dbStatementsCache: null,
-
- _dbFile: null,
- _dbFileName: "yasearch-storage.sqlite",
-
- _dbCreateStatement: function(aQuery, aParams) {
- let wrappedStatement = (aQuery in this._dbStatementsCache) ? this._dbStatementsCache[aQuery] : null;
-
- if (!wrappedStatement) {
- wrappedStatement = Cc["@mozilla.org/storage/statement-wrapper;1"].createInstance(Ci.mozIStorageStatementWrapper);
- wrappedStatement.initialize(this._dbConnection.createStatement(aQuery));
- this._dbStatementsCache[aQuery] = wrappedStatement;
- }
-
- if (aParams)
- for (let name in aParams)
- wrappedStatement.params[name] = aParams[name];
-
- return wrappedStatement;
- },
-
- _initStorage: function() {
- this._dbStatementsCache = {};
-
- try {
- if (!this._dbFile) {
- this._dbFile = gYaSearchService.getYandexDir();
- this._dbFile.append(this._dbFileName);
- }
-
- this._dbInit();
- this._storageInitialized = true;
-
- } catch(e) {
- gYaSearchService.log("gYaStorage: " + this._dbFileName + " initialization failed: " + e);
- }
- },
-
- _dbInit: function () {
- let isFirstRun = false;
-
- try {
- this._dbConnection = this._storageService.openDatabase(this._dbFile);
-
- let version = this._dbConnection.schemaVersion;
- if (version === 0) {
- this._dbCreate();
- isFirstRun = true;
- } else if (version != this.DB_VERSION) {
- this._dbMigrate(version);
- }
- } catch (e if e.result == Components.results.NS_ERROR_FILE_CORRUPTED) {
- this._dbCleanup(true);
- throw e;
- } catch (e) {
- throw e;
- }
-
- return isFirstRun;
- },
-
- _dbCreate: function() {
- this._dbCreateSchema();
- this._dbConnection.schemaVersion = this.DB_VERSION;
- },
-
- _dbCreateSchema: function() {
- this._dbCreateTables();
- },
-
- _dbCreateTables: function() {
- for (let name in this._dbSchema.tables)
- this._dbConnection.createTable(name, this._dbSchema.tables[name]);
- },
-
- _dbMigrate: function(aVersion) {
- this._dbConnection.schemaVersion = this.DB_VERSION;
- },
-
- _dbCleanup: function(aBackup) {
- this._dbClearStatementsCache();
-
- try {
- this._dbConnection.close();
- } catch(e) {}
-
- this._dbFile.remove(false);
- },
-
- _dbClearStatementsCache: function() {
- if (this._dbStatementsCache) {
- try {
- for each (let dbStatement in this._dbStatementsCache)
- dbStatement.statement.finalize();
- } catch(e) {}
- }
-
- this._dbStatementsCache = {};
- },
-
- _dbCleanupOldURLData: function(aExistURLs) {
- let query = "DELETE FROM page_snapshots WHERE checkTime < :checkTime";
-
- let params = {
- checkTime: Date.now() - DAY_SECS
- }
-
- let append = [];
- let i = 1;
- aExistURLs.forEach(function(aURL) {
- append.push(" url != :url" + i);
- params["url" + i++] = aURL;
- });
-
- if (append.length)
- query += " AND " + append.join(" AND ");
-
- let stmt;
- try {
- stmt = this._dbCreateStatement(query, params);
- stmt.execute();
- } catch(e) {
- gYaSearchService.log("gYaStorage: Couldn't cleanup old data from database: " + e);
- } finally {
- if (stmt)
- stmt.reset();
- }
- },
-
- get _rowNames() {
- delete this._rowNames;
-
- return this._rowNames = this._dbSchema.tables.page_snapshots
- .split(",")
- .map(function(aRowName) { return aRowName.replace(/^\s+/, "").replace(/\s.*/, ""); });
- },
-
- setURLData: function(aData) {
- this._insertURLData(aData);
- },
-
- getImageForURL: function(aURL) {
- if (!(aURL && typeof aURL == "string"))
- return null;
-
- let query = "SELECT img FROM page_snapshots WHERE url = :url";
-
- let params = {
- url: aURL
- }
-
- let result = null,
- stmt;
-
- try {
- stmt = this._dbCreateStatement(query, params);
- if (stmt.step()) {
- result = stmt.row["img"];
- }
- } catch(e) {
- gYaSearchService.log("gYaStorage: Couldn't read from database: " + e);
- } finally {
- if (stmt)
- stmt.reset();
- }
-
- return result;
- },
-
- getURLData: function(aURL) {
- if (!(aURL && typeof aURL == "string"))
- return null;
-
- let query = "SELECT * FROM page_snapshots WHERE url = :url";
-
- let params = {
- url: aURL
- }
-
- let result = null,
- stmt;
-
- try {
- stmt = this._dbCreateStatement(query, params);
- if (stmt.step()) {
- result = {};
- this._rowNames.forEach(function(aRowName) {
- result[aRowName] = stmt.row[aRowName];
- });
- }
- } catch(e) {
- gYaSearchService.log("gYaStorage: Couldn't read from database: " + e);
- } finally {
- if (stmt)
- stmt.reset();
- }
-
- if (result) {
- result.state = (result.checkTime && !result.img) ? "error" : "";
- }
-
- return result;
- },
-
- _insertURLData: function(aData) {
- let params = {},
- paramsName = [];
-
- this._rowNames.forEach(function(aParamName) {
- if (aParamName in aData) {
- params[aParamName] = (aData[aParamName] || "").toString();
- paramsName.push(aParamName);
- }
- });
-
- let query, stmt;
-
- let existPage = this.getURLData(aData.url);
- if (existPage) {
- let values = [];
- paramsName.forEach(function(name) {
- if (name !== "url")
- values.push(name + " = :" + name);
- });
- query = "UPDATE page_snapshots SET " + values.join(", ") + " WHERE url = :url";
- } else {
- query = "INSERT INTO page_snapshots (" + paramsName.join(",") + ") " +
- "VALUES (:" + paramsName.join(", :") + ")";
- }
-
- try {
- stmt = this._dbCreateStatement(query, params);
- stmt.execute();
- } catch(e) {
- gYaSearchService.log("gYaStorage: Couldn't write to database: " + e);
- } finally {
- if (stmt)
- stmt.reset();
- }
- }
- };