OSDN Git Service

Split database into 3 pieces to try and reduce the possibility of database corruption.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / sql / WordsTable.java
index c7ca076..66351c9 100644 (file)
 \r
 package cx.fbn.nevernote.sql;\r
 \r
-import cx.fbn.nevernote.Global;\r
-import cx.fbn.nevernote.sql.requests.WordRequest;\r
-\r
+import cx.fbn.nevernote.sql.driver.NSqlQuery;\r
+import cx.fbn.nevernote.utilities.ApplicationLogger;\r
 \r
 public class WordsTable {\r
-       private final int id;\r
+       private final ApplicationLogger                 logger;\r
+       private final DatabaseConnection                db;\r
+\r
        \r
        // Constructor\r
-       public WordsTable(int i) {\r
-               id = i;\r
+       public WordsTable(ApplicationLogger l, DatabaseConnection d) {\r
+               logger = l;\r
+               db = d;\r
        }\r
        // Create the table\r
        public void createTable() {\r
-               WordRequest request = new WordRequest();\r
-               request.requestor_id = id;\r
-               request.type = WordRequest.Create_Table;\r
-               Global.dbRunner.addWork(request);\r
+               NSqlQuery query = new NSqlQuery(db.getIndexConnection());\r
+        logger.log(logger.HIGH, "Creating table WORDS ...");\r
+        if (!query.exec("create table words (word varchar, guid varchar, source varchar, weight int, primary key (word, guid, source));")) {\r
+               logger.log(logger.HIGH, "Table WORDS creation FAILED!!!");   \r
+               logger.log(logger.HIGH, query.lastError());\r
+        }   \r
        }\r
        // Drop the table\r
        public void dropTable() {\r
-               WordRequest request = new WordRequest();\r
-               request.requestor_id = id;\r
-               request.type = WordRequest.Drop_Table;\r
-               Global.dbRunner.addWork(request);\r
+               NSqlQuery query = new NSqlQuery(db.getIndexConnection());\r
+               query.exec("drop table words");\r
        }\r
        // Count unindexed notes\r
        public int getWordCount() {\r
-               WordRequest request = new WordRequest();\r
-               request.requestor_id = id;\r
-               request.type = WordRequest.Get_Word_Count;\r
-               Global.dbRunner.addWork(request);\r
-               Global.dbClientWait(id);\r
-               WordRequest req = Global.dbRunner.wordResponse.get(id).copy();\r
-               return req.responseInt;\r
+        NSqlQuery query = new NSqlQuery(db.getIndexConnection());\r
+               query.exec("select count(*) from words");\r
+               query.next(); \r
+               int returnValue = new Integer(query.valueString(0));\r
+               return returnValue;\r
        }\r
 \r
        // Clear out the word index table\r
        public void clearWordIndex() {\r
-               WordRequest request = new WordRequest();\r
-               request.requestor_id = id;\r
-               request.type = WordRequest.Clear_Word_Index;\r
-               Global.dbRunner.addWork(request);\r
+               NSqlQuery query = new NSqlQuery(db.getIndexConnection());\r
+        logger.log(logger.HIGH, "DELETE FROM WORDS");\r
+        \r
+        boolean check = query.exec("DELETE FROM WORDS");\r
+        if (!check)\r
+               logger.log(logger.HIGH, "Table WORDS clear has FAILED!!!");  \r
        }       \r
 \r
        //********************************************************************************\r
@@ -70,24 +72,68 @@ public class WordsTable {
        //********************************************************************************\r
        //********************************************************************************\r
        public void expungeFromWordIndex(String guid, String type) {\r
-               WordRequest request = new WordRequest();\r
-               request.requestor_id = id;\r
-               request.type = WordRequest.Expunge_From_Word_Index;\r
-               request.string1 = guid;\r
-               request.string2 = type;\r
-               Global.dbRunner.addWork(request);\r
+               NSqlQuery deleteWords = new NSqlQuery(db.getIndexConnection());\r
+               if (!deleteWords.prepare("delete from words where guid=:guid and source=:source")) {\r
+                       logger.log(logger.EXTREME, "Note SQL select prepare deleteWords has failed.");\r
+                       logger.log(logger.MEDIUM, deleteWords.lastError());\r
+               }\r
+       \r
+               deleteWords.bindValue(":guid", guid);\r
+               deleteWords.bindValue(":source", type);\r
+               deleteWords.exec();\r
+\r
        }\r
        // Reindex a note\r
        public synchronized void addWordToNoteIndex(String guid, String word, String type, Integer weight) {\r
-               WordRequest request = new WordRequest();\r
-               request.requestor_id = id;\r
-               request.type = WordRequest.Add_Word_To_Note_Index;\r
-               request.string1 = guid;\r
-               request.string2 = word;\r
-               request.string3 = type;\r
-               request.int1 = weight;\r
-               Global.dbRunner.addWork(request);\r
-               Global.dbClientWait(id);\r
+               NSqlQuery findWords = new NSqlQuery(db.getIndexConnection());\r
+               if (!findWords.prepare("Select weight from words where guid=:guid and source=:type and word=:word")) {\r
+                       logger.log(logger.MEDIUM, "Prepare failed in addWordToNoteIndex()");\r
+                       logger.log(logger.MEDIUM, findWords.lastError());\r
+               }\r
+               \r
+               findWords.bindValue(":guid", guid);\r
+               findWords.bindValue(":type", type);\r
+               findWords.bindValue(":word", word);\r
+               \r
+               boolean addNeeded = true;\r
+               findWords.exec();\r
+               // If we have a match, find out which has the heigher weight & update accordingly\r
+               if (findWords.next()) {\r
+                       int recordWeight = new Integer(findWords.valueString(0));\r
+                       addNeeded = false;\r
+                       if (recordWeight < weight) {\r
+                               NSqlQuery updateWord = new NSqlQuery(db.getIndexConnection());\r
+                               if (!updateWord.prepare("Update words set weight=:weight where guid=:guid and source=:type and word=:word")) {\r
+                                       logger.log(logger.MEDIUM, "Prepare failed for find words in addWordToNoteIndex()");\r
+                                       logger.log(logger.MEDIUM, findWords.lastError());                                       \r
+                               }\r
+                               \r
+                               updateWord.bindValue(":weight", weight);\r
+                               updateWord.bindValue(":guid", guid);\r
+                               updateWord.bindValue(":type", type);\r
+                               updateWord.bindValue(":word",word);\r
+                               updateWord.exec();\r
+                       }\r
+               }\r
+               \r
+               \r
+               if (!addNeeded)\r
+                       return;\r
+               \r
+               NSqlQuery insertWords = new NSqlQuery(db.getIndexConnection());\r
+               if (!insertWords.prepare("Insert Into Words (word, guid, weight, source)"\r
+                               +" Values(:word, :guid, :weight, :type )")) {\r
+                       logger.log(logger.EXTREME, "Note SQL select prepare checkWords has failed.");\r
+                       logger.log(logger.MEDIUM, insertWords.lastError());\r
+               }\r
+               insertWords.bindValue(":word", word);\r
+               insertWords.bindValue(":guid", guid);\r
+               insertWords.bindValue(":weight", weight);\r
+               insertWords.bindValue(":type", type);\r
+               if (!insertWords.exec()) {\r
+                       String err = insertWords.lastError();\r
+                       logger.log(logger.MEDIUM, "Error inserting words into index: " +err);\r
+               }\r
        }\r
 \r
 \r