|
| 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 | +} |
0 commit comments