chad101 Posted April 27, 2009 Share Posted April 27, 2009 I need a way to generate an incrementing unique id for a flat file system without opening the file, finding the last id and incrementing it. It's impossible to generate a +1 incrementing system without opening the file. I have no intention on doing this. I just need the next unique id to be larger than the last. Also, generating a random unique id will not work since I plan on using a binary search for editing and removing data. I thought about using micro time but, I ran into a interesting problem while testing this system. <?php $time1 = 1; $time2 = 2; while(strcmp($time1, $time2) < 0) { $time1 = str_replace('.','',microtime(true)); $time2 = str_replace('.','',microtime(true)); } print "time1 = $time1 <br /> time2 = $time2 <br />"; print strcmp($time1, $time2); ?> Output: time1 = 12407838922167 time2 = 12407838922165 1 As you can see, time1 is actually larger than time2. Yet time2 was generated last. ??? Quote Link to comment https://forums.phpfreaks.com/topic/155772-incrementing-unique-id/ Share on other sites More sharing options...
premiso Posted April 27, 2009 Share Posted April 27, 2009 Why not have a separate file and store the last id in that file ??? Simple as that. Quote Link to comment https://forums.phpfreaks.com/topic/155772-incrementing-unique-id/#findComment-819985 Share on other sites More sharing options...
chad101 Posted April 27, 2009 Author Share Posted April 27, 2009 Thanks for the response premiso. That's an interesting idea. My only concern is I still have to open a file and read data. I want to avoid this at all costs. EDIT: I forgot to mention this script may have multiple users. A separate file w/ the unique id could cause an issue if script reads the same id for both users simultaneously. Quote Link to comment https://forums.phpfreaks.com/topic/155772-incrementing-unique-id/#findComment-819989 Share on other sites More sharing options...
premiso Posted April 27, 2009 Share Posted April 27, 2009 Thanks for the response premiso. That's an interesting idea. My only concern is I still have to open a file and read data. I want to avoid this at all costs. You can use the rand function along with along with time inside of an md5 to create a "unique" hash each time. Those should guarantee a unique id, if you do not mind it being a hash instead of just all numbers. Quote Link to comment https://forums.phpfreaks.com/topic/155772-incrementing-unique-id/#findComment-819993 Share on other sites More sharing options...
nankoweap Posted April 27, 2009 Share Posted April 27, 2009 if you're using a database (mysql, oracle...), then you could simply use the database to generate unique, incremental numbers. jason Quote Link to comment https://forums.phpfreaks.com/topic/155772-incrementing-unique-id/#findComment-819998 Share on other sites More sharing options...
chad101 Posted April 27, 2009 Author Share Posted April 27, 2009 Those should guarantee a unique id, if you do not mind it being a hash instead of just all numbers. The format is not a major concern. As long as the unique id is greater than all previous ids. Would hashing the current time + rand() always return a larger value? Quote Link to comment https://forums.phpfreaks.com/topic/155772-incrementing-unique-id/#findComment-819999 Share on other sites More sharing options...
PFMaBiSmAd Posted April 27, 2009 Share Posted April 27, 2009 My only concern is I still have to open a file and read dataWeb servers are stateless. All the resources (variables) are destroyed when the php script ends execution on any page request. Unless you store the last id somewhere, it will not exit outside of each page request. There is already existing code to do what you are writing your php code to do, it called a database engine and it is already written, tested, and since it is compiled code instead slow parsed/tokenized/interpreted php code, it executes several hundred times faster than the php script you are writing ever will. Quote Link to comment https://forums.phpfreaks.com/topic/155772-incrementing-unique-id/#findComment-820000 Share on other sites More sharing options...
chad101 Posted April 27, 2009 Author Share Posted April 27, 2009 if you're using a database (mysql, oracle...), then you could simply use the database to generate unique, incremental numbers. jason A database would make this allot easier lol. But that's no fun. The goal is to create a flat file database that is reasonably fast. That's why I need to use a binary search for editing data. I cannot do this without an ordered unique id. Quote Link to comment https://forums.phpfreaks.com/topic/155772-incrementing-unique-id/#findComment-820002 Share on other sites More sharing options...
chad101 Posted April 27, 2009 Author Share Posted April 27, 2009 There is already existing code to do what you are writing your php code to do, it called a database engine and it is already written, tested, and since it is compiled code instead slow parsed/tokenized/interpreted php code, it executes several hundred times faster than the php script you are writing ever will. Thank you PFMaBiSmAd This is fun project w/ the intention of not using a database engine. I’m very well versed in using database backends and well aware that this script will never reach it’s maximum potential without a database. Since I’m not using a RDBMS; I need to limit resources to an absolute minimum. Every tiny bit helps. Opening a file and reading the unique id uses 'some' additional resources. Most likely this will be a time based unique id. Micro time would have done trick had I not encountered this problem (see original post). You would think the code in my original post would be an infinite loop. Yet the initial micro time is 'sometimes' greater than the second (last generation). Quote Link to comment https://forums.phpfreaks.com/topic/155772-incrementing-unique-id/#findComment-820008 Share on other sites More sharing options...
nankoweap Posted April 27, 2009 Share Posted April 27, 2009 well, you're going to have push concurrency control to something in your application. in the databaseless (is that a word?) webserver/php world, i'm not sure what can be used to reliably provide concurrency control. Quote Link to comment https://forums.phpfreaks.com/topic/155772-incrementing-unique-id/#findComment-820017 Share on other sites More sharing options...
nankoweap Posted April 27, 2009 Share Posted April 27, 2009 hmmmm.... i reckon you could explore writing a php extension that provides a control layer for your app. it may be easier to implement using some gool ol' C and the OS resources it would have ready access to. ???? Quote Link to comment https://forums.phpfreaks.com/topic/155772-incrementing-unique-id/#findComment-820020 Share on other sites More sharing options...
nankoweap Posted April 27, 2009 Share Posted April 27, 2009 found this today... http://us.php.net/manual/en/function.sem-acquire.php using semaphores will allow your php code to effectively address concurrency by synchronizing access to a shared resource (e.g. a file used to maintain a counter/sequence). Quote Link to comment https://forums.phpfreaks.com/topic/155772-incrementing-unique-id/#findComment-820262 Share on other sites More sharing options...
chad101 Posted April 27, 2009 Author Share Posted April 27, 2009 found this today... http://us.php.net/manual/en/function.sem-acquire.php using semaphores will allow your php code to effectively address concurrency by synchronizing access to a shared resource (e.g. a file used to maintain a counter/sequence). Thanks! This is very handy :-) Quote Link to comment https://forums.phpfreaks.com/topic/155772-incrementing-unique-id/#findComment-820343 Share on other sites More sharing options...
Mchl Posted April 27, 2009 Share Posted April 27, 2009 Thought about using SQLite? Quote Link to comment https://forums.phpfreaks.com/topic/155772-incrementing-unique-id/#findComment-820357 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.