changeset 242:3bcaf12cf877

use MappedByteBuffer
author sugi
date Tue, 16 Apr 2013 18:26:07 +0900
parents 02783f3699b1
children c70cd1b2caca
files src/alice/test/codesegment/local/wordcount/CorrectResult.java src/alice/test/codesegment/local/wordcount/Range.java src/alice/test/codesegment/local/wordcount/SetTask.java src/alice/test/codesegment/local/wordcount/StartWordCount.java src/alice/test/codesegment/local/wordcount/WordConfig.java src/alice/test/codesegment/local/wordcount/WordCount.java
diffstat 6 files changed, 75 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/src/alice/test/codesegment/local/wordcount/CorrectResult.java	Tue Apr 16 15:09:47 2013 +0900
+++ b/src/alice/test/codesegment/local/wordcount/CorrectResult.java	Tue Apr 16 18:26:07 2013 +0900
@@ -26,6 +26,8 @@
 			line_num +=result.line_num;
 			word_num +=result.word_num;
 		}
+		long t = System.currentTimeMillis();
+		System.out.println(t - StartWordCount.t);
 		System.out.println(line_num+" "+word_num);
 		System.exit(0);
 		
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/alice/test/codesegment/local/wordcount/Range.java	Tue Apr 16 18:26:07 2013 +0900
@@ -0,0 +1,15 @@
+package alice.test.codesegment.local.wordcount;
+
+import org.msgpack.annotation.Message;
+
+@Message
+public class Range {
+
+	public int start;
+	public int end;
+	public Range(int _start, int _end) {
+		start = _start;
+		end = _end;
+	}
+
+}
--- a/src/alice/test/codesegment/local/wordcount/SetTask.java	Tue Apr 16 15:09:47 2013 +0900
+++ b/src/alice/test/codesegment/local/wordcount/SetTask.java	Tue Apr 16 18:26:07 2013 +0900
@@ -2,6 +2,8 @@
 
 import alice.codesegment.CodeSegment;
 import java.io.*;
+import java.nio.MappedByteBuffer;
+import java.nio.channels.FileChannel;
 
 public class SetTask extends CodeSegment {
 	
@@ -13,6 +15,40 @@
 
 	@Override
 	public void run() {
+		FileInputStream input;
+		try {
+			input = new FileInputStream(new File(conf.filename));
+			FileChannel channel = input.getChannel();
+			int length = (int)channel.size();
+			MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, length);
+			ods.put("array", buffer);
+			int start=0,end=0,i=0;
+			int size = conf.division * 1024;
+			
+			for (;start+size<length;i++){
+				end = start+size;
+				ods.put("range", new Range(start,end));
+				new WordCount();
+				start = end;
+			}
+			
+			if (start+size-1!=length){
+				end =length;
+				ods.put("range", new Range(start,end));
+				new WordCount();
+				i++;
+			}
+			
+			new CorrectResult(i);
+			//System.out.println((char)buffer.get(i));
+		} catch (FileNotFoundException e) {			
+			e.printStackTrace();
+		}  catch (IOException e) {
+			e.printStackTrace();
+		}
+		
+		
+		/*
 		BufferedReader br = null;
 		int i = 0;
 		try {
@@ -21,15 +57,17 @@
                        new FileInputStream(
                           new File(conf.filename)
                           )));
-			int size = conf.division * 1024 * 1024; // 16Kbyte
+			int size = conf.division * 1024 ; // 16Kbyte
 			char[] buf;
 			for (;;i++){
 				buf = new char[size];
 				int check = br.read(buf);
 				ods.put("array", buf);
+				new WordCount();
 				if (check==-1) break;
-				new WordCount();
+				
 			}
+			System.out.println(i);
 			new CorrectResult(i);
 		} catch (FileNotFoundException e) {
 			System.out.println("file not found");
@@ -42,7 +80,7 @@
 			br.close();
 		} catch (IOException e) {
 			e.printStackTrace();
-		}
+		}*/
 	}
 
 }
--- a/src/alice/test/codesegment/local/wordcount/StartWordCount.java	Tue Apr 16 15:09:47 2013 +0900
+++ b/src/alice/test/codesegment/local/wordcount/StartWordCount.java	Tue Apr 16 18:26:07 2013 +0900
@@ -5,7 +5,9 @@
 
 
 public class StartWordCount {
+	public static long t = System.currentTimeMillis();
 	public static void main(String[] args){
+		
 		new AliceDaemon(new Config(args)).listen(); // logger off
 		
 		WordConfig conf = new WordConfig(args);
--- a/src/alice/test/codesegment/local/wordcount/WordConfig.java	Tue Apr 16 15:09:47 2013 +0900
+++ b/src/alice/test/codesegment/local/wordcount/WordConfig.java	Tue Apr 16 18:26:07 2013 +0900
@@ -2,7 +2,6 @@
 
 public class WordConfig {
 	public String filename;
-	public int block = 1;
 	public static String[] array;
 	public int division = 16;
 	
@@ -10,8 +9,8 @@
 		for (int i=0;i<args.length; i++){
 			if ("-f".equals(args[i])){
 				filename = args[++i];	
-			} else if ("-b".equals(args[i])){
-				block = Integer.parseInt(args[++i]);	
+			} else if ("-d".equals(args[i])){
+				division = Integer.parseInt(args[++i]);	
 			}
 		}
 		
@@ -19,13 +18,6 @@
 			System.out.println("Usage: WordCount -f FILENAME");
 			System.exit(0);
 		}
-		createKey();
 	}
 
-	public void createKey(){
-		array = new String[block];
-		for(int i = 0 ; i < block ; i++) {
-			array[i] = "array" + i;
-		}
-	}
 }
--- a/src/alice/test/codesegment/local/wordcount/WordCount.java	Tue Apr 16 15:09:47 2013 +0900
+++ b/src/alice/test/codesegment/local/wordcount/WordCount.java	Tue Apr 16 18:26:07 2013 +0900
@@ -1,28 +1,33 @@
 package alice.test.codesegment.local.wordcount;
 
+import java.nio.MappedByteBuffer;
+
 import alice.codesegment.CodeSegment;
 import alice.datasegment.CommandType;
 import alice.datasegment.Receiver;
 
 public class WordCount extends CodeSegment{
 	
-	private Receiver r = ids.create(CommandType.TAKE); 
+	private Receiver info1 = ids.create(CommandType.PEEK);
+	private Receiver info2 = ids.create(CommandType.TAKE); 
 	
 	public WordCount(){
-		r.setKey("array");
+		info1.setKey("array");
+		info2.setKey("range");
 	}
 
 	@Override
 	public void run() {
-		char[] a = (char[]) r.getObj();
+		MappedByteBuffer buf = info1.asClass(MappedByteBuffer.class);
+		Range r = info2.asClass(Range.class);
 		int word_flag = 0;
 	    int word_num = 0;
 	    int line_num = 0;
-	    int i = 0;
-		for (; i < a.length; i++) {
-			if (a[i] == 0x20) { // 空白                                                                                                                                       
+	    
+		for (int i = r.start; i < r.end; i++) {
+			if ((char)buf.get(i) == 0x20) { // 空白                                                                                                                                       
 				word_flag = 1;
-			} else if (a[i] == 0x0A) { // 改行                                                                                                                                
+			} else if ((char)buf.get(i) == 0x0A) { // 改行                                                                                                                                
 				line_num += 1;
 				word_flag = 1;
 			} else {
@@ -30,6 +35,7 @@
 				word_flag = 0;
 			}
 		}
+
 		Result result = new Result(line_num,word_num);
 		ods.put("result", result);
 	}