From: Yegor Kozlov Date: Wed, 8 Jun 2011 12:38:07 +0000 (+0000) Subject: SXSSF documentation and polishing X-Git-Tag: REL_3_8_BETA4~396 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=8c530958ddc169eb275a31bd6429332fe9a3c78e;p=poi.git SXSSF documentation and polishing git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1133372 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/spreadsheet/how-to.xml b/src/documentation/content/xdocs/spreadsheet/how-to.xml index 35e4a00030..a8f6515315 100644 --- a/src/documentation/content/xdocs/spreadsheet/how-to.xml +++ b/src/documentation/content/xdocs/spreadsheet/how-to.xml @@ -58,6 +58,7 @@
  • Event API (HSSF Only)
  • Event API with extensions to be Record Aware (HSSF Only)
  • XSSF and SAX (Event API)
  • +
  • SXSSF (Streaming User API)
  • Low Level API
  • @@ -620,6 +621,74 @@ public class ExampleEventUserModel { howto.processAllSheets(args[0]); } } +]]> + + +
    SXSSF (Streaming Usermodel API) +

    + XSSF is an API-compatible streaming extension of XSSF to be used when + very large spreadsheets have to be produced, and heap space is limited. + SXSSF achieves its low memory footprint by limiting access to the rows that + are within a sliding window, while XSSF gives access to all rows in the + document. Older rows that are no longer in the window become inaccessible, + as they are written to the disk. +

    +

    + When a new row is created via createRow() and the total number + of unflushed records would exeed the specified window size, then the + row with the lowest index value is flushed and cannot be accessed + via getRow() anymore. +

    +

    + A value of -1 indicates unlimited access. In this case all + records that have not been flushed by a call to flush() are available + for random access. +

    + +
    diff --git a/src/documentation/content/xdocs/spreadsheet/index.xml b/src/documentation/content/xdocs/spreadsheet/index.xml index f52d60c3dd..514460106d 100644 --- a/src/documentation/content/xdocs/spreadsheet/index.xml +++ b/src/documentation/content/xdocs/spreadsheet/index.xml @@ -73,6 +73,42 @@ the memory footprint for processing them is higher than for the older HSSF supported (.xls) binary files.

    + + + - + +
    +SXSSF (SInce POI 3.8 beta3) +

    Since 3.8-beta3, POI provides a low-memory footprint SXSSF API built on top of XSSF.

    +

    +XSSF is an API-compatible streaming extension of XSSF to be used when +very large spreadsheets have to be produced, and heap space is limited. +SXSSF achieves its low memory footprint by limiting access to the rows that +are within a sliding window, while XSSF gives access to all rows in the +document. Older rows that are no longer in the window become inaccessible, +as they are written to the disk. +

    +

    +In auto-flush mode the size of the access window can be specified, to hold a certain number of rows in memory. When that value is reached, the creationof an additional row causes the row with the lowest index to to be removed from the access window and written to disk.. Or, the window size can be set to grow dynamically; it can be trimmed periodically by an explicit call to flush(int keepRows) as needed. +

    +

    +Due to the streaming nature of the implementation, there are the following +limitations when compared to XSSF: +

    +
      +
    • Only a limited number of rows are accessible at a point in time.
    • +
    • Sheet.clone() is not supported.
    • +
    • Formula evaluation is not supported
    • +
    + +

    The table below synopsizes the comparative features of POI's Spreadsheet API:

    +

    Spreadsheet API Feature Summary

    + +

    + Spreadsheet API Feature Summary +

    +
    + + diff --git a/src/documentation/resources/images/ss-features.png b/src/documentation/resources/images/ss-features.png new file mode 100644 index 0000000000..17554c7151 Binary files /dev/null and b/src/documentation/resources/images/ss-features.png differ diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/BigGridDemo.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/BigGridDemo.java index 723c424fd4..91092232a9 100644 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/BigGridDemo.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/BigGridDemo.java @@ -36,7 +36,18 @@ import org.apache.poi.xssf.usermodel.*; * 2. create an application that streams data in a text file * 3. Substitute the sheet in the template with the generated data * + *

    + * Since 3.8-beta3 POI provides a low-memory footprint SXSSF API which implementing the "BigGridDemo" strategy. + * XSSF is an API-compatible streaming extension of XSSF to be used when + * very large spreadsheets have to be produced, and heap space is limited. + * SXSSF achieves its low memory footprint by limiting access to the rows that + * are within a sliding window, while XSSF gives access to all rows in the + * document. Older rows that are no longer in the window become inaccessible, + * as they are written to the disk. + *

    + * * @author Yegor Kozlov + * @see http://poi.apache.org/spreadsheet/how-to.html#sxssf */ public class BigGridDemo { private static final String XML_ENCODING = "UTF-8"; diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java index be90606e1b..baea693418 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java @@ -17,15 +17,10 @@ package org.apache.poi.xssf.streaming; +import java.io.*; import java.util.Iterator; import java.util.TreeMap; import java.util.Map; -import java.io.Writer; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.FileInputStream; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellReference; @@ -46,7 +41,7 @@ public class SXSSFSheet implements Sheet, Cloneable XSSFSheet _sh; TreeMap _rows=new TreeMap(); SheetDataWriter _writer; - int _randomAccessWindowSize=5000; + int _randomAccessWindowSize = SXSSFWorkbook.DEFAULT_WINDOW_SIZE; public SXSSFSheet(SXSSFWorkbook workbook,XSSFSheet xSheet) throws IOException { @@ -1243,7 +1238,7 @@ public class SXSSFSheet implements Sheet, Cloneable { _fd = File.createTempFile("sheet", ".xml"); _fd.deleteOnExit(); - _out = new FileWriter(_fd); + _out = new BufferedWriter(new FileWriter(_fd)); _out.write("\n"); } public int getNumberOfFlushedRows() @@ -1262,9 +1257,10 @@ public class SXSSFSheet implements Sheet, Cloneable { _fd.delete(); } - public InputStream getWorksheetXMLInputStream() throws IOException + public InputStream getWorksheetXMLInputStream() throws IOException { _out.write(""); + _out.flush(); _out.close(); return new FileInputStream(_fd); } @@ -1275,7 +1271,7 @@ public class SXSSFSheet implements Sheet, Cloneable * @param rownum 0-based row number * @param row a row */ - public void writeRow(int rownum,SXSSFRow row) throws IOException + public void writeRow(int rownum,SXSSFRow row) throws IOException { if(_numberOfFlushedRows==0) _lowestIndexOfFlushedRows=rownum; diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java index aa17b536a0..365e58b337 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java @@ -50,11 +50,56 @@ import org.apache.poi.ss.usermodel.Row.MissingCellPolicy; */ public class SXSSFWorkbook implements Workbook { + /** + * Specifies how many rows can be accessed at most via getRow(). + * When a new node is created via createRow() and the total number + * of unflushed records would exeed the specified value, then the + * row with the lowest index value is flushed and cannot be accessed + * via getRow() anymore. + */ + public static final int DEFAULT_WINDOW_SIZE = 100; + XSSFWorkbook _wb=new XSSFWorkbook(); HashMap _sxFromXHash=new HashMap(); HashMap _xFromSxHash=new HashMap(); + int _randomAccessWindowSize = DEFAULT_WINDOW_SIZE; + + /** + * Construct a new workbook + */ + public SXSSFWorkbook(){ + + } + + /** + * Construct an empty workbook and specify the window for row access. + *

    + * When a new node is created via createRow() and the total number + * of unflushed records would exeed the specified value, then the + * row with the lowest index value is flushed and cannot be accessed + * via getRow() anymore. + *

    + *

    + * A value of -1 indicates unlimited access. In this case all + * records that have not been flushed by a call to flush() are available + * for random access. + *

    + *

    + * A value of 0 is not allowed because it would flush any newly created row + * without having a chance to specify any cells. + *

    + * + * @param rowAccessWindowSize + */ + public SXSSFWorkbook(int rowAccessWindowSize){ + if(rowAccessWindowSize == 0 || rowAccessWindowSize < -1) { + throw new IllegalArgumentException("rowAccessWindowSize must be greater than 0 or -1"); + } + _randomAccessWindowSize = rowAccessWindowSize; + } + XSSFSheet getXSSFSheet(SXSSFSheet sheet) { XSSFSheet result=_sxFromXHash.get(sheet); @@ -303,6 +348,7 @@ public class SXSSFWorkbook implements Workbook { throw new RuntimeException(ioe); } + sxSheet.setRandomAccessWindowSize(_randomAccessWindowSize); registerSheetMapping(sxSheet,xSheet); return sxSheet; }