changeset 246:6f8236208875

not use MappedByteBuffer
author sugi
date Tue, 21 May 2013 16:29:49 +0900
parents e5d0bd8200c6
children 6e042fde5662
files .settings/org.eclipse.core.resources.prefs src/alice/test/codesegment/local/wordcount/Range.java src/alice/test/codesegment/local/wordcount/ReadFileTest.java src/alice/test/codesegment/local/wordcount/SeparateArray.java src/alice/test/codesegment/local/wordcount/SetTask.java src/alice/test/codesegment/local/wordcount/StartWordCount.java src/alice/test/codesegment/local/wordcount/WordCount.java
diffstat 7 files changed, 126 insertions(+), 76 deletions(-) [+]
line wrap: on
line diff
--- a/.settings/org.eclipse.core.resources.prefs	Wed May 08 18:34:40 2013 +0900
+++ b/.settings/org.eclipse.core.resources.prefs	Tue May 21 16:29:49 2013 +0900
@@ -1,4 +1,5 @@
 eclipse.preferences.version=1
 encoding//src/alice/test/codesegment/local/bitonicsort/SortTest.java=UTF-8
+encoding//src/alice/test/codesegment/local/wordcount/SeparateArray.java=UTF-8
 encoding//src/alice/test/codesegment/local/wordcount/SetTask.java=UTF-8
 encoding//src/alice/test/codesegment/local/wordcount/WordCount.java=UTF-8
--- a/src/alice/test/codesegment/local/wordcount/Range.java	Wed May 08 18:34:40 2013 +0900
+++ b/src/alice/test/codesegment/local/wordcount/Range.java	Tue May 21 16:29:49 2013 +0900
@@ -1,17 +1,16 @@
 package alice.test.codesegment.local.wordcount;
 
