diff --git a/pom.xml b/pom.xml index f4cfe6f..fb445e0 100644 --- a/pom.xml +++ b/pom.xml @@ -50,7 +50,7 @@ 11 - 2.2.1.RELEASE + 2.2.2.RELEASE Hoxton.RC2 UTF-8 UTF-8 diff --git a/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteInstanz.java b/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteInstanz.java new file mode 100644 index 0000000..76883cc --- /dev/null +++ b/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteInstanz.java @@ -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 + * . + * #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; + } +} diff --git a/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteRepository.java b/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteRepository.java index 2892970..7d94728 100644 --- a/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteRepository.java +++ b/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteRepository.java @@ -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 * . * #L% */ +import org.dizitart.no2.WriteResult; import org.dizitart.no2.objects.ObjectRepository; public interface NitriteRepository extends ObjectRepository { + public WriteResult insert(T et); + } diff --git a/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplate.java b/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplate.java index bb4724d..2f2cc3d 100644 --- a/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplate.java +++ b/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplate.java @@ -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; @@ -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 NitriteRepository getRepository(Class type) { LOG.debug("Erzeuge neues Nitrite Repository für: {}", type.getName()); - final ObjectRepository tmpRepository = this.nitriteInstanz.getRepository(type); + final ObjectRepository tmpRepository = this.nitriteInstanz.getNitrite().getRepository(type); return new NitriteRepository() { protected ObjectRepository objectRepository = tmpRepository; @@ -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) { diff --git a/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplateProvider.java b/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplateProvider.java index aef9c3f..6e06926 100644 --- a/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplateProvider.java +++ b/src/main/java/com/github/funthomas424242/sbstarter/nitrite/NitriteTemplateProvider.java @@ -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 * . * #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); + } }