Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.robrua.orianna.store;
  
  import java.io.File;
 import java.util.List;
 import java.util.Map;
 
A very simple filesystem-based DB

Author(s):
Rob Rua (robrua@alumni.cmu.edu)
 
 public class FileSystemDB extends DataStore {
    
Iterator for DB entries on the file system

Author(s):
Rob Rua (robrua@alumni.cmu.edu)
 
     public static class FSIterator<T extends OriannaObject<?>> implements Iterator<T> {
         private final Class<T> clazz;
         private final Iterator<Filefiles;

        

Parameters:
folder root DB folder for type
clazz the type
 
         private FSIterator(final File folderfinal Class<T> clazz) {
              = Arrays.asList(folder.listFiles()).iterator();
             this. = clazz;
         }
 
         @Override
         public boolean hasNext() {
             return .hasNext();
         }
 
         @Override
         public T next() {
             final File file = .next();
             return read(file);
         }
     }
 
     private static final String HAVE_ALL_NAME = "meta";

    
Reads an object from a file

Parameters:
file the file to read from
type the type of the object stored
Returns:
the object in the file
 
     @SuppressWarnings("unchecked")
     private static <T extends OriannaObject<?>> T read(final File filefinal Class<T> type) {
         try {
             return (T)type.getDeclaredConstructors()[0].newInstance(readFile(file));
         }
             return null;
         }
     }

    
Reads an object from a file

Parameters:
file the file to read from
Returns:
the object in the file
 
     private static Object readFile(final File file) {
         ObjectInputStream ois = null;
         try {
             ois = new ObjectInputStream(new FileInputStream(file));
             return ois.readObject();
         }
         catch(IOException | ClassNotFoundException e) {
             throw new OriannaException("Unable to read file!");
         }
         finally {
             if(ois != null) {
                 try {
                     ois.close();
                 }
                 catch(final IOException e) {
                     throw new OriannaException("Unable to close file stream!");
                }
            }
        }
    }

    
Writes an object to a file

Parameters:
file the file to write to
object the object to write
    private static void write(final File filefinal OriannaObject<?> object) {
        writeFile(fileobject.getDto());
    }

    
Writes an object to a file

Parameters:
file the file to write to
object the object to write
    private static void writeFile(final File filefinal Object object) {
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(object);
        }
        catch(final IOException e) {
            throw new OriannaException("Unable to write to file!");
        }
        finally {
            if(oos != null) {
                try {
                    oos.close();
                }
                catch(final IOException e) {
                    throw new OriannaException("Unable to close file stream!");
                }
            }
        }
    }
    private final Map<Class<? extends OriannaObject<?>>, BooleanhaveAll;
    private final File root;

    

Parameters:
root the root folder of the DB
    @SuppressWarnings("unchecked")
    public FileSystemDB(final File root) {
        if(root == null || root.exists() && !root.isDirectory()) {
            throw new IllegalArgumentException("Root file must be a directory!");
        }
        this. = root;
        if(!root.exists()) {
            root.mkdirs();
        }
        final File allFile = new File(root);
        if(!allFile.exists()) {
             = new HashMap<>();
        }
        else {
             = (HashMap<Class<? extends OriannaObject<?>>, Boolean>)readFile(allFile);
        }
    }

    

Parameters:
rootPath the path to the root folder of the DB
    public FileSystemDB(final String rootPath) {
        this(new File(rootPath));
    }
    @Override
    protected <T extends OriannaObject<?>> boolean checkHasAll(final Class<T> type) {
        final Boolean val = .get(type);
        if(val == null) {
            return false;
        }
        return val.booleanValue();
    }
    @Override
    protected <T extends OriannaObject<?>> void doDelete(final Class<T> typefinal List<?> keys) {
        final File folder = getFolder(type);
        for(final Object key : keys) {
            final File file = getFile(folderkey);
            if(file.exists()) {
                file.delete();
            }
        }
    }
    @Override
    protected <T extends OriannaObject<?>> void doDelete(final Class<T> typefinal Object key) {
        final File file = getFile(getFolder(type), key);
        if(file.exists()) {
            file.delete();
        }
    }
    @Override
    protected <T extends OriannaObject<?>> List<T> doGet(final Class<T> typefinal List<?> keys) {
        final File folder = getFolder(type);
        final List<T> result = new ArrayList<>(keys.size());
        for(final Object key : keys) {
            final File file = getFile(folderkey);
            if(file.exists()) {
                result.add(read(filetype));
            }
            else {
                result.add(null);
            }
        }
        return result;
    }
    @Override
    protected <T extends OriannaObject<?>> T doGet(final Class<T> typefinal Object key) {
        final File file = getFile(getFolder(type), key);
        if(!file.exists()) {
            return null;
        }
        return read(filetype);
    }
    @Override
    protected <T extends OriannaObject<?>> List<T> doGetAll(final Class<T> type) {
        final File folder = getFolder(type);
        final List<T> result = new ArrayList<>();
        for(final File file : folder.listFiles()) {
            result.add(read(filetype));
        }
        return result;
    }
    @Override
    public <T extends OriannaObject<?>> Iterator<T> doGetIterator(final Class<T> type) {
        final File folder = getFolder(type);
        return new FSIterator<>(foldertype);
    }
    @Override
    protected <T extends OriannaObject<?>> void doStore(final Class<T> typefinal List<T> objsfinal List<?> keysfinal boolean isFullSet) {
        if(isFullSet) {
            .put(type.);
            writeFile(new File(), );
        }
        final File folder = getFolder(type);
        for(int i = 0; i < objs.size(); i++) {
            write(getFile(folderkeys.get(i)), objs.get(i));
        }
    }
    @Override
    protected <T extends OriannaObject<?>> void doStore(final T objfinal Object key) {
        write(getFile(getFolder(obj.getClass()), key), obj);
    }

    

Parameters:
folder the folder for the object's type
key the key
Returns:
the file that the key is or would be stored in
    private File getFile(final File folderfinal Object key) {
        return new File(folder, Integer.toString(key.hashCode()));
    }

    

Parameters:
type a type
Returns:
the folder objects of that type are found in
    private File getFolder(final Class<?> type) {
        final File folder = new File(, Integer.toString(type.getCanonicalName().hashCode()));
        if(!folder.exists()) {
            folder.mkdir();
        }
        return folder;
    }
New to GrepCode? Check out our FAQ X