Giter Club home page Giter Club logo

Comments (5)

jd327 avatar jd327 commented on September 18, 2024

I'm going to guess that you're trying to figure out a better way to compress Mongo's IDs because the current output is kinda lengthy.

There are two problems with your code that I see:

  1. When you do preg_replace with '', you cannot safely encode/decode "5ab3" and "a53b". And when you do str_replace with range, you cannot safely decode because you don't know which integer was a letter (5a => 50 or 50 => 50?).
  2. Additionally, your chunk_split will have an issue when you split a string like this: "200003", because encode will think you're encoding 200 and 3, when you should be encoding 200 and 003.

So, to answer your question -- yes, looks like you'll definitely have collisions with these methods.

from hashids.

 avatar commented on September 18, 2024

55a3f9bdd874595813000029 => 920000318595478ddb9f3a55 => 920000318595478331953055

[0] => 920000318
[1] => 595478331
[2] => 953055
[3] => time(1)

Right now i;m not care about decrypt ID back, need be only uniq, so i think to add mt_rand ord time(1) as additional param. Also MongoDB ids are normal ended with 0000001 so i just strrev base ID. So som kind of hard-fix.

Thank you for your patience

from hashids.

jd327 avatar jd327 commented on September 18, 2024

If you need them to be unique, I don't recommend doing what you're proposing, because you will generate duplicate ids (irregardless of what additional parameters you add). I'm not aware of any safer method other than the one provided by encode_hex and decode_hex.

from hashids.

 avatar commented on September 18, 2024

$id = new \MongoId($toHash);

echo $h->encode([$id->getTimestamp(),$id->getInc(),$id->getPID(),preg_replace('/[a-zA-Z]+/', '', substr($toHash,8,6))]);

ObjID

55c1f1f4e12f92d814000031
Timestamp: 1438773748
Hostname2: 1292 //MD5 3 bytes :: -A_Z
ProcesID: 5336
Increment: 49
0DWpDj6ulYUL3ZuR5

Tested on 2milion rows no duplicate, so this method is i think more clean

from hashids.

jd327 avatar jd327 commented on September 18, 2024

Hi @Dawjan, yes that's one of the methods listed under http://hashids.org/#alternatives (number 2). That's good that you didn't get duplicates for the first 2m rows.

from hashids.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.