訪問模型一 最簡單的訪問伺服器

來源:互聯網
上載者:User

標籤:void   ifstream   memory   指標   operator   ini   node   釋放   data   

假設一個網站,最初開始壓力不大,只有幾千條或者幾萬條資料存放區,約幾百個查詢訪問

那麼一般就是一台裝置應對 資料輸入和查詢 (後繼更新代碼)

1

 

目前完成代碼 鏈表和hash函數

#include <iostream>#include <fstream>#include "MyLink.h"// redis hash functionunsigned int dictGenHashFunction(const  char *buf, int len) {    unsigned int hash = 5381;    while (len--)        hash = ((hash << 5) + hash) + (*buf++);    return hash;}int main(){    MyLink bucket[7];    std::ifstream infstream("OutData.dat");    std::string key, value, tmp;    while (std::getline(infstream, key) &&        std::getline(infstream, value) &&        std::getline(infstream, tmp)) {        unsigned int hashNum = dictGenHashFunction(key.c_str(), key.size());        bucket[hashNum % 7].AppendNode(key, value);    }    for (int i = 0; i < 7; i++) {        std::cout << "bucket[" << i << "] has " << bucket[i].GetNodeCount() << " nodes" << std::endl;    }    return 0;}
#pragma once#include <memory>#include <string>class MyLink {private:struct LinkNode {std::string key_;std::string value_;std::shared_ptr<LinkNode> next_;std::shared_ptr<LinkNode> prev_;};public:MyLink() :head_(nullptr), tail_(nullptr), nodeCount_(0){}MyLink(std::string key,std::string value){head_ = tail_ = InitNode(key,value);nodeCount_ = 1;}~MyLink() {if (head_ == nullptr) {return;}while (head_ != nullptr) {std::shared_ptr<LinkNode> p = head_;if (p->next_ == nullptr) {head_ = tail_ = nullptr;nodeCount_--;break;}head_ = p->next_;p->next_ = nullptr;head_->prev_ = nullptr;nodeCount_--;}}size_t GetNodeCount() {return nodeCount_;}void PrintNode() {std::shared_ptr<LinkNode> p = head_;for (; p != nullptr; p = p->next_) {std::cout << p->key_ << " " << p->value_ << std::endl;}std::cout << std::endl;}std::shared_ptr<LinkNode> FindNode(const std::string key,const std::string value) {std::shared_ptr<LinkNode> p = nullptr;if (nullptr == tail_ || nullptr == head_) {return p;}for (std::shared_ptr<LinkNode> p = head_; p != nullptr; p = p->next_) {if (p->key_ == key && p->value_ == value )return p;}return nullptr;}std::shared_ptr<LinkNode> FindNode(std::shared_ptr<LinkNode> node) {std::shared_ptr<LinkNode> p = nullptr;if (node == nullptr)return p;if (nullptr == tail_ || nullptr == head_) {return p;}for (std::shared_ptr<LinkNode> p = head_; p != nullptr; p = p->next_) {if (p->key_ == node->key_ && p->value_ == node->value_)return p;}return nullptr;}bool DeleteNode(const std::string key, const std::string value) {std::shared_ptr<LinkNode>p = FindNode(key, value);if ( p  == nullptr  ) {return false;}if (p->prev_ == nullptr && p->next_ == nullptr) {head_ =  tail_ = nullptr;}else if (p->prev_ == nullptr) {head_ = p->next_;p->next_ = nullptr;head_->prev_ = nullptr;}else if (p->next_ == nullptr) {tail_ = p->prev_;tail_->next_ = nullptr;p->prev_ = nullptr;}else {p->prev_->next_ = p->next_;p->next_->prev_ = p->prev_;p->prev_ = p->next_ = nullptr;}nodeCount_--;return true;}bool DeleteNode(std::shared_ptr<LinkNode>& node) {std::shared_ptr<LinkNode>p = FindNode(node);if (p == nullptr) {return false;}if (p->prev_ == nullptr && p->next_ == nullptr) {head_ = tail_ = nullptr;}else if (p->prev_ == nullptr) {head_ = p->next_;p->next_ = nullptr;head_->prev_ = nullptr;}else if (p->next_ == nullptr) {tail_ = p->prev_;tail_->next_ = nullptr;p->prev_ = nullptr;}else {p->prev_->next_ = p->next_;p->next_->prev_ = p->prev_;p->prev_ = p->next_ = nullptr;}nodeCount_--;return true;}bool AppendNode(std::string key, std::string value) {return AppendNode(InitNode(key,value));}bool AppendNode(std::shared_ptr<LinkNode> node) {bool ret = false;if (nullptr == head_  && nullptr  == tail_) {head_ = tail_ = node;nodeCount_++;ret = true;return ret;}else if (nullptr == tail_ || nullptr == head_) {return ret;}tail_->next_ = node;node->prev_ = tail_;tail_ = node;node->next_ = nullptr;nodeCount_++;ret = true;return ret;}static std::shared_ptr<LinkNode> InitNode(const std::string& key, const std::string& value) {std::shared_ptr<LinkNode> p(new LinkNode);p->key_ = key;p->value_ = value;p->next_ = nullptr;p->prev_ = nullptr;return p;}private:MyLink(const MyLink&);const MyLink& operator=(const MyLink&);size_t nodeCount_;std::shared_ptr<LinkNode> head_;std::shared_ptr<LinkNode> tail_;};

 

 

 整個思路就是寫了一個 智能指標版的鏈表(注意指標間相互引用造成無法自動釋放,出現記憶體流失)

然後將測試資料hash 分別放進位定數量的鏈表中 hash函數使用的是redis2.4版

目前來看8千資料hash分布儲存5個鏈表 7個鏈表 11個鏈表 13個鏈表 還算比較均勻

測試資料如下:

5個鏈表格儲存體效果

bucket[0] has 1687 nodes
bucket[1] has 1681 nodes
bucket[2] has 1658 nodes
bucket[3] has 1662 nodes
bucket[4] has 1713 nodes

 

7個鏈表格儲存體效果

bucket[0] has 1153 nodes
bucket[1] has 1222 nodes
bucket[2] has 1195 nodes
bucket[3] has 1185 nodes
bucket[4] has 1142 nodes
bucket[5] has 1242 nodes
bucket[6] has 1262 nodes

 

11個鏈表格儲存體效果

bucket[0] has 780 nodes
bucket[1] has 740 nodes
bucket[2] has 722 nodes
bucket[3] has 720 nodes
bucket[4] has 748 nodes
bucket[5] has 808 nodes
bucket[6] has 756 nodes
bucket[7] has 781 nodes
bucket[8] has 704 nodes
bucket[9] has 884 nodes
bucket[10] has 758 nodes

 

 

13個鏈表格儲存體效果

bucket[0] has 609 nodes
bucket[1] has 630 nodes
bucket[2] has 645 nodes
bucket[3] has 644 nodes
bucket[4] has 649 nodes
bucket[5] has 682 nodes
bucket[6] has 674 nodes
bucket[7] has 591 nodes
bucket[8] has 654 nodes
bucket[9] has 646 nodes
bucket[10] has 657 nodes
bucket[11] has 632 nodes
bucket[12] has 688 nodes

 

 

37個鏈表格儲存體效果

bucket[0] has 255 nodes
bucket[1] has 206 nodes
bucket[2] has 232 nodes
bucket[3] has 228 nodes
bucket[4] has 194 nodes
bucket[5] has 259 nodes
bucket[6] has 243 nodes
bucket[7] has 229 nodes
bucket[8] has 215 nodes
bucket[9] has 217 nodes
bucket[10] has 245 nodes
bucket[11] has 235 nodes
bucket[12] has 245 nodes
bucket[13] has 223 nodes
bucket[14] has 202 nodes
bucket[15] has 235 nodes
bucket[16] has 216 nodes
bucket[17] has 236 nodes
bucket[18] has 215 nodes
bucket[19] has 217 nodes
bucket[20] has 218 nodes
bucket[21] has 217 nodes
bucket[22] has 237 nodes
bucket[23] has 223 nodes
bucket[24] has 213 nodes
bucket[25] has 236 nodes
bucket[26] has 241 nodes
bucket[27] has 225 nodes
bucket[28] has 230 nodes
bucket[29] has 207 nodes
bucket[30] has 224 nodes
bucket[31] has 234 nodes
bucket[32] has 241 nodes
bucket[33] has 216 nodes
bucket[34] has 228 nodes
bucket[35] has 231 nodes
bucket[36] has 233 nodes

訪問模型一 最簡單的訪問伺服器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.