All Projects → nitrite → Nitrite Java

nitrite / Nitrite Java

Licence: apache-2.0
Java embedded nosql document store

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Nitrite Java

Cog
A Persistent Embedded Graph Database for Python
Stars: ✭ 90 (-83.27%)
Mutual labels:  database, nosql, embedded-database
Xodus
Transactional schema-less embedded database used by JetBrains YouTrack and JetBrains Hub.
Stars: ✭ 864 (+60.59%)
Mutual labels:  database, nosql, embedded-database
Kache
A simple in memory cache written using go
Stars: ✭ 349 (-35.13%)
Mutual labels:  database, nosql
Mongo
The MongoDB Database
Stars: ✭ 20,883 (+3781.6%)
Mutual labels:  database, nosql
Awesome Elasticsearch
A curated list of the most important and useful resources about elasticsearch: articles, videos, blogs, tips and tricks, use cases. All about Elasticsearch!
Stars: ✭ 4,168 (+674.72%)
Mutual labels:  database, nosql
Concourse
Distributed database warehouse for transactions, search and analytics across time.
Stars: ✭ 310 (-42.38%)
Mutual labels:  database, nosql
Bitnami Docker Redis
Bitnami Redis Docker Image
Stars: ✭ 317 (-41.08%)
Mutual labels:  database, nosql
Objectbox Java
ObjectBox is a superfast lightweight database for objects
Stars: ✭ 3,950 (+634.2%)
Mutual labels:  database, nosql
sophy
Fast Python bindings to Sophia Database
Stars: ✭ 77 (-85.69%)
Mutual labels:  nosql, embedded-database
Sleekdb
Pure PHP NoSQL database with no dependency. Flat file, JSON based document database.
Stars: ✭ 450 (-16.36%)
Mutual labels:  database, nosql
Tinydb
TinyDB is a lightweight document oriented database optimized for your happiness :)
Stars: ✭ 4,713 (+776.02%)
Mutual labels:  database, nosql
Orientdb
OrientDB is the most versatile DBMS supporting Graph, Document, Reactive, Full-Text and Geospatial models in one Multi-Model product. OrientDB can run distributed (Multi-Master), supports SQL, ACID Transactions, Full-Text indexing and Reactive Queries. OrientDB Community Edition is Open Source using a liberal Apache 2 license.
Stars: ✭ 4,394 (+716.73%)
Mutual labels:  database, nosql
Inquiry Deprecated
[DEPRECATED]: Prefer Room by Google, or SQLDelight by Square.
Stars: ✭ 264 (-50.93%)
Mutual labels:  database, nosql
Bedquilt Core
A JSON document store on PostgreSQL
Stars: ✭ 256 (-52.42%)
Mutual labels:  database, nosql
Unqlite Python
Python bindings for the UnQLite embedded NoSQL database
Stars: ✭ 321 (-40.33%)
Mutual labels:  nosql, embedded-database
python-lsm-db
Python bindings for the SQLite4 LSM database.
Stars: ✭ 115 (-78.62%)
Mutual labels:  nosql, embedded-database
Dbreeze
C# .NET MONO NOSQL ( key value store embedded ) ACID multi-paradigm database management system.
Stars: ✭ 383 (-28.81%)
Mutual labels:  database, nosql
Csharp Driver
DataStax C# Driver for Apache Cassandra
Stars: ✭ 477 (-11.34%)
Mutual labels:  database, nosql
dockage
embedded document/json store
Stars: ✭ 20 (-96.28%)
Mutual labels:  nosql, embedded-database
sync-client
SyncProxy javascript client with support for all major embedded databases (IndexedDB, SQLite, WebSQL, LokiJS...)
Stars: ✭ 30 (-94.42%)
Mutual labels:  nosql, embedded-database

Nitrite Database

Build CodeQL Codacy codecov Javadocs Discussion Backers on Open Collective Backers on Open Collective Gitpod ready-to-code

Logo

NOsql Object (NO2 a.k.a Nitrite) database is an open source nosql embedded document store written in Java. It has MongoDB like API. It supports both in-memory and file based persistent store.

