検索

Redis源码研究

Jun 07, 2016 pm 05:39 PM
redisソースコード研究

计划每天花1小时学习Redis 源码。在博客上做个记录。 --------6月18日----------- redis的字典dict主要涉及几个数据结构, dictEntry:具体的k-v链表结点 dictht:哈希表 dict:字典 具体关系为 1 typedef struct dict { 2 dictType * type; 3 void * privda

计划每天花1小时学习Redis 源码。在博客上做个记录。

--------6月18日-----------

redis的字典dict主要涉及几个数据结构,

dictEntry:具体的k-v链表结点

dictht:哈希表

dict:字典

具体关系为

1 typedef struct dict { 2 dictType *type; 3 void *privdata; 4 dictht ht[2]; iterators; } dict;

1 typedef struct dictht { 2 dictEntry **table; 3 unsigned long size; 4 unsigned long sizemask; 5 unsigned long used; 6 } dictht;

1 typedef struct dictEntry { 2 void *key; 3 union { 4 void *val; 5 uint64_t u64; 6 int64_t s64; 7 } v; 8 struct dictEntry *next; 9 } dictEntry;

一个字典有两个哈希表, 冲突后采用了链地址法,很好理解。

一些简单操作采用了宏

#define dictGetKey(he) ((he)->key) #define dictGetVal(he) ((he)->v.val) #define dictGetSignedIntegerVal(he) ((he)->v.s64) #define dictGetUnsignedIntegerVal(he) ((he)->v.u64)

 ------------6月19日----------------------

字典具体用到了两种哈希算法,我只看了简单的那一种,没想到代码竟然可以那么少,算法名字为djb2,

unsigned int dictGenCaseHashFunction(const unsigned char *buf, int len) { 3 unsigned int hash = (unsigned int)dict_hash_function_seed; (len--) hash; 8 }

dict_hash_function_seed是个全局变量,为5381.
The magic of number 33 (why it works better than many other constants, prime or not) has never been adequately explained.
JDK中采用的哈希算法取得数字是31,一个素数。
创建一个新字典并初始化:

1 dict *dictCreate(dictType *type, void *privDataPtr){ 2 dict *d = malloc(sizeof(*d)); 3 _dictInit(d,type,privDataPtr); 4 return d; 5 } _dictInit(dict *d, dictType *type, void *privDataPtr){ 8 _dictReset(&d->ht[0]); 9 _dictReset(&d->ht[1]); 10 11 d->type = type; 12 d->privdata = privDataPtr; 13 d->rehashidx = -1; 14 d->iterators = 0; DICT_OK; 17 } _dictReset(dictht *ht){ 20 ht->table = NULL; 21 ht->size = 0; 22 ht->sizemask = 0; 23 ht->used = 0; 24 } 

学了这么多年c语言了,malloc(sizeof(*d))我还是第一次看到。
说到sizeof,我还要提一句,c99之后,sizeof是运行时确定的,c99还加入了动态数组这一概念。csdn上的回答是错的。
对字典进行紧缩处理,让 哈希表中的数/哈希表长度接近1:

1 int dictResize(dict *d){ 2 int minimal; (!dict_can_resize || dictIsRehashing(d)) return DICT_ERR; 5 6 minimal = d->ht[0].used; (minimal DICT_HT_INITIAL_SIZE) 9 minimal = DICT_HT_INITIAL_SIZE; dictExpand(d, minimal); 12 } dictIsRehashing(ht) ((ht)->rehashidx != -1) 15 #define DICT_HT_INITIAL_SIZE 4

当字典正在Rehash的时候不能进行Resize操作,初始时哈希表大小为4,哈希表大小一般都是2的幂次方。
如果minimal是5,经过dictExpand后,哈希表大小变为8.

1 static unsigned long _dictNextPower(unsigned long size){ 2 unsigned long i = DICT_HT_INITIAL_SIZE; (size >= LONG_MAX) return LONG_MAX; 5 while(1) { 6 if (i >= size) 7 return i; 8 i *= 2; 9 } 10 } dictExpand(dict *d, unsigned long size){ unsigned long realsize = _dictNextPower(size); the size is invalid if it is smaller than the number of (dictIsRehashing(d) || d->ht[0].used > size) 20 return DICT_ERR; n.size = realsize; 24 n.sizemask = realsize-1; 25 n.table = zcalloc(realsize*sizeof(dictEntry*)); 26 n.used = 0; Is this the first initialization? If so it's not really a rehashing (d->ht[0].table == NULL) { 31 d->ht[0] = n; 32 return DICT_OK; 33 } d->ht[1] = n; 37 d->rehashidx = 0; DICT_OK; 40 }

新建了一个哈希表n,size是扩展后的size,,ht[0].table 为空说明这是第一次初始化,不是扩展,直接赋值。
ht[0].table 不为空,说明这是一次扩展,把n赋给ht[1],ReHash标志rehashix也被设为0.
上边这段不大好理解,先看后面的,一会返过来再研究dictExpand函数。
--------------------6月20日--------------------------

向字典中添加元素需要调用dictAdd函数:
声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
mysql blob:制限はありますか?mysql blob:制限はありますか?May 08, 2025 am 12:22 AM

mysqlblobshavelimits:tinyblob(255bytes)、blob(65,535bytes)、mediumblob(16,777,215bytes)、andlongblob(4,294,967,295bytes).tousebl難易度:1)PROFFORMANCESANDSTORERGEBLOBSEXTERNALLY;

MySQL:ユーザーの作成を自動化するための最良のツールは何ですか?MySQL:ユーザーの作成を自動化するための最良のツールは何ですか?May 08, 2025 am 12:22 AM

MySQLでユーザーの作成を自動化するための最良のツールとテクノロジーには、次のものがあります。1。MySQLWorkBench、中小サイズの環境に適した、使いやすいがリソース消費量が高い。 2。アンシブル、マルチサーバー環境に適した、シンプルだが急な学習曲線。 3.カスタムPythonスクリプト、柔軟性がありますが、スクリプトセキュリティを確保する必要があります。 4。大規模な環境に適した人形とシェフ、複雑ですがスケーラブル。選択する際には、スケール、学習曲線、統合のニーズを考慮する必要があります。

mysql:blob内で検索できますか?mysql:blob内で検索できますか?May 08, 2025 am 12:20 AM

はい、youcansearchinsideablobinmysqlusingspecifictechniques.1)converttheblobtoautf-8stringwithconvert function andsearchusinglike.2)

