Browse Source

add to local

stanley-king 8 years ago
parent
commit
7993b90e7f

+ 0 - 2
alter.sql

@@ -1,2 +0,0 @@
-alter table 33hao.33hao_goods_common add `goods_mobile_name` varchar(50);
-alter table 33hao.33hao_goods add `goods_mobile_name` varchar(50);    

+ 1 - 3
fcgi_run.php

@@ -10,6 +10,4 @@ require_once (BASE_ROOT_PATH . '/helper/img_helper.php');
 require_once (BASE_ROOT_PATH . '/helper/exceptionex.php');
 require_once (BASE_ROOT_PATH . '/helper/fcgi_server.php');
 
-fcgi_server::instance()->run_looper();
-
-?>
+fcgi_server::instance()->run_looper();

+ 1 - 3
fooder.php

@@ -38,6 +38,4 @@ require_once(BASE_ROOT_PATH . '/helper/sensitive_word/dfa.php');
 require_once(BASE_ROOT_PATH . '/helper/request_helper.php');
 require_once(BASE_ROOT_PATH . '/helper/configure.php');
 require_once(BASE_ROOT_PATH . '/helper/pay_helper.php');
-require_once(BASE_ROOT_PATH . '/helper/model_helper.php');
-
-?>
+require_once(BASE_ROOT_PATH . '/helper/model_helper.php');

+ 101 - 0
helper/algorithm.php

@@ -0,0 +1,101 @@
+<?php
+
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 16/6/24
+ * Time: 上午1:18
+ */
+function less($x,$y)
+{
+    return $x < $y;
+}
+
+class algorithm
+{
+    static function bsearch($x, $list)
+    {
+        $left = 0;
+        $right = count($list)-1;
+
+        while ($left <= $right) {
+            $mid = ($left + $right) >> 1;
+
+            if ($list[$mid] == $x) {
+                return $mid;
+            } elseif ($list[$mid] > $x) {
+                $right = $mid - 1;
+            } elseif ($list[$mid] < $x) {
+                $left = $mid + 1;
+            }
+        }
+
+        return -1;
+    }
+
+    static function binary_search($sortarr, $val, $compare = "less")
+    {
+        $last = count($sortarr);
+        $first = self::lower_bonud($sortarr,$val,$compare);
+        return $first != $last && !$compare($val, $sortarr[$first]);
+    }
+
+    //返回位置标记了一个 >= value 的值。
+    static function lower_bonud($container, $val,$compare = "less")
+    {
+        if(!is_array($container)) {
+            return -1;
+        }
+
+        $len = count($container);
+        $first = 0;
+        while ($len != 0)
+        {
+            $l2 = intval($len / 2);
+            $m = $first + $l2;
+            if ($compare($container[$m],$val))
+            {
+                $first = ++$m;
+                $len -= $l2 + 1;
+            }
+            else {
+                $len = $l2;
+            }
+        }
+        return $first;
+
+    }
+
+    //返回位置标记了一个 > value 的值。
+    static function upper_bound($container, $val,$compare = "less")
+    {
+        if (!is_array($container)) {
+            return -1;
+        }
+
+        $len = count($container);
+        $first = 0;
+        while ($len != 0)
+        {
+            $l2 = intval($len / 2);
+            $m = $first + $l2;
+            if ($compare($val,$container[$m])) {
+                $len = $l2;
+            } else {
+                $first = ++$m;
+                $len -= $l2 + 1;
+            }
+        }
+
+        return $first;
+    }
+
+    static function array_insert(&$arr,$pos,$val)
+    {
+        array_splice($arr,$pos,0,$val);
+    }
+    static function array_erase(&$arr,$pos)
+    {
+        return array_splice($arr,$pos,1);
+    }
+}

+ 0 - 30
helper/algorithm_helper.php

@@ -1,30 +0,0 @@
-<?php
-
-/**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 16/6/24
- * Time: 上午1:18
- */
-class algorithm_helper
-{
-    static function bsearch($x, $list)
-    {
-        $left = 0;
-        $right = count($list)-1;
-
-        while ($left <= $right) {
-            $mid = ($left + $right) >> 1;
-
-            if ($list[$mid] == $x) {
-                return $mid;
-            } elseif ($list[$mid] > $x) {
-                $right = $mid - 1;
-            } elseif ($list[$mid] < $x) {
-                $left = $mid + 1;
-            }
-        }
-
-        return -1;
-    }
-}

