Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Generate a unique ID

by sundialsvc4 (Abbot)
on Nov 15, 2010 at 18:21 UTC ( [id://871531]=note: print w/replies, xml ) Need Help??


in reply to Generate a unique ID

As you further describe this as being related to “spill files,” if it is possible to do so (and if this is not already what you are doing), it seems to me that it would be a good idea to set up a randomly-named “spill directory.”   Put all of the spill-files, no matter what they may be named, into that.   When the process ends, it likewise tries to clean up “such-and-such directory, no matter what it contains.”

This might be a much less pervasive change ... requiring less code in the various programs to be considered and touched as you are implementing this change.   You no longer care about the name of the toys upon the floor; only the name of the playpen.

Replies are listed 'Best First'.
Re^2: Generate a unique ID
by BrowserUk (Patriarch) on Nov 15, 2010 at 19:50 UTC
    set up a randomly-named “spill directory.”

    Using a directory name instead of a prefix is a good notion.

    mkdir returns false if the directory already exists and is atomic. Problem is, how do you distinguish between failure to create because it exists, and failure to create for some other reason? Eg. permissions.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      If you use a random identifier that is long enough, I do believe that it becomes irrelevant to seriously consider “collisions.”   You will have won the Lottery in every state and every country, and retired to a place where you do not have to give a tinker’s dam about computers, long before a collision actually occurs.

      The sequence that you expect to succeed will be:   to create the directory, write some sentinel file into it, and verify that the sentinel file does exist.   If you can do all that, you’re good to go.

      If you cannot create the directory, then I submit that it is safe to assume that the reason is “permissions.”   Even though meteors can fall from the sky and lodge themselves in your microwave oven just to the left of your turkey sandwich, you don’t need to test for them.

        You will have won the Lottery in every state and every country,

        Most lotteries are won!

        The problem with a random number solution, is the quality of the random number generator:

        >perl -E"++$n, $h{rand()}++ and die qq[Repeat after $n iterations] for + 1 .. 1e6" Repeat after 110 iterations at -e line 1. >perl -E"++$n, $h{rand()}++ and die qq[Repeat after $n iterations] for + 1 .. 1e6" Repeat after 225 iterations at -e line 1. >perl -E"++$n, $h{rand()}++ and die qq[Repeat after $n iterations] for + 1 .. 1e6" Repeat after 115 iterations at -e line 1. >perl -E"++$n, $h{rand()}++ and die qq[Repeat after $n iterations] for + 1 .. 1e6" Repeat after 28 iterations at -e line 1.

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
      Problem is, how do you distinguish between failure to create because it exists, and failure to create for some other reason?
      You'd look at the error code. If the directory exists, the error will be EEXIST, and if it fails for some other reason, the error will be different.

      So far it looks for me like you trying to reinvent the wheel. You need to create directory with unique name?

      $dir = File::Temp->newdir;

      That's it. If you want, you can specify template for the directory name which will include timestamp or whatever you want. If it return failure, it's for some other reason.

        I started out looking to create unique filenames, that during a later stage of the processing, I could differentiate from those created by other runs of the code, via globbing, .

        Once sundialsvc4 suggested using a directory--which hadn't crossed my mind earlier--using a temporary directory and creating all my files within it make perfect sense.

        I'm not sure that module buy me anything useful over something like:

        my $path = $ENV{ TMP }; my $dir = 'temp' . join'', map{ ('A'..'Z')[rand 26] } 1 .. 4; ++$dir until mkdir $path . $dir or $! != 17 and die "$path$dir : $!;

        But I guess that depends whether this code will ever make it to another platform. I don't see that ever happening.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
      use -X
      unless (mkdir $dir) { -d $dir && ... dir exists; -w _ || ... not writable by euid -x _ || ... not traversable by euid }
      if you look for atomic test, you can also open files with O_CREAT | O_EXCL. Then, you can just use pid + start time + local counter
      $fh = IO::File->new ($filename, O_EXCL | O_CREAT | ...)
        use -X

        -X uses stat and that takes quite a long time, especially on Win32. It leaves the possibility that between your failed attempt and the return of the test, another program (or another copy of this program), will get in and create the directory.

        But as javafan pointed out, that isn't necessary. $! tells me the reason for failure. See Re^4: Generate a unique ID.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://871531]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-03-28 12:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found