5 million UID white list PHP bit map processing

Source: Internet
Author: User
Tags fread
5 million UID white list of PHP Bitmap processing

Recently, some colleagues said there is a 5 million white list processing, to find similar cases on the Internet, said there is a bitmap solution,

Google again, Baidu has no relevance to the solution of PHP, so he wrote a:

?

 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 is Greater than-1 ');($num < 4294967296) or Die (' Nu Mber must is 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 1fseek ($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 0fseek ($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-& Gt;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 white list $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)); Find white list $b->del ($UID); Delete Whitelist Var_dump ($b->find ($uid)); Find White List

?

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

The results of the implementation are 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:


?

?

  • Related Article

    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.