+ 128 - 100
helper/relation/mem_relation.php

@@ -10,7 +10,7 @@ namespace relation;
 
 use \Exception;
 use \session_helper;
-use \algorithm_helper;
+use \algorithm;
 use \errcode;
 use \member_info;
 use \Log;
@@ -25,12 +25,13 @@ use \Log;
 
 class mem_relation
 {
+    //对应数据库表中字段
     private $member_id;
     private $member_mobile;
     private $contact_name;
-    private $subscriber;
+    private $subscriber; //关注我的
     private $new_subscriber;
-    private $follower;
+    private $follower;   //我关注的
     private $build_mobiles;
     private $unbuild_mobiles;
 
@@ -50,7 +51,7 @@ class mem_relation
         $this->mod_relation = Model('member_relation');
         $member_id = intval($member_id);
 
-        if(isset($member_id) && intval($member_id) > 0)
+        if(isset($member_id) && $member_id > 0)
         {
             $member_info = new member_info($member_id);
             if(!empty($member_info))
@@ -95,7 +96,7 @@ class mem_relation
             if($mobile == false) {
                 throw new Exception("member_relation __construct error",errcode::ErrParamter);
             }
-
+            $mobile = intval($mobile);
             $info = $this->mod_relation->findByMobile($mobile);
             if(!empty($info)) {
                 $this->init($info);
@@ -123,56 +124,56 @@ class mem_relation
 
     public function __destruct()
     {
-        Log::record(__METHOD__,Log::DEBUG);
         if($this->dirty) {
             $this->save();
         }
     }
 
+    //用户($member_id) 关注 我($this)
     private function subscribe_me($member_id)
     {
-        $ret = algorithm_helper::bsearch($member_id,$this->subscriber);
-        if($ret == -1) {
-            array_push($this->subscriber,$member_id);
-            sort($this->subscriber);
-            $this->dirty = true;
+        if(algorithm::binary_search($this->subscriber,$member_id)) {
+            return;
         }
 
-        $ret = algorithm_helper::bsearch($member_id,$this->new_subscriber);
-        if($ret == -1) {
-            array_push($this->new_subscriber,$member_id);
-            sort($this->new_subscriber);
+        if(algorithm::binary_search($this->new_subscriber,$member_id) == false) {
+            $pos = algorithm::lower_bonud($this->new_subscriber,$member_id);
+            algorithm::array_insert($this->new_subscriber,$pos,$member_id);
             $this->dirty = true;
         }
     }
+    //用户($member_id) 取消关注 我($this)
     private function unsubscribe_me($member_id)
     {
-        $pos = algorithm_helper::bsearch($member_id,$this->subscriber);
-        if($pos != -1) {
-            array_splice($this->subscriber,$pos,1);
+        if(algorithm::binary_search($this->subscriber,$member_id)) {
+            $pos = algorithm::lower_bonud($this->subscriber,$member_id);
+            algorithm::array_erase($this->subscriber,$pos);
             $this->dirty = true;
+            return;
         }
 
-        $pos = algorithm_helper::bsearch($member_id,$this->new_subscriber);
-        if($pos != -1) {
-            array_splice($this->new_subscriber,$pos,1);
+        if(algorithm::binary_search($this->new_subscriber,$member_id)) {
+            $pos = algorithm::lower_bonud($this->new_subscriber,$member_id);
+            algorithm::array_erase($this->new_subscriber,$pos);
             $this->dirty = true;
         }
     }
 
-    private function doUnSubscribe($member_id,$mobile)
+    private function doUnSubscribe($member_id)
     {
-        $someone = new mem_relation($member_id, $mobile);
+        $someone = new mem_relation($member_id, null);
         $someone->unsubscribe_me($this->member_id);
         $someone->save();
         return $someone->member_id;
     }
 
-    private function doSubscribe($member_id,$mobile)
+    private function doSubscribe($member_id,&$mobile)
     {
         $someone = new mem_relation($member_id, $mobile);
         $someone->subscribe_me($this->member_id);
         $someone->save();
+        $mobile = intval($someone->member_mobile);
+
         return $someone->member_id;
     }
     public function member_id()
@@ -196,23 +197,32 @@ class mem_relation
     public function unfollower() {
         return $this->unbuild_mobiles;
     }
-    public function subscribe($member_id,$mobile = null)
+    //关注某人
+    public function subscribe($member_id)
     {
         if(!isset($member_id) || intval($member_id) == 0 || $this->member_id == $member_id) {
             return false;
         }
 
-        $someone_id = $this->doSubscribe($member_id,$mobile);
-        if($someone_id > 0) {
-            $this->add_fllower($someone_id);
-            $this->remove_unbuild($mobile);
-            $this->add_build($mobile);
-        } else {
-            $this->add_unbuild($mobile);
+        try
+        {
+            $member_id = intval($member_id);
+            $someone_id = $this->doSubscribe($member_id,$some_mobile);
+            if($someone_id > 0) {
+                $this->add_fllower($someone_id);
+                $this->remove_unbuild($some_mobile);
+                $this->add_build($some_mobile);
+            } else {
+                $this->add_unbuild($some_mobile);
+            }
+            $this->save();
+            return true;
+        }
+        catch (Exception $ex) {
+            Log::record($ex->getMessage(),Log::ERR);
+            return false;
         }
-        $this->save();
 
-        return true;
     }
     public function unsubscribe($member_id)
     {
@@ -220,11 +230,17 @@ class mem_relation
             return false;
         }
 
-        $this->doUnSubscribe($member_id);
-        $this->remove_fllower($member_id);
-        $this->save();
-
-        return true;
+        try
+        {
+            $member_id = intval($member_id);
+            $this->doUnSubscribe($member_id);
+            $this->remove_fllower($member_id);
+            $this->save();
+            return true;
+        } catch (Exception $ex) {
+            Log::record($ex->getMessage(),Log::ERR);
+            return false;
+        }
     }
 
     public function pass_subscribe()
@@ -238,11 +254,19 @@ class mem_relation
         
         foreach ($this->new_subscriber as $someone_id)
         {
-            $someone = new mem_relation($someone_id);
-            $someone->add_fllower($this->member_id);
-            $someone->remove_unbuild($this->member_mobile);
-            $someone->add_build($this->member_mobile);
-            $someone->save();
+            try
+            {
+                $someone = new mem_relation($someone_id);
+                $someone->add_fllower($this->member_id);
+                if(!empty($this->member_mobile)) {
+                    $mobile = intval($this->member_mobile);
+                    $someone->remove_unbuild($mobile);
+                    $someone->add_build($mobile);
+                }
+                $someone->save();
+            } catch (Exception $ex) {
+                Log::record($ex->getMessage(),Log::ERR);
+            }
         }
 
         $this->new_subscriber = array();
@@ -255,24 +279,17 @@ class mem_relation
     {
         if($this->member_id == $other_id) {
             return false;
-        }
-        else
-        {
-            $pos = algorithm_helper::bsearch($other_id,$this->follower);
-            if($pos == -1) {
-                return false;
-            } else {
-                return true;
-            }
+        } else {
+            return algorithm::binary_search($this->follower,$other_id);
         }
     }
 
     private function subscribed($mobile)
     {
-        if(algorithm_helper::bsearch($mobile,$this->build_mobiles) != -1) {
+        if(algorithm::binary_search($this->build_mobiles,$mobile)) {
             return true;
         }
-        if(algorithm_helper::bsearch($mobile,$this->unbuild_mobiles) != -1) {
+        if(algorithm::binary_search($this->unbuild_mobiles,$mobile)) {
             return true;
         }
         return false;
@@ -280,46 +297,44 @@ class mem_relation
 
     private function add_fllower($someone_id)
     {
-        if(algorithm_helper::bsearch($someone_id,$this->follower) == -1) {
-            array_push($this->follower,$someone_id);
-            sort($this->follower);
+        if(algorithm::binary_search($this->follower,$someone_id) == false) {
+            $pos = algorithm::lower_bonud($this->follower,$someone_id);
+            algorithm::array_insert($this->follower,$pos,$someone_id);
             $this->dirty = true;
         }
     }
     private function remove_fllower($someone_id)
     {
-        $pos = algorithm_helper::bsearch($someone_id,$this->follower);
-        if($pos != -1) {
-            array_splice($this->follower,$pos,1);
+        if(algorithm::binary_search($this->follower,$someone_id)) {
+            $pos = algorithm::lower_bonud($this->follower,$someone_id);
+            algorithm::array_erase($this->follower,$pos);
             $this->dirty = true;
         }
     }
 
     private function add_unbuild($mobile)
     {
-        $pos = algorithm_helper::bsearch($mobile,$this->unbuild_mobiles);
-        if($pos == -1) {
-            array_push($this->unbuild_mobiles,$mobile);
-            sort($this->unbuild_mobiles);
+        if(algorithm::binary_search($this->unbuild_mobiles,$mobile) == false) {
+            $pos = algorithm::lower_bonud($this->unbuild_mobiles,$mobile);
+            algorithm::array_insert($this->unbuild_mobiles,$pos,$mobile);
             $this->dirty = true;
         }
     }
 
     private function remove_unbuild($mobile)
     {
-        $pos = algorithm_helper::bsearch($mobile,$this->unbuild_mobiles);
-        if($pos != -1) {
-            array_splice($this->unbuild_mobiles,$pos,1);
+        if(algorithm::binary_search($this->unbuild_mobiles,$mobile)) {
+            $pos = algorithm::lower_bonud($this->unbuild_mobiles,$mobile);
+            algorithm::array_erase($this->unbuild_mobiles,$pos);
             $this->dirty = true;
         }
     }
 
     private function add_build($mobile)
     {
-        $ret = algorithm_helper::bsearch($mobile,$this->build_mobiles);
-        if($ret == -1) {
-            array_push($this->build_mobiles,$mobile);
-            sort($this->build_mobiles);
+        if(algorithm::binary_search($this->build_mobiles,$mobile) == false) {
+            $pos = algorithm::lower_bonud($this->build_mobiles,$mobile);
+            algorithm::array_insert($this->build_mobiles,$pos,$mobile);
             $this->dirty = true;
         }
     }
@@ -328,7 +343,8 @@ class mem_relation
     {
         foreach ($contacts as $mobile)
         {
-            if($this->member_mobile == $mobile) { //自己不能订阅自己
+            $mobile = intval($mobile);
+            if($this->member_mobile == $mobile || $mobile < 100000000000) { //自己不能订阅自己
                 continue;
             }
 
@@ -348,6 +364,45 @@ class mem_relation
         $this->save();
     }
 
+    private function init($info)
+    {
+        $this->member_id = intval($info['member_id']);
+        $this->member_mobile = $info['member_mobile'];
+        $this->contact_name = $info['contact_name'];
+        $this->subscriber = unserialize($info['subscriber']);
+        $this->new_subscriber = unserialize($info['new_subscriber']);
+        $this->follower = unserialize($info['follower']);
+        $this->build_mobiles = unserialize($info['build_mobiles']);
+        $this->unbuild_mobiles = unserialize($info['unbuild_mobiles']);
+    }
+
+    private function mobile_int($mobiles)
+    {
+        foreach ($mobiles as $mobile)
+        {
+            $val = intval($mobile);
+            if($val > 10000000000) {
+                $ret[] = $val;
+            }
+        }
+        if(isset($ret)) {
+            return $ret;
+        } else {
+            return array();
+        }
+    }
+
+    public function sort_mobile()
+    {
+        $this->mobile_int($this->build_mobiles);
+        sort($this->build_mobiles);
+
+        $this->mobile_int($this->unbuild_mobiles);
+        sort($this->unbuild_mobiles);
+
+        $this->dirty = true;
+    }
+
     private function save()
     {
         if($this->dirty) {
@@ -365,31 +420,4 @@ class mem_relation
             $this->dirty = false;
         }
     }
-
-    private function from_text($text)
-    {
-        if(is_null($text) || empty($text)) {
-            return array();
-        } else {
-            return unserialize($text);
-        }
-    }
-
-    private function init($info)
-    {
-        $this->member_id = intval($info['member_id']);
-        $this->member_mobile = $info['member_mobile'];
-        $this->contact_name = $info['contact_name'];
-        $this->subscriber = $this->from_text($info['subscriber']);
-        $this->new_subscriber = $this->from_text($info['new_subscriber']);
-        $this->follower = $this->from_text($info['follower']);
-        $this->build_mobiles = $this->from_text($info['build_mobiles']);
-        $this->unbuild_mobiles = $this->from_text($info['unbuild_mobiles']);
-
-        sort($this->subscriber);
-        sort($this->new_subscriber);
-        sort($this->follower);
-        sort($this->build_mobiles);
-        sort($this->unbuild_mobiles);
-    }
 }

+ 1 - 1
helper/relation_helper.php

@@ -8,7 +8,7 @@
  */
 
 require_once (BASE_ROOT_PATH . '/helper/relation/mem_relation.php');
-require_once (BASE_ROOT_PATH . '/helper/algorithm_helper.php');
+require_once (BASE_ROOT_PATH . '/helper/algorithm.php');
 
 class relation_helper
 {

+ 2 - 2
helper/session_helper.php

@@ -6,7 +6,7 @@
  * Time: 下午6:46
  */
 
-require_once (BASE_ROOT_PATH . '/helper/algorithm_helper.php');
+require_once (BASE_ROOT_PATH . '/helper/algorithm.php');
 require_once (BASE_ROOT_PATH . '/helper/relation_helper.php');
 require_once (BASE_ROOT_PATH . '/helper/account_helper.php');
 
@@ -213,7 +213,7 @@ class session_helper
             if($mobile == false) {
                 continue;
             }
-            if(algorithm_helper::bsearch($mobile,$ar_contact) != -1) {
+            if(algorithm::bsearch($mobile,$ar_contact) != -1) {
                 continue;
             }
 

+ 2 - 2
helper/shaker_helper.php

@@ -351,7 +351,7 @@ class shaker_helper
         sort($all_friends);
         foreach ($exids as $uid)
         {
-            $pos = algorithm_helper::bsearch($uid,$all_friends);
+            $pos = algorithm::bsearch($uid,$all_friends);
             if($pos != -1) {
                 array_splice($all_friends,$pos,1);
             }
@@ -385,7 +385,7 @@ class shaker_helper
 
         foreach ($exids as $uid)
         {
-            $pos = algorithm_helper::bsearch($uid,$all_friends);
+            $pos = algorithm::bsearch($uid,$all_friends);
             if($pos != -1) {
                 array_splice($all_friends,$pos,1);
             }

File diff suppressed because it is too large
+ 338 - 0
mobile/control/admin_oper.php


File diff suppressed because it is too large
+ 0 - 328
mobile/control/test.php


+ 2 - 2
test/algorithm_helperTest.php

@@ -9,7 +9,7 @@
 
 define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));
 
-require_once(BASE_ROOT_PATH . '/helper/algorithm_helper.php');
+require_once(BASE_ROOT_PATH . '/helper/algorithm.php');
 
 class algorithm_helperTest extends PHPUnit_Framework_TestCase
 {
@@ -17,7 +17,7 @@ class algorithm_helperTest extends PHPUnit_Framework_TestCase
     {
         $mobiles = array('13911129897','13951129867','13916129867','13918129867','13911179867','13911109867');
         sort($mobiles);
-        $ret = algorithm_helper::bsearch('13911109867',$mobiles);
+        $ret = algorithm::bsearch('13911109867',$mobiles);
         
         $ret = $mobiles[$ret];
         $x = $mobiles[0];

+ 0 - 1
test/category_helperTest.php

@@ -26,7 +26,6 @@ class category_helperTest extends PHPUnit_Framework_TestCase
     public function testSearch()
     {
         $cats = category_helper::instance()->categories();
-        
     }
 
     public static function tearDownAfterClass()

+ 98 - 0
test/relation_helperTest.php

@@ -11,6 +11,8 @@ define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));
 
 require_once(BASE_ROOT_PATH . '/fooder.php');
 require_once(BASE_ROOT_PATH . '/helper/relation_helper.php');
+require_once(BASE_ROOT_PATH . '/helper/performance_helper.php');
+require_once(BASE_ROOT_PATH . '/helper/algorithm.php');
 
 class relation_helperTest extends PHPUnit_Framework_TestCase
 {
@@ -80,4 +82,100 @@ class relation_helperTest extends PHPUnit_Framework_TestCase
     {
         $title = strftime("熊猫美妆%m月%d日红包风云榜",1469030400);
     }
+
+    public function testSort()
+    {
+        $mod = Model('member_relation');
+        $item = $mod->field('*')->where(array('member_mobile' => '13911129867'))->find();
+        $mobiles = unserialize($item['unbuild_mobiles']);
+
+//        perfor_start();
+//        for($i = 0; $i < 10000; ++$i) {
+//            sort($mobiles);
+//        }
+//        perfor_end("testSort","string sort");
+
+        foreach ($mobiles as $mobile) {
+            $iMobiles[] = intval($mobile);
+        }
+
+        perfor_start();
+        for($i = 0; $i < 10000; ++$i) {
+            sort($iMobiles);
+        }
+        perfor_end("testSort","int sort");
+
+
+        $len = count($iMobiles);
+        perfor_start();
+        for($i = 0; $i < 10000; ++$i) {
+            $pos = $i % $len;
+            $val = $iMobiles[$pos];
+            //algorithm::array_erase($iMobiles,$pos);
+            $pos = algorithm::lower_bonud($iMobiles,$val);
+            //algorithm::array_insert($iMobiles,$pos,$val);
+        }
+        perfor_end("testSort","int sort");
+
+        performance_helper::instance()->format_log();
+    }
+
+    public function testArrayInsert()
+    {
+        $x = array(1,3,5,9,10);
+        $pos = algorithm::lower_bonud($x,3);
+        $posx = algorithm::upper_bound($x,3);
+
+        $y = algorithm::array_insert($x,$pos,0);
+        $pos = algorithm::lower_bonud($x,4);
+        $posx = algorithm::upper_bound($x,4);
+
+        //$find = algorithm_helper::binary_search($x,0);
+    }
+
+    public function testLowerbound()
+    {
+        $x = array(1,3,5,9,10);
+        $find = algorithm::binary_search($x,0);
+        $find = algorithm::binary_search($x,1);
+        $pos = algorithm::lower_bonud($x,0);
+        $pos = algorithm::lower_bonud($x,2);
+        $pos = algorithm::lower_bonud($x,3);
+        $pos = algorithm::lower_bonud($x,14);
+    }
+
+    public function testUpper()
+    {
+        $x = array(1,3,5,9,10);
+        $pos = algorithm::upper_bound($x,0);
+        $pos = algorithm::upper_bound($x,2);
+        $pos = algorithm::upper_bound($x,3);
+        $pos = algorithm::upper_bound($x,14);
+    }
+    public function testRelation()
+    {
+        perfor_start();
+        for($i = 0; $i < 10000; ++$i) {
+            $mod = Model('member_relation');
+            $mod->findByMobile('13911129867');
+        }
+        perfor_end("testRelation","10000 findByMobile");
+
+        $mod = Model('member_relation');
+        $item = $mod->findByMobile('13911129867');
+
+        perfor_start();
+        for($i = 0; $i < 10000; ++$i) {
+            unserialize($item['unbuild_mobiles']);
+        }
+        perfor_end("testRelation","10000 unserialize");
+
+        perfor_start();
+        for($i = 0; $i < 10000; ++$i) {
+            $relation = new \relation\mem_relation(36490);
+            //$relation->pass_subscribe();
+        }
+        perfor_end("testRelation","10000 pass_subscribe");
+        performance_helper::instance()->format_log();
+    }
 }

+ 9 - 0
test/systemTest.php

@@ -32,4 +32,13 @@ class systemTest extends PHPUnit_Framework_TestCase
 
         $txt = json_encode($areas,JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
     }
+    public function testMobile()
+    {
+        $x = intval('13911129867');
+
+        if($x == '13911129867') {
+            echo "==";
+        }
+    }
+
 }