View Javadoc

1   package org.treetank.filelistener.file.data;
2   
3   import java.io.DataOutput;
4   import java.io.IOException;
5   
6   import org.treetank.api.IData;
7   import org.treetank.exception.TTIOException;
8   
9   import com.google.common.hash.Funnel;
10  import com.google.common.hash.PrimitiveSink;
11  
12  /**
13   * A sequence of Filenodes represents a full file. A Filnode has a content size
14   * of 512 bytes and has a reference to the following node if such a node exists.
15   * 
16   * The header filenode is being referenced by the page layer in a hash map. It
17   * consists of a relative path.
18   * 
19   * @author andreas
20   * 
21   */
22  public class FileData implements IData {
23  
24      /**
25       * Enum for FileNodeFunnel.
26       * 
27       * @author Sebastian Graf, University of Konstanz
28       * 
29       */
30      enum FileNodeFunnel implements Funnel<IData> {
31          INSTANCE;
32          public void funnel(IData data, PrimitiveSink into) {
33              final FileData from = (FileData)data;
34              into.putLong(from.dataKey).putBytes(from.val).putBoolean(from.header).putBoolean(from.eof);
35          }
36      }
37  
38      /**
39       * The nodes key value, which is equal with it's position in the list.
40       */
41      private final long dataKey;
42  
43      /**
44       * The size of the filenode
45       */
46      public static final int FILENODESIZE = 1024 * 16;
47  
48      /**
49       * The content of this node in form of a byte array.
50       */
51      private final byte[] val;
52  
53      /**
54       * Determines whether or not this filenode is the first in the sequence.
55       */
56      private final boolean header;
57  
58      /**
59       * Determines whether or not this filenode is the last in the sequence.
60       */
61      private final boolean eof;
62  
63      /**
64       * Creates a Filenode with given bytes
65       * 
66       * @param dataKey
67       * @param content
68       *            , as byte array
69       */
70      public FileData(long dataKey, byte[] content, boolean header, boolean eof) {
71          this.dataKey = dataKey;
72          this.val = content;
73          this.header = header;
74          this.eof = eof;
75      }
76  
77      /**
78       * Serializing to given dataput
79       * 
80       * @throws TTIOException
81       */
82      public void serialize(final DataOutput output) throws TTIOException {
83          try {
84              output.writeLong(dataKey);
85              output.writeBoolean(header);
86              output.writeBoolean(eof);
87              output.writeInt(val.length);
88              output.write(val);
89          } catch (final IOException exc) {
90              throw new TTIOException(exc);
91          }
92      }
93  
94      @Override
95      public long getDataKey() {
96          return this.dataKey;
97      }
98  
99      /**
100      * Check whether or not this filenode is the first in the sequence.
101      * 
102      * @return true if is a header element
103      */
104     public boolean isHeader() {
105         return header;
106     }
107 
108     /**
109      * Check whether or not this filenode is the last in the sequence.
110      * 
111      * @return true if this is the last element in the sequence
112      */
113     public boolean isEof() {
114         return eof;
115     }
116 
117     /**
118      * @return value held by this node
119      */
120     public byte[] getVal() {
121         return val;
122     }
123 
124     /**
125      * {@inheritDoc}
126      */
127     @Override
128     public Funnel<IData> getFunnel() {
129         return FileNodeFunnel.INSTANCE;
130     }
131 
132 }