/*
 * Copyright 2012 Takao Nakaguchi
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.trie4j.doublearray;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;

import org.junit.Test;
import org.trie4j.Trie;
import org.trie4j.util.CharsetUtil;
import org.trie4j.util.LapTimer;

public class TestIO {
	private static final int maxCount = 2000000;

	@Test
	public void testSave() throws Exception{
		// You can download archive from http://dumps.wikimedia.org/jawiki/latest/
		BufferedReader r = new BufferedReader(new InputStreamReader(
				new GZIPInputStream(new FileInputStream("jawiki-20120220-all-titles-in-ns0.gz"))
//				new GZIPInputStream(new FileInputStream("enwiki-20120403-all-titles-in-ns0.gz"))
				, CharsetUtil.newUTF8Decoder()));
		System.out.println("--- building patricia trie ---");
		Trie trie = new org.trie4j.patricia.multilayer.MultilayerPatriciaTrie();
		int c = 0;
		String word = null;
		LapTimer t1 = new LapTimer();
		while((word = r.readLine()) != null){
			trie.insert(word);
			c++;
			if(c == maxCount) break;
		}
		System.out.println("done in " + t1.lap() + " millis.");
		System.out.println(c + "entries in ja wikipedia titles.");

		System.out.println("-- building double array.");
		t1.lap();
		TailCompactionDoubleArray da = new TailCompactionDoubleArray(trie);
		trie = null;
		System.out.println("done in " + t1.lap() + " millis.");

		OutputStream os = new FileOutputStream("da.dat");
		try{
			System.out.println("-- saving double array.");
			t1.lap();
			da.save(os);
			System.out.println("done in " + t1.lap() + " millis.");
			da.dump();
		} finally{
			os.close();
		}
	}

	@Test
	public void testLoad() throws Exception{
		TailCompactionDoubleArray da = new TailCompactionDoubleArray();
		LapTimer t = new LapTimer();
		System.out.println("-- loading double array.");
		da.load(new FileInputStream("da.dat"));
		da.dump();
		System.out.println("done in " + t.lap() + " millis.");
		verify(da);
		System.out.println("---- common prefix search ----");
		System.out.println("-- for 東京国際フォーラム");
		for(String s : da.commonPrefixSearch("東京国際フォーラム")){
			System.out.println(s);
		}
		System.out.println("-- for 大阪城ホール");
		for(String s : da.commonPrefixSearch("大阪城ホール")){
			System.out.println(s);
		}
		System.out.println("---- predictive search ----");
		System.out.println("-- for 大阪城");
		for(String s : da.predictiveSearch("大阪城")){
			System.out.println(s);
		}
		Thread.sleep(10000);
		da.contains("hello");
	}

	private static void verify(Trie da) throws Exception{
		System.out.println("verifying double array...");
		BufferedReader r = new BufferedReader(new InputStreamReader(
				new GZIPInputStream(new FileInputStream("jawiki-20120220-all-titles-in-ns0.gz"))
//				new GZIPInputStream(new FileInputStream("enwiki-20120403-all-titles-in-ns0.gz"))
				, CharsetUtil.newUTF8Decoder()));
		int c = 0;
		int sum = 0;
		String word = null;
		LapTimer t1 = new LapTimer();
		LapTimer t = new LapTimer();
		while((word = r.readLine()) != null){
			if(c == maxCount) break;
			t.lap();
			boolean found = da.contains(word);
			sum += t.lap();
			c++;
			if(!found){
				System.out.println("verification failed.  trie not contains " + c + " th word: [" + word + "]");
				break;
			}
		}
		System.out.println("done " + c + "words in " + t1.lap() + " millis.");
		System.out.println("contains time: " + sum + " millis.");
	}
}

@@ -763,19 +763,33 @@
 rt java.
-io.
+util.zip.Deflater
 OutputSt
@@ -831,16 +831,58 @@
 tStream;
+%0Aimport java.util.zip.InflaterInputStream;
 %0A%0Aimport
@@ -1060,16 +1060,17 @@
 000000;%0A
+%09
 %0A%09@Test%0A
@@ -2090,16 +2090,24 @@
 .%22);%0A%0A%09%09
+Deflater
 OutputSt
@@ -2116,24 +2116,49 @@
 am os = new 
+DeflaterOutputStream(new 
 FileOutputSt
@@ -2171,16 +2171,17 @@
 da.dat%22)
+)
 ;%0A%09%09try%7B
@@ -2346,16 +2346,47 @@
 inally%7B%0A
+%09%09%09os.finish();%0A%09%09%09os.flush();%0A
 %09%09%09os.cl
@@ -2606,24 +2606,48 @@
 da.load(new 
+InflaterInputStream(new 
 FileInputStr
@@ -2664,20 +2664,8 @@
 t%22))
-;%0A%09%09da.dump(
 );%0A%09
@@ -2720,16 +2720,29 @@
 lis.%22);%0A
+%09%09da.dump();%0A
 %09%09verify

use inflater/deflater in IO test.