void cacheMasterInit() { pthread_attr_t attr; master_cache = dictCreate(&objSdsDictType,NULL); dictExpand(master_cache,PRESERVED_CACHE_ENTRIES); slave_caches = listCreate(); master_total_mem = 0; /* Default Http Not Found */ HTTP_NOT_FOUND = objSdsFromSds(sdsnew("HTTP/1.1 404 OK\r\nContent-Length: 9\r\n\r\nNot Found")); objSdsAddRef(HTTP_NOT_FOUND); HTTP_NOT_FOUND->state = OBJSDS_OK; /* status */ statusQuery = sdsnew("/status"); objSds *status_value = objSdsCreate(); status_value->ref = 2; /* ensure that '/status' entry will not be freed */ next_master_refresh_time += time(NULL) + MASTER_STATUS_REFRESH_PERIOD; dictAdd(master_cache,statusQuery,status_value); status_value->ptr = _masterGetStatus(); status_value->state = OBJSDS_OK; /* Initialize mutex and condition variable objects */ /* For portability, explicitly create threads in a joinable state */ pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); pthread_create(&master_thread, &attr, _masterWatch, NULL); bioInit(); /* favicon.ico */ faviconQuery = sdsnew("/favicon.ico"); objSds *favicon_value = objSdsCreate(); favicon_value->ref = 2; /* ensure that the entry will not be freed */ favicon_value->state = OBJSDS_WAITING; dictAdd(master_cache,faviconQuery,favicon_value); sds staticFaviconQuery = sdsnew("/static/favicon.ico"); /* static file query */ dictAdd(master_cache,staticFaviconQuery,favicon_value); bioPushGeneralJob(staticFaviconQuery); }
void pbwtInit (void) { variationDict = dictCreate (32) ; pack3init () ; sampleInit () ; }
ngx_int_t conn_srv_init_module(ngx_cycle_t *cycle) { conn_buf_pool = ngx_create_pool(1024, cycle->log); client_player_id_hash = dictCreate(&u64hash_type); return NGX_OK; }
static robj *createValSetObject(void) { dict *d = dictCreate(&valSetDictType, NULL); robj *r = createObject(REDIS_VAL_SET, d); return r; }
int database_init(Config_t * env) { db = dictCreate(&dictTypeMemDatabase, NULL); config = env; uv_rwlock_init(&numlock); }
int init_hash_dict(void) { g_d = dictCreate(&task_dict_type, NULL); return ((g_d != NULL) ? OK : FAIL); }
robj *createSetObject(void) { dict *d = dictCreate(&setDictType,NULL); robj *o = createObject(OBJ_SET,d); o->encoding = OBJ_ENCODING_HT; return o; }
robj *createSetObject(void) { dict *d = dictCreate(&setDictType,NULL); return createObject(REDIS_SET,d); }
int main(int argc, char *argv[]) { sds ex_1 = sdsnewlen("zhangjie", 8); sds ex_2 = sdsnewlen("zhbngjie", 8); sds ex_3 = sdsnewlen("zhcngjie", 8); sds ex_4 = sdsnewlen("zhengjie", 8); sds ex_5 = sdsnewlen("zh3ngjie", 8); sds ex_6 = sdsnewlen("zhrngjie", 8); sds ex_7 = sdsnewlen("zhfngjie", 8); sds ex_8 = sdsnewlen("zhgngjie", 8); sds ex_9 = sdsnewlen("zhingjie", 8); sds ex_10= sdsnewlen("zhajgjie", 8); sds ex_11= sdsnewlen("zhkngjie", 8); sds ex_12= sdsnewlen("zhjngjie", 8); sds ex_13= sdsnewlen("zhakgjie", 8); sds ex_14= sdsnewlen("zhtngjie", 8); sds ex_15= sdsnewlen("zhvngjie", 8); sds ex_16= sdsnewlen("zhyngjie", 8); sds ex_17= sdsnewlen("zhaqgjie", 8); sds ex_18= sdsnewlen("shangjie", 8); sds ex_19= sdsnewlen("zdangjie", 8); sds ex_20= sdsnewlen("shangjie", 8); sds ex_21= sdsnewlen("zhfngjie", 8); sds v_1 = sdsnewlen("zhangjie", 8); sds v_2 = sdsnewlen("zhangjie", 8); sds v_3 = sdsnewlen("zhangjie", 8); sds v_4 = sdsnewlen("zhangjie", 8); sds v_5 = sdsnewlen("zhangjie", 8); sds v_6 = sdsnewlen("zhangjie", 8); sds v_7 = sdsnewlen("zhangjie", 8); sds v_8 = sdsnewlen("zhangjie", 8); sds v_9 = sdsnewlen("zhangjie", 8); sds v_10= sdsnewlen("zhangjie", 8); sds v_11= sdsnewlen("zhangjie", 8); sds v_12= sdsnewlen("zhangjie", 8); sds v_13= sdsnewlen("zhangjie", 8); sds v_14= sdsnewlen("zhangjie", 8); sds v_15= sdsnewlen("zhangjie", 8); sds v_16= sdsnewlen("zhangjie", 8); sds v_17= sdsnewlen("zhangjie", 8); sds v_18= sdsnewlen("zhangjie", 8); sds v_19= sdsnewlen("zhangjie", 8); sds v_20= sdsnewlen("zhangjie", 8); sds v_21= sdsnewlen("zhangjie", 8); dict *d = dictCreate(&strTableDictType, NULL); dictAdd(d, (void *)ex_1, (void *)v_1); dictAdd(d, (void *)ex_2, (void *)v_2); dictAdd(d, (void *)ex_3, (void *)v_3); dictAdd(d, (void *)ex_4, (void *)v_4); dictAdd(d, (void *)ex_5, (void *)v_5); dictAdd(d, (void *)ex_6, (void *)v_6); dictAdd(d, (void *)ex_7, (void *)v_7); dictAdd(d, (void *)ex_8, (void *)v_8); dictAdd(d, (void *)ex_9, (void *)v_9); dictAdd(d, (void *)ex_10,(void *)v_10); dictAdd(d, (void *)ex_11,(void *)v_11); dictAdd(d, (void *)ex_12,(void *)v_12); dictAdd(d, (void *)ex_13,(void *)v_13); dictAdd(d, (void *)ex_14,(void *)v_14); dictAdd(d, (void *)ex_15,(void *)v_15); dictAdd(d, (void *)ex_16,(void *)v_16); dictAdd(d, (void *)ex_17,(void *)v_17); dictAdd(d, (void *)ex_18,(void *)v_18); dictAdd(d, (void *)ex_19,(void *)v_19); dictAdd(d, (void *)ex_20,(void *)v_20); dictAdd(d, (void *)ex_21,(void *)v_21); return 0; }
static rstatus_t gossip_pool_each_init(void *elem, void *data) { rstatus_t status; struct server_pool *sp = elem; gn_pool.ctx = sp->ctx; gn_pool.name = &sp->name; gn_pool.idx = sp->idx; gn_pool.g_interval = sp->g_interval; //dictDisableResize(); gn_pool.dict_dc = dictCreate(&string_table_dict_type, NULL); gossip_set_seeds_provider(&sp->seed_provider); uint32_t n_dc = array_n(&sp->datacenters); if (n_dc == 0) return DN_OK; if (n_dc > 0) { status = array_init(&gn_pool.datacenters, n_dc, sizeof(struct gossip_dc)); if (status != DN_OK) { return status; } } //add racks and datacenters uint32_t dc_cnt = array_n(&sp->datacenters); uint32_t dc_index; for(dc_index = 0; dc_index < dc_cnt; dc_index++) { struct datacenter *dc = array_get(&sp->datacenters, dc_index); uint32_t rack_cnt = array_n(&dc->racks); uint32_t rack_index; for(rack_index = 0; rack_index < rack_cnt; rack_index++) { struct rack *rack = array_get(&dc->racks, rack_index); if (dictFind(gn_pool.dict_dc, rack->dc) == NULL) { struct gossip_dc *g_dc = array_push(&gn_pool.datacenters); gossip_dc_init(g_dc, rack->dc); dictAdd(gn_pool.dict_dc, &g_dc->name, g_dc); } struct gossip_dc *g_dc = dictFetchValue(gn_pool.dict_dc, rack->dc); if (dictFind(g_dc->dict_rack, rack->name) == NULL) { log_debug(LOG_VERB, "What?? No rack in Dict for rack : '%.*s'", g_dc->name); struct gossip_rack *g_rack = array_push(&g_dc->racks); gossip_rack_init(g_rack, rack->dc, rack->name); dictAdd(g_dc->dict_rack, &g_rack->name, g_rack); } } } uint32_t i, nelem; for (i = 0, nelem = array_n(&sp->peers); i < nelem; i++) { struct server *peer = array_get(&sp->peers, i); struct gossip_dc *g_dc = dictFetchValue(gn_pool.dict_dc, &peer->dc); struct gossip_rack *g_rack = dictFetchValue(g_dc->dict_rack, &peer->rack); struct node *gnode = array_push(&g_rack->nodes); node_init(gnode); string_copy(&gnode->dc, peer->dc.data, peer->dc.len); string_copy(&gnode->rack, g_rack->name.data, g_rack->name.len); string_copy(&gnode->name, peer->name.data, peer->name.len); string_copy(&gnode->pname, peer->pname.data, peer->pname.len); //ignore the port for now gnode->port = peer->port; gnode->is_local = peer->is_local; if (i == 0) { //Don't override its own state gnode->state = sp->ctx->dyn_state; //likely it is JOINING state gnode->ts = (uint64_t)time(NULL); current_node = gnode; char *b_address = get_broadcast_address(sp); string_deinit(&gnode->name); string_copy(&gnode->name, b_address, dn_strlen(b_address)); } else { gnode->state = DOWN; gnode->ts = 1010101; //make this to be a very aged ts } struct dyn_token *ptoken = array_get(&peer->tokens, 0); copy_dyn_token(ptoken, &gnode->token); //copy socket stuffs g_rack->nnodes++; //add into dicts dictAdd(g_rack->dict_name_nodes, &gnode->name, gnode); dictAdd(g_rack->dict_token_nodes, token_to_string(&gnode->token), gnode); node_count++; } //gossip_debug(); status = gossip_start(sp); if (status != DN_OK) { goto error; } return DN_OK; error: gossip_destroy(sp); return DN_OK; }
/* Initialize the scripting environment. * * 初始化脚本环境。 * * It is possible to call this function to reset the scripting environment * assuming that we call scriptingRelease() before. * * 这个函数也可以在 scriptingRelease() 执行之后使用,用于对脚本环境进行重置。 * * See scriptingReset() for more information. */ void scriptingInit(void) { // 创建一个新的 lua 环境 lua_State *lua = lua_open(); // 载入内置函数库 luaLoadLibraries(lua); // 屏蔽所有不想暴露给脚本环境的函数(目前只有 loadfile) luaRemoveUnsupportedFunctions(lua); /* Initialize a dictionary we use to map SHAs to scripts. * 创建一个字典,用于将 SHA 校验码映射到脚本 * This is useful for replication, as we need to replicate EVALSHA * as EVAL, so we need to remember the associated script. * 因为 Redis 目前复制脚本的方法是将 EVALSHA 转换成 EVAL 来进行的, * 所以程序需要保存和 SHA 校验值相对应的脚本。 */ server.lua_scripts = dictCreate(&shaScriptObjectDictType,NULL); /* Register the redis commands table and fields */ // 创建 table ,并以给定名称将 C 函数注册到表格上 lua_newtable(lua); /* 注册 redis.call */ lua_pushstring(lua,"call"); lua_pushcfunction(lua,luaRedisCallCommand); lua_settable(lua,-3); /* 注册 redis.pcall */ lua_pushstring(lua,"pcall"); lua_pushcfunction(lua,luaRedisPCallCommand); lua_settable(lua,-3); /* 注册 redis.log 和 log levels. */ lua_pushstring(lua,"log"); lua_pushcfunction(lua,luaLogCommand); lua_settable(lua,-3); lua_pushstring(lua,"LOG_DEBUG"); lua_pushnumber(lua,REDIS_DEBUG); lua_settable(lua,-3); lua_pushstring(lua,"LOG_VERBOSE"); lua_pushnumber(lua,REDIS_VERBOSE); lua_settable(lua,-3); lua_pushstring(lua,"LOG_NOTICE"); lua_pushnumber(lua,REDIS_NOTICE); lua_settable(lua,-3); lua_pushstring(lua,"LOG_WARNING"); lua_pushnumber(lua,REDIS_WARNING); lua_settable(lua,-3); /* 注册 redis.sha1hex */ lua_pushstring(lua, "sha1hex"); lua_pushcfunction(lua, luaRedisSha1hexCommand); lua_settable(lua, -3); /* 注册 redis.error_reply and redis.status_reply */ lua_pushstring(lua, "error_reply"); lua_pushcfunction(lua, luaRedisErrorReplyCommand); lua_settable(lua, -3); lua_pushstring(lua, "status_reply"); lua_pushcfunction(lua, luaRedisStatusReplyCommand); lua_settable(lua, -3); /* Finally set the table as 'redis' global var. */ // 将 table 设置为 redis 全局变量 lua_setglobal(lua,"redis"); /* Replace math.random and math.randomseed with our implementations. */ // 将 math.random 和 math.randomseed 设置为 Redis 实现 // 用于生成确定性的随机数 // (对于同一个 seed ,生成的随机序列总是相同) lua_getglobal(lua,"math"); lua_pushstring(lua,"random"); lua_pushcfunction(lua,redis_math_random); lua_settable(lua,-3); lua_pushstring(lua,"randomseed"); lua_pushcfunction(lua,redis_math_randomseed); lua_settable(lua,-3); lua_setglobal(lua,"math"); /* Add a helper funciton that we use to sort the multi bulk output of non * deterministic commands, when containing 'false' elements. * 用于对不确定性多个回复的输出进行排序 */ { char *compare_func = "function __redis__compare_helper(a,b)\n" " if a == false then a = '' end\n" " if b == false then b = '' end\n" " return a<b\n" "end\n"; luaL_loadbuffer(lua,compare_func,strlen(compare_func),"@cmp_func_def"); lua_pcall(lua,0,0,0); } /* Create the (non connected) client that we use to execute Redis commands * inside the Lua interpreter. * 创建一个无网络连接的伪客户端,用于执行 Lua 脚本中的命令 * Note: there is no need to create it again when this function is called * by scriptingReset(). */ if (server.lua_client == NULL) { server.lua_client = createClient(-1); server.lua_client->flags |= REDIS_LUA_CLIENT; } /* Lua beginners ofter don't use "local", this is likely to introduce * subtle bugs in their code. To prevent problems we protect accesses * to global variables. * * 为了避免全局变量被意外地修改,保护全局变量。 */ scriptingEnableGlobalsProtection(lua); // 将 lua 环境赋值到服务器变量 server.lua = lua; }
rstatus_t init_server(struct instance *nci) { rstatus_t status; uint32_t i; redisDb *db; vr_conf *conf; conf = conf_create(nci->conf_filename); server.pid = getpid(); server.configfile = getAbsolutePath(nci->conf_filename); server.hz = 10; server.dbnum = 16; array_init(&server.dbs, server.dbnum, sizeof(redisDb)); server.pidfile = nci->pid_filename; server.executable = NULL; server.activerehashing = CONFIG_DEFAULT_ACTIVE_REHASHING; get_random_hex_chars(server.runid, CONFIG_RUN_ID_SIZE); server.arch_bits = (sizeof(long) == 8) ? 64 : 32; server.requirepass = NULL; server.starttime = time(NULL); server.maxclients = conf->max_clients; server.maxmemory = conf->maxmemory == CONF_UNSET_NUM ? 0 : conf->maxmemory; server.maxmemory_policy = CONFIG_DEFAULT_MAXMEMORY_POLICY; server.client_max_querybuf_len = PROTO_MAX_QUERYBUF_LEN; server.commands = dictCreate(&commandTableDictType,NULL); populateCommandTable(); server.delCommand = lookupCommandByCString("del"); server.multiCommand = lookupCommandByCString("multi"); server.lpushCommand = lookupCommandByCString("lpush"); server.lpopCommand = lookupCommandByCString("lpop"); server.rpopCommand = lookupCommandByCString("rpop"); server.sremCommand = lookupCommandByCString("srem"); server.execCommand = lookupCommandByCString("exec"); for (i = 0; i < server.dbnum; i ++) { db = array_push(&server.dbs); redisDbInit(db); } server.monitors = listCreate(); server.loading = 0; server.lua_timedout = 0; server.aof_state = AOF_OFF; server.stop_writes_on_bgsave_err = 0; server.ready_keys = listCreate(); server.slowlog = listCreate(); server.slowlog_entry_id = 0; server.slowlog_log_slower_than = -1; server.slowlog_max_len = CONFIG_DEFAULT_SLOWLOG_MAX_LEN; server.stat_peak_memory = 0; server.system_memory_size = zmalloc_get_memory_size(); server.rdb_child_pid = -1; server.aof_child_pid = -1; server.hash_max_ziplist_entries = OBJ_HASH_MAX_ZIPLIST_ENTRIES; server.hash_max_ziplist_value = OBJ_HASH_MAX_ZIPLIST_VALUE; server.list_max_ziplist_size = OBJ_LIST_MAX_ZIPLIST_SIZE; server.list_compress_depth = OBJ_LIST_COMPRESS_DEPTH; server.set_max_intset_entries = OBJ_SET_MAX_INTSET_ENTRIES; server.zset_max_ziplist_entries = OBJ_ZSET_MAX_ZIPLIST_ENTRIES; server.zset_max_ziplist_value = OBJ_ZSET_MAX_ZIPLIST_VALUE; server.hll_sparse_max_bytes = CONFIG_DEFAULT_HLL_SPARSE_MAX_BYTES; vr_replication_init(); createSharedObjects(); status = master_init(conf); if (status != VR_OK) { log_error("init master thread failed"); return VR_ERROR; } server.port = master.listen->port; status = workers_init(nci->thread_num); if (status != VR_OK) { log_error("init worker threads failed"); return VR_ERROR; } log_debug(LOG_NOTICE, "mem_alloc_lock_type: %s", malloc_lock_type()); log_debug(LOG_NOTICE, "malloc lib: %s", VR_MALLOC_LIB); return VR_OK; }
static int conf_pre_validate(rmt_conf *cf) { int ret; FILE *fh; char line[256]; dict *organization = NULL; sds str = NULL; sds org_name = NULL; sds *key_value = NULL; int key_value_count = 0; sds key = NULL; conf_value *cv = NULL, **cv_sub; if (cf == NULL) { return RMT_ERROR; } fh = cf->fh; if (fh == NULL) { return RMT_ERROR; } while (!feof(fh)) { if (fgets(line,256,fh) == NULL) { if(feof(fh)) break; log_error("ERROR: Read a line from conf file %s failed: %s", cf->fname, strerror(errno)); goto error; } if(str != NULL){ sdsfree(str); str = NULL; } if(key_value!= NULL){ sdsfreesplitres(key_value, key_value_count); key_value = NULL; key_value_count = 0; } ASSERT(str == NULL); str = sdsnew(line); if(str == NULL){ log_error("ERROR: Out of memory"); goto error; } sdstrim(str, " \n"); if(strchr(str, '#') != NULL){ if(str[0] == '#'){ log_debug(LOG_VVERB, "This line is comment"); sdsfree(str); str = NULL; continue; } sds *content_comment = NULL; int content_comment_count = 0; content_comment = sdssplitlenonce(str, sdslen(str), "#", 1, &content_comment_count); if(content_comment == NULL || content_comment_count != 2){ log_error("ERROR: Split content and comment error."); goto error; } sdsfree(str); str = content_comment[0]; content_comment[0] = NULL; sdsfreesplitres(content_comment, content_comment_count); } log_debug(LOG_VVERB, "%s", str); if(sdslen(str) == 0){ log_debug(LOG_VVERB, "This line is space"); sdsfree(str); str = NULL; continue; }else if(*str == '['){ if(sdslen(str) <= 2 || *(str+sdslen(str)-1) != ']') { log_error("ERROR: Organization name %s in conf file %s error", str, cf->fname); goto error; } if(key != NULL || cv != NULL){ ret = conf_key_value_insert(organization, key, cv); if(ret != RMT_OK){ log_error("ERROR: Key value insert into organization failed"); goto error; } organization = NULL; key = NULL; cv = NULL; } ASSERT(org_name == NULL); sdsrange(str,1,-2); org_name = sdstrim(str, " "); str = NULL; if(sdslen(org_name) == 0){ log_error("ERROR: Organization name %s in conf file %s error", str, cf->fname); goto error; } organization = dictCreate(&KeyValueDictType, NULL); if(organization == NULL){ log_error("ERROR: Create dict organization %s failed", org_name); goto error; } ret = dictAdd(cf->organizations, org_name, organization); if(ret != DICT_OK){ log_error("ERROR: Organization %s in conf file is duplicate", org_name); goto error; } org_name = NULL; }else if(*str == '-'){ if(cv == NULL || cv->type != CONF_VALUE_ARRAY){ log_error("ERROR: Array %s in conf file %s has wrong conf value", str, cf->fname); goto error; } sdsrange(str,1,-1); sdstrim(str, " "); cv_sub = array_push(cv->value); *cv_sub = conf_value_create(CONF_VALUE_STRING); if(*cv_sub == NULL){ log_error("ERROR: Conf value create failed"); goto error; } (*cv_sub)->value = str; str = NULL; }else{ key_value = sdssplitlenonce(str,sdslen(str),":",1,&key_value_count); log_debug(LOG_VVERB, "key_value_count: %d", key_value_count); if(key_value == NULL || key_value_count == 0){ log_error("ERROR: line %s split by : failed", str); goto error; }else if(key_value_count == 1){ log_error("ERROR: Line %s in conf file %s has no :", str, cf->fname); goto error; }else if(key_value_count == 2){ if(key != NULL || cv != NULL){ ret = conf_key_value_insert(organization, key, cv); if(ret != RMT_OK){ log_error("ERROR: Key value insert into organization failed"); goto error; } key = NULL; cv = NULL; } sdstrim(key_value[0]," "); sdstrim(key_value[1]," "); if(sdslen(key_value[1]) == 0){ ASSERT(cv == NULL); cv = conf_value_create(CONF_VALUE_ARRAY); if(cv == NULL){ log_error("ERROR: Conf value create failed"); goto error; } ASSERT(key == NULL); key = key_value[0]; key_value[0] = NULL; continue; } ASSERT(cv == NULL); cv = conf_value_create(CONF_VALUE_STRING); if(cv == NULL){ log_error("ERROR: Conf value create failed"); goto error; } cv->value = key_value[1]; key_value[1] = NULL; if(organization == NULL){ log_error("ERROR: line %s in conf file %s has no organization", str, cf->fname); goto error; } ret = dictAdd(organization,key_value[0],cv); if(ret != DICT_OK){ log_error("ERROR: Key %s in organization of conf file is duplicate", key_value[0]); goto error; } key_value[0] = NULL; cv = NULL; }else{ NOT_REACHED(); } } } if(key != NULL || cv != NULL){ ret = conf_key_value_insert(organization, key, cv); if(ret != RMT_OK){ log_error("ERROR: Key value insert into organization failed"); goto error; } key = NULL; cv = NULL; } if(str != NULL){ sdsfree(str); str = NULL; } if(key_value!= NULL){ sdsfreesplitres(key_value, key_value_count); key_value = NULL; key_value_count = 0; } return RMT_OK; error: if(str != NULL){ sdsfree(str); } if(key != NULL){ sdsfree(key); } if(org_name != NULL){ sdsfree(org_name); } if(key_value != NULL){ sdsfreesplitres(key_value, key_value_count); } if(cv != NULL){ conf_value_destroy(cv); } return RMT_ERROR; }
/**************************************************************************** Desc: Create a database - initialize all physical areas & data dictionary. ****************************************************************************/ RCODE F_Db::initDbFiles( const char * pszRflDir, const char * pszDictFileName, // Name of dictionary file. This // is only used if pszDictBuf is // NULL. If both pszDictFileName // and pszDictBuf are NULL, the // file will be craeted with an empty // dictionary. const char * pszDictBuf, // Buffer containing dictionary in // XML format. If NULL, // pszDictFileName will be used. XFLM_CREATE_OPTS * pCreateOpts) // Create options for the database. { RCODE rc = NE_XFLM_OK; FLMUINT bTransStarted = FALSE; FLMBYTE * pucBuf = NULL; FLMUINT uiBlkSize; FLMUINT uiWriteBytes; FLMUINT uiRflToken = 0; XFLM_DB_HDR * pDbHdr; F_BLK_HDR * pBlkHdr; F_CachedBlock * pSCache = NULL; FLMBYTE * pucWrappingKey = NULL; FLMUINT32 ui32KeyLen = 0; // Determine what size of buffer to allocate. uiBlkSize = (FLMUINT)(pCreateOpts ? flmAdjustBlkSize( (FLMUINT)pCreateOpts->ui32BlockSize) : (FLMUINT)XFLM_DEFAULT_BLKSIZ); // Allocate a buffer for writing. if (RC_BAD( rc = f_alloc( (FLMUINT)uiBlkSize, &pucBuf))) { goto Exit; } // Initialize the database header structure. pDbHdr = (XFLM_DB_HDR *)pucBuf; flmInitDbHdr( pCreateOpts, TRUE, m_pDatabase->m_bTempDb, pDbHdr); m_pDatabase->m_uiBlockSize = (FLMUINT)pDbHdr->ui16BlockSize; m_pDatabase->m_uiDefaultLanguage = (FLMUINT)pDbHdr->ui8DefaultLanguage; m_pDatabase->m_uiMaxFileSize = (FLMUINT)pDbHdr->ui32MaxFileSize; m_pDatabase->m_uiSigBitsInBlkSize = calcSigBits( uiBlkSize); f_memcpy( &m_pDatabase->m_lastCommittedDbHdr, pDbHdr, sizeof( XFLM_DB_HDR)); // Create the first block file. if (!m_pDatabase->m_bTempDb) { if (RC_BAD( rc = m_pSFileHdl->createFile( 1))) { goto Exit; } } if( RC_OK( rc = createDbKey())) { if (RC_BAD( rc = m_pDatabase->m_pWrappingKey->getKeyToStore( &pucWrappingKey, &ui32KeyLen, m_pDatabase->m_pszDbPasswd, NULL))) { goto Exit; } f_memcpy( m_pDatabase->m_lastCommittedDbHdr.DbKey, pucWrappingKey, ui32KeyLen); m_pDatabase->m_lastCommittedDbHdr.ui32DbKeyLen = ui32KeyLen; m_pDatabase->m_rcLimitedCode = NE_XFLM_OK; m_pDatabase->m_bInLimitedMode = FALSE; m_pDatabase->m_bHaveEncKey = TRUE; } else if( rc == NE_XFLM_ENCRYPTION_UNAVAILABLE) { rc = NE_XFLM_OK; m_pDatabase->m_rcLimitedCode = NE_XFLM_ENCRYPTION_UNAVAILABLE; m_pDatabase->m_bInLimitedMode = TRUE; m_pDatabase->m_bHaveEncKey = FALSE; } else { goto Exit; } // Write out the log header if (RC_BAD( rc = m_pDatabase->writeDbHdr( m_pDbStats, m_pSFileHdl, &m_pDatabase->m_lastCommittedDbHdr, NULL, TRUE))) { goto Exit; } // Initialize and output the first LFH block if (m_pDatabase->m_bTempDb) { getDbHdrInfo( &m_pDatabase->m_lastCommittedDbHdr); if (RC_BAD( rc = m_pDatabase->createBlock( this, &pSCache))) { goto Exit; } pBlkHdr = (F_BLK_HDR *)pSCache->m_pBlkHdr; m_pDatabase->m_lastCommittedDbHdr.ui32FirstLFBlkAddr = pBlkHdr->ui32BlkAddr; } else { // Copy the Db header to the checkpointDbHdr buffer. // This is now the first official checkpoint version of the log // header. It must be copied to the checkpointDbHdr buffer so that // it will not be lost in subsequent calls to flmWriteDbHdr. f_memcpy( &m_pDatabase->m_checkpointDbHdr, &m_pDatabase->m_lastCommittedDbHdr, sizeof( XFLM_DB_HDR)); f_memset( pucBuf, 0, uiBlkSize); pBlkHdr = (F_BLK_HDR *)pucBuf; pBlkHdr->ui32BlkAddr = m_pDatabase->m_lastCommittedDbHdr.ui32FirstLFBlkAddr; pBlkHdr->ui64TransID = 0; } pBlkHdr->ui8BlkType = BT_LFH_BLK; pBlkHdr->ui16BlkBytesAvail = (FLMUINT16)(uiBlkSize - SIZEOF_STD_BLK_HDR); blkSetNativeFormat( pBlkHdr); if (!m_pDatabase->m_bTempDb) { pBlkHdr->ui32BlkCRC = calcBlkCRC( pBlkHdr, SIZEOF_STD_BLK_HDR); if (RC_BAD( rc = m_pSFileHdl->writeBlock( pBlkHdr->ui32BlkAddr, uiBlkSize, pucBuf, &uiWriteBytes))) { goto Exit; } // Force things to disk. if (RC_BAD( rc = m_pSFileHdl->flush())) { goto Exit; } } // Allocate the pRfl object. Could not do this until this point // because we need to have the version number, block size, etc. // setup in the database header flmAssert( !m_pDatabase->m_pRfl); if (!m_pDatabase->m_bTempDb) { if ((m_pDatabase->m_pRfl = f_new F_Rfl) == NULL) { rc = RC_SET( NE_XFLM_MEM); goto Exit; } if( RC_BAD( rc = m_pDatabase->m_pRfl->setup( m_pDatabase, pszRflDir))) { goto Exit; } // Disable RFL logging m_pDatabase->m_pRfl->disableLogging( &uiRflToken); // Start an update transaction and populate the dictionary. // This also creates the default collections and indexes. if (RC_BAD( rc = beginTrans( XFLM_UPDATE_TRANS))) { goto Exit; } bTransStarted = TRUE; if (RC_BAD( rc = dictCreate( pszDictFileName, pszDictBuf))) { goto Exit; } // Because the checkpoint thread has not yet been created, // flmCommitDbTrans will force a checkpoint when it completes, // ensuring a consistent database state. bTransStarted = FALSE; if (RC_BAD( rc = commitTrans( 0, TRUE))) { goto Exit; } } else { // The uncommitted header must have all of the stuff from the committed header // in order for this to be able to work as if an update transaction was in // progress. f_memcpy( &m_pDatabase->m_uncommittedDbHdr, &m_pDatabase->m_lastCommittedDbHdr, sizeof( XFLM_DB_HDR)); } Exit: // Free the temporary buffer, if it was allocated. f_free( &pucBuf); if (pucWrappingKey) { f_free( &pucWrappingKey); } if (bTransStarted) { abortTrans(); } if( uiRflToken) { m_pDatabase->m_pRfl->enableLogging( &uiRflToken); } if (pSCache) { ScaReleaseCache( pSCache, FALSE); } return( rc); }
// public api params_map *params_map_init() { return dictCreate(¶ms_dict, NULL); }
/* * main function. */ int main() { struct timeval tv; srand(time(NULL)^getpid()); gettimeofday(&tv,NULL); // rand seed or fixed seed. unsigned int mod = tv.tv_sec^tv.tv_usec^getpid(); printf("%u\n", mod); //dictSetHashFunctionSeed(mod); dictSetHashFunctionSeed(12391); // create <k,v> = <str, str>. dictType myType = { myHash, /* hash function */ NULL, /* key dup */ NULL, /* val dup */ myCompare, /* key compare */ myDestructor, /* key destructor */ NULL /* val destructor */ }; // step 1: create. dict* myDict = dictCreate(&myType, NULL); assert(myDict != NULL); printf("-------------------\n"); printState(myDict); char* key[10] = {"hello0", "hello1", "hello2", "hello3", "hello4", "hello5", "hello6", "hello7", "hello8", "hello9"}; char* val[10] = {"h0", "h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9"}; for(int i=0; i<10; i++) { unsigned int hash = myHash(key[i]); unsigned int idx = hash & 3; printf("real key: %u, real idx=%d\n", hash, idx); } // step 2: add printf("----------add first 5-----------------\n"); for(int i = 0; i<5; i++) { printf("add %d\n", i); int ret = dictAdd(myDict, key[i], val[i]); printState(myDict); assert(ret==0); } printf("----------start rehashing..------------\n"); for(int i=0; i<5; i++) { dictRehash(myDict, 1); printState(myDict); } printf("----------add last 5.-----------------\n"); for(int i = 5; i<10; i++) { printf("add %d\n", i); int ret = dictAdd(myDict, key[i], val[i]); printState(myDict); assert(ret==0); } // index. printf("------------index---------------\n"); for(int i = 0; i < 10; i++) { printf("i=%d\n", i); char* v = dictFetchValue(myDict, key[i]); int ret = strcmp(v, val[i]); assert(ret == 0); } char* v = dictFetchValue(myDict, "hello world2"); assert(v == NULL); // foreach dict. unsigned long cur = 0; while(1) { cur = dictScan(myDict, cur, dictScanCallback, NULL); if(cur == 0) { break; } } // release. dictRelease(myDict); return 0; }
//创建一个ht编码的集合对象 robj *createSetObject(void) { dict *d = dictCreate(&setDictType,NULL);//创建一个字典 robj *o = createObject(OBJ_SET,d); //创建一个对象,对象的数据类型为OBJ_SET o->encoding = OBJ_ENCODING_HT; //对象的编码类型OBJ_ENCODING_HT return o; }
/************************************************************************** f i c l I n i t S y s t e m ** Binds a global dictionary to the interpreter system. ** You specify the address and size of the allocated area. ** After that, ficl manages it. ** First step is to set up the static pointers to the area. ** Then write the "precompiled" portion of the dictionary in. ** The dictionary needs to be at least large enough to hold the ** precompiled part. Try 1K cells minimum. Use "words" to find ** out how much of the dictionary is used at any time. **************************************************************************/ FICL_SYSTEM *ficlInitSystemEx(FICL_SYSTEM_INFO *fsi) { int nDictCells; int nEnvCells; FICL_SYSTEM *pSys = ficlMalloc(sizeof (FICL_SYSTEM)); assert(pSys); assert(fsi->size == sizeof (FICL_SYSTEM_INFO)); memset(pSys, 0, sizeof (FICL_SYSTEM)); nDictCells = fsi->nDictCells; if (nDictCells <= 0) nDictCells = FICL_DEFAULT_DICT; nEnvCells = fsi->nEnvCells; if (nEnvCells <= 0) nEnvCells = FICL_DEFAULT_DICT; pSys->dp = dictCreateHashed((unsigned)nDictCells, HASHSIZE); pSys->dp->pForthWords->name = "forth-wordlist"; pSys->envp = dictCreate((unsigned)nEnvCells); pSys->envp->pForthWords->name = "environment"; pSys->textOut = fsi->textOut; pSys->pExtend = fsi->pExtend; #if FICL_WANT_LOCALS /* ** The locals dictionary is only searched while compiling, ** but this is where speed is most important. On the other ** hand, the dictionary gets emptied after each use of locals ** The need to balance search speed with the cost of the 'empty' ** operation led me to select a single-threaded list... */ pSys->localp = dictCreate((unsigned)FICL_MAX_LOCALS * CELLS_PER_WORD); #endif /* ** Build the precompiled dictionary and load softwords. We need a temporary ** VM to do this - ficlNewVM links one to the head of the system VM list. ** ficlCompilePlatform (defined in win32.c, for example) adds platform specific words. */ ficlCompileCore(pSys); ficlCompilePrefix(pSys); #if FICL_WANT_FLOAT ficlCompileFloat(pSys); #endif #if FICL_PLATFORM_EXTEND ficlCompilePlatform(pSys); #endif ficlSetVersionEnv(pSys); /* ** Establish the parse order. Note that prefixes precede numbers - ** this allows constructs like "0b101010" which might parse as a ** hex value otherwise. */ ficlAddPrecompiledParseStep(pSys, "?prefix", ficlParsePrefix); ficlAddPrecompiledParseStep(pSys, "?number", ficlParseNumber); #if FICL_WANT_FLOAT ficlAddPrecompiledParseStep(pSys, ">float", ficlParseFloatNumber); #endif /* ** Now create a temporary VM to compile the softwords. Since all VMs are ** linked into the vmList of FICL_SYSTEM, we don't have to pass the VM ** to ficlCompileSoftCore -- it just hijacks whatever it finds in the VM list. ** ficl 2.05: vmCreate no longer depends on the presence of INTERPRET in the ** dictionary, so a VM can be created before the dictionary is built. It just ** can't do much... */ ficlNewVM(pSys); ficlCompileSoftCore(pSys); ficlFreeVM(pSys->vmList); return pSys; }
void srandmemberWithCountCommand(redisClient *c) { long l; unsigned long count, size; int uniq = 1; robj *set, *ele; int64_t llele; int encoding; dict *d; if (getLongFromObjectOrReply(c,c->argv[2],&l,NULL) != REDIS_OK) return; if (l >= 0) { count = (unsigned) l; } else { /* A negative count means: return the same elements multiple times * (i.e. don't remove the extracted element after every extraction). */ count = -l; uniq = 0; } if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.emptymultibulk)) == NULL || checkType(c,set,REDIS_SET)) return; size = setTypeSize(set); /* If count is zero, serve it ASAP to avoid special cases later. */ if (count == 0) { addReply(c,shared.emptymultibulk); return; } /* CASE 1: The count was negative, so the extraction method is just: * "return N random elements" sampling the whole set every time. * This case is trivial and can be served without auxiliary data * structures. */ if (!uniq) { addReplyMultiBulkLen(c,count); while(count--) { encoding = setTypeRandomElement(set,&ele,&llele); if (encoding == REDIS_ENCODING_INTSET) { addReplyBulkLongLong(c,llele); } else { addReplyBulk(c,ele); } } return; } /* CASE 2: * The number of requested elements is greater than the number of * elements inside the set: simply return the whole set. */ if (count >= size) { sunionDiffGenericCommand(c,c->argv+1,1,NULL,REDIS_OP_UNION); return; } /* For CASE 3 and CASE 4 we need an auxiliary dictionary. */ d = dictCreate(&setDictType,NULL); /* CASE 3: * The number of elements inside the set is not greater than * SRANDMEMBER_SUB_STRATEGY_MUL times the number of requested elements. * In this case we create a set from scratch with all the elements, and * subtract random elements to reach the requested number of elements. * * This is done because if the number of requsted elements is just * a bit less than the number of elements in the set, the natural approach * used into CASE 3 is highly inefficient. */ if (count*SRANDMEMBER_SUB_STRATEGY_MUL > size) { setTypeIterator *si; /* Add all the elements into the temporary dictionary. */ si = setTypeInitIterator(set); while((encoding = setTypeNext(si,&ele,&llele)) != -1) { int retval = DICT_ERR; if (encoding == REDIS_ENCODING_INTSET) { retval = dictAdd(d,createStringObjectFromLongLong(llele),NULL); } else if (ele->encoding == REDIS_ENCODING_RAW) { retval = dictAdd(d,dupStringObject(ele),NULL); } else if (ele->encoding == REDIS_ENCODING_INT) { retval = dictAdd(d, createStringObjectFromLongLong((long)ele->ptr),NULL); } redisAssert(retval == DICT_OK); } setTypeReleaseIterator(si); redisAssert(dictSize(d) == size); /* Remove random elements to reach the right count. */ while(size > count) { dictEntry *de; de = dictGetRandomKey(d); dictDelete(d,dictGetKey(de)); size--; } } /* CASE 4: We have a big set compared to the requested number of elements. * In this case we can simply get random elements from the set and add * to the temporary set, trying to eventually get enough unique elements * to reach the specified count. */ else { unsigned long added = 0; while(added < count) { encoding = setTypeRandomElement(set,&ele,&llele); if (encoding == REDIS_ENCODING_INTSET) { ele = createStringObjectFromLongLong(llele); } else if (ele->encoding == REDIS_ENCODING_RAW) { ele = dupStringObject(ele); } else if (ele->encoding == REDIS_ENCODING_INT) { ele = createStringObjectFromLongLong((long)ele->ptr); } /* Try to add the object to the dictionary. If it already exists * free it, otherwise increment the number of objects we have * in the result dictionary. */ if (dictAdd(d,ele,NULL) == DICT_OK) added++; else decrRefCount(ele); } } /* CASE 3 & 4: send the result to the user. */ { dictIterator *di; dictEntry *de; addReplyMultiBulkLen(c,count); di = dictGetIterator(d); while((de = dictNext(di)) != NULL) addReplyBulk(c,dictGetKey(de)); dictReleaseIterator(di); dictRelease(d); } }
/* Latency monitor initialization. We just need to create the dictionary * of time series, each time serie is craeted on demand in order to avoid * having a fixed list to maintain. */ void latencyMonitorInit(void) { server.latency_events = dictCreate(&latencyTimeSeriesDictType,NULL); }
static void initServer_Extra() { if (DynLuaD) dictRelease(DynLuaD); DynLuaD = dictCreate(&dbDictType, NULL); }
int initLookupTable(void) { dt = dictCreate(&stringDictType, NULL); return 0; }
database *db_init(database *db) { db->opt_load_initial_content = NULL; db->docs = dictCreate(&db_dict_type, NULL); return db; }
dict* createTaskMap() { return dictCreate(&callbackDict,NULL); }
static robj *createAppendSetObject(void) { dict *d = dictCreate(&appendDictType, NULL); robj *r = createObject(REDIS_APPEND_SET, d); return r; }
redisMap * redisMapCreate(const char *name) { return dictCreate(&sdsDictCallBacks, (void *)name); }
/* Initialize the scripting environment. * It is possible to call this function to reset the scripting environment * assuming that we call scriptingRelease() before. * See scriptingReset() for more information. */ void scriptingInit(void) { lua_State *lua = lua_open(); luaLoadLibraries(lua); luaRemoveUnsupportedFunctions(lua); /* Initialize a dictionary we use to map SHAs to scripts. * This is useful for replication, as we need to replicate EVALSHA * as EVAL, so we need to remember the associated script. */ server.lua_scripts = dictCreate(&shaScriptObjectDictType,NULL); /* Register the redis commands table and fields */ lua_newtable(lua); /* redis.call */ lua_pushstring(lua,"call"); lua_pushcfunction(lua,luaRedisCallCommand); lua_settable(lua,-3); /* redis.pcall */ lua_pushstring(lua,"pcall"); lua_pushcfunction(lua,luaRedisPCallCommand); lua_settable(lua,-3); /* redis.log and log levels. */ lua_pushstring(lua,"log"); lua_pushcfunction(lua,luaLogCommand); lua_settable(lua,-3); lua_pushstring(lua,"LOG_DEBUG"); lua_pushnumber(lua,REDIS_DEBUG); lua_settable(lua,-3); lua_pushstring(lua,"LOG_VERBOSE"); lua_pushnumber(lua,REDIS_VERBOSE); lua_settable(lua,-3); lua_pushstring(lua,"LOG_NOTICE"); lua_pushnumber(lua,REDIS_NOTICE); lua_settable(lua,-3); lua_pushstring(lua,"LOG_WARNING"); lua_pushnumber(lua,REDIS_WARNING); lua_settable(lua,-3); /* redis.sha1hex */ lua_pushstring(lua, "sha1hex"); lua_pushcfunction(lua, luaRedisSha1hexCommand); lua_settable(lua, -3); /* redis.error_reply and redis.status_reply */ lua_pushstring(lua, "error_reply"); lua_pushcfunction(lua, luaRedisErrorReplyCommand); lua_settable(lua, -3); lua_pushstring(lua, "status_reply"); lua_pushcfunction(lua, luaRedisStatusReplyCommand); lua_settable(lua, -3); /* Finally set the table as 'redis' global var. */ lua_setglobal(lua,"redis"); /* Replace math.random and math.randomseed with our implementations. */ lua_getglobal(lua,"math"); lua_pushstring(lua,"random"); lua_pushcfunction(lua,redis_math_random); lua_settable(lua,-3); lua_pushstring(lua,"randomseed"); lua_pushcfunction(lua,redis_math_randomseed); lua_settable(lua,-3); lua_setglobal(lua,"math"); /* Add a helper function that we use to sort the multi bulk output of non * deterministic commands, when containing 'false' elements. */ { char *compare_func = "function __redis__compare_helper(a,b)\n" " if a == false then a = '' end\n" " if b == false then b = '' end\n" " return a<b\n" "end\n"; luaL_loadbuffer(lua,compare_func,strlen(compare_func),"@cmp_func_def"); lua_pcall(lua,0,0,0); } /* Add a helper function we use for pcall error reporting. * Note that when the error is in the C function we want to report the * information about the caller, that's what makes sense from the point * of view of the user debugging a script. */ { char *errh_func = "function __redis__err__handler(err)\n" " local i = debug.getinfo(2,'nSl')\n" " if i and i.what == 'C' then\n" " i = debug.getinfo(3,'nSl')\n" " end\n" " if i then\n" " return i.source .. ':' .. i.currentline .. ': ' .. err\n" " else\n" " return err\n" " end\n" "end\n"; luaL_loadbuffer(lua,errh_func,strlen(errh_func),"@err_handler_def"); lua_pcall(lua,0,0,0); } /* Create the (non connected) client that we use to execute Redis commands * inside the Lua interpreter. * Note: there is no need to create it again when this function is called * by scriptingReset(). */ if (server.lua_client == NULL) { server.lua_client = createClient(-1); server.lua_client->flags |= REDIS_LUA_CLIENT; } /* Lua beginners ofter don't use "local", this is likely to introduce * subtle bugs in their code. To prevent problems we protect accesses * to global variables. */ scriptingEnableGlobalsProtection(lua); server.lua = lua; }
int debugger_set_breakpoint(lua_State* L) { if (lua_type(L, 1) == LUA_TNUMBER && lua_type(L, 2) == LUA_TSTRING) { dict* linedefined_dictionary = NULL; int line = luaL_checkint(L, 1); const char* source = luaL_checkstring(L, 2); const char* file_name = trim_path(source); if (file_name == NULL) { lua_pushinteger(L, 0); return 1; } linedefined_dictionary = dictFetchValue(breakpoint_dictionary, (const void*)line); if (linedefined_dictionary == NULL) { linedefined_dictionary = dictCreate(&dictTypeHeapStringCopyKey, NULL); if (linedefined_dictionary == NULL || dictAdd(breakpoint_dictionary, (void*)line, linedefined_dictionary) != DICT_OK) { lua_pushinteger(L, 0); return 1; } } if (dictFetchValue(linedefined_dictionary, file_name) == NULL) { breakpoint_info* bp = malloc(sizeof(breakpoint_info)); if (bp == NULL) { lua_pushinteger(L, 0); return 1; } if (dictAdd(linedefined_dictionary, (void*)file_name, bp) != DICT_OK) { free(bp); lua_pushinteger(L, 0); return 1; } memset(bp, 0, sizeof(*bp)); if (lua_type(L, 3) == LUA_TSTRING) { const char* condition = lua_tostring(L, 3); if (luaL_loadstring(L, condition) == 0) { bp->type |= breakpoint_type_condition; bp->conditionFunction = luaL_ref(L, LUA_REGISTRYINDEX); } else { free(bp); lua_pushinteger(L, 0); return 1; } } if (lua_type(L, 4) == LUA_TBOOLEAN) { if (lua_toboolean(L, 4)) { bp->type |= breakpoint_type_temp; } } bp->line = line; strncpy(bp->file_name, file_name, sizeof(bp->file_name) - 1); } lua_pushinteger(L, 1); } else { lua_pushinteger(L, 0); } return 1; }