Twitter, Snowflake, Números grandes y PHP

Desde que los ID’s de los estados en Twitter son mayores que la representación de enteros usando 32bits, había estado usando el tipo float para filtrar rápidamente esos valores, digamos:

$statusid = isset($_GET['statusid']) ? (float)$_GET['statusid'] : 0;

Con float se pueden tener hasta 14 dígitos sin pasar a la representación exponencial (1.xxxxxxE+NN), y como Twitter solo estaba generando ID’s de 11 dígitos, me pareció que había bastante tiempo antes de que se pasara el límite de float. Además me sirve que tenga la representación como número para comparaciones ($lastid > $firstdbid ) o para hacer cálculos sobre estos (etags).

Ahora que Twitter anda generando los ID’s con Snowflake (más información al respecto en el blog de Twitter), cada ID requiere 15 dígitos o más (53bits por ahora).

Esto no es problema si se tiene PHP a 64bits, ya que se pueden tener hasta 19 dígitos antes de alcanzar el límite. Si se tiene PHP compilado a 32bits, no queda de otra más que usar strings. Un forma rápida para filtrar los IDs tanto para 64bit y 32bit sería:

function safelongint($strint)
{
	$intval = intval($strint);
	if ( $intval == PHP_INT_MAX)
	{
		return preg_replace('/[^0-9]/','',$strint); //eliminar cualquier cosa que no sea dígito
	}
		
	return $intval;
}

Aún si los ID’s pasan el límite de los 64bits, debería usar strings automáticamente. Ya solo queda que la base de datos no tenga problemas con números tan grandes 😉 .

Actualización: Al 10 de Noviembre, Twitter ya llegó a los 17 dígitos en los ID’s… a ese paso el límite de los 64bit durará menos de lo que imaginé.

8 thoughts on “Twitter, Snowflake, Números grandes y PHP”

  1. No conozco de Twitter y sus ID, pero tus sistemas tienen un error de diseño si dependen de la representación que twitter pone a su ID.
    Tenés que reificar eso y en todo tu sistema la ID de Twitter debería ser una variable de tipo IdTwitter.
    Saludos!

  2. Hola, espero que no te moleste esto, pero quería comentarte que hasta el próximo 19 de Marzo de 2011 se encuentra abierto el plazo para la inscripción en el “II VillaBlog – Comarca de Doñana”.

    El “II VillaBlog – Comarca de Doñana” es un encuentro de Blogs que se celebrará el próximo día 9 de Abril en Rociana del Condado (Huelva) al que asistirán personas interesadas en el mundo de los Blogs (tengan o no tengan uno) y que servirá para formarse, dar a conocer tu blog y pasar una jornada de convivencia en un enclave rural con actividades muy variadas. Puedes encontrar más información e inscribirte en Villablog.es

    No te lo pierdas!!

  3. Great post and straight to the point. I don’t know if this is actually the best place to ask but do you people have any ideea where to employ some professional writers? Thanks in advance 🙂

  4. Generally speaking, if only need the prototyping to measure the size of the product, there is no need to
    spray oil, then can save time to do exterior treatment,
    certainly can save cost.
    If need to make a lot of every product, then we suggest produce it by vacuum casting, the cost is lower
    than CNC processing.
    If the structure of the product is complex, hard to use CNC processing, and hard to control the size, we
    suggest adopt SLA. But the cost is much higher than CNC
    processing and vacuum casting.
    In a word, adopt different processing method according to your requirements, we can help you save a lot of
    cost.
    You need to provide IGS,PART,STP form 3D picture if need us to make prototyping, the best is the IGS form 3D
    picture. Also related requirements of
    after-treatment., such as color, surface effect,etc.Please login http://www.prototypingchina.net to know more about
    this .

  5. Some brands will be down jacket moncler jacken designed jacket style, with waist, embossed, big lapels, such as design, you can also highlight the graceful posture. Moncler jackets outlet season down jacket with tall stature and gentle man’s shoulder curves and are made with the design fit without exaggeration,more cut physique paste, moncler daunenjacke hit a short paragraph style, moncler wien it looks fine without a bloated , the simple luxury of air in the end.

  6. I like how this article is written. Your points are sound, original, fresh and interesting. This information has been made so clear there’s no way to misunderstand it. Thank you.

Comments are closed.