001package io.ebean.config.dbplatform; 002 003import io.ebean.BackgroundExecutor; 004 005import javax.sql.DataSource; 006import java.sql.ResultSet; 007import java.sql.SQLException; 008import java.util.ArrayList; 009import java.util.List; 010 011/** 012 * Database sequence based IdGenerator using Sequence Step 1 but batch fetch many sequence values. 013 */ 014public abstract class SequenceBatchIdGenerator extends SequenceIdGenerator { 015 016 /** 017 * Construct where batchSize is the sequence step size. 018 * 019 */ 020 public SequenceBatchIdGenerator(BackgroundExecutor be, DataSource ds, String seqName, int batchSize) { 021 super(be, ds, seqName, batchSize); 022 } 023 024 /** 025 * If allocateSize is large load some sequences in a background thread. 026 * <p> 027 * For example, when inserting a bean with a cascade on a OneToMany with many 028 * beans Ebean can call this to ensure . 029 * </p> 030 */ 031 @Override 032 public void preAllocateIds(int requestSize) { 033 if (allocationSize > 1 && requestSize > allocationSize) { 034 // only bother if allocateSize is bigger than 035 // the normal loading batchSize 036 if (requestSize > 100) { 037 // max out at 100 for now 038 requestSize = 100; 039 } 040 loadInBackground(requestSize); 041 } 042 } 043 044 /** 045 * Add the next set of Ids as the next value plus all the following numbers up to the step size. 046 */ 047 @Override 048 protected List<Long> readIds(ResultSet resultSet, int loadSize) throws SQLException { 049 050 List<Long> newIds = new ArrayList<>(loadSize); 051 while (resultSet.next()) { 052 newIds.add(resultSet.getLong(1)); 053 } 054 return newIds; 055 } 056 057}