Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
65 changes: 55 additions & 10 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,22 @@ on:
- "README.md"

jobs:
build:
runs-on: ${{ matrix.os }}
build_linux:
runs-on: ubuntu-latest
strategy:
matrix:
java: ['11', '17']
os: ['ubuntu-latest']
env:
JAVA_OPTS: "-XX:+TieredCompilation -XX:TieredStopAtLevel=1"
MAVEN_USERNAME: ${{ secrets.OSSRH_USER }}
MAVEN_PASSWORD: ${{ secrets.OSSRH_PASSWORD }}
PGP_KEY_ID: ${{ secrets.PGP_KEY_ID }}
PGP_KEY_PASSWORD: ${{ secrets.PGP_KEY_PASSWORD }}
name: Build with Java ${{ matrix.java }} on ${{ matrix.os }}

name: Build with Java ${{ matrix.java }} in Ubuntu
steps:
- uses: actions/checkout@v4
- name: Set up JDK ${{ matrix.java }} on ${{ matrix.os }}
- uses: actions/checkout@v3
- name: Set up JDK ${{ matrix.java }}
uses: actions/setup-java@v3
with:
java-version: ${{ matrix.java }}
Expand All @@ -55,7 +54,7 @@ jobs:
run: mvn -B -ff -ntp clean install

- name: Deploy Snapshot
if: github.ref == 'refs/heads/develop'
if: github.ref == 'refs/heads/develop' && matrix.java == '11'
run: mvn -B -ff -ntp deploy -DskipTests
env:
MAVEN_USERNAME: ${{ secrets.OSSRH_USER }}
Expand All @@ -64,7 +63,7 @@ jobs:
PGP_KEY_PASSWORD: ${{ secrets.PGP_KEY_PASSWORD }}

- name: Deploy Release
if: github.ref == 'refs/heads/release'
if: github.ref == 'refs/heads/release' && matrix.java == '11'
run: mvn -B -ff -ntp release:clean release:prepare release:perform -DskipTests
env:
MAVEN_USERNAME: ${{ secrets.OSSRH_USER }}
Expand All @@ -73,9 +72,55 @@ jobs:
PGP_KEY_PASSWORD: ${{ secrets.PGP_KEY_PASSWORD }}

- name: Publish Code Coverage
if: github.ref == 'refs/heads/main'
if: github.ref == 'refs/heads/main' && matrix.java == '11'
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
flags: unittests
name: codecov-umbrella
name: codecov-umbrella

build_macos:
runs-on: macos-latest
strategy:
matrix:
java: [ '11', '17' ]
env:
JAVA_OPTS: "-XX:+TieredCompilation -XX:TieredStopAtLevel=1"

name: Build with Java ${{ matrix.java }} in MacOS
steps:
- uses: actions/checkout@v3
- name: Set up JDK ${{ matrix.java }}
uses: actions/setup-java@v3
with:
java-version: ${{ matrix.java }}
distribution: 'zulu'
java-package: jdk
architecture: x64
cache: 'maven'

- name: Build with Maven
run: mvn -B -ff -ntp clean install

build_windows:
runs-on: windows-latest
strategy:
matrix:
java: [ '11', '17' ]
env:
JAVA_OPTS: "-XX:+TieredCompilation -XX:TieredStopAtLevel=1"

name: Build with Java ${{ matrix.java }} in Windows
steps:
- uses: actions/checkout@v3
- name: Set up JDK ${{ matrix.java }}
uses: actions/setup-java@v3
with:
java-version: ${{ matrix.java }}
distribution: 'zulu'
java-package: jdk
architecture: x64
cache: 'maven'

