Jump to content

Recommended Posts

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. ???

Link to comment
https://forums.phpfreaks.com/topic/155772-incrementing-unique-id/
Share on other sites

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.   

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.

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?     

My only concern is I still have to open a file and read data
Web 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.

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.     

 

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).

 

 

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 :-)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.