Skip to content

Commit

Permalink
Merge pull request eclipse#594 from eclipse/fix-nonNegativeIdGenerator
Browse files Browse the repository at this point in the history
fix negative number id generator (only positive number will be genera…
  • Loading branch information
Coduz authored May 30, 2017
2 parents a137f76 + 34f5474 commit 5216570
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ private IdGenerator() {
* @return
*/
public static BigInteger generate() {
byte[] bytes = new byte[ID_SIZE];
SECURE_RANDOM.nextBytes(bytes);
return new BigInteger(bytes);
return new BigInteger(ID_SIZE, SECURE_RANDOM);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public enum SystemSettingKey implements SettingKey {
OSGI_CONTEXT("commons.osgi.context"),

/**
* Set the Kapua key size
* Set the Kapua key size (the size is expressed in bits)
*/
KAPUA_KEY_SIZE("commons.entity.key.size"),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@ commons.osgi.context=false
#
# Entity settings
#
commons.entity.key.size=8
#set the generated ids size (in bits) (please don't use key size greater than 63 with H2 since H2 maps the biginteger to a long. see http://www.h2database.com/html/datatypes.html#bigint_type)
commons.entity.key.size=63
commons.entity.insert.max.retry=3
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
package org.eclipse.kapua.commons.model;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;

import java.math.BigInteger;

import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.commons.model.id.IdGenerator;
import org.eclipse.kapua.commons.model.misc.CollisionEntity;
import org.eclipse.kapua.commons.model.misc.CollisionIdGenerator;
import org.eclipse.kapua.commons.model.misc.CollisionServiceImpl;
Expand Down Expand Up @@ -109,4 +111,19 @@ public void testKeyNoKeyCollision() {
collisionIdGenerator.getGeneretedValuesCount());
}
}

@Test
/**
* Just generate few ids and check if the numbers are fitted into the expected limits
*
* @throws Exception
*/
public void testIdGeneratorBound() throws Exception {
int idSize = SystemSetting.getInstance().getInt(SystemSettingKey.KAPUA_KEY_SIZE);
BigInteger upperLimit = BigInteger.valueOf(2).pow(idSize);
for (int i = 0; i < 1000; i++) {
BigInteger generated = IdGenerator.generate();
assertFalse("The generated id is out of the expected bounds!", generated.compareTo(BigInteger.ZERO) < 0 || generated.compareTo(upperLimit) != -1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@ commons.osgi.context=false
#
# Entity settings
#
commons.entity.key.size=8
#set the generated ids size (in bits) (please don't use key size greater than 63 with H2 since H2 maps the biginteger to a long. see http://www.h2database.com/html/datatypes.html#bigint_type)
commons.entity.key.size=63
commons.entity.insert.max.retry=3
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,10 @@ broker.port=1884
#
# Entity settings
#
commons.entity.key.size=8
#set the generated ids size (in bits) (please don't use key size greater than 63 with H2 since H2 maps the biginteger to a long. see http://www.h2database.com/html/datatypes.html#bigint_type)
commons.entity.key.size=63
commons.entity.insert.max.retry=3

character.encoding=UTF-8

commons.osgi.context=false

#
# Entity settings
#
commons.entity.key.size=8
commons.entity.insert.max.retry=3
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@ commons.osgi.context=false
#
# Entity settings
#
commons.entity.key.size=8
#set the generated ids size (in bits) (please don't use key size greater than 63 with H2 since H2 maps the biginteger to a long. see http://www.h2database.com/html/datatypes.html#bigint_type)
commons.entity.key.size=63
commons.entity.insert.max.retry=3
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@ commons.osgi.context=false
#
# Entity settings
#
commons.entity.key.size=8
#set the generated ids size (in bits) (please don't use key size greater than 63 with H2 since H2 maps the biginteger to a long. see http://www.h2database.com/html/datatypes.html#bigint_type)
commons.entity.key.size=63
commons.entity.insert.max.retry=3
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@ commons.osgi.context=false
#
# Entity settings
#
commons.entity.key.size=8
#set the generated ids size (in bits) (please don't use key size greater than 63 with H2 since H2 maps the biginteger to a long. see http://www.h2database.com/html/datatypes.html#bigint_type)
commons.entity.key.size=63
commons.entity.insert.max.retry=3
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@ commons.osgi.context=false
#
# Entity settings
#
commons.entity.key.size=8
#set the generated ids size (in bits) (please don't use key size greater than 63 with H2 since H2 maps the biginteger to a long. see http://www.h2database.com/html/datatypes.html#bigint_type)
commons.entity.key.size=63
commons.entity.insert.max.retry=3

0 comments on commit 5216570

Please sign in to comment.