Aaron Digulla
Aaron Digulla

Reputation: 328780

Which NoSQL database for Java needs the least amount of setup?

I'm looking for a NoSQL database for Java which meets these requirements:

Upvotes: 6

Views: 2687

Answers (2)

Peter Lawrey
Peter Lawrey

Reputation: 533820

For the last possible setup, you can do all that in plain Java. Personally that would be the easiest to learn/maintain.

Can you include some requirements which make using a NoSQL library essential?

public class FileSystemNoSQL {
    private final File basePath;
    private final Map<String, String> documents = new TreeMap<String, String>();

    public FileSystemNoSQL(File basePath) {
        this.basePath = basePath;
        basePath.mkdirs();

        try {
            for (File file : basePath.listFiles()) {
                documents.put(file.getName(), FileUtils.readFileToString(file));
            }
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }

    public String get(String key) {
        return documents.get(key);
    }

    public void put(String key, String content) {
        try {
            FileUtils.write(new File(basePath, key), content);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
        documents.put(key, content);
    }

    public Map<String, String> findKeyContains(String text) {
        Map<String, String> set = new TreeMap<String, String>();
        for(Map.Entry<String, String> entry: documents.entrySet())
            if (entry.getKey().contains(text))
                set.put(entry.getKey(), entry.getValue());
        return set;
    }

    public Map<String, String> findContains(String text) {
        Map<String, String> set = new TreeMap<String, String>();
        for(Map.Entry<String, String> entry: documents.entrySet())
            if (entry.getKey().contains(text) || entry.getValue().contains(text))
                set.put(entry.getKey(), entry.getValue());
        return set;
    }

    public static void main(String ... args) {
        char[] spaces = new char[10240];
        Arrays.fill(spaces, ' ');
        String blank10k = new String(spaces);

        // build a database
        long start1 = System.nanoTime();
        FileSystemNoSQL fileSystemNoSQL1 = new FileSystemNoSQL(new File(System.getProperty("java.io.tmpdir"), "no-sql"));
        for(int i=0;i<1000;i++) {
            fileSystemNoSQL1.put("key: "+i, "value: "+i + blank10k);
        }
        long time1 = System.nanoTime() - start1;
        System.out.printf("Took %.3f seconds to build a database of 10 MB%n", time1 / 1e9);

        // reload the database
        long start2 = System.nanoTime();
        FileSystemNoSQL fileSystemNoSQL2 = new FileSystemNoSQL(new File(System.getProperty("java.io.tmpdir"), "no-sql"));
        long time2 = System.nanoTime() - start2;
        System.out.printf("Took %.3f seconds to load a database of 10 MB%n", time2/1e9);

        // perform queries
        long start3 = System.nanoTime();
        for(int i=0;i<1000;i++) {
            Map<String, String> contains = fileSystemNoSQL1.findKeyContains("key: " + i);
            if (contains.size() < 1) throw new AssertionError();
        }
        long time3 = System.nanoTime() - start3;
        System.out.printf("Took %.3f seconds to scan the keys of a database of 10 MB%n", time3/1e9);

        long start4 = System.nanoTime();
        for(int i=0;i<1000;i++) {
            Map<String, String> contains = fileSystemNoSQL1.findContains("value: " + i + ' ');
            if (contains.size() != 1) throw new AssertionError();
        }
        long time4 = System.nanoTime() - start4;
        System.out.printf("Took %.3f seconds to brute force scan of a database of 10 MB%n", time4/1e9);
    }
}

prints

Took 0.171 seconds to build a database of 10 MB
Took 0.088 seconds to load a database of 10 MB
Took 0.030 seconds to scan the keys of a database of 10 MB
Took 3.872 seconds to brute force scan of a database of 10 MB

Doing a brute force scan is the worst case. You can build application specific indexes fairly easily which can cut the time to sub-millisecond.

Upvotes: 4

Sebastian Łaskawiec
Sebastian Łaskawiec

Reputation: 2737

I've successfully used H2 DB. Is pretty fast and easy to use. It should meet your requirements, here is feature comparison matrix.

Upvotes: 0

Related Questions