Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

java code for a mini db using a hashtable and serialization

  1. User

    java code for a mini db using a hashtable and serialization

  2. Assistant

    Here's an example of a simple implementation of a mini key-value database using Hashtable, serialization, and deserialization. It uses basic file I/O operations to read and write data from a file.

    MiniDB.java:

    import java.io.*;
    import java.util.Hashtable;
    
    public class MiniDB {
        private String file;
        private Hashtable<String, Serializable> hashtable;
    
        public MiniDB(String file) {
            this.file = file;
            File dbFile = new File(file);
    
            try {
                if (!dbFile.exists()) {
                    dbFile.createNewFile();
                    hashtable = new Hashtable<>();
                    flush();
                } else {
                    FileInputStream fileInputStream = new FileInputStream(file);
                    if (fileInputStream.available() > 0) {
                        ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
                        hashtable = (Hashtable<String, Serializable>) objectInputStream.readObject();
                        objectInputStream.close();
                    } else {
                        hashtable = new Hashtable<>();
                    }
                    fileInputStream.close();
                }
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    
        public Serializable get(String key) {
            return hashtable.get(key);
        }
    
        public boolean containsKey(String key) {
            return hashtable.containsKey(key);
        }
    
        public void put(String key, Serializable value) {
            hashtable.put(key, value);
            flush();
        }
    
        public void delete(String key) {
            hashtable.remove(key);
            flush();
        }
    
        private void flush() {
            try {
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
                objectOutputStream.writeObject(hashtable);
                objectOutputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    Usage example:

    public class MiniDBExample {
        public static void main(String[] args) {
            MiniDB miniDB = new MiniDB("mydb.dat");
    
            miniDB.put("key1", "value1");
            miniDB.put("key2", 42);
    
            System.out.println("Key1: " + miniDB.get("key1"));
            System.out.println("Key2: " + miniDB.get("key2"));
    
            miniDB.delete("key1");
    
            System.out.println("Key1: " + miniDB.get("key1"));
        }
    }
    

    This example creates a MiniDB instance based on a file named mydb.dat, and stores "value1" as Serializable data with the key "key1" and 42 as Serializable data with the key "key2" in the Hashtable. Finally, it removes the key "key1" from the Hashtable.