Skip to content
This repository was archived by the owner on Jun 7, 2025. It is now read-only.

Commit 5904c20

Browse files
authored
Implement UUIDv7 value generator (#28)
1 parent afaaac5 commit 5904c20

13 files changed

Lines changed: 219 additions & 69 deletions

File tree

.checkstyle/header

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* This file is part of versatile.
2+
* This file is part of datanucleus-postgresql.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

plugin/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
<artifactId>datanucleus-rdbms</artifactId>
2525
<scope>provided</scope>
2626
</dependency>
27+
28+
<dependency>
29+
<groupId>com.fasterxml.uuid</groupId>
30+
<artifactId>java-uuid-generator</artifactId>
31+
</dependency>
2732
</dependencies>
2833

2934
<build>

plugin/src/main/java/io/github/nscuro/datanucleus/postgresql/method/AbstractJsonbMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* This file is part of versatile.
2+
* This file is part of datanucleus-postgresql.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

plugin/src/main/java/io/github/nscuro/datanucleus/postgresql/method/JsonbContainsMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* This file is part of versatile.
2+
* This file is part of datanucleus-postgresql.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

plugin/src/main/java/io/github/nscuro/datanucleus/postgresql/method/Operators.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* This file is part of versatile.
2+
* This file is part of datanucleus-postgresql.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* This file is part of datanucleus-postgresql.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* Copyright (c) Niklas Düster. All Rights Reserved.
18+
*/
19+
package io.github.nscuro.datanucleus.postgresql.valuegenerator;
20+
21+
import com.fasterxml.uuid.Generators;
22+
import com.fasterxml.uuid.impl.TimeBasedEpochRandomGenerator;
23+
import org.datanucleus.store.StoreManager;
24+
import org.datanucleus.store.valuegenerator.AbstractGenerator;
25+
import org.datanucleus.store.valuegenerator.ValueGenerationBlock;
26+
27+
import java.util.Properties;
28+
29+
public class UUIDv7Generator extends AbstractGenerator<String> {
30+
31+
private final TimeBasedEpochRandomGenerator generator;
32+
33+
public UUIDv7Generator(final StoreManager storeMgr, final String name, final Properties ignored) {
34+
super(storeMgr, name);
35+
this.generator = Generators.timeBasedEpochRandomGenerator();
36+
}
37+
38+
@Override
39+
protected ValueGenerationBlock<String> reserveBlock(final long size) {
40+
final var ids = new String[(int) size];
41+
for (int i = 0; i < size; i++) {
42+
ids[i] = generator.generate().toString();
43+
}
44+
45+
return new ValueGenerationBlock<>(ids);
46+
}
47+
48+
}

plugin/src/main/resources/plugin.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@
33
<extension point="org.datanucleus.store.rdbms.sql_method">
44
<sql-method class="java.lang.String" method="jsonbContains" evaluator="io.github.nscuro.datanucleus.postgresql.method.JsonbContainsMethod"/>
55
</extension>
6+
7+
<extension point="org.datanucleus.store_valuegenerator">
8+
<valuegenerator name="uuid-v7" class-name="io.github.nscuro.datanucleus.postgresql.valuegenerator.UUIDv7Generator" datastore="rdbms"/>
9+
</extension>
610
</plugin>

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
<lib.datanucleus-core.version>6.0.9</lib.datanucleus-core.version>
7070
<lib.datanucleus-javax-jdo.version>3.2.1</lib.datanucleus-javax-jdo.version>
7171
<lib.datanucleus-rdbms.version>6.0.9</lib.datanucleus-rdbms.version>
72+
<lib.java-uuid-generator.version>5.1.0</lib.java-uuid-generator.version>
7273
<lib.junit-jupiter.version>5.11.3</lib.junit-jupiter.version>
7374
<lib.postgresql.version>42.7.4</lib.postgresql.version>
7475
<lib.slf4j.version>2.0.16</lib.slf4j.version>
@@ -108,6 +109,12 @@
108109
<version>${lib.datanucleus-javax-jdo.version}</version>
109110
</dependency>
110111

112+
<dependency>
113+
<groupId>com.fasterxml.uuid</groupId>
114+
<artifactId>java-uuid-generator</artifactId>
115+
<version>${lib.java-uuid-generator.version}</version>
116+
</dependency>
117+
111118
<dependency>
112119
<groupId>org.postgresql</groupId>
113120
<artifactId>postgresql</artifactId>

test/src/main/java/io/github/nscuro/datanucleus/postgresql/test/model/Person.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* This file is part of versatile.
2+
* This file is part of datanucleus-postgresql.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import javax.jdo.annotations.PersistenceCapable;
2525
import javax.jdo.annotations.Persistent;
2626
import javax.jdo.annotations.PrimaryKey;
27+
import java.util.UUID;
2728

2829
@PersistenceCapable
2930
public class Person {
@@ -42,6 +43,9 @@ public class Person {
4243
})
4344
private String properties;
4445

46+
@Persistent(customValueStrategy = "uuid-v7")
47+
private UUID uuid;
48+
4549
public long getId() {
4650
return id;
4751
}
@@ -66,4 +70,12 @@ public void setProperties(final String properties) {
6670
this.properties = properties;
6771
}
6872

73+
public UUID getUuid() {
74+
return uuid;
75+
}
76+
77+
public void setUuid(final UUID uuid) {
78+
this.uuid = uuid;
79+
}
80+
6981
}

test/src/main/resources/schema.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ create table if not exists "PERSON" (
22
"ID" int primary key generated always as identity
33
, "NAME" text
44
, "PROPERTIES" jsonb
5+
, "UUID" TEXT
56
);

0 commit comments

Comments
 (0)