首页 > 代码库 > 访问模型一 最简单的访问服务器

访问模型一 最简单的访问服务器

假设一个网站,最初开始压力不大,只有几千条或者几万条数据存储,约几百个查询访问

那么一般就是一台设备应对 数据输入和查询 (后继更新代码)

1

技术分享

 

目前完成代码 链表和hash函数

#include <iostream>
#include <fstream>
#include "MyLink.h"



// redis hash function
unsigned 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

访问模型一 最简单的访问服务器