/[storm]/storm/org/nongnu/storm/impl/DirPool.java
ViewVC logotype

Diff of /storm/org/nongnu/storm/impl/DirPool.java

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

revision 1.2 by benja, Thu Apr 3 19:07:54 2003 UTC revision 1.3 by benja, Tue Apr 8 08:04:46 2003 UTC
# Line 3  DirPool.java Line 3  DirPool.java
3   *       *    
4   *    Copyright (c) 2002, Benja Fallenstein   *    Copyright (c) 2002, Benja Fallenstein
5   *       *    
6   *    This file is part of Storm.   *    This file is part of Fenfire.
7   *       *    
8   *    Storm is free software; you can redistribute it and/or modify it under   *    Fenfire is free software; you can redistribute it and/or modify it under
9   *    the terms of the GNU Lesser General Public License as published by   *    the terms of the GNU Lesser General Public License as published by
10   *    the Free Software Foundation; either version 2 of the License, or   *    the Free Software Foundation; either version 2 of the License, or
11   *    (at your option) any later version.   *    (at your option) any later version.
12   *       *    
13   *    Storm is distributed in the hope that it will be useful, but WITHOUT   *    Fenfire is distributed in the hope that it will be useful, but WITHOUT
14   *    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY   *    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15   *    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General   *    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
16   *    Public License for more details.   *    Public License for more details.
17   *       *    
18   *    You should have received a copy of the GNU Lesser General   *    You should have received a copy of the GNU Lesser General
19   *    Public License along with Storm; if not, write to the Free   *    Public License along with Fenfire; if not, write to the Free
20   *    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,   *    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21   *    MA  02111-1307  USA   *    MA  02111-1307  USA
22   *       *    
# Line 27  DirPool.java Line 27  DirPool.java
27   */   */
28  package org.nongnu.storm.impl;  package org.nongnu.storm.impl;
29  import org.nongnu.storm.*;  import org.nongnu.storm.*;
30  import org.nongnu.storm.headers.*;  import org.nongnu.storm.util.*;
31  import java.io.*;  import java.io.*;
32  import java.util.*;  import java.util.*;
33    
34  /** A StormPool storing blocks in individual files in a directory.  /** A StormPool storing blocks in individual files in a directory.
35   *  File names have the form <code>b_</code><i>idstring</i>.   *  File names have the form <code>data_</code><i>idstring</i>.
36   */   */
37  public class DirPool extends AbstractLocalPool {  public class DirPool extends AbstractLocalPool {
38    
# Line 50  public class DirPool extends AbstractLoc Line 50  public class DirPool extends AbstractLoc
50       *  function makes code easier to read & maintain.       *  function makes code easier to read & maintain.
51       */       */
52      protected final File getFile(BlockId id) {      protected final File getFile(BlockId id) {
53          String hex = org.nongnu.storm.util.HexUtil.byteArrToHex(id.getBytes());          return new File(dir, "data_" + id.getBitprint());
         return new File(dir, "b_" + hex);  
     }  
   
     /** Read the header of the block  
      *  corresponding to the given id.  
      *  Like getFile(), this is needed in a number  
      *  of different places.  
      */  
     protected final Header822 getFileHeader(BlockId id) throws IOException {  
         InputStream is = new BufferedInputStream(  
             id.getCheckedInputStream(new FileInputStream(getFile(id))));  
         Header822 header = Headers822.readHeader(is);  
         is.close();  
         return header;  
54      }      }
55            
56    
57      protected class FileBlockOutputStream extends AbstractBlockOutputStream {      protected class FileBlockOutputStream extends AbstractBlockOutputStream {
58          protected File tempFile;          protected File tempFile;
59    
60          protected FileBlockOutputStream(Header822 header, File tempFile)          protected FileBlockOutputStream(File tempFile, String contentType)
61                                                           throws IOException {                                                           throws IOException {
62              super(new BufferedOutputStream(new FileOutputStream(tempFile)),              super(new BufferedOutputStream(new FileOutputStream(tempFile)),
63                    header);                    contentType);
64              this.tempFile = tempFile;              this.tempFile = tempFile;
65          }          }
66    
# Line 97  public class DirPool extends AbstractLoc Line 83  public class DirPool extends AbstractLoc
83          protected File file;          protected File file;
84    
85          protected FileBlock(BlockId id) throws IOException {          protected FileBlock(BlockId id) throws IOException {
86              super(id, getFileHeader(id));              super(id);
87              this.file = getFile(id);              this.file = getFile(id);
88                if(!file.exists())
89                    throw new FileNotFoundException("Block: "+id);
90          }          }
91    
92          public InputStream getRawInputStream() throws IOException {          public InputStream getInputStream() throws IOException {
93              return new BufferedInputStream(              return new BufferedInputStream(
94                  id.getCheckedInputStream(new FileInputStream(file)));                  id.getCheckedInputStream(new FileInputStream(file)));
95          }          }
# Line 126  public class DirPool extends AbstractLoc Line 114  public class DirPool extends AbstractLoc
114          return new FileBlock(id);          return new FileBlock(id);
115      }      }
116      public void add(Block b) throws IOException {      public void add(Block b) throws IOException {
117          File temp = org.nongnu.storm.util.TempFileUtil.tmpFile(dir);          File temp = TempFileUtil.tmpFile(dir);
118                    
119          BlockId id = b.getId();          BlockId id = b.getId();
120          InputStream is = id.getCheckedInputStream(b.getRawInputStream());          InputStream is = id.getCheckedInputStream(b.getInputStream());
121          OutputStream os =          OutputStream os =
122              new BufferedOutputStream(new FileOutputStream(temp));              new BufferedOutputStream(new FileOutputStream(temp));
123                    
124          org.nongnu.storm.util.CopyUtil.copy(is, os);          CopyUtil.copy(is, os);
125    
126          if(!temp.renameTo(getFile(id)))          if(!temp.renameTo(getFile(id)))
127              throw new IOException("Could not rename temporary file");              throw new IOException("Could not rename temporary file");
# Line 143  public class DirPool extends AbstractLoc Line 131  public class DirPool extends AbstractLoc
131      public void delete(Block b) throws IOException {      public void delete(Block b) throws IOException {
132          getFile(b.getId()).delete();          getFile(b.getId()).delete();
133      }      }
134      public SetCollector getIds() {      public SetCollector getIds() throws IOException {
135          HashSet ids = new HashSet();          HashSet ids = new HashSet();
136          String[] list = dir.list();          String[] list = dir.list();
137                    
138          for(int i=0; i<list.length; i++)          for(int i=0; i<list.length; i++)
139              if(list[i].startsWith("b_"))              if(list[i].startsWith("data_")) {
140                  ids.add(new BlockId("storm:block:" + list[i].substring(2)));                  String bitprint = list[i].substring(5);
141                    Set types = readTypes(bitprint);
142                    for(Iterator j=readTypes(bitprint).iterator(); j.hasNext();) {
143                        ids.add(new BlockId(BlockId.PREFIX + j.next() +
144                                            "," + bitprint));
145                    }
146                }
147    
148          return new SimpleSetCollector(ids);          return new SimpleSetCollector(ids);
149      }      }
150      public BlockOutputStream getBlockOutputStream(String contentType)      public BlockOutputStream getBlockOutputStream(String contentType)
151                                                            throws IOException {                                                            throws IOException {
152          Header822 hdr = new UniqueHeader822();          File tempFile = TempFileUtil.tmpFile(dir);
153          hdr.add("Content-Type", contentType);          return new FileBlockOutputStream(tempFile, contentType);
         File tempFile = org.nongnu.storm.util.TempFileUtil.tmpFile(dir);  
         return new FileBlockOutputStream(hdr, tempFile);  
     }  
     public BlockOutputStream getBlockOutputStream(Header822 hdr)  
                                                           throws IOException {  
         File tempFile = org.nongnu.storm.util.TempFileUtil.tmpFile(dir);  
         return new FileBlockOutputStream(new VerbatimHeader822(hdr), tempFile);  
154      }      }
155    
156      protected DB getDB(IndexType indexType) throws IOException {      protected DB getDB(IndexType indexType) throws IOException {
# Line 187  public class DirPool extends AbstractLoc Line 174  public class DirPool extends AbstractLoc
174          return db;          return db;
175      }      }
176    
177        protected Set readTypes(String bitprint) throws IOException {
178            HashSet types = new HashSet();
179            File file = new File("types_"+bitprint);
180            if(!file.exists()) return types;
181            String t;
182            BufferedReader r = new BufferedReader(new FileReader(file));
183            while((t = r.readLine()) != null) types.add(t);
184            r.close();
185            return types;
186        }
187    
188      protected void added(Block block) throws IOException {      protected void added(Block block) throws IOException {
189            String file = "types_"+block.getId().getBitprint();
190            Set types = readTypes(block.getId().getBitprint());
191            types.add(block.getId().getContentType());
192            PrintWriter p = new PrintWriter(new FileWriter(file));
193            for(Iterator i = types.iterator(); i.hasNext();)
194                p.println(i.next());
195            p.close();
196    
197          for(Iterator i = indexTypes.iterator(); i.hasNext();) {          for(Iterator i = indexTypes.iterator(); i.hasNext();) {
198              IndexType it = (IndexType)i.next();              IndexType it = (IndexType)i.next();
199              Set mappings = it.getMappings(block);              Set mappings = it.getMappings(block);

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

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