Skip to content

Commit

Permalink
support for generator/sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille committed Jan 17, 2024
1 parent 24f746f commit cae3c28
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 63 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0 */
package io.github.mmm.entity.id;
package io.github.mmm.entity.id.generator;

import io.github.mmm.entity.id.Id;

/**
* Interface for a generator of {@link Id}s that are unique (for a type of {@link io.github.mmm.entity.Entity}).
Expand All @@ -9,7 +11,8 @@ public interface IdGenerator {

/**
* @param <E> type of the {@link io.github.mmm.entity.Entity}.
* @param template the {@link Id#isTransient() transient} {@link Id} that may be used as template.
* @param template the {@link Id#isTransient() transient} {@link Id} of the {@link io.github.mmm.entity.Entity entity}
* to insert.
* @return the new unique {@link Id}.
*/
<E> Id<E> generate(Id<E> template);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0 */
package io.github.mmm.entity.id;
package io.github.mmm.entity.id.generator;

import java.time.Instant;
import java.util.function.Function;

import io.github.mmm.entity.id.AbstractVersionId;
import io.github.mmm.entity.id.Id;
import io.github.mmm.entity.id.LongId;
import io.github.mmm.entity.id.LongInstantId;
import io.github.mmm.entity.id.LongVersionId;
import io.github.mmm.entity.id.sequence.IdSequence;

/**
* {@link IdGenerator} for {@link LongId}.
*/
public class LongIdGenerator implements IdGenerator {

private final Function<Id<?>, Long> sequence;
private final IdSequence sequence;

/**
* The constructor.
*
* @param sequence the {@link Function} doing the actual job. Typically via a sequence.
* @param sequence the {@link IdSequence}.
*/
public LongIdGenerator(Function<Id<?>, Long> sequence) {
public LongIdGenerator(IdSequence sequence) {

super();
this.sequence = sequence;
Expand All @@ -27,7 +33,7 @@ public LongIdGenerator(Function<Id<?>, Long> sequence) {
public <E> LongId<E, ?> generate(Id<E> template) {

Class<E> entityType = template.getEntityClass();
Long pk = this.sequence.apply(template);
Long pk = Long.valueOf(this.sequence.next(template));
Class<? extends Comparable<?>> revisionType = template.getRevisionType();
if (revisionType == Long.class) {
return new LongVersionId<>(entityType, pk, AbstractVersionId.INSERT_REVISION);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0 */
package io.github.mmm.entity.id;
package io.github.mmm.entity.id.generator;

import java.time.Instant;
import java.util.UUID;

import io.github.mmm.entity.id.AbstractVersionId;
import io.github.mmm.entity.id.Id;
import io.github.mmm.entity.id.UuidId;
import io.github.mmm.entity.id.UuidInstantId;
import io.github.mmm.entity.id.UuidVersionId;

/**
* {@link IdGenerator} for {@link UuidId}.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0 */
package io.github.mmm.entity.id.sequence;

import io.github.mmm.entity.id.Id;
import io.github.mmm.entity.id.LongId;
import io.github.mmm.entity.id.generator.IdGenerator;

/**
* Interface for a sequence that can generate the next unique {@link Id#get() primary key} of type {@link Long} for a
* new {@link LongId}.
*
* @see IdGenerator#generate(Id)
*/
public interface IdSequence {

/**
* @param template the {@link Id#isTransient() transient} {@link Id} of the {@link io.github.mmm.entity.Entity entity}
* to insert.
* @return the next long value guaranteed to be unique for this sequence. Proper implementations delegate to a
* database sequence.
*/
long next(Id<?> template);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0 */
package io.github.mmm.entity.id.sequence;

import io.github.mmm.entity.id.Id;

/**
* {@link IdSequence} implementation using an in-memory counter that is NOT persistent. By design this implementation
* can not guarantee correctness. Only use this for pragmatic scenarios e.g. for tests or if you are not using a
* database at all (e.g. also all data is hold in memory for simple apps that persist all data to JSON or the like).
*/
public final class IdSequenceMemory implements IdSequence {

private volatile long sequence;

/**
* The constructor.
*/
public IdSequenceMemory() {

this(10_000_000L); // enough room for your own master data
}

/**
* The constructor.
*
* @param sequenceStart the start value of the sequence.
*/
public IdSequenceMemory(long sequenceStart) {

super();
this.sequence = sequenceStart;
}

/**
* @return the current sequence value.
*/
public long getSequence() {

return this.sequence;
}

/**
* @param sequence new value of {@link #getSequence()}.
*/
public void setSequence(long sequence) {

this.sequence = sequence;
}

@Override
public long next(Id<?> template) {

return this.sequence++;
}

}
4 changes: 4 additions & 0 deletions core/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

exports io.github.mmm.entity.id;

exports io.github.mmm.entity.id.generator;

exports io.github.mmm.entity.id.sequence;

exports io.github.mmm.entity.link;

}

0 comments on commit cae3c28

Please sign in to comment.