You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by David North <dt...@corefiling.co.uk> on 2015/08/06 18:28:21 UTC

Re: Extraction of row/column super-interfaces to make streaming more friendly

On 16/05/15 11:02, Nick Burch wrote:
> On Fri, 15 May 2015, David North wrote:
>> One change which we have in our local build of POI is the extraction of
>> super-interfaces to make streaming read of worksheets a bit nicer:
>>
>> "CellEssence" is implemented by XSSFCell, HSSFCell and SXSSFCell:
>>
>> "RowEssence" is similar and exposes CellEssences for that row.
>>
>> Is the introduction of these interfaces something you'd consider
>> accepting a patch for? How do the names sound? Full list of methods
>> below for the record.
>
> Are you able to post a bit of your streaming reading code so we can
> see how it fits in there? Might help us understand the idea a bit
> better, and why you couldn't just use the existing Row and Cell
> interfaces on your code
>
> Naming wise, I wonder if the common SL stuff on the branch might have
> a good convention there already we could follow? (I can't see an
> equivalent from a quick grep to base off of)

As we understand it, the SXSSF interfaces are all about streaming
/write/ of workbooks. Streaming read is possible but rather more
low-level, for example:-

    XSSFReader reader = new XSSFReader(pkg);
    ReadOnlySharedStringsTable sst = new ReadOnlySharedStringsTable(pkg);
    XMLReader parser = createSheetReader(workbookIsDate1904, sheetName,
sst, reader.getStylesTable());
    try (final InputStream targetSheet = reader.getSheet(sheetId)) {
      InputSource sheetSource = new InputSource(targetSheet);
      parser.parse(sheetSource);
    }

And the content handler would look like:

SheetContentsHandler sheetContentsHandler = new SheetContentsHandler() {

      public void startRow(final int rowNum) {
      }

      public void endRow() {
      }

      @Override
      public void stringCell(final CellReference cellCoordinate, final
RichTextString text, final CellStyle style) {
      }

       // more methods for different types of cell...
}

This is quite low-level, though the pieces are all there. In order to
make it possible to transition some existing code written in terms of
Row and Cell across to streaming (while still being able to test it
against the non-streaming read), we invented CellEssence and RowEssence
as the minimal set of information from Row/Cell which is easy to
assemble during a streaming read using the above handler. So if we push
the interfaces upstream it would make sense to provide a handler, at
least as an example, showing how to create row and cell essences from a
streaming parse. Here's an extract from ours:

      public void startRow(final int rowNum) {
        _currentRow = new StreamedRowEssence(rowNum);
      }

      public void endRow() throws E {
        essenceHandler.handleRow(_currentRow);
      }

      @Override
      public void stringCell(final CellReference cellCoordinate, final
RichTextString text, final CellStyle style) {
        StreamedCellEssence cell = addCell(cellCoordinate, style);
        cell.setStringValue(text);
        cell.setCellType(STCellType.STR);
      }

Implementing Row/Cell on the objects created here would be a bit of a
lie as lots of methods would have to throw UnsupportedOperationException.

Thanks,
David

-- 
David North, Technical Lead, CoreFiling Limited
http://www.corefiling.com
Phone: +44-1865-203192


Re: Extraction of row/column super-interfaces to make streaming more friendly

Posted by Nick Burch <ap...@gagravarr.org>.
On Thu, 6 Aug 2015, David North wrote:
> On 16/05/15 11:02, Nick Burch wrote:
>> On Fri, 15 May 2015, David North wrote:
>>> One change which we have in our local build of POI is the extraction of
>>> super-interfaces to make streaming read of worksheets a bit nicer:
>>>
>>> "CellEssence" is implemented by XSSFCell, HSSFCell and SXSSFCell:
>>>
>>> "RowEssence" is similar and exposes CellEssences for that row.
>>>
>>> Is the introduction of these interfaces something you'd consider
>>> accepting a patch for? How do the names sound? Full list of methods
>>> below for the record.

Thinking about it some more, with your details, I'd say it should probably 
be

org.apache.poi.ss.usermodel.(something cell)
org.apache.poi.ss.usermodel.Cell extends (something cell)
org.apache.poi.hssf.usermodel.HSSFCell implements Cell
org.apache.poi.xssf.usermodel.XSSFCell implements Cell
org.apache.poi.xssf.eventusermodel.(something else cell) implements 
(somthing cell)

The right combination or words for something and something else + cell I'm 
still not quite sure on.

We use Base quite a bit, but I'm not sure if that's quite the right 
naming?

For stripped-down cell etc interfaces, we also have the EvaluationCell / 
EvaluationSheet / etc family of interfaces. I'd guess that quite a bit of 
that will overlap between the core/base/essence cell and EvaluationCell, 
so we should keep that in mind and try to re-use patterns / extend as 
appropriate


> This is quite low-level, though the pieces are all there. In order to
> make it possible to transition some existing code written in terms of
> Row and Cell across to streaming (while still being able to test it
> against the non-streaming read), we invented CellEssence and RowEssence
> as the minimal set of information from Row/Cell which is easy to
> assemble during a streaming read using the above handler. So if we push
> the interfaces upstream it would make sense to provide a handler, at
> least as an example, showing how to create row and cell essences from a
> streaming parse. Here's an extract from ours:
>
>      public void startRow(final int rowNum) {
>        _currentRow = new StreamedRowEssence(rowNum);
>      }
>
>      public void endRow() throws E {
>        essenceHandler.handleRow(_currentRow);
>      }
>
>      @Override
>      public void stringCell(final CellReference cellCoordinate, final
> RichTextString text, final CellStyle style) {
>        StreamedCellEssence cell = addCell(cellCoordinate, style);
>        cell.setStringValue(text);
>        cell.setCellType(STCellType.STR);
>      }
>
> Implementing Row/Cell on the objects created here would be a bit of a
> lie as lots of methods would have to throw UnsupportedOperationException.

If we do this, I give it under 5 minutes until someone asks "how can I 
read in with the streaming usermodel read then write out changes again 
with SXSSF".

Given that, and give the logic already in SXSSFCell for going to/from raw 
XML, would it be possible to have your XSSFSheetXMLHandler generate 
SXSSFCells instead of a brand new cut-down cell object?

Also, as for UnsupportedOperationException, surely hitting one of those is 
a challenge to the community to add in the missing functionality? ;-)

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org