Feeds:
文章
迴響

Archive for 2016 年 12 月

Code: https://github.com/mongodb/mongo-php-driver/blob/4b49b1ad80a300000cc7beab927ea43bdc6673fb/php_phongo.c

 

// 模組初始化
PHP_GINIT_FUNCTION(mongodb)
{
    // 初始化 hash
    zend_hash_init_ex(&mongodb_globals->clients, 0, NULL, php_phongo_client_dtor, 1, 0);
}

static void php_phongo_client_dtor(zval *zv)
{
   // http://mongoc.org/libmongoc/1.4.0/mongoc_client_destroy.html
   // Release all resources associated with client and free the structure.
    mongoc_client_destroy((mongoc_client_t *) Z_PTR_P(zv));
}

// Manager 初始化
void phongo_manager_init(php_phongo_manager_t *manager, const char *uri_string, zval *options, zval *driverOptions TSRMLS_DC) /* {{{ */
{
    mongoc_client_t  *client_ptr;

   // 產生hash
    hash = php_phongo_manager_make_client_hash(uri_string, options, driverOptions, &hash_len TSRMLS_CC)
   
   // 找尋 hash 吻合的連線, 吻合返回快取
    if ((client_ptr = zend_hash_str_find_ptr(&MONGODB_G(clients), hash, hash_len)) != NULL) {
        manager->client = client_ptr;
        goto cleanup;
    }
   
   // 建立新的 client
    manager->client = php_phongo_make_mongo_client(uri, ssl_opt TSRMLS_CC);
   
   // 更新 hash
    zend_hash_str_update_ptr(&MONGODB_G(clients), hash, hash_len, manager->client);

}

// 模組卸載
PHP_MSHUTDOWN_FUNCTION(mongodb)
{
   // 釋放所有的連線 
   // 觸發 php_phongo_client_dtor()
    zend_hash_destroy(&MONGODB_G(clients));
   
    mongoc_cleanup();

    return SUCCESS;
}

Read Full Post »