- name: Build with Maven
run: mvn -B -ff -ntp clean install
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
[![Discussion](https://img.shields.io/badge/chat-Discussion-blueviolet)](https://github.com/nitrite/nitrite-java/discussions)

<p align="center">
<img src="assets/nitrite-logo.svg" width="256" alt="nitrite logo">
<img src="assets/nitrite-logo.svg" width="256" alt="nitrite logo">
</p>

**NO**sql **O**bject (**NO<sub>2</sub>** a.k.a Nitrite) database is an open source nosql embedded
document store written in Java. It supports both in-memory and file based persistent store.
document store. It supports both in-memory and file based persistent store.

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

Expand All @@ -37,6 +37,10 @@ Visit [here](https://github.com/nitrite/nitrite-java/tree/main/potassium-nitrite

If you are looking for Nitrite for Flutter/Dart, head over to [nitrite-flutter](https://github.com/nitrite/nitrite-flutter).

## Deprecation Notice

Nitrite DataGate and Nitrite Explorer is now deprecated and no longer maintained.

## Getting Started with Nitrite

**NOTE:** There are breaking api changes in version 4.x. So please exercise caution when upgrading from 3.x.x
Expand Down Expand Up @@ -87,6 +91,9 @@ implementation 'org.dizitart:nitrite-mvstore-adapter'

```

## Examples

A Todo android application is available [here](https://github.com/nitrite/nitrite-android-demo) to demonstrate the usage of Nitrite in android.

### Quick Examples

Expand All @@ -95,7 +102,7 @@ implementation 'org.dizitart:nitrite-mvstore-adapter'
```java
// create a mvstore backed storage module
MVStoreModule storeModule = MVStoreModule.withConfig()
.filePath("/tmp/test.db") // for android - .filePath(getFilesDir().getPath() + "/test.db")
.filePath("/tmp/test.db")
.compress(true)
.build();

Expand Down
5 changes: 5 additions & 0 deletions nitrite-jackson-mapper/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<excludePackageNames>
org.dizitart.no2.common.mapper.modules,
</excludePackageNames>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,41 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.*;
import lombok.extern.slf4j.Slf4j;
import org.dizitart.no2.NitriteConfig;
import org.dizitart.no2.collection.Document;
import org.dizitart.no2.common.mapper.modules.NitriteIdModule;
import org.dizitart.no2.exceptions.ObjectMappingException;
import org.dizitart.no2.exceptions.ValidationException;

import java.io.IOException;
import java.util.*;

import static org.dizitart.no2.common.util.ValidationUtils.notNull;

/**
* A {@link NitriteMapper} implementation that uses Jackson ObjectMapper to
* convert objects to and from Nitrite document.
*
* @since 4.0
* @see NitriteMapper
* @author Anindya Chatterjee
*/
@Slf4j
public class JacksonMapper implements NitriteMapper {
private ObjectMapper objectMapper;

/**
* Returns the object mapper instance used for document conversion.
*
* @return the object mapper instance used for document conversion
*/
protected ObjectMapper getObjectMapper() {
if (objectMapper == null) {
objectMapper = new ObjectMapper();
objectMapper.setVisibility(
objectMapper.getSerializationConfig().getDefaultVisibilityChecker()
.withFieldVisibility(JsonAutoDetect.Visibility.ANY)
.withGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withIsGetterVisibility(JsonAutoDetect.Visibility.NONE));
objectMapper.getSerializationConfig().getDefaultVisibilityChecker()
.withFieldVisibility(JsonAutoDetect.Visibility.ANY)
.withGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withIsGetterVisibility(JsonAutoDetect.Visibility.NONE));
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
Expand All @@ -55,11 +64,34 @@ protected ObjectMapper getObjectMapper() {
return objectMapper;
}

/**
* Registers a Jackson module with the object mapper.
*
* @param module the Jackson module to register
* @throws ValidationException if the module is null
*/
public void registerJacksonModule(Module module) {
notNull(module, "module cannot be null");
getObjectMapper().registerModule(module);
}

/**
* Tries to convert the given source object to the target type using Jackson
* ObjectMapper.
* <p>
* If the source object is null, returns null. If the source
* object is a value node, returns the node value. If the target type is
* Document, converts the source object to a Document. If the source object is
* already a Document, converts it to the target type. If the conversion fails,
* throws an ObjectMappingException.
*
* @param source the source object to convert
* @param type the target type to convert to
* @param <Source> the type of the source object
* @param <Target> the type of the target object
* @return the converted object of the target type
* @throws ObjectMappingException if the conversion fails
*/
@Override
public <Source, Target> Object tryConvert(Source source, Class<Target> type) {
if (source == null) {
Expand All @@ -68,7 +100,8 @@ public <Source, Target> Object tryConvert(Source source, Class<Target> type) {

try {
JsonNode node = getObjectMapper().convertValue(source, JsonNode.class);
if (node == null) return null;
if (node == null)
return null;

if (node.isValueNode()) {
return getNodeValue(node);
Expand All @@ -81,18 +114,28 @@ public <Source, Target> Object tryConvert(Source source, Class<Target> type) {
}
} catch (Exception e) {
throw new ObjectMappingException("Failed to convert object of type "
+ source.getClass() + " to type " + type, e);
+ source.getClass() + " to type " + type, e);
}

throw new ObjectMappingException("Can't convert object to type " + type
+ ", try registering a jackson Module for it.");
+ ", try registering a jackson Module for it.");
}


@Override
public void initialize(NitriteConfig nitriteConfig) {
}

/**
* Converts a Nitrite Document to an object of the specified class type using
* Jackson ObjectMapper.
*
* @param source the Nitrite Document to be converted
* @param type the class type of the object to be converted to
* @param <Target> the type of the object to be converted to
* @return the converted object of the specified class type
* @throws ObjectMappingException if there is an error in the object mapping
* process
*/
protected <Target> Target convertFromDocument(Document source, Class<Target> type) {
try {
return getObjectMapper().convertValue(source, type);
Expand All @@ -107,6 +150,14 @@ protected <Target> Target convertFromDocument(Document source, Class<Target> typ
}
}

/**
* Converts the given source object to a Nitrite {@link Document} using
* Jackson's {@link ObjectMapper}.
*
* @param source the source object to convert
* @param <Source> the type of the source object
* @return the converted Nitrite {@link Document}
*/
protected <Source> Document convertToDocument(Source source) {
JsonNode node = getObjectMapper().convertValue(source, JsonNode.class);
return readDocument(node);
Expand Down Expand Up @@ -168,7 +219,7 @@ private Object readObject(JsonNode node) {
return null;
}

@SuppressWarnings({"unchecked", "rawtypes"})
@SuppressWarnings({ "unchecked", "rawtypes" })
private List readArray(JsonNode array) {
if (array.isArray()) {
List list = new ArrayList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,30 @@
import static org.dizitart.no2.common.util.Iterables.setOf;

/**
* A Nitrite module that provides a jackson based {@link NitriteMapper}
* implementation for object to document conversion.
*
* @since 4.0
* @see NitriteMapper
* @see JacksonMapper
* @author Anindya Chatterjee
*/
public class JacksonMapperModule implements NitriteModule {
private final JacksonMapper jacksonMapper;

/**
* Instantiates a new {@link JacksonMapperModule}.
*/
public JacksonMapperModule() {
jacksonMapper = new JacksonMapper();
}

/**
* Instantiates a new {@link JacksonMapperModule} with custom
* jackson modules.
*
* @param jacksonModules the jackson modules
*/
public JacksonMapperModule(Module... jacksonModules) {
jacksonMapper = new JacksonMapper();
for (Module jacksonModule : jacksonModules) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.IOException;

/**
* @since 4.0
* @author Anindya Chatterjee
*/
class NitriteIdDeserializer extends StdScalarDeserializer<NitriteId> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.IOException;

/**
* @since 4.0
* @author Anindya Chatterjee
*/
class NitriteIdSerializer extends StdScalarSerializer<NitriteId> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,18 @@
import org.dizitart.no2.common.mapper.JacksonMapperModule;
import org.dizitart.no2.exceptions.MigrationException;
import org.dizitart.no2.index.IndexType;
import org.dizitart.no2.integration.repository.Retry;
import org.dizitart.no2.integration.repository.TestUtil;
import org.dizitart.no2.migration.InstructionSet;
import org.dizitart.no2.migration.Migration;
import org.dizitart.no2.migration.TypeConverter;
import org.dizitart.no2.mvstore.MVStoreModule;
import org.dizitart.no2.repository.ObjectRepository;
import org.dizitart.no2.integration.repository.Retry;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.UUID;

import static org.dizitart.no2.filters.FluentFilter.where;
Expand All @@ -66,12 +64,12 @@ public void setUp() {
}

@After
public void cleanUp() throws IOException {
public void cleanUp() {
if (!db.isClosed()) {
db.close();
}

Files.delete(Paths.get(dbPath));
TestUtil.deleteDb(dbPath);
}

@Test
Expand Down
Loading