decodeCMemDump_v2.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!php.exe -q
  2. <?php
  3. /**
  4. * 解码CMEM的dump文件, 输出为key作为文件名的txt文件,value作为其文本内容.
  5. * @author gwang email:wanggangzero@qq.com
  6. * @copyright ? 2015-6-8, SJZ LoyalSoft Corporation & gwang. All rights reserved.
  7. * @version 2017.04.27 更新了解析规则, 对方发来的数据带有了4个字节的类型信息如果是0则没有压缩,2采用压缩.
  8. * 这比原来的数据友好多了, 全部数据完美解析.
  9. * 2015-6-8 created
  10. * @description
  11. * CMEM有两中模式二进制和字符串.一般都用字符串模式,概因效率相差不多.
  12. * 本脚本内解码算法亦以字符串模式为基础.
  13. * 原理: php写入Memcache的字符串对象(c格式的即字节数组)将会被转换为hex字符串
  14. * 再写入memcache, memcache的压缩标志 MEMCACHE_COMPRESSED 指定使用zlib压缩.
  15. * 如果自己的cmem库中使用了压缩,那么需要在将hex还原为字符串后再解压缩一遍还原为
  16. * 原来的数组.
  17. * addition: 即便使用了 MEMCACHE_COMPRESSED 标志位, memcache规定,
  18. * 压缩比大于0.8的不采用压缩value
  19. * 将不被压缩. 因此,下文代码中针对这一特殊规定做了处理.
  20. */
  21. # 导入主代码库
  22. include_once __DIR__ . '/../../main.php';
  23. # 设置时区为中国区(东8区)
  24. date_default_timezone_set("PRC");
  25. set_time_limit(0); # cli 不限定执行时间
  26. //
  27. // <editor-fold defaultstate="collapsed" desc=" hack for hex2bin">
  28. if (!function_exists('hex2bin')) { // first include in php 5.4
  29. function hex2bin($str) {
  30. $sbin = "";
  31. $len = strlen($str);
  32. for ($i = 0; $i < $len; $i += 2) {
  33. $sbin .= pack("H*", substr($str, $i, 2));
  34. }
  35. return $sbin;
  36. }
  37. }
  38. // </editor-fold>
  39. //
  40. /**
  41. * 全局
  42. * @staticvar CRedisUtil $gRedis
  43. * @return \CRedisUtil
  44. */
  45. function gRedis() {
  46. static $gRedis;
  47. if ($gRedis == null) {
  48. $gRedis = new CRedisUtil();
  49. $host = '192.168.10.51';
  50. $port = '6666';
  51. $pwd = 'wanggang1985';
  52. $gRedis->conn($host, $port, $pwd);
  53. }
  54. return $gRedis;
  55. }
  56. function output($key, $value) {
  57. if (strpos($key, '-info') !== FALSE) { // userinfo
  58. $userinfo = JsonUtil::decode($value);
  59. if (property_exists($userinfo, 'user') //
  60. && property_exists($userinfo->user, 'ts') //
  61. && (day() - day($userinfo->user->ts) > 60)) { // 距上次登录60天以上了
  62. // uid, zoneid, tsday, info(bin2hex(gzcompress())
  63. $uid = $userinfo->user->oId;
  64. $zoneid = $userinfo->zoneid;
  65. $tsday = day($userinfo->user->ts);
  66. $info = bin2hex(gzdeflate($value));
  67. $SQL = sprintf("call mgodpay.insertUserInfo('%s',%d,%d,'%s');", $uid, $zoneid, $tsday, $info);
  68. daoInst()->exec($SQL);
  69. echo $key . "\r\n";
  70. return;
  71. }
  72. }
  73. $redis = gRedis();
  74. $redis->set($key, $value);
  75. // $handle = fopen(__DIR__ . "/datas4/" . "$key.txt", "w");
  76. // fwrite($handle, $value);
  77. // fclose($handle);
  78. }
  79. if ($argc <= 1 # 未传参数的情况下
  80. || $argv[1] == "-h" || $argv[1] == "\\h") { # 或者是输入\h -h 查询帮助
  81. echo " usage: php.exe $argv[0] dumpfilename \n";
  82. echo " output: a txt file that use key as filename,value as contents.\n";
  83. echo "\t(attension: to avoid too many output in console, filter is suggested.)\n";
  84. } else {
  85. $n = 1;
  86. $filename = $argv[1];
  87. $zoneid = intval(str_replace('cmem_zone', '', $filename));
  88. if (!file_exists($filename)) {
  89. echo " file $filename not exist!";
  90. } else {
  91. $handle = fopen($filename, "rb"); # 打开文件
  92. if ($handle) {
  93. while (( $line = fgets($handle)) !== false) {
  94. $strarr = explode(" ", $line);
  95. $key = hex2bin($strarr[0]);
  96. $sec = $strarr[1];
  97. $type = (int) substr($sec, 0, 8);
  98. $value = hex2bin(substr($sec, 8));
  99. // echo "type: $type\n\ndecoded value: \n\n$value\n\n";
  100. if ($type == 2) {
  101. $value = gzuncompress($value);
  102. // echo "\n\ndecoded value: \n\n$value\n\n";
  103. }
  104. // echo "\"". $strarr[0]."\":".$value ."\n";
  105. output("$key-zone$zoneid", $value);
  106. if ($n++ % 50 == 0) {
  107. echo "dealed $n records!\n";
  108. // break;
  109. }
  110. }
  111. if (!feof($handle)) {
  112. // echo "Error: unexpected fgets() fail\n" ;
  113. }
  114. fclose($handle);
  115. }
  116. }
  117. }