OSDN Git Service

Add connection throttling to try and reduce indexing & synchronizing impact.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / sql / DatabaseConnection.java
1 /*
2  * This file is part of NeverNote 
3  * Copyright 2009 Randy Baumgarte
4  * 
5  * This file may be licensed under the terms of of the
6  * GNU General Public License Version 2 (the ``GPL'').
7  *
8  * Software distributed under the License is distributed
9  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
10  * express or implied. See the GPL for the specific language
11  * governing rights and limitations.
12  *
13  * You should have received a copy of the GPL along with this
14  * program. If not, go to http://www.gnu.org/licenses/gpl.html
15  * or write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18 */
19 package cx.fbn.nevernote.sql;
20
21 import java.io.File;
22 import java.sql.Connection;
23 import java.sql.DriverManager;
24 import java.sql.SQLException;
25
26 import com.trolltech.qt.sql.QJdbc;
27
28 import cx.fbn.nevernote.Global;
29 import cx.fbn.nevernote.sql.driver.NSqlQuery;
30 import cx.fbn.nevernote.utilities.ApplicationLogger;
31
32
33 public class DatabaseConnection {
34         // Table helpers
35         private WordsTable                                      wordsTable;
36         private TagTable                                        tagTable;
37         private NotebookTable                           notebookTable;
38         private NoteTable                                       noteTable;
39         private DeletedTable                            deletedTable;
40         private SavedSearchTable                        searchTable;
41         private WatchFolderTable                        watchFolderTable;
42         private InvalidXMLTable                         invalidXMLTable;
43         private LinkedNotebookTable                     linkedNotebookTable;
44         private SharedNotebookTable                     sharedNotebookTable;
45         private InkImagesTable                          inkImagesTable;
46         private SyncTable                                       syncTable;
47         private SystemIconTable                         systemIconTable;
48         private final ApplicationLogger         logger;
49         private Connection                                      conn;
50         int throttle=0;
51         int id;
52
53         
54         public DatabaseConnection(ApplicationLogger l, String url, String userid, String password, String cypherPassword, int throttle) {
55                 logger = l;
56                 this.throttle = throttle;
57                 dbSetup(url, userid, password, cypherPassword);
58         }
59         
60         private void setupTables() {
61                 tagTable = new TagTable(logger, this);
62                 notebookTable = new NotebookTable(logger, this);
63                 noteTable = new NoteTable(logger, this);
64                 deletedTable = new DeletedTable(logger, this);
65                 searchTable = new SavedSearchTable(logger, this);       
66                 watchFolderTable = new WatchFolderTable(logger, this);
67                 invalidXMLTable = new InvalidXMLTable(logger, this);
68                 wordsTable = new WordsTable(logger, this);
69                 syncTable = new SyncTable(logger, this);
70                 linkedNotebookTable = new LinkedNotebookTable(logger, this);
71                 sharedNotebookTable = new SharedNotebookTable(logger, this);
72                 systemIconTable = new SystemIconTable(logger, this);
73                 inkImagesTable = new InkImagesTable(logger, this);
74         }
75         
76         
77         // Compact the database
78         public void compactDatabase() {
79                 
80         }
81         
82         // Initialize the database connection
83         public void dbSetup(String url,String userid, String userPassword, String cypherPassword) {
84                 logger.log(logger.HIGH, "Entering DatabaseConnection.dbSetup " +id);
85
86                 
87                 try {
88                         Class.forName("org.h2.Driver");
89                 } catch (ClassNotFoundException e1) {
90                         e1.printStackTrace();
91                         System.exit(16);
92                 }
93                 
94                 QJdbc.initialize();
95                 
96                 setupTables();
97                 
98                 File f = Global.getFileManager().getDbDirFile(Global.databaseName + ".h2.db");
99                 boolean dbExists = f.exists(); 
100                 
101                 logger.log(logger.HIGH, "Entering RDatabaseConnection.dbSetup");
102                 
103
104                 try {
105                         String passwordString = null;
106                         if (cypherPassword==null || cypherPassword.trim().equals(""))
107                                 passwordString = userPassword;
108                         else
109                                 passwordString = cypherPassword+" "+userPassword;
110 //                      conn = DriverManager.getConnection(url,userid,passwordString);
111 //                      conn = DriverManager.getConnection(url+";CACHE_SIZE=4096",userid,passwordString);
112                         if (throttle == 0)
113                                 conn = DriverManager.getConnection(url+";CACHE_SIZE="+Global.databaseCache,userid,passwordString);
114                         else
115                                 conn = DriverManager.getConnection(url+";THROTTLE=" +new Integer(throttle).toString()+";CACHE_SIZE="+Global.databaseCache,userid,passwordString);
116 //                      conn = DriverManager.getConnection(url+";AUTO_SERVER=TRUE",userid,passwordString);
117                 } catch (SQLException e) {
118                         e.printStackTrace();
119                         return;
120                 }
121                 
122                 // If it doesn't exist and we are the main thread, then we need to create stuff.
123                 if (!dbExists)  {
124                         createTables();
125                         Global.setAutomaticLogin(false);
126                 }               
127                 
128                 logger.log(logger.HIGH, "Leaving DatabaseConnection.dbSetup" +id);
129         }
130         
131         
132         public void dbShutdown() {
133                 logger.log(logger.HIGH, "Entering RDatabaseConnection.dbShutdown");
134                 try {
135                         conn.close();
136                 } catch (SQLException e) {
137                         e.printStackTrace();
138                 }
139                 logger.log(logger.HIGH, "Leaving RDatabaseConnection.dbShutdown");
140         }
141         
142         public void upgradeDb(String version) {
143                 if (version.equals("0.85")) {
144                         executeSql("alter table note add column titleColor integer");
145                         executeSql("alter table note add column thumbnail blob");
146                         executeSql("alter table note add column thumbnailneeded boolean");
147                         executeSql("Update note set thumbnailneeded = true;");
148                         executeSql("create index NOTE_NOTEBOOK_INDEX on note (notebookguid, guid);");
149                         executeSql("create index NOTETAGS_TAG_INDEX on notetags (tagguid, noteguid);");
150                         version = "0.86";
151                         Global.setDatabaseVersion(version);
152                 } 
153                 if (version.equals("0.86")) {
154         
155                         executeSql("alter table notebook add column publishingUri VarChar");
156                         executeSql("alter table notebook add column publishingOrder Integer");
157                         executeSql("alter table notebook add column publishingAscending Boolean");
158                         executeSql("alter table notebook add column publishingPublicDescription varchar");
159                         executeSql("alter table notebook add column stack varchar");
160                         executeSql("alter table notebook add column icon blob");
161                         executeSql("alter table notebook add column readOnly boolean");
162                         executeSql("alter table notebook add column linked boolean");
163                         
164                         executeSql("alter table tag add column realname varchar");
165                         executeSql("alter table tag add column linked boolean");
166                         executeSql("alter table tag add column icon blob");
167                         executeSql("alter table tag add column notebookguid varchar");
168                         executeSql("alter table SavedSearch add column icon blob");
169
170                         executeSql("create index NOTE_THUMBNAIL_INDEX on note (thumbnailneeded, guid);");
171                         executeSql("create index NOTE_EXPUNGED_INDEX on note (isExpunged, guid);");
172                         executeSql("create index NOTE_DUEDATE_INDEX on note (attributeSubjectDate, guid);");
173                         executeSql("create index RESOURCES_GUID_INDEX on noteresources (noteGuid, guid);");
174                         executeSql("create index TAG_NOTEBOOK_INDEX on tag (notebookGuid);");
175                         
176                         executeSql("update note set thumbnailneeded=true, thumbnail=null;");
177                         executeSql("update notebook set publishingUri='', " +
178                                         "publishingAscending=false, stack='', readonly=false, publishingOrder=1, " +
179                                         "publishingPublicDescription='', linked=false");
180                         executeSql("update tag set linked=false, realname='', notebookguid=''");
181                         
182                         sharedNotebookTable.createTable();
183                         linkedNotebookTable.createTable();
184                         systemIconTable.createTable();
185                         inkImagesTable.createTable();
186                         
187                         version = "0.95";
188                         executeSql("Insert into Sync (key, value) values ('FullNotebookSync', 'true')");
189                         executeSql("Insert into Sync (key, value) values ('FullLinkedNotebookSync', 'true')");
190                         executeSql("Insert into Sync (key, value) values ('FullSharedNotebookSync', 'true')");
191                         executeSql("Insert into Sync (key, value) values ('FullInkNoteImageSync', 'true')");
192                         executeSql("Update note set indexneeded='true'");
193                         executeSql("Update noteresources set indexneeded='true'");
194                         Global.setDatabaseVersion(version);
195                 } 
196         }
197         
198         public void executeSql(String sql) {
199                 NSqlQuery query = new NSqlQuery(conn);
200                 query.exec(sql);        
201         }
202         
203         public void checkDatabaseVersion() {
204                 if (!Global.getDatabaseVersion().equals("0.86")) {
205                         upgradeDb(Global.getDatabaseVersion());
206                 }
207                 if (!Global.getDatabaseVersion().equals("0.95")) {
208                         upgradeDb(Global.getDatabaseVersion());
209                 }
210         }
211         
212
213         public void backupDatabase(int highSequence, long date) {
214                 
215         }
216         
217         
218         public void createTables() {
219                 Global.setDatabaseVersion("0.85");
220                 Global.setAutomaticLogin(false);
221                 Global.saveCurrentNoteGuid("");
222                 Global.saveUploadAmount(0);
223                 
224                 getTagTable().createTable();
225                 notebookTable.createTable(true);
226                 noteTable.createTable();
227                 deletedTable.createTable();             
228                 searchTable.createTable();
229                 watchFolderTable.createTable();
230                 invalidXMLTable.createTable();
231                 wordsTable.createTable();
232                 syncTable.createTable();
233         }
234         
235         public Connection getConnection() {
236                 return conn;
237         }
238         
239         //***************************************************************
240         //* Table get methods
241         //***************************************************************
242         public DeletedTable getDeletedTable() {
243                 return deletedTable;
244         }
245         public TagTable getTagTable() {
246                 return tagTable;
247         }
248         public NoteTable getNoteTable() {
249                 return noteTable;
250         }
251         public NotebookTable getNotebookTable() {
252                 return notebookTable;
253         }
254         public SavedSearchTable getSavedSearchTable() {
255                 return searchTable;
256         }
257         public WatchFolderTable getWatchFolderTable() {
258                 return watchFolderTable;
259         }
260         public WordsTable getWordsTable() {
261                 return wordsTable;
262         }
263         public InvalidXMLTable getInvalidXMLTable() {
264                 return invalidXMLTable;
265         }
266         public SyncTable getSyncTable() {
267                 return syncTable;
268         }
269         public LinkedNotebookTable getLinkedNotebookTable() {
270                 return linkedNotebookTable;
271         }
272         public SharedNotebookTable getSharedNotebookTable() {
273                 return sharedNotebookTable;
274         }
275         public SystemIconTable getSystemIconTable() {
276                 return systemIconTable;
277         }
278         public InkImagesTable getInkImagesTable() {
279                 return inkImagesTable;
280         }
281
282 }