Forum BigDB Autoincrement ID

Discussion and help relating to the PlayerIO database solution, BigDB.

Autoincrement ID

Postby uterian » April 24th, 2013, 12:22 am

Hi
There is a system in my game that randomly creates an enemy and inserts it into database before fight but I need an unique ID for each enemy. I found a way to do it; first I decresadly list ID in db and take the biggest ID then I increment it and write the new enemy on db but there is a problem, what if 2 operations take the same "maxID" from db?
Is there a way to autoincrement ID?
uterian
 
Posts: 8
Joined: March 29th, 2013, 2:09 am

Re: Autoincrement ID

Postby dreamora » April 24th, 2013, 9:43 am

simply create a db object without giving it an identifier. This will generated a granted unique id which you can read from it afterwards
dreamora
 
Posts: 225
Joined: March 2nd, 2012, 9:58 am

Re: Autoincrement ID

Postby MoDDiB » August 29th, 2015, 6:17 pm

It doesn't work :

Code: Select all
PlayerIOClient.PlayerIOError: InvalidBigDBKey; The key '' is invalid. Keys must be between 1 and 50 word characters (no spaces).


Any other idea ?
MoDDiB
 
Posts: 30
Joined: July 13th, 2014, 8:41 pm

Re: Autoincrement ID

Postby archipdev » August 29th, 2015, 11:34 pm

I don't know what you did (pretty hard without seeing any code) but if you did something like
Code: Select all
myObject.key = '';

remove that line.
archipdev
 
Posts: 28
Joined: September 6th, 2014, 10:35 am

Re: Autoincrement ID

Postby MoDDiB » August 30th, 2015, 2:11 pm

You were right, thank you.

By the way it doesn't really help me : it generates a random string instead of an autoincremented value :/
MoDDiB
 
Posts: 30
Joined: July 13th, 2014, 8:41 pm

Re: Autoincrement ID

Postby archipdev » August 30th, 2015, 6:25 pm

You should look optimistic locking here : https://gamesnet.yahoo.net/documentatio ... b/advanced
the example is pretty much what you're looking for ;)
archipdev
 
Posts: 28
Joined: September 6th, 2014, 10:35 am

Re: Autoincrement ID

Postby Henrik » August 31st, 2015, 5:37 pm

Why do you need an auto-incrementing id? A random id should be perfect in this case. If you want to know which item was created before another, put a timestamp property on each object instead.
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Autoincrement ID

Postby MoDDiB » August 31st, 2015, 5:54 pm

Henrik wrote:Why do you need an auto-incrementing id

With an auto-incrementing ID I can easily get a random DataObject even in 500K+ entries.
Do you have a better solution ?
MoDDiB
 
Posts: 30
Joined: July 13th, 2014, 8:41 pm

Re: Autoincrement ID

Postby Guillaume » August 31st, 2015, 9:31 pm

MoDDiB wrote:
Henrik wrote:Why do you need an auto-incrementing id

With an auto-incrementing ID I can easily get a random DataObject even in 500K+ entries.
Do you have a better solution ?


As Henrik said, a random Id seem sufficient according to all possibilities...Or you can even create your own id with a generated GUID in your code.

Its unlikely that you will have a id collision with a random id. If its REALLY your concern, why don't use LoadOrCreate function, when retrieving your DatabaseObject ? You can flag a property yourself when your data is inserted, like a DateTime property with the name "creationDate".

This way, you can "LoadOrCreate" the data. Then just check if the "creationDate" property exist. If it happen, then it was bad luck, then just LoadOrCreate a new DatabaseObject instead. If "creationDate" was not present, then your data was not here before.

This said, maybe BigDB already have a unicity verification system when creating with a random Id (with the null parameter), but only Henrik know if this is the case.
Guillaume
 
Posts: 277
Joined: March 28th, 2014, 1:59 am
Location: France

Re: Autoincrement ID

Postby Henrik » August 31st, 2015, 9:53 pm

So you would know the max and min ID of all your objects, assume there's an entry for each number, pick a random number between min and max, and load that object to get a random object?

An easier way that fits BigDB better is to let it generate a random ID by passing in a null key, and add a property to each object with the current timestamp. You can easily know the max and min values for that property without doing a lookup in BigDB, and to get a random item from the table, simply pick a random timestamp in the range, and do a LoadRange, set that random value as the start value, and set the limit to 1. That way you will get a random object, and you need no lookups or synchronization or sequences.

You will not get a uniform distribution of random items you are selecting, since they won't be generated uniformly, but does it matter? Some monsters will be more common than others.
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Autoincrement ID

Postby MoDDiB » August 31st, 2015, 10:06 pm

Henrik wrote:You will not get a uniform distribution of random items you are selecting, since they won't be generated uniformly, but does it matter? Some monsters will be more common than others.

The uniform distribution is an important factor for me :/
MoDDiB
 
Posts: 30
Joined: July 13th, 2014, 8:41 pm

Re: Autoincrement ID

Postby Henrik » September 1st, 2015, 5:55 pm

Ok. You will need to use optimistic locking to generate unique sequence numbers. Basically, have an object in a table with one int property.

1) Load the object.
2) Set the property to itself + 1.
3) Try to save back with optimistic locking.
4) If you fail, go to 1.
5) If not, you have your number.

https://gamesnet.yahoo.net/documentatio ... b/advanced
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Autoincrement ID

Postby MoDDiB » September 2nd, 2015, 9:05 am

Henrik wrote:Ok. You will need to use optimistic locking to generate unique sequence numbers. Basically, have an object in a table with one int property.

1) Load the object.
2) Set the property to itself + 1.
3) Try to save back with optimistic locking.
4) If you fail, go to 1.
5) If not, you have your number.

https://gamesnet.yahoo.net/documentatio ... b/advanced

Yes I will try this, and I will put it directly on the server ;
I guess only a really big number of requests at the same time will make the server timeout because it will most of the time fail to save.
MoDDiB
 
Posts: 30
Joined: July 13th, 2014, 8:41 pm

Re: Autoincrement ID

Postby Henrik » September 2nd, 2015, 7:10 pm

Yes, you need to make sure you only do X retries (maybe 20 or so?), and if you can't get a number in that many tries, give up or do something else or throw an error or whatever. :-)
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Autoincrement ID

Postby MoDDiB » September 2nd, 2015, 7:50 pm

Ahah ok thank you !
MoDDiB
 
Posts: 30
Joined: July 13th, 2014, 8:41 pm


Return to BigDB



cron