/[gzz]/gzz/lava/gzz/storm/impl/DirPool.java
ViewVC logotype

Diff of /gzz/lava/gzz/storm/impl/DirPool.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.8 by benja, Sat Nov 16 02:15:14 2002 UTC revision 1.9 by benja, Sat Nov 16 04:38:04 2002 UTC
# Line 29  import java.util.*; Line 29  import java.util.*;
29  /** A StormPool storing blocks in individual files in a directory.  /** A StormPool storing blocks in individual files in a directory.
30   *  File names have the form <code>b_</code><i>idstring</i>.   *  File names have the form <code>b_</code><i>idstring</i>.
31   *  <p>   *  <p>
32   *  Ideas for making this more efficient if needed:   *  XXX check id on reading blocks in (use id-checking input stream)
  *  Don't use byte arrays. When creating a new block, write it  
  *  to a temporary file first and multiplex to another  
  *  output stream to get the id; rename the file when  
  *  the whole block is through and the id is known.  
33   */   */
34  public class DirPool extends AbstractPool {  public class DirPool extends AbstractPool {
35    
# Line 53  public class DirPool extends AbstractPoo Line 49  public class DirPool extends AbstractPoo
49       *  corresponding to the given id.       *  corresponding to the given id.
50       */       */
51      protected Header822 getFileHeader(BlockId id) throws IOException {      protected Header822 getFileHeader(BlockId id) throws IOException {
52          InputStream is = new FileInputStream(getFile(id));          InputStream is =
53                new BufferedInputStream(new FileInputStream(getFile(id)));
54          Header822 header = Headers822.readHeader(is);          Header822 header = Headers822.readHeader(is);
55          is.close();          is.close();
56          return header;          return header;
# Line 62  public class DirPool extends AbstractPoo Line 59  public class DirPool extends AbstractPoo
59    
60      protected class FileBlockOutputStream extends AbstractBlockOutputStream {      protected class FileBlockOutputStream extends AbstractBlockOutputStream {
61          protected ByteArrayOutputStream baos;          protected ByteArrayOutputStream baos;
62            protected File tempFile;
63    
64          protected FileBlockOutputStream(Header822 header) throws IOException {          protected FileBlockOutputStream(Header822 header, File tempFile)
65              super(new ByteArrayOutputStream(), header);                                                           throws IOException {
66              baos = (ByteArrayOutputStream)out;              super(new BufferedOutputStream(new FileOutputStream(tempFile)),
67              header.writeTo(baos);                    header);
68                this.tempFile = tempFile;
69          }          }
70    
71          public void close() throws IOException {          public void close() throws IOException {
72              byte[] bytes = baos.toByteArray();              out.close();
73              BlockId id = BlockId.getIdForData(bytes);              
74                BlockId id = makeIdFromDigest();
75              File file = getFile(id);              File file = getFile(id);
76    
77              OutputStream os = new FileOutputStream(file);              if(!tempFile.renameTo(file)) {
78              os.write(bytes);                  tempFile.delete();
79              os.close();                  throw new IOException("Could not rename temporary file");
80                }
81    
82              block = new FileBlock(id);              block = new FileBlock(id);
83          }          }
# Line 91  public class DirPool extends AbstractPoo Line 92  public class DirPool extends AbstractPoo
92          }          }
93    
94          public InputStream getRawInputStream() throws IOException {          public InputStream getRawInputStream() throws IOException {
95              return new FileInputStream(file);              return new BufferedInputStream(new FileInputStream(file));
96          }          }
97      }      }
98    
# Line 115  public class DirPool extends AbstractPoo Line 116  public class DirPool extends AbstractPoo
116          if(!id.equals(b.getId()))          if(!id.equals(b.getId()))
117              throw new IOException("Block with bad id supplied");              throw new IOException("Block with bad id supplied");
118    
119          OutputStream os = new FileOutputStream(getFile(id));          OutputStream os =
120                new BufferedOutputStream(new FileOutputStream(getFile(id)));
121          os.write(bytes);          os.write(bytes);
122          os.close();          os.close();
123      }      }
# Line 136  public class DirPool extends AbstractPoo Line 138  public class DirPool extends AbstractPoo
138                                                            throws IOException {                                                            throws IOException {
139          Header822 hdr = new UniqueHeader822();          Header822 hdr = new UniqueHeader822();
140          hdr.add("Content-Type", contentType);          hdr.add("Content-Type", contentType);
141          return new FileBlockOutputStream(hdr);          File tempFile = gzz.util.TempFileUtil.tmpFile(dir);
142            return new FileBlockOutputStream(hdr, tempFile);
143      }      }
144      public BlockOutputStream getBlockOutputStream(Header822 hdr)      public BlockOutputStream getBlockOutputStream(Header822 hdr)
145                                                            throws IOException {                                                            throws IOException {
146          return new FileBlockOutputStream(new VerbatimHeader822(hdr));          File tempFile = gzz.util.TempFileUtil.tmpFile(dir);
147            return new FileBlockOutputStream(new VerbatimHeader822(hdr), tempFile);
148      }      }
149  }  }

Legend:
Removed from v.1.8  
changed lines
  Added in v.1.9

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26