Skip to content
This repository has been archived by the owner on Apr 23, 2019. It is now read-only.

Softmotions/ejdb-java

Repository files navigation

Embedded JSON database library Java binding

EJDB Java API documentation

Installation

Required tools/system libraries:

(A) Installing directly from sources

git clone https://github.com/Softmotions/ejdb-java.git
cd ./ejdb-java
./configure --with-jdk=<path to JDK> --prefix=<installation prefix>
make
sudo make install

Note: Ensure that libejdb is available from LD_LIBRARY_PATH

One snippet intro

package org.ejdb.sample1;

import org.ejdb.bson.BSONObject;
import org.ejdb.driver.EJDB;
import org.ejdb.driver.EJDBCollection;
import org.ejdb.driver.EJDBQueryBuilder;
import org.ejdb.driver.EJDBResultSet;

import java.util.Calendar;
import java.util.Date;

/**
 * @author Tyutyunkov Vyacheslav (tve@softmotions.com)
 * @version $Id$
 */
public class Main {

    public static void main(String[] args) {
        EJDB ejdb = new EJDB();

        try {
            // Used modes:
            //  - read
            //  - write
            //  - create db if not exists
            //  - truncate existing db
            ejdb.open("zoo", EJDB.JBOREADER | EJDB.JBOWRITER | EJDB.JBOCREAT | EJDB.JBOTRUNC);

            BSONObject parrot1 = new BSONObject("name", "Grenny")
                    .append("type", "African Grey")
                    .append("male", true)
                    .append("age", 1)
                    .append("birthhdate", new Date())
                    .append("likes", new String[]{"green color", "night", "toys"})
                    .append("extra1", null);

            Calendar calendar = Calendar.getInstance();
            calendar.set(2013, 1, 1, 0, 0, 1);

            BSONObject parrot2 = new BSONObject();
            parrot2.put("name", "Bounty");
            parrot2.put("type", "Cockatoo");
            parrot2.put("male", false);
            parrot2.put("age", 15);
            parrot2.put("birthdate", calendar.getTime());
            parrot2.put("likes", new String[]{"sugar cane"});
            parrot2.put("extra1", null);

            System.out.println("parrot1 =\n\t\t" + parrot1);
            System.out.println("parrot2 =\n\t\t" + parrot2);

            EJDBCollection parrots = ejdb.getCollection("parrots");

            // saving
            parrots.save(parrot1);
            parrots.save(parrot2);

            EJDBQueryBuilder qb;
            EJDBResultSet rs;

            // Below two equivalent queries:
            // Q1
            qb = new EJDBQueryBuilder();
            qb.field("likes", "toys")
                    .orderBy().asc("name").desc("age");

            rs = parrots.createQuery(qb).find();
            System.out.println();
            System.out.println("Results (Q1): " + rs.length());
            for (BSONObject r : rs) {
                System.out.println("\t" + r);
            }
            rs.close();

            // Q2
            qb = new EJDBQueryBuilder();
            qb.field("likes").eq("toys")
                    .orderBy().add("name", true).add("age", false);

            rs = parrots.createQuery(qb).find();
            System.out.println();
            System.out.println("Results (Q2): " + rs.length());
            for (BSONObject r : rs) {
                System.out.println("\t" + r);
            }

            // Second way to iterate
            System.out.println();
            System.out.println("Results (Q2): " + rs.length());
            for (int i = 0; i < rs.length(); ++i) {
                System.out.println("\t" + i + " => " + rs.get(i));
            }

            rs.close();
        } finally {
            if (ejdb.isOpen()) {
                ejdb.close();
            }
        }
    }
}

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •