stanley-king il y a 8 ans
Parent
commit
454f14c79f

+ 10 - 0
centra_srv.php

@@ -0,0 +1,10 @@
+<?php
+
+define('BASE_ROOT_PATH',str_replace('\\','/',dirname(__FILE__)));
+
+require_once (BASE_ROOT_PATH . '/fooder.php');
+require_once (BASE_ROOT_PATH . '/helper/search/server.php');
+require_once (BASE_ROOT_PATH . '/helper/search/processor.php');
+
+search\CentraHelper::instance()->init(new search\processor());
+search\CentraHelper::instance()->run_loop();

+ 0 - 1
data/session/index.html

@@ -1 +0,0 @@
- 

+ 28 - 0
helper/algorithm.php

@@ -98,4 +98,32 @@ class algorithm
     {
         return array_splice($arr,$pos,1);
     }
+
+    static function intersection($ar1,$ar2)
+    {
+        $count1 = count($ar1);
+        $count2 = count($ar2);
+        $pos1 = 0;
+        $pos2 = 0;
+
+        $result = [];
+        while ($pos1 != $count1 && $pos2 != $count2)
+        {
+            $val1 = $ar1[$pos1];
+            $val2 = $ar2[$pos2];
+
+            if ($val1 < $val2) {
+                ++$pos1;
+            }
+            elseif($val1 == $val2) {
+                $result[] = $val1;
+                ++$pos1;
+                ++$pos2;
+            }
+            else {
+                ++$pos2;
+            }
+        }
+        return $result;
+    }
 }

+ 1 - 1
helper/goods/commonid_helper.php

@@ -31,7 +31,7 @@ class commonid_helper
 
         if($goods_id <= 0) return false;
         if(array_key_exists($goods_id,$this->mIdContainer)) {
-            return $this->mIdContainer;
+            return $this->mIdContainer[$goods_id];
         }
         else
         {

+ 29 - 0
helper/search/processor.php

@@ -0,0 +1,29 @@
+<?php
+
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 2016/10/6
+ * Time: 下午10:35
+ */
+namespace search;
+
+class processor implements IProcessor
+{
+    const GetRelatedWord = 1;
+    const SearchReasult  = 2;
+    public function handle_input($body)
+    {
+        $param = unserialize($body);
+        $type = $param['type'];
+        if($type = self::GetRelatedWord) {
+
+        }
+        else if($type == self::SearchReasult) {
+
+        }
+        else {
+
+        }
+    }
+}

+ 207 - 0
helper/search/searcher.php

@@ -0,0 +1,207 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 2016/10/7
+ * Time: 下午12:44
+ */
+
+namespace search;
+
+use Model;
+use algorithm;
+
+function mb_str_split( $string ) {
+    return preg_split('/(?<!^)(?!$)/u', $string );
+}
+class filter
+{
+    const special_character = array(' ','“','”','Ⅰ','、','。','「','」','【','】','!','&','(',')',',',':','’','┊',
+        '╭','╮','╯','╰','▔','▽',' ','《','》','の','*','8',';','?','°');
+
+    static public function is_character($word)
+    {
+        $ar = str_split($word);
+        return count($ar) == 1 ? true : false;
+    }
+    static public function filter($word)
+    {
+        if(empty($word)) {
+            return false;
+        }
+
+        if(self::is_character($word))
+        {
+            if(ctype_space($word)) {
+                return false;
+            }
+            elseif (ctype_graph($word)) {
+                return false;
+            }
+            elseif (ctype_cntrl($word)) {
+                return false;
+            }
+            else {
+                return false;
+            }
+        }
+        else
+        {
+            if(in_array($word,self::special_character)) {
+                return false;
+            }
+            else {
+                return true;
+            }
+        }
+    }
+}
+
+class words
+{
+    protected $mDict;
+    public function __construct()
+    {
+        $this->mDict = array();
+    }
+
+    protected function parase($words,$value)
+    {
+        foreach (mb_str_split($words) as $word)
+        {
+            if(filter::filter($word)) {
+                $this->add($word,$value);
+            }
+        }
+    }
+
+    protected function add($key,$value)
+    {
+        if(array_key_exists($key,$this->mDict))
+        {
+            $datas = &$this->mDict[$key];
+            if(algorithm::binary_search($datas,$value) == false) {
+                $pos = algorithm::lower_bonud($datas,$value);
+                algorithm::array_insert($datas,$pos,$value);
+            }
+        }
+        else {
+            $this->mDict[$key] = array($value);
+        }
+    }
+    public function find($key)
+    {
+        if(array_key_exists($key,$this->mDict)) {
+            return $this->mDict[$key];
+        } else {
+            return array();
+        }
+    }
+}
+
+class brand_dict extends words
+{
+    public function __construct()
+    {
+        parent::__construct();
+    }
+    public function init()
+    {
+
+    }
+}
+
+class category_dict extends words
+{
+    public function __construct()
+    {
+        parent::__construct();
+
+    }
+    public function init()
+    {
+    }
+}
+
+class goods_dict extends words
+{
+    const filter = "goods_commonid,goods_name,goods_jingle,goods_specname,brand_name";
+    public function __construct()
+    {
+        parent::__construct();
+
+    }
+    public function init()
+    {
+        $mod_common = Model("goods_common");
+        $items = $mod_common->field(self::filter)->limit(false)->select();
+        foreach ($items as $item)
+        {
+            $common_id = intval($item['goods_commonid']);
+
+            $this->parase($item['goods_name'],$common_id);
+            $this->parase($item['goods_jingle'],$common_id);
+            $this->parase($item['brand_name'],$common_id);
+        }
+    }
+}
+
+class searcher
+{
+    private $goods_dict;
+    private $brand_dict;
+    private $category_dict;
+
+    public function __construct()
+    {
+        $this->goods_dict = new goods_dict();
+        $this->goods_dict->init();
+
+        $this->brand_dict = new brand_dict();
+        $this->brand_dict->init();
+
+        $this->category_dict = new category_dict();
+        $this->category_dict->init();
+    }
+
+    private function get_goods($words)
+    {
+        if(empty($words)) {
+            return array();
+        }
+
+        $i = 0;
+        $result = [];
+        foreach ($words as $word)
+        {
+            $ids = $this->goods_dict->find($word);
+            if($i == 0) {
+                $result = $ids;
+            } else {
+                $result = algorithm::intersection($result,$ids);
+            }
+            ++$i;
+        }
+
+        return $result;
+    }
+
+    public function get_word($input)
+    {
+        $words = [];
+        foreach (mb_str_split($input) as $word)
+        {
+            if(filter::filter($word)) {
+                $words[] = $word;
+            }
+        }
+        $ids = $this->get_goods($words);
+
+        return $ids;
+    }
+
+    public function get_result($key)
+    {
+
+    }
+}

+ 146 - 0
helper/search/server.php

@@ -0,0 +1,146 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 2016/10/5
+ * Time: 下午11:39
+ */
+
+namespace search;
+
+function ev_accept($socket, $flag, $base)
+{
+    CentraHelper::instance()->ev_accept($socket,$flag,$base);
+}
+function ev_read($buffer, $id)
+{
+    CentraHelper::instance()->ev_read($buffer,$id);
+}
+
+function ev_error($buffer, $error, $id)
+{
+    CentraHelper::instance()->ev_error($buffer,$error,$id);
+}
+
+interface IProcessor
+{
+    public function handle_input($body);
+}
+
+
+class CentraHelper
+{
+    const body_len = 10;
+
+    private $socket;
+    private $ev_base;
+    private $ev;
+    private $connections;
+    private $buffers;
+    private $contents;
+    private $connect_id;
+
+    private $processor;
+    private static $stInstance;
+
+    public static function instance()
+    {
+        if(self::$stInstance == null) {
+            self::$stInstance = new CentraHelper();
+        }
+        return self::$stInstance;
+    }
+
+    private function __construct()
+    {
+        $this->connect_id = 0;
+        $this->connections = array();
+        $this->buffers = array();
+    }
+    public function init(IProcessor $processor)
+    {
+        $this->processor = $processor;
+    }
+
+    public function run_loop()
+    {
+        $this->socket = stream_socket_server ('tcp://0.0.0.0:2000', $errno, $errstr);
+        stream_set_blocking($this->socket, 0);
+        $this->ev_base = event_base_new();
+        $this->ev = event_new();
+        event_set($this->ev, $this->socket, EV_READ | EV_PERSIST, 'ev_accept', $this->ev_base);
+        event_base_set($this->ev, $this->ev_base);
+        event_add($this->ev);
+        event_base_loop($this->ev_base);
+    }
+
+    public function ev_accept($socket, $flag, $base)
+    {
+        $this->connect_id += 1;
+        $connection = stream_socket_accept($socket);
+        stream_set_blocking($connection, 0);
+        $buffer = event_buffer_new($connection, 'ev_read', NULL, 'ev_error', $this->connect_id);
+
+        event_buffer_base_set($buffer, $base);
+        event_buffer_timeout_set($buffer, 30, 30);
+        event_buffer_watermark_set($buffer, EV_READ, 0, 0xffffff);
+        event_buffer_priority_set($buffer, 10);
+        event_buffer_enable($buffer, EV_READ | EV_PERSIST);
+
+        // we need to save both buffer and connection outside
+        $this->connections[$this->connect_id] = $connection;
+        $this->buffers[$this->connect_id] = $buffer;
+        $this->contents[$this->connect_id] = "";
+    }
+
+    public function ev_read($buffer, $id)
+    {
+        $content = &$this->contents[$id];
+        while ($read = event_buffer_read($buffer, 256)) {
+            $content .= $read;
+        }
+
+        $start = 0;
+        $left = strlen($content);
+        do
+        {
+            if($left > self::body_len)
+            {
+                $len = substr($content,$start,self::body_len);
+                $len = intval($len);
+                if($left >= self::body_len + $len)
+                {
+                    $body = substr($content,$start + self::body_len,$len);
+                    if($this->processor != null) {
+                        $data = $this->processor->handle_input($body);
+                        $header = sprintf("%010d",strlen($data));
+                        $data = $header . $data;
+                        event_buffer_write($buffer,$data,strlen($data));
+                    }
+                    $start += self::body_len + $len;
+                    $left = $left - self::body_len - $len;
+                }
+                else {
+                    break;
+                }
+            }
+            else {
+                break;
+            }
+        } while ($left > 0);
+
+        if($start > 0) {
+            $content = substr($content,$start);
+        }
+    }
+
+    public function ev_error($buffer, $error, $id)
+    {
+        event_buffer_disable($this->buffers[$id], EV_READ | EV_WRITE);
+        event_buffer_free($this->buffers[$id]);
+        fclose($this->connections[$id]);
+        unset($this->connections[$id]);
+        unset($this->buffers[$id]);
+        unset($this->contents[$id]);
+    }
+}

+ 36 - 0
test/TestSearch.php

@@ -0,0 +1,36 @@
+<?php
+
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 2016/10/7
+ * Time: 下午1:03
+ */
+
+define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));
+require_once(BASE_ROOT_PATH . '/fooder.php');
+require_once(BASE_ROOT_PATH . '/helper/search/searcher.php');
+require_once(BASE_ROOT_PATH . '/helper/search/server.php');
+require_once(BASE_ROOT_PATH . '/helper/search/processor.php');
+
+
+class TestSearch extends PHPUnit_Framework_TestCase
+{
+    public static function setUpBeforeClass()
+    {
+        Base::run_util();
+    }
+
+    public function testInit()
+    {
+        $dict = new search\goods_dict();
+        $dict->init();
+    }
+
+    public function testSearcher()
+    {
+        $searcher = new search\searcher();
+        $result = $searcher->get_word('相宜本草红');
+    }
+
+}

