Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #61 from eo-cqrs/55
Browse files Browse the repository at this point in the history
CMIG default dir, master `0.0.1`
  • Loading branch information
Aliaksei Bialiauski authored Aug 22, 2023
2 parents 4325ed7 + 5946f9c commit c61ca7d
Show file tree
Hide file tree
Showing 19 changed files with 371 additions and 105 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ SOFTWARE.
<limit>
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>0.65</minimum>
<minimum>0.59</minimum>
</limit>
<limit>
<counter>LINE</counter>
Expand Down
82 changes: 56 additions & 26 deletions src/main/java/io/github/eocqrs/cmig/Master.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,21 @@

import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import io.github.eocqrs.cmig.meta.Author;
import io.github.eocqrs.cmig.meta.Ids;
import io.github.eocqrs.cmig.meta.Names;
import io.github.eocqrs.cmig.session.Cassandra;
import io.github.eocqrs.cmig.session.InFile;
import io.github.eocqrs.cmig.session.InText;
import io.github.eocqrs.cmig.sha.Sha;
import org.cactoos.Scalar;
import org.cactoos.io.ResourceOf;
import org.cactoos.list.ListOf;
import org.cactoos.text.TextOf;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
/*
* @todo #32:45m/DEV Master is set of states inside XML document.
*/
Expand All @@ -45,7 +55,7 @@ public final class Master implements Scalar<String> {
/**
* XML.
*/
private final XML xml;
private final XML self;
/**
* Cassandra.
*/
Expand All @@ -54,55 +64,75 @@ public final class Master implements Scalar<String> {
/**
* Ctor.
*
* @param doc XML
* @param doc XML document
* @param cs Cassandra
*/
public Master(
final XML doc,
final Cassandra cs
) {
this.xml = doc;
this.self = doc;
this.cassandra = cs;
}

/**
* Ctor.
*
* @param nm File name
* @param cs Cassandra
* @param name XML master name
* @param cs Cassandra
* @throws Exception if something went wrong
*/
public Master(
final String nm,
final String name,
final Cassandra cs
) throws Exception {
this(
new XMLDocument(
new ResourceOf(
"cmig/%s"
.formatted(nm)
).stream()
new ResourceOf(name).stream()
),
cs
);
}

/*
* @todo #48:90m/DEV Decompose Master #value().
*/
@Override
public String value() throws Exception {
new Names(this.xml, "").value()
.forEach(file -> {
try {
new InFile(
this.cassandra, "cmig/%s".formatted(file)
).apply();
} catch (final Exception ex) {
throw new IllegalStateException(
"Schema migration cannot be executed",
ex
);
}
}
);
return "c8be525311cfd5f5ac7bf1c7d41a61fd82ae5e384b9b7b490358c1cb038c46c9";
public String value() {
final List<String> shas = new ListOf<>();
new Ids(this.self)
.value()
.forEach(id -> {
final String author = new Author(this.self, id)
.asString();
final String sha = new Sha(
id, this.self
).asString();
new InText(
new TextOf(
// @todo #36:60m/DEV Implement PreparedCql to avoid cql injection.
"INSERT INTO cmig.states(id, author, sha, seen) "
+ "VALUES (%s, '%s', '%s', '%s');"
.formatted(
id,
author,
sha,
LocalDateTime.now().toInstant(
ZoneOffset.UTC
).toEpochMilli()
)
),
this.cassandra
).apply();
shas.add(sha);
new Names(this.self, id)
.value()
.forEach(
name -> new InFile(
this.cassandra, "cmig/%s".formatted(name)
).apply()
);
});
return shas.get(shas.size() - 1);
}
}
83 changes: 83 additions & 0 deletions src/main/java/io/github/eocqrs/cmig/Preload.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2023 Aliaksei Bialiauski, EO-CQRS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.github.eocqrs.cmig;

import io.github.eocqrs.cmig.check.CmigKeyspace;
import io.github.eocqrs.cmig.check.StatesTable;
import io.github.eocqrs.cmig.session.Cassandra;
import io.github.eocqrs.cmig.session.InText;
import org.cactoos.Scalar;

/**
* Preload decorator for {@link Master}.
*
* @author Aliaksei Bialiauski ([email protected])
* @since 0.0.0
*/
public final class Preload implements Scalar<String> {

/**
* Scalar of Master.
*/
private final Scalar<String> master;

/**
* Cassandra.
*/
private final Cassandra cassandra;

/**
* Datacenter.
*/
private final String datacenter;

/**
* Ctor.
*
* @param mstr Scalar of Master
* @param cs Cassandra
* @param dc Datacenter
*/
public Preload(
final Scalar<String> mstr,
final Cassandra cs,
final String dc
) {
this.master = mstr;
this.cassandra = cs;
this.datacenter = dc;
}

@Override
public String value() throws Exception {
new InText(
new CmigKeyspace(this.datacenter),
this.cassandra
).apply();
new InText(
new StatesTable(),
this.cassandra
).apply();
return this.master.value();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public CmigKeyspace(final String datacenter) {

@Override
public String asString() throws Exception {
return ("CREATE KEYSPACE cmig\n"
return ("CREATE KEYSPACE IF NOT EXISTS cmig\n"
+ "WITH REPLICATION = {\n"
+ "'class': 'NetworkTopologyStrategy',\n"
+ "'datacenter1': %s\n"
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/github/eocqrs/cmig/check/StatesTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public final class StatesTable implements Text {

@Override
public String asString() throws Exception {
return "CREATE TABLE cmig.states\n"
return "CREATE TABLE IF NOT EXISTS cmig.states\n"
+ "(\n"
+ "id INT PRIMARY KEY,\n"
+ "author TEXT,\n"
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/io/github/eocqrs/cmig/session/InText.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.eocqrs.cmig.session;

import lombok.SneakyThrows;
import org.cactoos.Text;

/**
Expand Down Expand Up @@ -34,8 +35,9 @@ public InText(
this.cassandra = cssndr;
}

@SneakyThrows
@Override
public void apply() throws Exception {
public void apply() {
this.cassandra.value().execute(
this.text.asString()
);
Expand Down
11 changes: 1 addition & 10 deletions src/main/java/io/github/eocqrs/cmig/sha/ContentsOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@
*/
public final class ContentsOf implements Text {

/**
* CMIG directory.
*/
private final String cmig;

/**
* File name.
*/
Expand All @@ -48,14 +43,11 @@ public final class ContentsOf implements Text {
/**
* Ctor.
*
* @param cmg CMIG directory
* @param name File name
*/
public ContentsOf(
final String cmg,
final String name
) {
this.cmig = cmg;
this.file = name;
}

Expand All @@ -64,9 +56,8 @@ public ContentsOf(
public String asString() {
return new UnixizedOf(
new ResourceOf(
"%s/%s"
"cmig/%s"
.formatted(
this.cmig,
this.file
)
)
Expand Down
23 changes: 8 additions & 15 deletions src/main/java/io/github/eocqrs/cmig/sha/Sha.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package io.github.eocqrs.cmig.sha;

import com.jcabi.xml.XML;
import io.github.eocqrs.cmig.meta.Names;
import lombok.SneakyThrows;
import org.cactoos.Text;
Expand All @@ -46,29 +47,22 @@ public final class Sha implements Text {
private final String id;

/**
* Master file.
* Master XML.
*/
private final String master;

/**
* CMIG directory.
*/
private final String cmig;
private final XML master;

/**
* Ctor.
* @param id State ID
* @param mst Master file
* @param cmg CMIG directory
*
* @param id State ID
* @param mst Master XML
*/
public Sha(
final String id,
final String mst,
final String cmg
final XML mst
) {
this.id = id;
this.master = mst;
this.cmig = cmg;
}

@SneakyThrows
Expand All @@ -78,8 +72,7 @@ public String asString() {
new Names(
this.master,
this.id
),
this.cmig
)
).value();
return new HexOf(
new Sha256DigestOf(
Expand Down
11 changes: 2 additions & 9 deletions src/main/java/io/github/eocqrs/cmig/sha/StateChanges.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,13 @@ public final class StateChanges implements Scalar<List<String>> {
*/
private final XpathList xpath;

/**
* CMIG directory.
*/
private final String cmig;

/**
* Ctor.
*
* @param lst XPATH list
* @param cmg CMIG directory
*/
public StateChanges(final XpathList lst, final String cmg) {
public StateChanges(final XpathList lst) {
this.xpath = lst;
this.cmig = cmg;
}

@Override
Expand All @@ -62,7 +55,7 @@ public List<String> value() throws Exception {
.stream()
.map(
name ->
new ContentsOf(this.cmig, name).asString()
new ContentsOf(name).asString()
).toList();
}
}
Loading

4 comments on commit c61ca7d

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on c61ca7d Aug 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 36-b04b7b08 disappeared from src/test/java/io/github/eocqrs/cmig/MasterTest.java), that's why I closed #47. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on c61ca7d Aug 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 36-fd704288 disappeared from src/test/java/io/github/eocqrs/cmig/MasterTest.java), that's why I closed #48. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on c61ca7d Aug 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 48-dfb2ad52 discovered in src/main/java/io/github/eocqrs/cmig/Master.java) and submitted as #64. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on c61ca7d Aug 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 36-9288a325 discovered in src/main/java/io/github/eocqrs/cmig/Master.java) and submitted as #65. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.