View Javadoc

1   package org.treetank.access;
2   
3   import java.io.File;
4   import java.io.FileOutputStream;
5   import java.io.IOException;
6   import java.util.Map;
7   import java.util.Set;
8   
9   import org.treetank.api.IBucketReadTrx;
10  import org.treetank.api.IFilelistenerReadTrx;
11  import org.treetank.api.IMetaEntry;
12  import org.treetank.exception.TTException;
13  import org.treetank.exception.TTIOException;
14  import org.treetank.filelistener.file.StorageManager;
15  import org.treetank.filelistener.file.data.FileData;
16  import org.treetank.filelistener.file.data.FilelistenerMetaDataFactory.MetaKey;
17  import org.treetank.filelistener.file.data.FilelistenerMetaDataFactory.MetaValue;
18  
19  import com.google.common.io.Files;
20  import com.google.common.io.OutputSupplier;
21  
22  /**
23   * @author Andreas Rain
24   */
25  public class FilelistenerReadTrx implements IFilelistenerReadTrx {
26  
27      /** State of transaction including all cached stuff. */
28      protected IBucketReadTrx mPageReadTrx;
29  
30      /** A dir used to generate the files in. */
31      private final File mTmpDir;
32  
33      /** A special Key for empty files */
34      public static final long emptyFileKey = Long.MIN_VALUE;
35  
36      /**
37       * Constructor.
38       * 
39       * 
40       * @param pPageTrx
41       *            Transaction state to work with.
42       * @throws TTIOException
43       *             if something odd happens within the creation process.
44       */
45      public FilelistenerReadTrx(final IBucketReadTrx pPageTrx) throws TTException {
46          mPageReadTrx = pPageTrx;
47          mTmpDir = Files.createTempDir();
48      }
49  
50      /**
51       * {@inheritDoc}
52       */
53      @Override
54      public String[] getFilePaths() {
55          Set<Map.Entry<IMetaEntry, IMetaEntry>> metaKeys = mPageReadTrx.getMetaBucket().entrySet();
56  
57          String[] filePaths = new String[metaKeys.size()];
58  
59          int i = 0;
60          for (Map.Entry<IMetaEntry, IMetaEntry> entry : metaKeys) {
61              filePaths[i] = entry.getKey().toString();
62              i++;
63          }
64  
65          return filePaths;
66      }
67  
68      /**
69       * {@inheritDoc}
70       */
71      @Override
72      public boolean fileExists(String pRelativePath) {
73          String paths[] = this.getFilePaths();
74          for (String s : paths) {
75              if (s.equals(pRelativePath)) {
76                  return true;
77              }
78          }
79  
80          return false;
81      }
82  
83      /**
84       * {@inheritDoc}
85       * 
86       * @throws TTIOException
87       */
88      @Override
89      public File getFullFile(String pRelativePath) throws TTIOException, IOException {
90          MetaValue value = (MetaValue)mPageReadTrx.getMetaBucket().get(new MetaKey(pRelativePath));
91  
92          File file =
93              new File(new StringBuilder().append(mTmpDir.getAbsolutePath()).append(File.separator).append(
94                  pRelativePath).toString());
95  
96          if (file.exists()) {
97              file.delete();
98          }
99  
100         file.createNewFile();
101 
102         if (value.getData() == emptyFileKey) {
103             return file;
104         }
105 
106         FileData node = (FileData)mPageReadTrx.getData(value.getData());
107 
108         OutputSupplier<FileOutputStream> supplier = Files.newOutputStreamSupplier(file, true);
109 
110         // Iterating as long as we didn't find the end of the file
111         // and writing the bytes to a temporary file.
112         do {
113             supplier.getOutput().write(node.getVal());
114             node = (FileData)mPageReadTrx.getData(node.getDataKey() + 1);
115         } while (!node.isEof());
116 
117         supplier.getOutput().close();
118 
119         return file;
120     }
121 
122     /**
123      * {@inheritDoc}
124      */
125     public int getCount() {
126         // This will only apply for the filebench or if this resource name on a local storage is used
127         String resourceName = "bench53473ResourcegraveISCSI9283";
128         String bucketFolder =
129             StorageManager.ROOT_PATH + File.separator + "storage" + File.separator + "resources"
130                 + File.separator + resourceName + File.separator + "data" + File.separator + resourceName;
131         String[] buckets = new File(bucketFolder).list();
132         if (buckets == null) {
133             return -1;
134         }
135         return buckets.length;
136     }
137 
138     /**
139      * {@inheritDoc}
140      */
141     @Override
142     public void close() throws TTIOException {
143         mTmpDir.delete();
144         mPageReadTrx.close();
145     }
146 
147     /**
148      * {@inheritDoc}
149      */
150     @Override
151     public boolean isClosed() {
152         return mPageReadTrx.isClosed();
153     }
154 
155     /**
156      * Make sure that the session is not yet closed when calling this method.
157      */
158     protected final void assertNotClosed() {
159         if (mPageReadTrx.isClosed()) {
160             throw new IllegalStateException("Transaction is already closed.");
161         }
162     }
163 
164     /**
165      * Replace the state of the transaction.
166      * 
167      * @param paramTransactionState
168      *            State of transaction.
169      */
170     protected final void setPageTransaction(final IBucketReadTrx paramTransactionState) {
171         mPageReadTrx = paramTransactionState;
172     }
173 
174 }