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 intbucketIterationNumberprotected longbucketPrefixCodePopulationprotected intbucketsNeededprotected intcharsNeededForEachBucketprotected CodeFormatAvailableCharHelperhelperprotected inttotalCharsInFormatForBucket
-
Constructor Summary
Constructors Constructor Description CodeGeneratorBucket(CodeFormatAvailableCharHelper helper, int totalCodesNeeded, String codeFormat, int batchSize)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description longcalculateBucketPrefixNumber(Random r)Calculates an integer representation of this bucket.protected voidcomputeBaseDivisors(String codeFormat)The base divisors are used to effectively determine the "base" of each character in the code.protected intdetermineNumberOfCharsNeededForBuckets(String codeFormat, CodeFormatAvailableCharHelper helper)Returns the number of buckets of "batch size" needed to produce uniqueintgetBucketIterationNumber()Starts at one and is incremented each time a new bucket of codes has been generated.longgetBucketPrefixCodePopulation()Number of codes available in a given bucket.intgetBucketsNeeded()intgetCharsNeededForEachBucket()voidsetBucketPrefixCodePopulation(long bucketPrefixCodePopulation)voidsetBucketsNeeded(int bucketsNeeded)voidsetCharsNeededForEachBucket(int charsNeededForEachBucket)voidupdateBucketIteration()StringupdateCodeFormatWithBucketPrefix(String codeFormat, Random r)Returns the passed in code after substituting the first "charNeededForEachBucket" in the code format with the appropriate bucket prefix.
-
-
-
Field Detail
-
bucketIterationNumber
protected int bucketIterationNumber
-
bucketsNeeded
protected int bucketsNeeded
-
charsNeededForEachBucket
protected int charsNeededForEachBucket
-
totalCharsInFormatForBucket
protected int totalCharsInFormatForBucket
-
bucketPrefixCodePopulation
protected long bucketPrefixCodePopulation
-
helper
protected CodeFormatAvailableCharHelper helper
-
-
Constructor Detail
-
CodeGeneratorBucket
public CodeGeneratorBucket(CodeFormatAvailableCharHelper helper, int totalCodesNeeded, String codeFormat, int batchSize)
-
-
Method Detail
-
computeBaseDivisors
protected void computeBaseDivisors(String codeFormat)
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).- Parameters:
codeFormat-
-
updateCodeFormatWithBucketPrefix
public String updateCodeFormatWithBucketPrefix(String codeFormat, Random r)
Returns the passed in code after substituting the first "charNeededForEachBucket" in the code format with the appropriate bucket prefix.- Parameters:
codeFormat-r-- Returns:
-
calculateBucketPrefixNumber
public long calculateBucketPrefixNumber(Random r)
Calculates an integer representation of this bucket. If you agetBucketPrefixCodePopulation()size of 10 and you also needed 10 buckets needed, then this method would return the bucketIterationNumber. Otherwise, it segments the bucketPopulation and returns a random value that is valid for the range of the current segment.- 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()
-
setBucketsNeeded
public void setBucketsNeeded(int bucketsNeeded)
- Parameters:
bucketsNeeded-- See Also:
getBucketsNeeded()
-
getCharsNeededForEachBucket
public int getCharsNeededForEachBucket()
-
setCharsNeededForEachBucket
public void setCharsNeededForEachBucket(int charsNeededForEachBucket)
- Parameters:
charsNeededForEachBucket-- See Also:
getCharsNeededForEachBucket()
-
getBucketPrefixCodePopulation
public long getBucketPrefixCodePopulation()
Number of codes available in a given bucket. This is important. Say for example that 2000 codes were being generated for a code that used alphanumerics. For a batch size of 1000, only 2 buckets would be required.- Returns:
-
setBucketPrefixCodePopulation
public void setBucketPrefixCodePopulation(long bucketPrefixCodePopulation)
- Parameters:
bucketPrefixCodePopulation-- See Also:
getBucketPrefixCodePopulation()
-
updateBucketIteration
public void updateBucketIteration()
-
determineNumberOfCharsNeededForBuckets
protected int determineNumberOfCharsNeededForBuckets(String codeFormat, CodeFormatAvailableCharHelper helper)
Returns the number of buckets of "batch size" needed to produce unique- Returns:
-
-