OSDN Git Service

git-svn-id: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core@3358 ae02f08e...
authorleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Fri, 5 Jun 2009 01:11:35 +0000 (01:11 +0000)
committerleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Fri, 5 Jun 2009 01:11:35 +0000 (01:11 +0000)
src/main/java/org/xerial/silk/SilkLineFastParser.java
src/test/java/org/xerial/json/JSONArrayTest.java
src/test/java/org/xerial/silk/SilkStreamReaderTest.java

index 03da100..ce414c3 100644 (file)
@@ -40,6 +40,7 @@ import java.util.concurrent.LinkedBlockingQueue;
 import org.xerial.core.XerialErrorCode;
 import org.xerial.core.XerialException;
 import org.xerial.silk.impl.SilkLexer;
+import org.xerial.util.ArrayDeque;
 import org.xerial.util.log.Logger;
 
 /**
@@ -54,7 +55,7 @@ public class SilkLineFastParser
 
     private final BufferedReader buffer;
     private final ExecutorService threadManager;
-    private final LinkedBlockingQueue<Future<List<SilkEvent>>> eventContainer;
+    private final LinkedBlockingQueue<Future<ArrayDeque<SilkEvent>>> eventContainer;
     private final SilkParserConfig config;
 
     public SilkLineFastParser(URL resourceURL) throws IOException
@@ -75,7 +76,7 @@ public class SilkLineFastParser
     public SilkLineFastParser(Reader reader, SilkParserConfig config)
     {
         this.config = config;
-        this.eventContainer = new LinkedBlockingQueue<Future<List<SilkEvent>>>(config.numWorkers);
+        this.eventContainer = new LinkedBlockingQueue<Future<ArrayDeque<SilkEvent>>>(config.numWorkers);
 
         if (reader.getClass().isAssignableFrom(BufferedReader.class))
             buffer = BufferedReader.class.cast(reader);
@@ -103,13 +104,14 @@ public class SilkLineFastParser
                 int lineCount = 0;
                 while (lineCount < config.numLinesInBlock)
                 {
-                    lineCount++;
+
                     String line = buffer.readLine();
                     if (line == null)
                     {
                         foundEOF = true;
                         break;
                     }
+                    lineCount++;
                     cache.add(line);
                 }
 
@@ -118,7 +120,7 @@ public class SilkLineFastParser
                     // map the input
                     Mapper map = new Mapper(workerCount++, cache);
 
-                    Future<List<SilkEvent>> future = threadManager.submit(map);
+                    Future<ArrayDeque<SilkEvent>> future = threadManager.submit(map);
                     eventContainer.put(future);
                 }
             }
@@ -128,6 +130,7 @@ public class SilkLineFastParser
             {
                 Thread.sleep(1);
             }
+
             reducerTask.cancel(true);
 
         }
@@ -146,10 +149,10 @@ public class SilkLineFastParser
 
     }
 
-    private class Mapper implements Callable<List<SilkEvent>>
+    private class Mapper implements Callable<ArrayDeque<SilkEvent>>
     {
         final List<String> cache;
-        final List<SilkEvent> eventQueue;
+        final ArrayDeque<SilkEvent> eventQueue;
         final SilkLexer lexer = new SilkLexer();
         final int lsn;
 
@@ -157,10 +160,10 @@ public class SilkLineFastParser
         {
             this.lsn = lsn;
             this.cache = cache;
-            eventQueue = new ArrayList<SilkEvent>(cache.size());
+            eventQueue = new ArrayDeque<SilkEvent>(cache.size());
         }
 
-        public List<SilkEvent> call() throws Exception
+        public ArrayDeque<SilkEvent> call() throws Exception
         {
             for (int cursor = 0; cursor < cache.size(); cursor++)
             {
@@ -198,6 +201,7 @@ public class SilkLineFastParser
     private class Reducer implements Callable<Void>
     {
         private SilkEventHandler handler;
+        private ArrayDeque<SilkEvent> eventQueue;
 
         public Reducer(SilkEventHandler handler)
         {
@@ -210,20 +214,41 @@ public class SilkLineFastParser
             {
                 while (true)
                 {
-                    Future<List<SilkEvent>> container = eventContainer.take();
-                    List<SilkEvent> eventQueue = container.get();
-                    for (SilkEvent e : eventQueue)
+                    try
+                    {
+                        Future<ArrayDeque<SilkEvent>> container = eventContainer.take();
+                        eventQueue = container.get();
+                    }
+                    catch (InterruptedException e)
                     {
-                        handler.handle(e);
+                        _logger.info("interrupted");
+                        return null;
+                    }
+
+                    while (!eventQueue.isEmpty())
+                    {
+                        SilkEvent e = null;
+                        try
+                        {
+                            e = eventQueue.getFirst();
+                        }
+                        finally
+                        {
+                            if (e != null)
+                                handler.handle(e);
+                            eventQueue.removeFirst();
+                        }
                     }
                 }
             }
-            catch (InterruptedException e)
+            finally
             {
+                // handle the remaining events
+                for (SilkEvent e : eventQueue)
+                    handler.handle(e);
+
                 handler.handle(new SilkEvent(SilkEventType.END_OF_FILE, null));
             }
-
-            return null;
         }
     }
 
index 8e87b15..42e7afe 100644 (file)
@@ -24,7 +24,7 @@
 //--------------------------------------
 package org.xerial.json;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
 
 import org.junit.After;
 import org.junit.Before;
@@ -63,14 +63,15 @@ public class JSONArrayTest
         assertEquals("leo", p.getString("name"));
     }
 
-    @Test
-    public void testParse() throws JSONException
+    final int N = 5000;
+
+    public String createSampleJSONArrayData()
     {
         // generate a sample JSON array
         StringBuilder sample = new StringBuilder();
         sample.append("[");
         int i = 0;
-        final int N = 5000;
+
         for (; i < N - 1; i++)
         {
             sample.append(i);
@@ -79,8 +80,13 @@ public class JSONArrayTest
         sample.append(i);
         sample.append("]");
 
-        String json = sample.toString();
+        return sample.toString();
+    }
 
+    @Test
+    public void testParse() throws JSONException
+    {
+        String json = createSampleJSONArrayData();
         StopWatch timer = new StopWatch();
         for (int n = 0; n < 500; n++)
         {
@@ -89,28 +95,12 @@ public class JSONArrayTest
         }
         _logger.info("time: " + timer.getElapsedTime());
 
-        // i:1000, n:100   time=18.4 sec (2009.4.23 using ANTLR JSON.g)
-        // i:1000, n:100   time=2.248 (2009. 4.23 using JSONTokener)
-
     }
 
     @Test
     public void testParseANTLRLexer() throws JSONException
     {
-        // generate a sample JSON array
-        StringBuilder sample = new StringBuilder();
-        sample.append("[");
-        int i = 0;
-        final int N = 5000;
-        for (; i < N - 1; i++)
-        {
-            sample.append(i);
-            sample.append(",");
-        }
-        sample.append(i);
-        sample.append("]");
-
-        String json = sample.toString();
+        String json = createSampleJSONArrayData();
 
         StopWatch timer = new StopWatch();
         for (int n = 0; n < 500; n++)
@@ -120,9 +110,6 @@ public class JSONArrayTest
         }
         _logger.info("time: " + timer.getElapsedTime());
 
-        // i:1000, n:100   time=18.4 sec (2009.4.23 using ANTLR JSON.g)
-        // i:1000, n:100   time=2.248 (2009. 4.23 using JSONTokener)
-
     }
 
 }
index 328addb..791e136 100644 (file)
@@ -66,26 +66,14 @@ public class SilkStreamReaderTest
     public void setUp() throws Exception\r
     {\r
         config = new SilkParserConfig();\r
-        config.bufferSize = 1024 * 1024 * 16; // 16MB\r
+        config.bufferSize = 1024 * 1024 * 8; // 8MB\r
+        config.numWorkers = 1;\r
     }\r
 \r
     @After\r
     public void tearDown() throws Exception\r
     {}\r
 \r
-    //private static final String largeFile = "file:///c:/Users/leo/work/t2k/hdrr_hni_allaxt_revised.silk";\r
-\r
-    //private static final String largeFile = "file:///f:/cygwin/home/leo/work/t2k/hdrr_hni_allaxt_revised.silk";\r
-\r
-    //private static final String largeFile = "file:///d:/tmp/hdrr_hni_allaxt_revised.silk";\r
-    //private static final String largeFile = "file:///f:/cygwin/home/leo/work/t2k/hdrr_hni_allaxt_revised.silk";\r
-\r
-    @Test\r
-    public void dummy()\r
-    {\r
-\r
-    }\r
-\r
     @Ignore\r
     @Test\r
     public void xmlReadTest() throws Exception\r
@@ -313,31 +301,6 @@ public class SilkStreamReaderTest
 \r
     }\r
 \r
-    int count = 0;\r
-\r
-    @Test\r
-    public void parserPerformance() throws Exception\r
-    {\r
-        SilkParser parser = new SilkParser(largeFile, config);\r
-        final StopWatch timer = new StopWatch();\r
-\r
-        count = 0;\r
-        parser.parse(new TreeEventHandlerBase() {\r
-\r
-            @Override\r
-            public void visitNode(String nodeName, String immediateNodeValue) throws Exception\r
-            {\r
-                count++;\r
-                if (count % 1000000 == 0)\r
-                    reportNodeCountStat(count, timer.getElapsedTime());\r
-            }\r
-\r
-        });\r
-\r
-        reportTotalSpeed("SilkParser", count, timer.getElapsedTime());\r
-\r
-    }\r
-\r
     @Ignore\r
     @Test\r
     public void pullParserPerformanceTest() throws Exception\r
@@ -411,4 +374,29 @@ public class SilkStreamReaderTest
         reportTotalSpeed("SilkFastPushParser", timer.getElapsedTime());\r
     }\r
 \r
+    int count = 0;\r
+\r
+    @Test\r
+    public void parserPerformance() throws Exception\r
+    {\r
+        SilkParser parser = new SilkParser(largeFile, config);\r
+        final StopWatch timer = new StopWatch();\r
+\r
+        count = 0;\r
+        parser.parse(new TreeEventHandlerBase() {\r
+\r
+            @Override\r
+            public void visitNode(String nodeName, String immediateNodeValue) throws Exception\r
+            {\r
+                count++;\r
+                if (count % 1000000 == 0)\r
+                    reportNodeCountStat(count, timer.getElapsedTime());\r
+            }\r
+\r
+        });\r
+\r
+        reportTotalSpeed("SilkParser", count, timer.getElapsedTime());\r
+\r
+    }\r
+\r
 }\r