-import org.msgpack.annotation.Message;
-
-@Message
 public class Range {
 
-	public int start;
-	public int end;
-	public boolean flag;
-	public Range(int _start, int _end, boolean _flag) {
-		start = _start;
-		end = _end;
-		flag = _flag;
+	public char[] array;
+	public char nextchar;
+	
+	public Range(int size){
+		this.array = new char[size];
+	}
+
+	public Range(char[] _array) {
+		this.array = _array;
 	}
 
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/alice/test/codesegment/local/wordcount/ReadFileTest.java	Tue May 21 16:29:49 2013 +0900
@@ -0,0 +1,46 @@
+package alice.test.codesegment.local.wordcount;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+public class ReadFileTest {
+	
+	public static void main(String[] args){
+		WordConfig conf = new WordConfig(args);
+		BufferedReader br = null;
+		int i = 0;
+		try {
+			br= new BufferedReader(
+					new InputStreamReader(
+							new FileInputStream(
+									new File(conf.filename)
+									)));
+			int size = 4; // Kbyte
+			
+			Range range = new Range(size);
+			int check = br.read(range.array);
+			for (;check!=-1;i++){
+			
+				char[] array;
+				array = new char[size];
+				check = br.read(array);
+				range.nextchar = array[0];
+				System.out.print(range.array); // ods
+				System.out.println(range.nextchar);
+				range = new Range(array);
+			} 
+			System.out.println(i);
+		} catch (FileNotFoundException e) {
+			System.out.println("file was not found");
+			System.exit(1);
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+		
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/alice/test/codesegment/local/wordcount/SeparateArray.java	Tue May 21 16:29:49 2013 +0900
@@ -0,0 +1,57 @@
+package alice.test.codesegment.local.wordcount;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import alice.codesegment.CodeSegment;
+
+public class SeparateArray extends CodeSegment{
+
+	private WordConfig conf;
+	
+	SeparateArray(WordConfig conf){
+		this.conf = conf;
+	}
+	
+	@Override
+	public void run() {
+		
+		// cpu分だけTaskをあらかじめ作成
+	 	int cpu_num = Runtime.getRuntime().availableProcessors();
+	 	for (int cnt =0;cnt < cpu_num;cnt++){
+	 		new WordCount();
+	 	}
+	 	
+		BufferedReader br = null;
+		int i = 0;
+		try {
+			br= new BufferedReader(
+					new InputStreamReader(
+							new FileInputStream(
+									new File(conf.filename)
+									)));
+			int size = conf.division * 1024; // Kbyte
+			Range range = new Range(size);
+			int check = br.read(range.array);
+			for (;check!=-1;i++){
+			
+				char[] array;
+				array = new char[size];
+				check = br.read(array);
+				range.nextchar = array[0];
+				ods.put("array", range);
+				range = new Range(array);
+			} 
+			new CorrectResult(i);
+		} catch (FileNotFoundException e) {
+			System.exit(1);
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+	}
+
+}
--- a/src/alice/test/codesegment/local/wordcount/SetTask.java	Wed May 08 18:34:40 2013 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,51 +0,0 @@
-package alice.test.codesegment.local.wordcount;
-
-import alice.codesegment.CodeSegment;
-import java.io.*;
-import java.nio.MappedByteBuffer;
-import java.nio.channels.FileChannel;
-
-public class SetTask extends CodeSegment {
-	
-	private WordConfig conf;
-
-	public SetTask(WordConfig conf) {
-		this.conf = conf;
-	}
-
-	@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;
-
-			System.out.println(length);
-			for (;start+size<length;i++){
-				end = start+size;
-				ods.put("range", new Range(start, end, false));
-				new WordCount();
-				start = end;
-			}
-			
-			if (end!=length){
-				end =length;
-				ods.put("range", new Range(start, end, true));
-				new WordCount();
-				i++;
-			}
-			new CorrectResult(i);
-		} catch (FileNotFoundException e) {			
-			e.printStackTrace();
-		}  catch (IOException e) {
-			e.printStackTrace();
-		}
-		 
-	}
-
-}
--- a/src/alice/test/codesegment/local/wordcount/StartWordCount.java	Wed May 08 18:34:40 2013 +0900
+++ b/src/alice/test/codesegment/local/wordcount/StartWordCount.java	Tue May 21 16:29:49 2013 +0900
@@ -11,6 +11,6 @@
 		new AliceDaemon(new Config(args)).listen(); // logger off
 		
 		WordConfig conf = new WordConfig(args);
-		new SetTask(conf).execute();
+		new SeparateArray(conf).execute();
 	}
 }
--- a/src/alice/test/codesegment/local/wordcount/WordCount.java	Wed May 08 18:34:40 2013 +0900
+++ b/src/alice/test/codesegment/local/wordcount/WordCount.java	Tue May 21 16:29:49 2013 +0900
@@ -1,33 +1,28 @@
 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 info1 = ids.create(CommandType.PEEK);
-	private Receiver info2 = ids.create(CommandType.TAKE); 
+	private Receiver info1 = ids.create(CommandType.TAKE);
 	
 	public WordCount(){
 		info1.setKey("array");
-		info2.setKey("range");
 	}
 
 	@Override
 	public void run() {
-		MappedByteBuffer buf = info1.asClass(MappedByteBuffer.class);
-		Range r = info2.asClass(Range.class);
+		Range r = info1.asClass(Range.class);
 		int word_flag = 0;
 	    int word_num = 0;
 	    int line_num = 0;
-	    
-		for (int i = r.start; i < r.end; i++) {
-			if ((char)buf.get(i) == 0x20) { // 空白                                                                                                                                
+	    int i = 0;
+		for (; i < r.array.length; i++) {
+			if (r.array[i] == 0x20) { // 空白                                                                                                                                
 				word_flag = 1;
-			} else if ((char)buf.get(i) == 0x0A) { // 改行
+			} else if (r.array[i] == 0x0A) { // 改行
 				line_num += 1;
 				word_flag = 1;
 			} else {
@@ -35,18 +30,21 @@
 				word_flag = 0;
 			}
 		}
+		//System.out.println(i);
+		//System.out.println(r.array.length);
 		word_num += word_flag;
-		if (!r.flag){
+		if (r.nextchar!='\0'){ // null means last block
 			// buf.get(r.end) is next block's char.
 			// buf.get(r.end-1) is this block's last char
-			if (((char)buf.get(r.end-1) == 0x0A||(char)buf.get(r.end-1) == 0x20) &&
-					((char)buf.get(r.end) == 0x0A||(char)buf.get(r.end) == 0x20)){
+			if ((r.array[i-1] == 0x0A||r.array[i-1] == 0x20) &&
+					(r.nextchar == 0x0A||r.nextchar == 0x20)){
 				word_num--;
 			}
 		}
 		
 		Result result = new Result(line_num,word_num);
 		ods.put("result", result);
+		new WordCount();
 	}
 
 }