Nitrite is an embedded database ideal for desktop, mobile or small web applications.

It features:

  • Schemaless document collection and object repository
  • In-memory / file-based store
  • Pluggable storage engines - mvstore, mapdb, rocksdb
  • ACID transaction
  • Schema migration
  • Indexing
  • Full text search
  • Rx-Java support
  • Both way replication via Nitrite DataGate server
  • Very fast, lightweight and fluent API
  • Android compatibility (API Level 19)

Kotlin Extension

Nitrite has a kotlin extension called Potassium Nitrite for kotlin developers. Visit here for more details.

Getting Started with Nitrite

NOTE: There are breaking api changes in version 4.x.x. So please exercise caution when upgrading from 3.x.x
especially for package name changes.

How To Install

To use Nitrite in any Java application, first add the nitrite bill of materials, then add required dependencies:

Maven

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.dizitart</groupId>
            <artifactId>nitrite-bom</artifactId>
            <version>4.0.0-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.dizitart</groupId>
        <artifactId>nitrite</artifactId>
    </dependency>

    <dependency>
        <groupId>org.dizitart</groupId>
        <artifactId>nitrite-mvstore-adapter</artifactId>
    </dependency>
</dependencies>

Gradle

implementation(platform("org.dizitart:nitrite-bom:4.0.0-SNAPSHOT"))
    
implementation 'org.dizitart:nitrite'
implementation 'org.dizitart:nitrite-mvstore-adapter'

Quick Examples

Initialize Database

// create a mvstore backed storage module
MVStoreModule storeModule = MVStoreModule.withConfig()
    .filePath("/tmp/test.db")  // for android - .filePath(getFilesDir().getPath() + "/test.db")
    .compress(true)
    .build();

// or a rocksdb based storage module
RocksDBModule storeModule = RocksDBModule.withConfig()
    .filePath("/tmp/test.db")
    .build();


// initialization using builder
Nitrite db = Nitrite.builder()
        .loadModule(storeModule)
        .loadModule(new JacksonMapperModule())  // optional
        .openOrCreate("user", "password");

Create a Collection

// Create a Nitrite Collection
NitriteCollection collection = db.getCollection("test");

// Create an Object Repository
ObjectRepository<Employee> repository = db.getRepository(Employee.class);

Annotations for POJO

@Entity(value = "retired-employee",     // entity name (optional), 
    indices = {
        @Index(value = "firstName", type = IndexType.NonUnique),
        @Index(value = "lastName", type = IndexType.NonUnique),
        @Index(value = "note", type = IndexType.Fulltext),
})
public class Employee implements Serializable {
    // provides id field to uniquely identify an object inside an ObjectRepository
    @Id
    private long empId;
    private Date joinDate;
    private String firstName;
    private String lastName;
    private String note;

    // ... public getters and setters
}

CRUD Operations

// create a document to populate data
Document doc = createDocument("firstName", "John")
     .put("lastName", "Doe")
     .put("birthDay", new Date())
     .put("data", new byte[] {1, 2, 3})
     .put("fruits", new ArrayList<String>() {{ add("apple"); add("orange"); add("banana"); }})
     .put("note", "a quick brown fox jump over the lazy dog");

// insert the document
collection.insert(doc);

