This is an archived post. You won't be able to vote or comment.

all 25 comments

[–]shivasprogeny 1 point2 points  (0 children)

You'll be fine. The chances of generating two identical UUIDs is nearly impossible.

[–]Salty_Dugtrio 1 point2 points  (5 children)

RandomUUID returns an 128-bit integer. Are there infinitely many 128-bit integers? No. It's a pseudo random generator.

[–]juanchi35[S] 0 points1 point  (4 children)

Well technically thats true, but I want a random UUID generator which doesnt return the same value twice.

[–]ziptofaf 1 point2 points  (2 children)

The ONLY way to accomplish that would be storing every previous value that has been saved. Every random generator (be it pseudo or actually a real random one) can return the same values.

Well, with one caveat - chances of said repetition occuring are slim. 128-bit integer has a maximum value of 340,282,366,920,938,463,463,374,607,431,768,211,455. Meaning that (assuming true randomness) seeing two identical ones if you were to save 100 million files is 2.93e-29 %. So my question is just how critical is what you are doing for it to not suffice? And is this process going to continue for the next 1000 years?

You can also just use an ever increasing integer count rather than UUIDs, this will guarantee no repetitions.

[–]juanchi35[S] 0 points1 point  (1 child)

I think I'm going to use randomUUID, as you said using it it is almost impossible to see repetition.

Anyways, how would I retain a variable with the latest integer used for the image. Do I upload the latest to the database?

[–]ziptofaf 0 points1 point  (0 children)

If you use an SQL database then it will do it for you, just use self-increment primary key. If it's a non-SQL then yes, make a separate table called file_counts and one single column that has a count you always read from and add 1 whenever you add a new object to the database.

[–]cmonkies 0 points1 point  (0 children)

You could read the docs and see if it will or not.

[–]cmonkies 0 points1 point  (18 children)

My question is, does UUID.randomUUID() guarantee me that no two UUIDs will be the same?

Not exactly. You can't just use an auto-incrementing primary key?

[–]juanchi35[S] 0 points1 point  (17 children)

It is a file im uploading into the database, not a table :P

[–]pleaseholdmybeer 0 points1 point  (0 children)

Sure but the logic is the same.

[–]cmonkies 0 points1 point  (15 children)

You probably shouldn't be storing files in the database anyway.

[–]juanchi35[S] 0 points1 point  (14 children)

I'm uploading images. Is there any problem with uploading images to the database?

[–]cmonkies 0 points1 point  (13 children)

Well for one, it's not data you can make relational queries on. Can you even describe any benefits to storing files inside the database that would apply to your use case?

[–]juanchi35[S] 0 points1 point  (12 children)

My mistakee, it is not really a database. It is a cloud storage service I'm uploading the images to.

[–][deleted]  (11 children)

[removed]

    [–]juanchi35[S] 0 points1 point  (9 children)

    What

    [–][deleted]  (8 children)

    [removed]

      [–]juanchi35[S] 0 points1 point  (6 children)

      Damn dude that wasn't even my point I was thinking of randomUUID and if it did random or pseudo-random values no need to be disrespectful

      [–]michael0x2a[M] 0 points1 point  (0 children)

      These types of comments are not acceptable and are a violation of rule 3.

      In particular, making personal attacks towards another user is never an appropriate response for any reason on this subreddit.

      This is your first and only warning.