// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;

public class Big {
    private static DatabaseEntry toEntry(String s) {
        return new DatabaseEntry(s.getBytes());
    }
    private static String fromEntry(DatabaseEntry dbe) {
        return new String(dbe.getData());
    }
    private static String load(String fnm) throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader br = new BufferedReader(new FileReader(fnm));
        String line;
        while((line = br.readLine()) != null) {
            sb.append(line + "\n");
        }
        br.close();
        return sb.toString();
    }
    private static void store(String txt, String fnm) throws IOException {
        PrintWriter pw = new PrintWriter(new FileWriter(fnm));
        pw.print(txt);
        pw.close();
    }
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        // open database
        EnvironmentConfig envcfg = new EnvironmentConfig();
        envcfg.setAllowCreateVoid(true);
        Environment env = new Environment(new File("data/"), envcfg);
        DatabaseConfig dbcfg = new DatabaseConfig();
        dbcfg.setAllowCreate(true);
        Database db = env.openDatabase(null, "bigdb", dbcfg);
        db.put(null, toEntry("copy1"), toEntry(load("4300-0.txt")));
        db.put(null, toEntry("copy2"), toEntry(load("4300-0.txt")));
        DatabaseEntry copy1 = new DatabaseEntry();
        db.get(null, toEntry("copy1"), copy1, null);
        store(fromEntry(copy1), "copy1.txt");
        DatabaseEntry copy2 = new DatabaseEntry();
        db.get(null, toEntry("copy2"), copy2, null);
        store(fromEntry(copy2), "copy2.txt");
        db.close();
        env.close();
    }
}