// find a document
collection.find(where("firstName").eq("John").and(where("lastName").eq("Doe"));

// update the document
collection.update(where("firstName").eq("John"), createDocument("lastName", "Wick"));

// remove the document
collection.remove(doc);

// insert an object in repository
Employee emp = new Employee();
emp.setEmpId(124589);
emp.setFirstName("John");
emp.setLastName("Doe");

repository.insert(emp);

Create Indices

// create document index
collection.createIndex("firstName", indexOptions(IndexType.NonUnique));
collection.createIndex("note", indexOptions(IndexType.Fulltext));

// create object index. It can also be provided via annotation
repository.createIndex("firstName", indexOptions(IndexType.NonUnique));

Query a Collection

DocumentCursor cursor = collection.find(
    where("firstName").eq("John")               // firstName == John
    .and(
        where("data").elemMatch("$".lt(4))      // AND elements of data array is less than 4
            .and(
                where("note").text("quick")     // AND note field contains string 'quick' using full-text index
        )       
    )
);

for (Document document : cursor) {
    // process the document
}

// get document by id
Document document = collection.getById(nitriteId);

// query an object repository and create the first result
Cursor<Employee> cursor = repository.find(where("firstName").eq("John"));
Employee employee = cursor.firstOrNull();

Transaction

try (Session session = db.createSession()) {
    Transaction transaction = session.beginTransaction();
    try {
        NitriteCollection txCol = transaction.getCollection("test");

        Document document = createDocument("firstName", "John");
        txCol.insert(document);

        transaction.commit();
    } catch (TransactionException e) {
        transaction.rollback();
    }
}


Schema Migration

Migration migration1 = new Migration(Constants.INITIAL_SCHEMA_VERSION, 2) {
    @Override
    public void migrate(Instruction instruction) {
        instruction.forDatabase()
            // make a non-secure db to secure db
            .addPassword("test-user", "test-password");

        // create instruction for existing repository
        instruction.forRepository(OldClass.class, null)

            // rename the repository (in case of entity name changes)
            .renameRepository("migrated", null)

            // change datatype of field empId from String to Long and convert the values
            .changeDataType("empId", (TypeConverter<String, Long>) Long::parseLong)

            // change id field from uuid to empId
            .changeIdField("uuid", "empId")

            // delete uuid field
            .deleteField("uuid")
    
            // rename field from lastName to familyName
            .renameField("lastName", "familyName")

            // add new field fullName and add default value as - firstName + " " + lastName
            .addField("fullName", document -> document.get("firstName", String.class) + " "
                + document.get("familyName", String.class))

            // drop index on firstName
            .dropIndex("firstName")

            // drop index on embedded field literature.text
            .dropIndex("literature.text")

            // change data type of embedded field from float to integer and convert the values 
            .changeDataType("literature.ratings", (TypeConverter<Float, Integer>) Math::round);
    }
};

Migration migration2 = new Migration(2, 3) {
    @Override
    public void migrate(Instruction instruction) {
        instruction.forCollection("test")
            .addField("fullName", "Dummy Name");
    }
};

MVStoreModule storeModule = MVStoreModule.withConfig()
    .filePath("/temp/employee.db")
    .compressHigh(true)
    .build();

db = Nitrite.builder()
    .loadModule(storeModule)
    
    // schema versioning is must for migration
    .schemaVersion(2)

    // add defined migration paths
    .addMigrations(migration1, migration2)
    .openOrCreate();

Automatic Replication

NitriteCollection collection = db.getCollection("products");

Replica replica = Replica.builder()
    .of(collection)
    // replication via websocket (ws/wss)
    .remote("ws://127.0.0.1:9090/datagate/john/products")
    // user authentication via JWT token
    .jwtAuth("john", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c")
    .create();

replica.connect();

Import/Export Data

// Export data to a file
Exporter exporter = Exporter.of(db);
exporter.exportTo(schemaFile);

//Import data from the file
Importer importer = Importer.of(db);
importer.importFrom(schemaFile);

More details are available in the reference document.

Release Notes

Release notes are available here.

Documentation

Reference API

Document

JavaDoc

Build

To build and test Nitrite

git clone https://github.com/nitrite/nitrite-java.git
cd nitrite-java
./gradlew build

Support / Feedback

For issues with, questions about, or feedback talk to us at Gitter.

Bugs / Feature Requests

Think you’ve found a bug? Want to see a new feature in the Nitrite? Please open an issue here. But before you file an issue please check if it is already existing or not.

Maintainers

  • Anindya Chatterjee

Contributors

This project exists thanks to all the people who contribute. Contribute. Contributors

Backers

Thank you to all our backers! 🙏 Become a backer

Backers

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. Become a sponsor

Sponsor Sponsor Sponsor Sponsor Sponsor

Presentation & Talks

Idan Sheinberg has given a talk on Nitrite at Kotlin Everywhere - TLV Edition meetup on October 27, 2019. Please find his presentation here.

Special Thanks

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].