/* DirDB.java * * Copyright (c) 2003, Benja Fallenstein * * This file is part of Storm. * * Storm is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Storm is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General * Public License for more details. * * You should have received a copy of the GNU Lesser General * Public License along with Storm; if not, write to the Free * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * * */ /* * Written by Benja Fallenstein */ package org.nongnu.storm.impl; import org.nongnu.storm.*; import org.nongnu.storm.util.*; import java.io.*; import java.util.*; /** An IndexedPool.DB implementation for DirPools. */ public class DirDB implements IndexedPool.DB { protected File dbDir; /** * @param inDir The directory of the DirPool. * @param indexTypeURI The URI of the index type, used to * determine the subdirectory indexing info * is to be stored in: * /idx_ */ public DirDB(File inDir, IndexedPool.IndexType indexType) throws IOException { if(inDir == null) throw new NullPointerException("null directory"); byte[] ascii = indexType.getIndexTypeURI().getBytes("US-ASCII"); String hex = HexUtil.byteArrToHex(ascii); dbDir = new File(inDir, "idx_"+hex); if(!dbDir.exists()) { dbDir.mkdir(); OutputStream os = new FileOutputStream(new File(dbDir, "index_type")); os.write(ascii); os.write((byte)'\n'); os.write(indexType.getHumanReadableName().getBytes()); os.write((byte)'\n'); os.close(); os = new FileOutputStream(new File(dbDir, "indexed_blocks")); os.close(); } } protected File getKeyFile(byte[] key) throws IOException { return new File(dbDir, "key_"+HexUtil.byteArrToHex(key)); } public Collector get(byte[] key) throws IOException { Set result = new HashSet(); File f = getKeyFile(key); if(f.exists()) { InputStream in = new FileInputStream(getKeyFile(key)); Reader ir = new InputStreamReader(in, "US-ASCII"); BufferedReader r = new BufferedReader(ir); String line = r.readLine(); while(line != null && !line.equals("")) { int i = line.indexOf(' '); BlockId block = new BlockId(line.substring(0, i)); byte[] value = HexUtil.hexToByteArr(line.substring(i+1)); result.add(new IndexedPool.Mapping(block, key, value)); line = r.readLine(); } } return new SimpleSetCollector(result); } public Set getIndexed() throws IOException { HashSet result = new HashSet(); InputStream in = new FileInputStream(new File(dbDir, "indexed_blocks")); Reader ir = new InputStreamReader(in, "US-ASCII"); BufferedReader r = new BufferedReader(ir); String line = r.readLine(); while(line != null && !line.equals("")) { result.add(new BlockId(line)); line = r.readLine(); } return result; } public void add(IndexedPool.Mapping m) throws IOException { if(getIndexed().contains(m.block)) return; OutputStream os = new FileOutputStream(getKeyFile(m.key).getPath(), true); os.write(m.block.getURI().getBytes("US-ASCII")); os.write((byte)' '); os.write(HexUtil.byteArrToHex(m.value).getBytes("US-ASCII")); os.write((byte)'\n'); os.close(); os = new FileOutputStream(new File(dbDir, "indexed_blocks").getPath(), true); os.write(m.block.getURI().getBytes("US-ASCII")); os.write((byte)'\n'); os.close(); } public String toString() { return ""; } }