feat: jpa, messaging, notification, mongo, graphql 어댑터터 구현체 추가
This commit is contained in:
+42
@@ -0,0 +1,42 @@
|
||||
package dev.caskeleton.shared.opstore;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* One durable operational record: an opaque value under a namespaced key, with a version.
|
||||
*
|
||||
* <p>Text rather than bytes, so the record has value equality and no defensive copying: a record
|
||||
* holding a {@code byte[]} compares by array identity, which makes two equal records unequal and
|
||||
* every {@code contains} check wrong. The namespace owner decides the encoding.
|
||||
*
|
||||
* <p>Opaque on purpose. The store must not know what a GraphQL persisted operation, a feature flag
|
||||
* or a schema pin looks like, because the moment it does, every adapter that wants durable
|
||||
* operational state has to negotiate its shape with a module that has nothing to do with it.
|
||||
*
|
||||
* <p>The version is the whole reason this is a record rather than a map entry. Operational state is
|
||||
* written by admin planes and by more than one instance at once; without a compare-and-set token,
|
||||
* "block this operation" and "approve this operation" issued a second apart resolve by arrival
|
||||
* order rather than by intent, and the loser leaves no trace.
|
||||
*
|
||||
* @param namespace bounded owner of the key space, e.g. the capability that owns these records
|
||||
* @param key identity within the namespace
|
||||
* @param value the opaque payload, encoded by whoever owns the namespace
|
||||
* @param version the value's version; {@link #ABSENT_VERSION} means the key holds nothing yet
|
||||
*/
|
||||
public record OperationalRecord(String namespace, String key, String value, long version) {
|
||||
|
||||
/** The version a key holds before anything has been written to it. */
|
||||
public static final long ABSENT_VERSION = 0L;
|
||||
|
||||
public OperationalRecord {
|
||||
Objects.requireNonNull(namespace, "namespace is required");
|
||||
Objects.requireNonNull(key, "key is required");
|
||||
Objects.requireNonNull(value, "value is required");
|
||||
if (namespace.isBlank() || key.isBlank()) {
|
||||
throw new IllegalArgumentException("operational record namespace and key are required");
|
||||
}
|
||||
if (version < ABSENT_VERSION) {
|
||||
throw new IllegalArgumentException("operational record version cannot be negative");
|
||||
}
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package dev.caskeleton.shared.opstore;
|
||||
|
||||
/**
|
||||
* A compare-and-set lost: the stored version was not the one the caller read.
|
||||
*
|
||||
* <p>An exception rather than a boolean, because the caller who ignores a returned {@code false} is
|
||||
* exactly the caller whose admin action silently did not happen.
|
||||
*/
|
||||
public final class OperationalRecordConflictException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** The stable code operators and clients match on. */
|
||||
public static final String CODE = "OPERATIONAL_RECORD_CONFLICT";
|
||||
|
||||
/**
|
||||
* Creates the exception.
|
||||
*
|
||||
* @param namespace bounded owner of the key space
|
||||
* @param key identity within the namespace
|
||||
* @param expectedVersion the version the caller read
|
||||
* @param actualVersion the version the store holds
|
||||
*/
|
||||
public OperationalRecordConflictException(
|
||||
String namespace, String key, long expectedVersion, long actualVersion) {
|
||||
super(
|
||||
CODE
|
||||
+ ": "
|
||||
+ namespace
|
||||
+ "/"
|
||||
+ key
|
||||
+ " expected version "
|
||||
+ expectedVersion
|
||||
+ " but found "
|
||||
+ actualVersion);
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package dev.caskeleton.shared.opstore;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Provider-neutral durable storage for operational records.
|
||||
*
|
||||
* <p>Owned here rather than by whichever adapter happens to need it first. An adapter that declares
|
||||
* its own storage interface makes every durable implementation depend on that adapter: a Postgres
|
||||
* or Redis outbound adapter implementing an inbound transport's interface points the dependency
|
||||
* from infrastructure back at a transport boundary, which is the direction this repository's
|
||||
* architecture exists to prevent. A neutral contract lets both sides depend on the contract and on
|
||||
* nothing else.
|
||||
*
|
||||
* <p>Writes are compare-and-set because operational state has more than one writer. The expected
|
||||
* version is the caller's evidence about what they read; a store that accepted a blind write would
|
||||
* make a lost admin action indistinguishable from an applied one.
|
||||
*/
|
||||
public interface OperationalRecordStorePort {
|
||||
|
||||
/**
|
||||
* Reads a record.
|
||||
*
|
||||
* @param namespace bounded owner of the key space
|
||||
* @param key identity within the namespace
|
||||
*/
|
||||
Optional<OperationalRecord> find(String namespace, String key);
|
||||
|
||||
/**
|
||||
* Writes a record if its stored version is still the expected one.
|
||||
*
|
||||
* @param record the record to write, carrying the value to store
|
||||
* @param expectedVersion the version the caller read, or {@link OperationalRecord#ABSENT_VERSION}
|
||||
* to require that the key holds nothing
|
||||
* @return the stored record with its new version
|
||||
* @throws OperationalRecordConflictException when the stored version differs
|
||||
*/
|
||||
OperationalRecord compareAndSet(OperationalRecord record, long expectedVersion);
|
||||
|
||||
/**
|
||||
* Removes a record if its stored version is still the expected one.
|
||||
*
|
||||
* @param namespace bounded owner of the key space
|
||||
* @param key identity within the namespace
|
||||
* @param expectedVersion the version the caller read
|
||||
* @throws OperationalRecordConflictException when the stored version differs
|
||||
*/
|
||||
void compareAndRemove(String namespace, String key, long expectedVersion);
|
||||
}
|
||||
Reference in New Issue
Block a user