Class CodeGeneratorBucket
- java.lang.Object
-
- com.broadleafcommerce.promotion.campaign.service.helper.CodeGeneratorBucket
-
public class CodeGeneratorBucket extends Object
A code generation bucket provides a way to generate large numbers of random codes while minimizing collisions in the database. For example, - We want to generate a million codes - Since this could be a long(ish) running process, we want to do the database writes in batches - So, if we generate the database traffic in batches of 1000 we can avoid collisions of randomly generated numbers by reserving part of the code ("e.g. a bucket") that will be guaranteed not to collide. This works best when using unique code patterns for each large campaign involved with offers. Example: CAMPAIGN1-@@@@@@@@@@ where @@@@@@@ is the random part of the code.- Author:
- bpolster
-
-
Field Summary
Fields Modifier and Type Field Description protected int
batchSize
protected int
bucketIterationNumber
protected long
bucketPrefixCodePopulation
protected int
bucketsNeeded
protected Map<Character,String>
cachedCharMap
protected int
charsNeededForEachBucket
protected CodeGenerator
codeGenerator
protected CodeFormatAvailableCharHelper
helper
protected int
totalCharsInFormatForBucket
-
Constructor Summary
Constructors Constructor Description CodeGeneratorBucket(CodeFormatAvailableCharHelper helper, CodeGenerator generator, int batchSize)
The
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected int[]
computeBaseDivisors()
The base divisors are used to effectively determine the "base" of each character in the code.protected long
determineBucketPrefixCodePopulation()
protected int
determineNumberOfCharsNeededForBuckets()
Returns the number of buckets of "batch size" needed to produce uniqueint
getBucketIterationNumber()
Starts at one and is incremented each time a new bucket of codes has been generated.int
getBucketsNeeded()
CodeGenerator
getCodeGenerator()
protected void
initializeBucket()
Prepares the bucket with data needed for code generation.void
updateBucketIteration()
String
updateCodeFormatWithBucketPrefix(String codeFormat)
Returns the passed in code after substituting the first "charNeededForEachBucket" in the code format with the appropriate bucket prefix.
-
-
-
Field Detail
-
batchSize
protected int batchSize
-
bucketIterationNumber
protected int bucketIterationNumber
-
bucketsNeeded
protected int bucketsNeeded
-
charsNeededForEachBucket
protected int charsNeededForEachBucket
-
totalCharsInFormatForBucket
protected int totalCharsInFormatForBucket
-
bucketPrefixCodePopulation
protected long bucketPrefixCodePopulation
-
helper
protected CodeFormatAvailableCharHelper helper
-
codeGenerator
protected CodeGenerator codeGenerator
-
-
Constructor Detail
-
CodeGeneratorBucket
public CodeGeneratorBucket(CodeFormatAvailableCharHelper helper, CodeGenerator generator, int batchSize)
The- Parameters:
helper
-generator
-batchSize
-
-
-
Method Detail
-
initializeBucket
protected void initializeBucket()
Prepares the bucket with data needed for code generation.
-
computeBaseDivisors
protected int[] computeBaseDivisors()
The base divisors are used to effectively determine the "base" of each character in the code. Think about this like decimal versus hex. The decimal base would (10,10,10, etc.) whereas the Hexadecimal base would be (16,16,16). Since we can have a variable number of possibilities per digit, this is more confusing when building codes. For example, the code pattern (##-@@) has 2 numbers, a dash, and 2 letters. Because we remove some numbers and letters from the available alphabet, the base divisors that would be computed for this example would be (7,7,23,23).
-
updateCodeFormatWithBucketPrefix
public String updateCodeFormatWithBucketPrefix(String codeFormat)
Returns the passed in code after substituting the first "charNeededForEachBucket" in the code format with the appropriate bucket prefix.- Parameters:
codeFormat
-- Returns:
-
getBucketIterationNumber
public int getBucketIterationNumber()
Starts at one and is incremented each time a new bucket of codes has been generated.- Returns:
-
getBucketsNeeded
public int getBucketsNeeded()
-
getCodeGenerator
public CodeGenerator getCodeGenerator()
-
updateBucketIteration
public void updateBucketIteration()
-
determineBucketPrefixCodePopulation
protected long determineBucketPrefixCodePopulation()
-
determineNumberOfCharsNeededForBuckets
protected int determineNumberOfCharsNeededForBuckets()
Returns the number of buckets of "batch size" needed to produce unique- Returns:
-
-