31 Mayıs 2023 Çarşamba

MicroStream - Persistence Framework

Maven
Şu satırı dahil ederiz
<dependency>
   <groupId>expert.os.integration</groupId>
   <artifactId>microstream-jakarta-data</artifactId>
   <version>${microstream.data.version}</version>
</dependency>
<dependency>
    <groupId>one.microstream</groupId>
    <artifactId>microstream-afs-sql</artifactId>
    <version>${microstream.version}</version>
</dependency>
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.2.14</version>
</dependency>
Örnek
Şöyle yaparız
@ApplicationScoped
class DataSourceSupplier implements Supplier<DataSource> {
  private static final String JDBC = "microstream.postgresql.jdbc";
  private static final String USER = "microstream.postgresql.user";
  private static final String PASSWORD = "microstream.postgresql.password";

  @Override
  @Produces
  @ApplicationScoped
  public DataSource get() {
    Config config = ConfigProvider.getConfig();
    PGSimpleDataSource dataSource = new PGSimpleDataSource();
    dataSource.setUrl(config.getValue(JDBC, String.class));
    dataSource.setUser(config.getValue(USER, String.class));
    dataSource.setPassword(config.getValue(PASSWORD, String.class));
    return dataSource;
  }
}

@Alternative
@Priority(Interceptor.Priority.APPLICATION)
@ApplicationScoped
class SQLSupplier implements Supplier<StorageManager> {

  @Inject
  private DataSource dataSource;

  @Override
  @Produces
  @ApplicationScoped
  public StorageManager get() {
    SqlFileSystem fileSystem = SqlFileSystem.New(
      SqlConnector.Caching(
        SqlProviderPostgres.New(dataSource)
      )
    );
   return EmbeddedStorage.start(fileSystem.ensureDirectoryPath("microstream_storage"));
  }

  public void close(@Disposes StorageManager manager) {
    manager.close();
  }
}
Kullanmak için şöyle yaparız
@Repository
public interface Airport extends CrudRepository<Airplane, String> {
    List<Airplane> findByModel(String model);
}

@Entity
public class Airplane {
    @Id
    private String id;
    @Column("title")
    private String model;
    @Column("year")
    private Year year;

    @Column
    private String manufacturer;
}

try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
    Airplane airplane = ...;
    Airplane airplane2 = ...;
    Airplane airplane3 = ...;
    Airplane airplane4 = ...;
    Airplane airplane5 = ...;
    Airport airport = container.select(Airport.class).get();
    airport.saveAll(List.of(airplane, airplane2, airplane3, airplane4, airplane5));
    var boings = airport.findByModel(airplane.getModel());
    var all = airport.findAll().toList();
    System.out.println("The boings: " + boings);
    System.out.println("The boing models avialables: " + boings.size());
    System.out.println("The airport total: " + all.size());
}




Object Model API For JSON

Giriş
Yeni paket ismi jakarta.json. Açıklaması şöyle
The javax.json package provides an Object Model API to process JSON. The Object Model API is a high-level API that provides immutable object models for JSON object and array structures. 
These JSON structures can be represented as object models using JsonObject and JsonArray interfaces.
Açıklaması şöyle
The Javax.json package is a collection of all the utilities that are available for use in Java environments to process JSON. It includes facilities such as getting immutable objects or event streams by parsing input streams, feeding output streams with these immutable objects or event streams, building immutable objects using builders, and navigating immutable objects.
Maven
Şu satırı dahil ederiz
<dependency>
  <groupId>org.glassfish</groupId>
  <artifactId>javax.json</artifactId>
  <version>1.1.2</version>
</dependency>
JsonGenerator Arayüzü
Açıklaması şöyle
We can use the JsonGenerator interface to write the JSON data to an output in a streaming way. 
JsonReader Arayüzü
read metodu
Örnek
Şöyle yaparız
// Read json and create get the structure from it
JsonReader reader = Json.createReader(new FileReader("src/main/resources/profile.json"));
JsonStructure jsonStructure = reader.read();
JsonPointer Arayüzü
Bir anlamda XPATH gibidir.

add metodu
Örnek
Şöyle yaparız
JsonStructure jsonStructure = ...

// add a new value
JsonPointer agePointer = Json.createPointer("/age");
JsonNumber age = Json.createValue(30);
jsonStructure = agePointer.add(jsonStructure, age);
System.out.println(jsonStructure);

JsonPointer skillsPointer = Json.createPointer("/skills/-");
JsonString skill = Json.createValue("JsonPointer");
jsonStructure = skillsPointer.add(jsonStructure, skill);
System.out.println(jsonStructure);
containsValue metodu
Örnek
Şöyle yaparız
JsonStructure jsonStructure = ...

// checking a key exists
JsonPointer fakeKeyPointer = Json.createPointer("/fake");
boolean found = fakeKeyPointer.containsValue(jsonStructure);
System.out.println(found);

JsonPointer outOfBounds = Json.createPointer("/projects/3");
boolean foundOOB = outOfBounds.containsValue(jsonStructure);
System.out.println(foundOOB);
getValue metodu
Örnek
Şöyle yaparız
JsonStructure jsonStructure = ...

// getting a value from path

// using just object keys
JsonPointer cityPointer = Json.createPointer("/address/city"); 
JsonString city = (JsonString) cityPointer.getValue(jsonStructure);
System.out.println(city.getString());

// using just array indexes
JsonPointer projectTwoPointer = Json.createPointer("/projects/1"); 
JsonObject project = (JsonObject) projectTwoPointer.getValue(jsonStructure);
System.out.println(project.toString());
remove metodu
Örnek
Şöyle yaparız
JsonStructure jsonStructure = ...

// remove a field
JsonPointer addressPointer = Json.createPointer("/address");
jsonStructure = addressPointer.remove(jsonStructure);
System.out.println(jsonStructure);
replace metodu
Örnek
Şöyle yaparız
JsonStructure jsonStructure = ...

// change a value
JsonPointer namePointer = Json.createPointer("/name");
JsonString name = Json.createValue("Bill Nye");
jsonStructure = namePointer.replace(jsonStructure, name);
System.out.println(jsonStructure);

Bean Validation @GroupSequence Anotasyonu

Örnek Elimizde şöyle bir kod olsun public class SampleRequest {   @NotNull   LocalDate startDate;   @NotNull   LocalDate endDate;   @AssertT...