-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b7caf2
commit 002977c
Showing
13 changed files
with
570 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.eclipse.store</groupId> | ||
<artifactId>store-parent</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>examples</artifactId> | ||
|
||
<packaging>pom</packaging> | ||
|
||
<modules> | ||
<module>spring-boot3-simple</module> | ||
</modules> | ||
|
||
<properties> | ||
<maven.compiler.source>17</maven.compiler.source> | ||
<maven.compiler.target>17</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.eclipse.store</groupId> | ||
<artifactId>examples</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>spring-boot3-simple</artifactId> | ||
|
||
<properties> | ||
<maven.compiler.source>17</maven.compiler.source> | ||
<maven.compiler.target>17</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<spring.boot.version>3.1.5</spring.boot.version> | ||
</properties> | ||
|
||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-dependencies</artifactId> | ||
<version>${spring.boot.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
<version>${spring.boot.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<version>${spring.boot.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.eclipse.store</groupId> | ||
<artifactId>spring-boot3</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
|
||
|
||
|
||
```shell | ||
|
||
curl --location --request POST 'http://localhost:8080/init' \ | ||
--header 'Content-Type: application/json' | ||
|
||
|
||
curl --location --request GET 'http://localhost:8080/jokes' \ | ||
--header 'Content-Type: application/json' | ||
|
||
``` |
16 changes: 16 additions & 0 deletions
16
...ring-boot3-simple/src/main/java/org/microstream/spring/boot/example/JokesApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.microstream.spring.boot.example; | ||
|
||
import org.eclipse.store.integrations.spring.boot.types.EclipseStoreSpringBoot; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.annotation.Import; | ||
|
||
@SpringBootApplication | ||
@Import(EclipseStoreSpringBoot.class) | ||
public class JokesApplication | ||
{ | ||
public static void main(String... args) | ||
{ | ||
SpringApplication.run(JokesApplication.class, args); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...-simple/src/main/java/org/microstream/spring/boot/example/controller/JokesController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.microstream.spring.boot.example.controller; | ||
|
||
import org.microstream.spring.boot.example.service.JokesServices; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
public class JokesController | ||
{ | ||
|
||
private final JokesServices jokesServices; | ||
|
||
public JokesController(JokesServices jokesServices) | ||
{ | ||
this.jokesServices = jokesServices; | ||
} | ||
|
||
@GetMapping("/jokes") | ||
public List<String> getAll() | ||
{ | ||
return jokesServices.allJokes(); | ||
} | ||
|
||
@GetMapping("/joke") | ||
public String getOneJoke(@RequestParam(required = false) Integer id) | ||
{ | ||
return jokesServices.oneJoke(id); | ||
} | ||
|
||
@PostMapping("/add") | ||
public Integer putOne(String joke) | ||
{ | ||
return jokesServices.addNewJoke(joke); | ||
} | ||
|
||
@PostMapping("/init") | ||
public void init() | ||
{ | ||
jokesServices.loadPredefiniedJokes(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...les/spring-boot3-simple/src/main/java/org/microstream/spring/boot/example/model/Root.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.microstream.spring.boot.example.model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Root | ||
{ | ||
private final List<String> jokes = new ArrayList<>(); | ||
|
||
public List<String> getJokes() | ||
{ | ||
return jokes; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...boot3-simple/src/main/java/org/microstream/spring/boot/example/service/JokesServices.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.microstream.spring.boot.example.service; | ||
|
||
import org.eclipse.store.storage.embedded.types.EmbeddedStorageManager; | ||
import org.microstream.spring.boot.example.model.Root; | ||
import org.microstream.spring.boot.example.storage.JokesStorage; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.util.ResourceUtils; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
@Service | ||
public class JokesServices | ||
{ | ||
private final JokesStorage jokesStorage; | ||
|
||
|
||
public String oneJoke(Integer id) { | ||
String joke; | ||
joke = jokesStorage.oneJoke(Objects.requireNonNullElse(id, 0)); | ||
return joke; | ||
} | ||
|
||
public JokesServices(JokesStorage jokesStorage) | ||
{ | ||
this.jokesStorage = jokesStorage; | ||
} | ||
|
||
public List<String> allJokes() | ||
{ | ||
return jokesStorage.allJokes(); | ||
} | ||
|
||
public Integer addNewJoke(String joke) | ||
{ | ||
return jokesStorage.addNewJoke(joke); | ||
} | ||
|
||
public void loadPredefiniedJokes() { | ||
List<String> jokes = null; | ||
try | ||
{ | ||
File file = ResourceUtils.getFile("classpath:jokes.txt"); | ||
jokes = Files.readAllLines(file.toPath()); | ||
} catch (IOException e) | ||
{ | ||
throw new RuntimeException(e); | ||
} | ||
List<String> existingJokes = jokesStorage.allJokes(); | ||
if (existingJokes.containsAll(jokes)) { | ||
return; | ||
} | ||
jokesStorage.addJokes(jokes); | ||
} | ||
|
||
} |
84 changes: 84 additions & 0 deletions
84
...-boot3-simple/src/main/java/org/microstream/spring/boot/example/storage/JokesStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package org.microstream.spring.boot.example.storage; | ||
|
||
import org.eclipse.store.storage.embedded.types.EmbeddedStorageManager; | ||
import org.microstream.spring.boot.example.model.Root; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
import java.util.List; | ||
|
||
|
||
@Component | ||
public class JokesStorage | ||
{ | ||
private final EmbeddedStorageManager storageManager; | ||
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); | ||
|
||
public JokesStorage(EmbeddedStorageManager storageManager) | ||
{ | ||
this.storageManager = storageManager; | ||
} | ||
|
||
public String oneJoke(Integer id) { | ||
String joke; | ||
lock.readLock().lock(); | ||
try | ||
{ | ||
Root root = (Root) storageManager.root(); | ||
if (id > root.getJokes().size()) { | ||
throw new IllegalArgumentException("No jokes with this id"); | ||
} | ||
joke = root.getJokes().get(id); | ||
|
||
} | ||
finally | ||
{ | ||
lock.readLock().unlock(); | ||
} | ||
return joke; | ||
} | ||
|
||
public List<String> allJokes() | ||
{ | ||
List<String> jokes = null; | ||
lock.readLock().lock(); | ||
try | ||
{ | ||
Root root = (Root) storageManager.root(); | ||
jokes = root.getJokes(); | ||
} finally | ||
{ | ||
lock.readLock().unlock(); | ||
} | ||
return jokes; | ||
} | ||
|
||
public Integer addNewJoke(String joke) | ||
{ | ||
lock.writeLock().lock(); | ||
Root root = (Root) storageManager.root(); | ||
try | ||
{ | ||
root.getJokes().add(joke); | ||
storageManager.storeRoot(); | ||
} finally | ||
{ | ||
lock.writeLock().unlock(); | ||
} | ||
return root.getJokes().size(); | ||
} | ||
|
||
public void addJokes(List<String> jokes) | ||
{ | ||
lock.writeLock().lock(); | ||
Root root = (Root) storageManager.root(); | ||
try | ||
{ | ||
root.getJokes().addAll(jokes); | ||
} finally | ||
{ | ||
lock.writeLock().unlock(); | ||
} | ||
storageManager.store(root.getJokes()); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
examples/spring-boot3-simple/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
org.eclipse.store.auto-start=true | ||
org.eclipse.store.root=org.microstream.spring.boot.example.model.Root | ||
org.eclipse.store.storage-directory=jokes_storage |
Oops, something went wrong.