MySQL文字列データ型:包括的なガイドMySQL文字列データ型:包括的なガイドMay 08, 2025 am 12:14 AM

mysqloffersvariousstringdatypes:1)charfofixed-lengthstrings、italforconsentlengtalikecountrycodes; 2)varcharforvariable-lengthstrings、適切なForfieldslikenames;

MySQLブロブのマスター:ステップバイステップのチュートリアルMySQLブロブのマスター:ステップバイステップのチュートリアルMay 08, 2025 am 12:01 AM

tomastermysqlblobs、soflowthesesteps:1)shoseetheapsosupturateblobtype(tinyblob、blob、mediumblob、longblob)basedOndatasize.2)insertDatausingload_fileforefficiency.3)storefilereferenceinsinsteadoffilestoimpeperformance.4)

MySQLのBLOBデータ型:開発者の詳細な概要MySQLのBLOBデータ型:開発者の詳細な概要May 07, 2025 pm 05:41 PM

blobdatatypesinmysqlareusedlarginglaredatalikeimagesorudio.1)useblobtypes(tinyblobtolongblob)Basedatasizeneeds。 2)storeblobsin perplate petooptimize performance.3)scondididididididididersxternalストレージBlob Romanaデータベースindimprovebackupe

コマンドラインからMySQLにユーザーを追加する方法コマンドラインからMySQLにユーザーを追加する方法May 07, 2025 pm 05:01 PM

toadduserstomysqlfromthecommandline、loginasroot、thenusecreateuser'username '@' host'ident'ident'identifidedby'password '; tocreateanewuser.grantpermissions with grantpermissions with grantalgegesondatabase

mysqlの文字列データ型は何ですか?詳細な概要mysqlの文字列データ型は何ですか?詳細な概要May 07, 2025 pm 03:33 PM

mysqlofferseightStringDatatypes:char、varchar、binary、varbinary、blob、text、enum、andset.1)charisfixed-length、yealforconsistent datalikecountrycodes.2)varcharisvariable length、efficational forvaryingdatalikenames.3)binaryandvanterbinarydata a similati

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

MantisBT

MantisBT

Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

Dreamweaver Mac版

Dreamweaver Mac版

ビジュアル Web 開発ツール

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境