25 July 2009

Lucene and MySQL

I read about the full text search provided by MySQL over the web and had to look for a better option for indexing my data which grows everyday. Sphinx looked a better choice but the Java api is not fully mature yet so I had to choose Lucene which takes little more time than Sphinx in displaying the search results but is far better than MySQL full text search, comparison is given here. In some cases Lucene outperforms other two. Other advantage of Lucene is its sub-projects like: Solr, Compass, Nutch etc.

Here is the sample code for indexing the MySQL data: (Please find the corrected code here for the method getSqlData())

 
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.TopDocCollector;
import java.sql.*;
import java.io.IOException;

/**
*
* @author viksin
*/
public class LuceneImp {

public void getSqlData() {
   String IndexStoreDir = "F:/LuceneIndex";
   ResultSet rs;
   StandardAnalyzer analyzer = new StandardAnalyzer();
   IndexWriter writer = null;

   try {
       String connUrl = "jdbc:mysql://localhost:3306/SQLDb"; 
        // put your mysql database name in place of SQLDb
       Connection con = DriverManager.getConnection(connUrl, "root", "SQLPassword");
       // put your mysql password in place of SQLPassword
       Statement stmt = con.createStatement();
       Class.forName("com.mysql.jdbc.Driver");
       stmt.executeQuery("select * from table");
       writer = new IndexWriter(IndexStoreDir, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);
       rs = stmt.getResultSet();
       while (rs.next()) {
      
           addDoc(writer, "title", rs.getString("title"));
           addDoc(writer, "content", rs.getString("content"));

       }
       writer.close();
       rs.close();
       stmt.close();
   } catch (Exception ex) {
   }
}

private static void addDoc(IndexWriter w, String field, String value) throws IOException {
   Document doc = new Document();
   doc.add(new Field(field, value, Field.Store.YES, Field.Index.ANALYZED));
   w.addDocument(doc);
}
}

If you want to add a new sql row to an existing index replace true with false in the following line and run the above program.
 writer = new IndexWriter(IndexStoreDir, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);