+ 35 - 8
test/user_helperTest.php

@@ -9,24 +9,51 @@
 
 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/session_helper.php');
+//require_once(BASE_ROOT_PATH . '/fooder.php');
+//require_once(BASE_ROOT_PATH . '/helper/relation_helper.php');
+//require_once(BASE_ROOT_PATH . '/helper/session_helper.php');
 
 
 class user_helperTest extends PHPUnit_Framework_TestCase
 {
     public static function setUpBeforeClass()
     {
-        Base::run_util();
+//        Base::run_util();
     }
 
     public function testUpContacts()
     {
-        $datas = array('13911129867','13911129868','13911129869','13911129870','13911129870');
-        $jsData = json_encode($datas);
-        $jsData = urlencode($jsData);
-        session_helper::parse_contacts($jsData);
+//        $datas = array('13911129867','13911129868','13911129869','13911129870','13911129870');
+//        $jsData = json_encode($datas);
+//        $jsData = urlencode($jsData);
+//        session_helper::parse_contacts($jsData);
+    }
+
+    public function testSocket()
+    {
+        $socket = stream_socket_client ("tcp://192.168.1.105:2000");
+
+        $data = array('type' => 1,'keyword' => "美白");
+        $data = serialize($data);
+
+        $i = 0;
+        while ($i < 10000) {
+            $len = sprintf("%010d",strlen($data));
+            stream_socket_sendto($socket,$len . $data);
+            $i++;
+        }
+        stream_socket_shutdown($socket,STREAM_SHUT_RDWR);
+    }
+
+    public function testLen()
+    {
+        $x = sprintf("%010d",500);
+        $y = intval($x);
+
+        $read = "";
+        while ($read) {
+            echo "erad";
+        }
     }
 
 }