/[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.16 by benja, Mon Dec 30 13:48:23 2002 UTC revision 1.17 by benja, Mon Jan 13 23:21:44 2003 UTC
# Line 23  DirPool.java Line 23  DirPool.java
23  package gzz.storm.impl;  package gzz.storm.impl;
24  import gzz.storm.*;  import gzz.storm.*;
25  import gzz.storm.headers.*;  import gzz.storm.headers.*;
26    import gzz.util.*;
27  import java.io.*;  import java.io.*;
28  import java.util.*;  import java.util.*;
29    
# Line 35  public class DirPool extends AbstractLoc Line 36  public class DirPool extends AbstractLoc
36       */       */
37      File dir;      File dir;
38    
39        /** The DB objects by index type URI.
40         */
41        protected Map dbs;
42    
43      /** Get the File object for the block      /** Get the File object for the block
44       *  corresponding to a given id.       *  corresponding to a given id.
45       *  This is needed in many places and wrapping it in a convenience       *  This is needed in many places and wrapping it in a convenience
# Line 81  public class DirPool extends AbstractLoc Line 86  public class DirPool extends AbstractLoc
86              }              }
87    
88              block = new FileBlock(id);              block = new FileBlock(id);
89                added(block);
90          }          }
91      }      }
92    
# Line 98  public class DirPool extends AbstractLoc Line 104  public class DirPool extends AbstractLoc
104          }          }
105      }      }
106    
107        protected class DirDB implements DB {
108            protected File dbDir;
109    
110            protected DirDB(String indexTypeURI) throws IOException {
111                byte[] ascii = indexTypeURI.getBytes("US-ASCII");
112                String hex = HexUtil.byteArrToHex(ascii);
113                dbDir = new File(dir, "idx_"+hex);
114    
115                if(!dbDir.exists()) {
116                    dbDir.mkdir();
117                    
118                    OutputStream os = new FileOutputStream(new File(dbDir, "index_type"));
119                    os.write(ascii); os.write((byte)'\n');
120                    os.close();
121                }
122            }
123    
124            protected File getKeyFile(byte[] key) throws IOException {
125                return new File(dbDir, "key_"+HexUtil.byteArrToHex(key));
126            }
127    
128            public Collector get(byte[] key) throws IOException {
129                Set result = new HashSet();
130                File f = getKeyFile(key);
131    
132                if(f.exists()) {
133                    InputStream in = new FileInputStream(getKeyFile(key));
134                    Reader ir = new InputStreamReader(in, "US-ASCII");
135                    BufferedReader r = new BufferedReader(ir);
136                    
137                    String line = r.readLine();
138                    while(line != null && !line.equals("")) {
139                        int i = line.indexOf(' ');
140                        BlockId block = new BlockId(line.substring(0, i));
141                        byte[] value = HexUtil.hexToByteArr(line.substring(i+1));
142                        
143                        result.add(new Mapping(block, key, value));
144    
145                        line = r.readLine();
146                    }
147                }
148    
149                return new SimpleSetCollector(result);
150            }
151    
152            protected void add(Mapping m) throws IOException {
153                OutputStream os = new FileOutputStream(getKeyFile(m.key), true);
154                os.write(m.block.getURI().getBytes("US-ASCII"));
155                os.write((byte)' ');
156                os.write(HexUtil.byteArrToHex(m.value).getBytes("US-ASCII"));
157                os.write((byte)'\n');
158                os.close();
159            }
160        }
161    
162      /** Create a new DirPool.      /** Create a new DirPool.
163       *  @param dir The directory blocks are stored in.       *  @param dir The directory blocks are stored in.
164       *             Must already exist.       *             Must already exist.
165       *  @throws IllegalArgumentException if the file isn't a directory       *  @throws IllegalArgumentException if the file isn't a directory
166       *                                   or does not exist yet.       *                                   or does not exist yet.
167       */       */
168      public DirPool(File dir, Set indexTypes) {      public DirPool(File dir, Set indexTypes) throws IOException {
169          super(indexTypes);          super(indexTypes);
170          this.dir = dir;          this.dir = dir;
171      }      }
# Line 124  public class DirPool extends AbstractLoc Line 185  public class DirPool extends AbstractLoc
185    
186          if(!temp.renameTo(getFile(id)))          if(!temp.renameTo(getFile(id)))
187              throw new IOException("Could not rename temporary file");              throw new IOException("Could not rename temporary file");
188            
189            added(get(id));
190      }      }
191      public void delete(Block b) throws IOException {      public void delete(Block b) throws IOException {
192          getFile(b.getId()).delete();          getFile(b.getId()).delete();
# Line 150  public class DirPool extends AbstractLoc Line 213  public class DirPool extends AbstractLoc
213          File tempFile = gzz.util.TempFileUtil.tmpFile(dir);          File tempFile = gzz.util.TempFileUtil.tmpFile(dir);
214          return new FileBlockOutputStream(new VerbatimHeader822(hdr), tempFile);          return new FileBlockOutputStream(new VerbatimHeader822(hdr), tempFile);
215      }      }
216    
217        protected DB getDB(String typeURI) throws IOException {
218            if(dbs == null) dbs = new HashMap();
219            DB db = (DB)dbs.get(typeURI);
220            if(db == null) {
221                db = new DirDB(typeURI);
222                dbs.put(typeURI, db);
223            }
224            return db;
225        }
226    
227        protected void added(Block block) throws IOException {
228            for(Iterator i = indexTypes.iterator(); i.hasNext();) {
229                IndexType it = (IndexType)i.next();
230                Set mappings = it.getMappings(block);
231                for(Iterator j = mappings.iterator(); j.hasNext();) {
232                    Mapping m = (Mapping)j.next();
233                    if(!m.block.equals(block.getId()))
234                        throw new Error("Wrong block in mapping: "+m);
235    
236                    ((DirDB)getDB(it.getIndexTypeURI())).add(m);
237                }
238            }
239        }
240  }  }

Legend:
Removed from v.1.16  
changed lines
  Added in v.1.17

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