Skip to content

Commit

Permalink
Merge pull request #3 from FunThomas424242/#2_singletonfix
Browse files Browse the repository at this point in the history
close #2 singletonfix
  • Loading branch information
Huluvu424242 authored Dec 12, 2019
2 parents e3a7984 + ded0f80 commit 3f106fc
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 65 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

<properties>
<java.version>11</java.version>
<spring-boot-version>2.2.1.RELEASE</spring-boot-version>
<spring-boot-version>2.2.2.RELEASE</spring-boot-version>
<spring-cloud.version>Hoxton.RC2</spring-cloud.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.github.funthomas424242.sbstarter.nitrite;

/*-
* #%L
* Nitrite Spring Boot Starter
* %%
* Copyright (C) 2019 PIUG
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
*/

import org.dizitart.no2.Nitrite;
import org.dizitart.no2.NitriteBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PreDestroy;

public class NitriteInstanz {

protected static final Logger LOG = LoggerFactory.getLogger(NitriteInstanz.class);

protected final Nitrite nitrite;

protected NitriteInstanz(NitriteAutoConfiguration nitriteConfig) {

LOG.debug("Starte die Nitrite Datenbank.");
final NitriteBuilder builder = Nitrite.builder();

LOG.debug("[Nitrite Config]\n dbFile: {} \n compressed {} \n disableautocommit {} \n username {} \n password {}\n"
, nitriteConfig.dbfilePath
, nitriteConfig.compressed
, nitriteConfig.disableautocommit
, nitriteConfig.username
, nitriteConfig.password
);


if (nitriteConfig.dbfilePath != null && !nitriteConfig.dbfilePath.isEmpty()) {
LOG.info("[Nitrite] used dbFile: {}", nitriteConfig.dbfilePath);
builder.filePath(nitriteConfig.dbfilePath);
} else {
LOG.info("[Nitrite] used as in-memory database.");
}

if (nitriteConfig.compressed != null && nitriteConfig.compressed) {
LOG.info("[Nitrite] will be compressed automatically.");
builder.compressed();
}

if (nitriteConfig.disableautocommit != null && nitriteConfig.disableautocommit) {
LOG.info("[Nitrite] has autocommit disabled.");
builder.disableAutoCommit();
}

if (nitriteConfig.username == null || nitriteConfig.username.isEmpty() || nitriteConfig.password == null || nitriteConfig.password.isEmpty()) {
LOG.info("[Nitrite] openOrCreate without Credentials.");
this.nitrite = builder.openOrCreate();
} else {
LOG.info("[Nitrite] openOrCreate with Credentials.");
this.nitrite = builder.openOrCreate(nitriteConfig.username, nitriteConfig.password);
}
}

@PreDestroy
protected void destroy() {
LOG.debug("Zerstöre die Nitrite Datenbank.");
/**
* Shutdown Hook
*
* While opening the database, Nitrite registers itself to a JVM shutdown hook, which before exiting will close the
* database without persisting any unsaved changes to the disk. This shutdown hook protects the data file from
* corruption due to JVM shutdown before properly closing the database.
*/
this.nitrite.close();
}

public Nitrite getNitrite() {
return this.nitrite;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,23 @@
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
*/

import org.dizitart.no2.WriteResult;
import org.dizitart.no2.objects.ObjectRepository;

public interface NitriteRepository<T> extends ObjectRepository<T> {

public WriteResult insert(T et);

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.dizitart.no2.Index;
import org.dizitart.no2.IndexOptions;
import org.dizitart.no2.Nitrite;
import org.dizitart.no2.NitriteBuilder;
import org.dizitart.no2.NitriteCollection;
import org.dizitart.no2.NitriteId;
import org.dizitart.no2.RemoveOptions;
Expand All @@ -48,67 +47,15 @@ public class NitriteTemplate {
protected static final Logger LOG = LoggerFactory.getLogger(NitriteTemplate.class);

@Autowired
protected NitriteAutoConfiguration nitriteConfig;


// Das funktioniert hier nur, weil der Scope vom NitriteTemplate auf Singleton gesetzt ist.
// https://www.baeldung.com/spring-bean-scopes
protected Nitrite nitriteInstanz;

protected void init() {
LOG.debug("Starte die Nitrite Datenbank.");
final NitriteBuilder builder = Nitrite.builder();

LOG.debug("[Nitrite Config]\n dbFile: {} \n compressed {} \n disableautocommit {} \n username {} \n password {}\n"
, nitriteConfig.dbfilePath
, nitriteConfig.compressed
, nitriteConfig.disableautocommit
, nitriteConfig.username
, nitriteConfig.password
);


if (nitriteConfig.dbfilePath != null && !nitriteConfig.dbfilePath.isEmpty()) {
LOG.info("[Nitrite] used dbFile: {}", nitriteConfig.dbfilePath);
builder.filePath(nitriteConfig.dbfilePath);
} else {
LOG.info("[Nitrite] used as in-memory database.");
}

if (nitriteConfig.compressed != null && nitriteConfig.compressed) {
LOG.info("[Nitrite] will be compressed automatically.");
builder.compressed();
}

if (nitriteConfig.disableautocommit != null && nitriteConfig.disableautocommit) {
LOG.info("[Nitrite] has autocommit disabled.");
builder.disableAutoCommit();
}

if (nitriteConfig.username == null || nitriteConfig.username.isEmpty() || nitriteConfig.password == null || nitriteConfig.password.isEmpty()) {
LOG.info("[Nitrite] openOrCreate without Credentials.");
this.nitriteInstanz = builder.openOrCreate();
} else {
LOG.info("[Nitrite] openOrCreate with Credentials.");
this.nitriteInstanz = builder.openOrCreate(nitriteConfig.username, nitriteConfig.password);
}
}
protected NitriteInstanz nitriteInstanz;

protected void destroy() {
LOG.debug("Zerstöre die Nitrite Datenbank.");
/**
* Shutdown Hook
*
* While opening the database, Nitrite registers itself to a JVM shutdown hook, which before exiting will close the
* database without persisting any unsaved changes to the disk. This shutdown hook protects the data file from
* corruption due to JVM shutdown before properly closing the database.
*/
this.nitriteInstanz.close();
public Nitrite getNitrite(){
return nitriteInstanz.getNitrite();
}

public <ET> NitriteRepository<ET> getRepository(Class<ET> type) {
LOG.debug("Erzeuge neues Nitrite Repository für: {}", type.getName());
final ObjectRepository<ET> tmpRepository = this.nitriteInstanz.getRepository(type);
final ObjectRepository<ET> tmpRepository = this.nitriteInstanz.getNitrite().getRepository(type);
return new NitriteRepository<ET>() {

protected ObjectRepository<ET> objectRepository = tmpRepository;
Expand All @@ -118,6 +65,10 @@ public WriteResult insert(ET et, ET... ets) {
return objectRepository.insert(et, ets);
}

@Override
public WriteResult insert(ET et) {
return objectRepository.insert(et);
}

@Override
public WriteResult update(ObjectFilter objectFilter, ET et) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,38 @@
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
*/

import org.dizitart.no2.Nitrite;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

@Service
class NitriteTemplateProvider {

@ConditionalOnClass({Nitrite.class})
@ConditionalOnMissingBean
@Bean(initMethod = "init", destroyMethod = "destroy")
@Scope("singleton") // Trotz default, hier soll sichergestellt werden, dass beim upgrade alles so bleibt
@Bean
public NitriteTemplate nitriteTemplate() {
return new NitriteTemplate();
}

@ConditionalOnClass({Nitrite.class})
@ConditionalOnMissingBean
@Bean
public NitriteInstanz nitriteInstanz(final NitriteAutoConfiguration config) {
return new NitriteInstanz(config);
}
}

0 comments on commit 3f106fc

Please sign in to comment.