Add support for @Schema and @Migrate annotations to declare storage record schema versions and migrations.
This commit is contained in:
1 parent
1664bb6f28
commit
b10504e9af
8 files changed
+326
-10
No files matched your search
@@ -0,0 +1,26 @@
|
||||
package dev.overlord.zomboidforge.api.annotations;
|
||||
|
||||
import dev.overlord.zomboidforge.api.storage.Row;
|
||||
import dev.overlord.zomboidforge.api.storage.TableDefinition;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* A migration of a storage record, as {@link TableDefinition#migrate} adds one: turns a row stored
|
||||
* with schema version {@link #from} into the record. Declared in the record itself, as a static
|
||||
* method with one {@link Row} parameter that returns the record; it may be private. A record has
|
||||
* at most one migration per version, whether from this annotation or from
|
||||
* {@link TableDefinition#migrate}. See {@link Schema}.
|
||||
*/
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface Migrate
|
||||
{
|
||||
/** The stored schema version this method migrates from; at least 1. */
|
||||
int from();
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package dev.overlord.zomboidforge.api.annotations;
|
||||
|
||||
import dev.overlord.zomboidforge.api.storage.ModStorage;
|
||||
import dev.overlord.zomboidforge.api.storage.TableDefinition;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* The schema version of a storage record, read by {@link ModStorage#table} and
|
||||
* {@link ModStorage#define}; {@link TableDefinition#version} overrides it. Records without it
|
||||
* have version 1. The migrations from older versions are the record's {@link Migrate} methods.
|
||||
*
|
||||
* <pre>{@code
|
||||
* @Schema(version = 2)
|
||||
* record Balance(long coins, @Nullable Instant lastPaid)
|
||||
* {
|
||||
* @Migrate(from = 1)
|
||||
* static Balance fromV1(Row row) {
|
||||
* return new Balance(row.getLong("coins"), null);
|
||||
* }
|
||||
* }
|
||||
* }</pre>
|
||||
*/
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface Schema
|
||||
{
|
||||
/** At least 1. */
|
||||
int version();
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
package dev.overlord.zomboidforge.api.storage;
|
||||
|
||||
import dev.overlord.zomboidforge.api.annotations.Migrate;
|
||||
import dev.overlord.zomboidforge.api.annotations.Schema;
|
||||
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.Blocking;
|
||||
|
||||
@@ -22,11 +25,23 @@ import java.util.function.Supplier;
|
||||
@ApiStatus.NonExtendable
|
||||
public interface ModStorage
|
||||
{
|
||||
/** Opens the table {@code name} with schema version 1 and no migrations. */
|
||||
/**
|
||||
* Opens the table {@code name} with the schema version and migrations that {@code type}
|
||||
* declares with {@link Schema} and {@link Migrate}; without them, version 1 and no migrations.
|
||||
*
|
||||
* @throws IllegalArgumentException when the name, or a {@link Schema} or {@link Migrate} of
|
||||
* {@code type}, is invalid
|
||||
*/
|
||||
@Blocking
|
||||
<R extends Record> Table<R> table(String name, Class<R> type);
|
||||
|
||||
/** A table with a schema version and migrations; see {@link TableDefinition}. */
|
||||
/**
|
||||
* A table to open with a schema version and migrations, starting from those of
|
||||
* {@link Schema} and {@link Migrate} on {@code type}; see {@link TableDefinition}.
|
||||
*
|
||||
* @throws IllegalArgumentException when the name, or a {@link Schema} or {@link Migrate} of
|
||||
* {@code type}, is invalid
|
||||
*/
|
||||
<R extends Record> TableDefinition<R> define(String name, Class<R> type);
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package dev.overlord.zomboidforge.api.storage;
|
||||
|
||||
import dev.overlord.zomboidforge.api.annotations.Migrate;
|
||||
import dev.overlord.zomboidforge.api.annotations.Schema;
|
||||
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.Blocking;
|
||||
import org.jetbrains.annotations.Range;
|
||||
@@ -7,7 +10,8 @@ import org.jetbrains.annotations.Range;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* A table to open with a schema version and migrations:
|
||||
* A table to open with a schema version and migrations. It starts from the record's
|
||||
* {@link Schema} and {@link Migrate} methods, if any; the calls below add to them:
|
||||
*
|
||||
* <pre>{@code
|
||||
* Table<Balance> balances = this.storage().define("balances", Balance.class)
|
||||
@@ -19,13 +23,16 @@ import java.util.function.Function;
|
||||
@ApiStatus.NonExtendable
|
||||
public interface TableDefinition<R extends Record>
|
||||
{
|
||||
/** Schema version of the record, at least 1. Default: 1. */
|
||||
/** Schema version of the record, at least 1. Default: the record's {@link Schema}, or 1. */
|
||||
TableDefinition<R> version(@Range(from = 1, to = Integer.MAX_VALUE) int version);
|
||||
|
||||
/**
|
||||
* How to turn a row stored with schema version {@code from} into the current record. Needed
|
||||
* when the stored version is older than {@link #version} or the record changed in a way that
|
||||
* cannot be applied without data.
|
||||
*
|
||||
* @throws IllegalArgumentException when a migration from {@code from} exists already, also one
|
||||
* from a {@link Migrate} method of the record
|
||||
*/
|
||||
TableDefinition<R> migrate(int from, Function<Row, R> migration);
|
||||
|
||||
|
||||
Reference in new issue
Block a user