View Javadoc
1   package nom.tam.image.compression.bintable;
2   
3   /*
4    * #%L
5    * nom.tam FITS library
6    * %%
7    * Copyright (C) 1996 - 2024 nom-tam-fits
8    * %%
9    * This is free and unencumbered software released into the public domain.
10   *
11   * Anyone is free to copy, modify, publish, use, compile, sell, or
12   * distribute this software, either in source code form or as a compiled
13   * binary, for any purpose, commercial or non-commercial, and by any
14   * means.
15   *
16   * In jurisdictions that recognize copyright laws, the author or authors
17   * of this software dedicate any and all copyright interest in the
18   * software to the public domain. We make this dedication for the benefit
19   * of the public at large and to the detriment of our heirs and
20   * successors. We intend this dedication to be an overt act of
21   * relinquishment in perpetuity of all present and future rights to this
22   * software under copyright law.
23   *
24   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27   * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
28   * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
29   * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30   * OTHER DEALINGS IN THE SOFTWARE.
31   * #L%
32   */
33  
34  import java.nio.Buffer;
35  import java.util.concurrent.ExecutorService;
36  import java.util.concurrent.Future;
37  
38  import nom.tam.fits.Header;
39  import nom.tam.fits.HeaderCardException;
40  import nom.tam.fits.compression.algorithm.api.ICompressorControl;
41  import nom.tam.fits.compression.provider.CompressorProvider;
42  import nom.tam.fits.header.Compression;
43  import nom.tam.util.ColumnTable;
44  import nom.tam.util.type.ElementType;
45  
46  /**
47   * (<i>for internal use</i>) A table 'tile' representing a set of consecutive table rows that are compressed together as
48   * a block.
49   */
50  @SuppressWarnings("javadoc")
51  public abstract class BinaryTableTile implements Runnable {
52  
53      protected final ColumnTable<?> data;
54  
55      /**
56       * start row.
57       */
58      protected final int rowStart;
59  
60      /**
61       * last row (exclusive)
62       */
63      protected final int rowEnd;
64  
65      protected final int column;
66  
67      protected String compressionAlgorithm;
68  
69      protected final ElementType<Buffer> type;
70  
71      protected final int length;
72  
73      protected final int tileIndex; /// 0-based tile index
74  
75      private Future<?> future;
76  
77      public BinaryTableTile(ColumnTable<?> data, BinaryTableTileDescription description) {
78          this.data = data;
79          rowStart = description.getRowStart();
80          rowEnd = description.getRowEnd();
81          column = description.getColumn();
82          tileIndex = description.getTileIndex() - 1;
83          compressionAlgorithm = description.getCompressionAlgorithm();
84          type = ElementType.forClass(data.getElementClass(column));
85          length = (rowEnd - rowStart) * data.getElementSize(column);
86      }
87  
88      public void execute(ExecutorService threadPool) {
89          future = threadPool.submit(this);
90      }
91  
92      /**
93       * @deprecated (<i> for internal use</i>) No longer used.
94       */
95      public void fillHeader(Header header) throws HeaderCardException {
96          header.card(Compression.ZCTYPn.n(column + 1)).value(compressionAlgorithm);
97      }
98  
99      /**
100      * Returns the zero-based tile index.
101      * 
102      * @return the zero-based tile index (a.k.a. Java index).
103      */
104     public int getTileIndex() {
105         return tileIndex;
106     }
107 
108     public void waitForResult() {
109         try {
110             future.get();
111         } catch (Exception e) {
112             throw new IllegalStateException(
113                     "could not process tile " + (tileIndex + 1) + ", column " + column + ": " + e.getMessage(), e);
114         }
115     }
116 
117     protected ICompressorControl getCompressorControl() {
118         return CompressorProvider.findCompressorControl(null, compressionAlgorithm, type.primitiveClass());
119     }
120 
121     protected ICompressorControl getCompressorControl(Class<?> dataType) {
122         return CompressorProvider.findCompressorControl(null, compressionAlgorithm, dataType);
123     }
124 
125     protected ICompressorControl getGZipCompressorControl() {
126         return CompressorProvider.findCompressorControl(null, Compression.ZCMPTYPE_GZIP_1, byte.class);
127     }
128 
129     protected int getUncompressedSizeInBytes() {
130         return length * type.size();
131     }
132 }