You can put a column in the sql table (a flag) for checking and updating the indexing status of the row. In that case the you'll have to add following lines in the above code:
int indexStatus = 0;            
while (rs.next()) {
                indexStatus = rs.getInt("isindexed");
                if (indexStatus != 1) {
                    addDoc(writer, "title", rs.getString("title"));
                    addDoc(writer, "content", rs.getString("content"));
                    addDoc(writer, "link", rs.getString("link"));
                    addDoc(writer, "source", rs.getString("source"));
                    updateIndexStatus(rs.getString("id"));
                }

updateIndexStatus method will look like this:
            
    private static void updateIndexStatus(String Id) {
        try {
           String connUrl = "jdbc:mysql://localhost:3306/SQLDb"; 
           // put your mysql database name in place of SQLDb
           Connection con = DriverManager.getConnection(connUrl, "root", "SQLPassword");
           // put your mysql password in place of SQLPassword
            Statement stmt = con.createStatement();
            Class.forName("com.mysql.jdbc.Driver");
            stmt.executeQuery("update table set isindexed='1' where id="+Id+"");
            stmt.close();
        } catch (Exception ex) {
        }
    }

For searching your index this method can help:
 private static void searchIndex() {
      StandardAnalyzer analyzer = new StandardAnalyzer();
      String qu = "Java and Linux"; // put your keyword here
      String IndexStoreDir = "F:/LuceneIndex";
      try {
          Query q = new QueryParser("title", analyzer).parse(qu);
          int hitspp = 100; //hits per page
          IndexSearcher searcher = new IndexSearcher(IndexStoreDir);
          TopDocCollector collector = new TopDocCollector(hitspp);
          searcher.search(q, collector);
          ScoreDoc[] hits = collector.topDocs().scoreDocs;
          System.out.println("Found " + hits.length + " hits.");
          for (int i = 0; i < hits.length; ++i) {
                int docId = hits[i].doc;
                Document d = searcher.doc(docId);
                System.out.println((i + 1) + ". " + d.get("title"));
            }
            searcher.close();
        } catch (Exception ex1) {
        }
    }

Or you can use Luke for browsing index created by Lucene.

13 June 2009

Phenom II X4 940 BE Temperature Analysis

Little Overclocking and you'll hit the Max Temperature allowed for Phenom II X4 940 BE which is 62°C. I hit that temperature when I overclocked to 3.6 GHz with 1.4 V. Cool'n'Quiet helps in decreasing the CPU temperature by 5-6°C. My current idle temperature is 39°C-45°C with Cool'n'Quiet enabled and full load temperature goes upto 55°C (with playing FarCry2). Here is the analysis (using Biostar TA790GX 128MB):



Figure 1 (Y-axis: Temperature in °C, X-axis: Time in Mins)

Figure 1 shows the temperature for 3 different cases:
  • No Overclocking + Cool'N'Quiet enabled (Blue Line)
  • OCed to 3.4GHz + Cool'N'Quiet disabled + stock voltage (Red Line)
  • No Overclocking + Cool'N'Quiet disabled (Green Line)
Ran the test for one hour for each setting. The maximum temperature I hit was 55°C under full load and the lowest temperature was 39°C. My room temperature stays between 25-35°C so idle CPU temperature is not bad but its not good either. With stock CPU cooler I'll suggest to enable the cool n quiet feature. And if you want to overclock, don't exceed more than 3.4GHz, because that's the max you can go with the stock voltage. The Figure 2 shows the % of CPU usage in three conditions I mentioned above.



Figure 2

When OCed the CPU usage doesn't go more than 60%, but in case of Cool N Quiet enabled the CPU usage is around 90% most of the time.

Opening the case (CoolerMaster CM 690) from both the sides decreases the CPU temperature by 5°C and idle temperature hovers around 34°C-40°C (Figure 3). Will put more fans to improve the air flow, also will apply a new thermal paste some time soon. I am expecting an idle temperature around 30°C.

Figure 3

Here are a few screen shots of CPUID HWMonitor, CoreTemp64, CPU-Z:









07 June 2009

Overclocking Phenom II 940BE with Biostar TA790GX 128MB

I tried the Automate OverClock given in the Bios but it didn’t work, kept getting the blue screens on Vista x64. Tried to get the info about manual overclock option on the Google with no luck. Therefore I explored all the options which were available in the Bios. Here is how to over clock Phenom II 940 by increasing the Multiplier: (Will show how to overclock upto 3.6Ghz)

Step0: Disable Cool N Quiet in the Bios



Step1: Go to your bios T-Series tab, select Manual OverClock option



Step2: Click on the CPU FID/UID Control and Enable the Custom P-States



Step3: Select a Core FID



Note: Select a multiplier which your system can handle, higher multiplier means higher frequency hence it requires higher voltage which means higher CPU temperature. You need a very good cooling system (Cabinet!) for keeping your CPU temp low at higher frequencies.

Step4: Select a suitable voltage according the frequency selected in Step3 by clicking on the Core VID on the same screen. For 3.6Ghz select a voltage 1.4000V or little high but not very high.
Note: Over Clocking reduces the CPU life because of high voltage and temp. So if you want your CPU to last long (5+ years) don't go for over clocking.

24 May 2009

Crysis Ends

Finished playing Crysis, it was a great fun ride with a lot of action and also with a last moment crysis. Last level of Crysis has got a huge memory leak, it starts using 100% RAM and hangs. Patch 1.2 helps and running the game in DX 9 mode improves performance. This is how to set the Crysis in DirectX 9 mode: Open the Game Explorer (Vista) and right click on the Crysis icon.



The game is smaller than I expected, I was thinking in the line of FarCry but still it was fun playing Crysis. Downloading Crysis Warhead. One screenshot from last level of Crysis:



Also finished playing NFS Undercover, it is similar to Carbon, but again its just another NFS (Let me forget the Prostreet Horror!).

19 May 2009

State of Broadband in India

Today I received a surprise call from my Broadband provider, Tata Indicom. The person on the other end talked about their new Fair Usage Policy for those who have unlimited plans. The details of this policy are given here. The Indicom guy said that it was forced by the government (a lie!!). I have an unlimited 384Kbps plan, now according to this policy I can not upload+download more than 70GB (limitation!). I wonder if both speed and data transfer are restricted, why they call it an unlimited plan. On asking the same question from the Indicom guy, he said that this policy enables other users to use the Internet fairly (I use it unfairly!).

I have around 200GB in the downloading queue and with these restrictions in the place it will take around 4 months to download (not to forget the downtime and power cuts!). I can not get rid of Tata Indicom because Airtel does not cover my area (heard they also have similar policy!), Reliance broadband is worse than Indicom, BSNL guys are deaf and they don't really care about a thing in the world (govt employees!). Other option, USB Modems is another nightmare, never heard a good thing about them. Only BSNLs EVDO is good (based on rumours!) but again its availability and support is NIL in the Indian IT Hub.

To be frank, India (
a third world country!!) is still far behing in this field, we pay huge amounts to get nothing but headache in the name of broadband. Lets pray that one day after hundred years we'll get an unlimited 32 Mbps connection.

Here are my speedtest results:



And I pay 1100+ Rs. for this, which works 15 days a month and 15 hrs in a day.

18 May 2009

Gaming Begins

I bought my first PC in 2004 having configuration: 2.5 Gz P4, 128MB of RAM, 40 GB HDD, CD-R, 15'' Monitor. With 16 MB shared graphics I was left with 112MB of RAM but the worst part was its SiS chipset which is known for its bad graphics and also a 4x AGP slot (not 8x !) and with this combination gaming was just a dream. However in 2007 I did a full upgrade with 1GB RAM (Max supported!), an Nvidia FX 5 series 8x 256 MB card and a 160 GB hard drive, this enabled me to play the games which were released in 2005 or before. This was a branded PC from Compaq IL-5030 Series.

This time I decided to assemble a PC with my configuration, which can play latest games coming out in the market as well as next two years games with a little part upgrades. So I went for this configuration:

AMD Phenom II X4 940 BE
ATI 4870 1 GB DDR5
4GB DDR2 800 MHz (will upgrade to 8GB soon!)
Biostar TA 790GX 128MB Mobo with Crossfire (I can put one more Gfx Card :D)
500GB HDD + 320GB (Ext) + 500GB (Ext) - will add another 1.5 TB internal HDD
Here are a few screenshots of NFS Undercover (with 1440x900 @ 75 Hz):







Click on the image to see the full resolution image. FYI currently I am playing NFS and Crysis.

17 May 2009

Re-Turn

After a big confusion between Wordpress and Blogger I had to come back here. Since I rarely write anything, I have decided not to use any of the seven domain names I own. Wordpress (endex.wordpress.com) was my first choice but after coming across its limitations, I had to choose Blogger. My other two sites vikasing.com and viks.in will be redirected here.