PHPBit map processing for a 5 million uid white list

Source: Internet
Author: User
PHPBitmap processing for a 5-million uid white list recently, some colleagues said that there was a 5-million white list processing, and found similar cases on the Internet, that is, there was a bitmap solution, again google, baidu has no related PHP solution, so I wrote one myself :? & Lt ;? Php * 5 million uid whitelist-PHPBitmap processing * author: hushuilo 5 million uid whitelist-PHP Bitmap processing

Recently, some colleagues said that there was a 5 million white list processing service. they searched for similar cases on the Internet and said there was a bitmap solution,

Google again, baidu has no related PHP solution, so I wrote one myself:

?

 Handler = @ fopen ($ file, 'R + ') OR die ('open bitmap file failed'); else $ this-> handler = @ fopen ($ file, 'W + ') OR die ('open bitmap file failed'); $ this-> max = file_exists ($ file )? (Filesize ($ file) * 8-1): 0;} public function _ destruct () {@ fclose ($ this-> handler );} private function binary_dump ($ binary_data) {return sprintf ('% 08d', decbin (hexdec (bin2hex ($ binary_data);} private function num_check ($ num) {($ num>-1) OR die ('number must be greater than-1'); ($ num <4294967296) or die ('number must be less than 4294967296 '); // 2 ^ 32if ($ this-> max <$ num) {fseek ($ this-> handler, 0, SEEK_END); fwrite ($ this-> handler, str_repeat ("\ x00", ceil ($ num-$ this-> max)/8 ))); // fill with 0 $ this-> max = ceil ($ num/8) * 8-1;} public function set ($ num) {$ this-> num_check ($ num); fseek ($ this-> handler, floor ($ num/8), SEEK_SET ); $ bin = fread ($ this-> handler, 1) | pack ('C', 0x100> fmod ($ num, 8) + 1 ); // mark with 1 fseek ($ this-> handler, ftell ($ this-> handler)-1, SEEK_SET); // write a new bytefwrite ($ this-> Handler, $ bin); fflush ($ this-> handler);} public function del ($ num) {$ this-> num_check ($ num ); fseek ($ this-> handler, floor ($ num/8), SEEK_SET); $ bin = fread ($ this-> handler, 1 )&~ Pack ('C', 0x100> fmod ($ num, 8) + 1); // mark with 0 fseek ($ this-> handler, ftell ($ this-> handler)-1, SEEK_SET); // write a new bytefwrite ($ this-> handler, $ bin ); fflush ($ this-> handler);} public function find ($ num) {if (fseek ($ this-> handler, floor ($ num/8), SEEK_SET) =-1) return FALSE; $ bin = fread ($ this-> handler, 1); if ($ bin = FALSE | strlen ($ bin) = 0) return FALSE; $ bin = $ bin & pack ('C', 0x100> fmod ($ num, 8) + 1 ); if ($ bin = "\ x00") return FALSE; return TRUE ;}}$ B = new Bitmap ('/dev/shm/bitmapdata '); // set the whitelist $ B-> set (1); $ B-> set (3); $ B-> set (5 ); $ B-> set (7); $ B-> set (9); $ B-> set (501); $ uid = 501; var_dump ($ B-> find ($ uid); // search for a whitelist $ B-> del ($ uid ); // delete the white list var_dump ($ B-> find ($ uid); // find the white list

?

Where "$ B = new Bitmap ('/dev/shm/bitmapdata ');"? Put the file in the memory to increase the read/write speed

The execution result is as follows:

[Email protected]: ~ /Web/test/shm $/bin/sh./geany_run_script.sh
Bool (true)
Bool (false)

?

The generated bitmapdata file is in the attachment:


?

?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.