C++è¨èªã§æ¸ããã¦ãããã¼ã¿ãã¼ã¹ã©ã¤ãã©ãªTkrzwã¯ãå½ç¶ãªããC++ã®APIãæä¾ããããCè¨èªããã¯ä½¿ããªãããªã®ã§ã主è¦æ©è½ãã¯ã©ã¹ã§ãªãé¢æ°ã§ã©ããããCè¨èªã®APIãæä¾ãããã¨ã«ãããCè¨èªã®ããã¸ã§ã¯ãã§ä½¿ãããã¨ã¯ãã¡ããããã®ä»ã®è¨èªã§å©ç¨ããéã«ãCè¨èªã®APIãã©ããããæ¹ã楽ãªãã¨ãããããã ãå ·ä½çã«è¨ãã°ãGoè¨èªã«çµã¿è¾¼ãããã«Cè¨èªã®APIãããã¨ä¾¿å©ã ã
çµè«ã¨ãã¦ã¯ããããªæãã§Cè¨èªããã使ããããã«ãªã£ããã¾ãã§ANSI-Cæ¨æºã®FILEãã¤ã³ã¿ãæ±ã£ã¦ãããã®ãããªç°¡åãã§ããã¼ã¿ãã¼ã¹ããã°ã©ãã³ã°ãã§ããã
#include <stdio.h> #include "tkrzw_langc.h" int main(int argc, char** argv) { // ãã¼ã¿ãã¼ã¹ãã¡ã¤ã«ãéã TkrzwDBM* dbm = tkrzw_dbm_open( "casket.tkh", true, "truncate=true,num_buckets=100"); // ã¬ã³ã¼ããæ ¼ç´ããããµã¤ãºã-1ã ã¨ãstrlenã§ãµã¤ãºã決ãã¦ããã tkrzw_dbm_set(dbm, "foo", -1, "hop", -1, true); tkrzw_dbm_set(dbm, "bar", -1, "step", -1, true); tkrzw_dbm_set(dbm, "baz", -1, "jump", -1, true); // ã¬ã³ã¼ããæ¤ç´¢ããããã¤ããªãåå¾ã§ããããæååã¨ãã¦ãæ±ãã char* value_ptr = tkrzw_dbm_get(dbm, "foo", -1, NULL); if (value_ptr) { puts(value_ptr); free(value_ptr); } // ãã¼ã¿ãã¼ã¹å ã®å ¨ã¬ã³ã¼ããã¤ãã¬ã¼ã¿ã§å·¡ã TkrzwDBMIter* iter = tkrzw_dbm_make_iterator(dbm); tkrzw_dbm_iter_first(iter); while (true) { char* key_ptr = nullptr; if (!tkrzw_dbm_iter_get(iter, &key_ptr, NULL, &value_ptr, NULL)) { break; } printf("%s:%s\n", key_ptr, value_ptr); free(key_ptr); free(value_ptr); tkrzw_dbm_iter_next(iter); } tkrzw_dbm_iter_free(iter); // ãã¼ã¿ãã¼ã¹ãã¡ã¤ã«ãéãã tkrzw_dbm_close(dbm); return 0; }
C++ã使ããç°å¢ã§Cè¨èªã使ãæå³ã¯ä»ã¨ãªã£ã¦ã¯ã»ã¨ãã©ãªããTkrzwãC++ã®ã©ã³ã¿ã¤ã ã©ã¤ãã©ãªã«ä¾åãã¦ããæç¹ã§ããã¨ãCã®APIãå©ç¨ããã¨ãã¦ãããªãã¸ã§ã¯ããã¡ã¤ã«ã®ãµã¤ãºãããã»ã¹ã®RSSï¼resident set size = å ±æã©ã¤ãã©ãªã®ä½¿ç¨åãå«ããã¡ã¢ãªä½¿ç¨éï¼ãç¯ç´ããå¹æã¯ãªãããã£ã¦ãçµã¿è¾¼ã¿ç³»ãªã©ã§Cãã使ããªãã¨ããå ´åã«Tkrzwã使ãããã¨ã¯ãªããããããããã°ã©ãã³ã°ã®åå¿è ã§ãã£ãããå¦çãææ¥ã§åãçµããªã©ã®å ´åã«ã¯ãCè¨èªããå¦ãã§ããªãã¨ããçç±ã§é¸æãããã¨ãããã ãããDBMã¯ç·´ç¿ç¨ã®èª²é¡ã§ä½¿ãã«ãæé©ãªåç´ããåããã®ã§ããããã人ãã¡ã«ãæ¯é使ã£ã¦ãããããã¨ããã ã
話ã¯é¸è±ããããä¸è¿°ã®ä¾ã§ãä½äºããªãã£ããã®ããã«ãã¤ã³ã¿ãåºã¦ããããå¦ç¿è ã«ã¨ã£ã¦ã¯ãã¤ã³ã¿ã£ã¦åä»ãªãã®ã ãããªãã¸ã§ã¯ãã®ãã³ãã«ã®ç´æ¥çãªè¡¨ç¾ã§ãã£ã¦ãå®éã«ã¯ä»®æ³ã¡ã¢ãªä¸ã®ã¢ãã¬ã¹ãªãã ããã¨ããã説æãããããã®ãªããããã°ã©ãã³ã°ãå«ãã«ãªããã¨è«ãåãã ãC++ãªããã¤ã³ã¿ãé è½ããããã¯ãªãã®ã ãã代ããã«è¦ãã«ããªããä»æ§ãè¨å¤§ã«ãªãããã©ã£ã¡ã«ãããã¤ã³ã¿ããªã½ã¼ã¹ç®¡çã®æ¦å¿µã¯ç¿å¾ããªãã¨è¨èªã使ãããªããªãã®ã§ãC++ã§å¦ç¿æ²ç·ãæ¹åããã¨ãæããªããé¢æ°ããã¼ã¿æ½è±¡ããªãã¸ã§ã¯ãæåã®æ¦å¿µã身ã«ã¤ããããã«ã¯ããã°ã©ãã³ã°ã®æ¥½ãããå³ããã®ã§ããã°ãJavaScriptãPythonãRubyãå ã«å¦ã¶ã¹ãã ããããããã身ã«çãã¦èªä¿¡ãã¤ãã¦ãããJavaãC++ãå¦ãã§ãé ãã¯ãªããããã¨åå¾ãã¦ãåå§çãªãªã½ã¼ã¹ç®¡çã®ä¿®è¡ã¨ãã¦Cãå¦ã¶ã®ãããã ã¨ã¯æãããã¾ãæ£ç´ãããªã«æå³ãããã¨ã¯æããªãã
話ãæ»ãã¨ãCè¨èªãç©æ¥µçã«ä½¿ãå¿ è¦ã¯ãªãã¨ç§ã¯æãããCè¨èªã使ããããå¾ãªãå ´åã¯å®éã«ãããã¨ããã§ãTkrzwã®Goè¨èªã®ã¤ã³ã¿ã¼ãã§ã¤ã¹ãæ¸ããã¨ããã¨ãããcgoã¨ããæ©è½ã§Cã®å¤æ°ãé¢æ°ãã©ããããã®ã常éã ã¨ãããã¨ã«ãªã£ããC++ã®ã·ã³ãã«ã使ãã®ã¯ãã³ã°ãªã³ã°ã®åé¡ããã£ã¦ä¸çç¸ã§ã¯ãããªãããããããã¯ä½ã¨ãé å¼µã£ã¦ãã¨æ£ç´æãã¨ããã ããGoè¨èªã®éçºåæ©ã¯C++ãå«ãã ããã¨ãããã¨ãããã®ã§ãGoã®æ¨æºæ©è½ã§ããcgoãC++ã«æ©ã¿å¯ããã¨ã¯ããã¾ãæå¾ ã§ããªããããªæ°ããããã¨ãããã¨ã§ãCè¨èªã使ããããå¾ãªãå ´åã«èªåãå½ã¦ã¯ã¾ã£ã¦ãã¾ã£ãã使ãããã®ã¯Goè¨èªãªãã ãã©ãã
C++ã®APIãCç¨ã«æ¸ãæããã®ã¯ãããªã«é£ãã話ã§ã¯ãªããTkrzwã®ä¸»è¦æ©è½ã¯PolyDBMã¨ããã¢ããã¿ã¯ã©ã¹ããå©ç¨ã§ããããã«ãã¦ããã®ã§ããã®ã©ããã¼ãæ¸ãã°è¯ããPythonã®ããã«ãã¡ã½ããã®ã¬ã·ã¼ãã第1å¼æ°ã«ãã¦ããã®ä»ã®å¼æ°ããã®å¾ã«ä¸¦ã¹ãã°ãããã¾ããCã«ã¯åå空éããªãã®ã§ãååã®ä¸æãªè¡çªãé¿ããããã«å ¨ã¦ã®èå¥åã«ã¯æ示çã«æ¥é è¾tkrzwãã¤ããããã«ã¡ã½ããã§ãã£ãé¢æ°ã«ã¯å ã®ã¯ã©ã¹ã®ååãæ¥é è¾ã¨ãã¦ä¸¦ã¹ãã
C++: dbm.Set("hello", "world"); C: tkrzw_dbm_open(dbm, "hello", -1, "world", -1);
ç´°ããã¨ããã§é¢åäºããã£ã±ããããã¾ããstringã¯ã©ã¹ãstring_viewã¯ã©ã¹ã使ããªãã®ã§ããã¤ãåã¯ãã¤ã³ã¿ã¨ãµã¤ãºã®äºã¤ã®å¤æ°ã§ç®¡çããå¿ è¦ããããã¾ããstringã¯ã©ã¹ã使ãã°RAIIã¨ãã¹ãã©ã¯ã¿ã«ãã£ã¦ãªã½ã¼ã¹ã®æé»çãªè§£æ¾ãã§ããããCã§ã¯ãã®æ©æ§ããªãã®ã§ãæ示çã«freeãæ¸ããªãã¨ãããªãã
C++: string value; dbm.Get("hello", &value); cout << value << endl; C: char* value_ptr = dbm_get(dbm, "hello", 5, NULL); puts(value_ptr); free(value_ptr);
ã¬ã³ã¼ãã®æ¤ç´¢çµæãæ»ãå¤ã¨ãã¦è¿ãã¨ã失æããæã®ã¹ãã¼ã¿ã¹ã³ã¼ããã©ããã£ã¦è¿ããã¨ããã®ãåé¡ã«ãªããã¹ãã¼ã¿ã¹ã³ã¼ããæ»ãå¤ã«ããæãããããããããã¨æ¤ç´¢çµæãæ ¼ç´ããé åã¸ã®ãã¤ã³ã¿ãå¼æ°ã®ãã¤ã³ã¿ã®åç §å ã«æ ¼ç´ãããã¨ã«ãªããã¤ã¾ãããã¤ã³ã¿ãã¤ã³ã¿ãå¼æ°ã¨ãã¦åãåããã¨ã«ãªãããããè¤éã«ãªã£ã¦ãã¾ããå¦ç¿è ã«ã¨ã£ã¦ã¯ãã¤ã³ã¿ã§ããå«ãªã®ã«ããã¤ã³ã¿ãã¤ã³ã¿ã£ã¦ä½ã ãã¨ã
int32_t dbm_get(TkrzwDBM*, const char* key_ptr, size_t key_size, char** value_ptr_ptr, size_t* value_size_ptr); ... char* value_ptr = NULL; size_t value_size = 0; int32_t error_code = dbm_get("dbm, "hello", &value_ptr, &value_size);
ããã§ãPOSIXã®errnoãWindowsã®GetLastErrorã¨åãããã«ãã¹ãã¼ã¿ã¹ã³ã¼ãã¯ã¹ã¬ãããã¼ã«ã«ã¹ãã¬ã¼ã¸ã§ç®¡çãããã¨ã«ãããtkrzw_last_status_codeãtkrzw_last_status_messageã¨ãã£ãé¢æ°ã§ãç´åã®é¢æ°å¼ã³åºãã®ã¨ã©ã¼æ å ±ãåå¾ãããã¨ãã§ããããããã¯ã¹ã¬ããæ¯ã«å¥ã ã®ãã¼ã¿ãä¿æããã®ã§ãã¹ã¬ããã»ã¼ãã§ããã
char* value_ptr = dbm_get(dbm, "hello", 5, NULL); if (value_ptr) { puts(value_ptr); free(value_ptr); } else { print("Error: %d: %s\n", tkrzw_last_status_code(), tkrzw_last_status_message()); }
ã¤ãã§ã«è¨ãã¨ãæååã表ãã®ã«stringã®ä»£ããã«char*ã使ãå ´åãæ«å°¾ã®å ã«ãã«ã³ã¼ã0x00ãçªå µã¨ãã¦ç½®ãå¿ è¦ãããããã¤ããªãã¼ã¿ãæ±ããããã«ããããããã¤ã³ã¿ã¨ãµã¤ãºã§ç®¡çããã®ãåæã§ã¯ããããgetã¡ã½ãããªã©ãè¿ããã¤ãåã«ã¯ãã¾ãã§ãã«ã³ã¼ãã®çªå µãç½®ãããä»æ§ã«ãªã£ã¦ããããã£ã¦ãè¿ããããã¤ã³ã¿ããã®ã¾ã¾Cæåååæã®å¦çã«ä½¿ããã¨ãã§ããã
ä»ã«ããããããé¢åã ãstringãvectorãmapããªãã®ã§ãååä»ããã©ã¡ã¼ã¿ã®key/valueé£æ³é åãç°¡æ½ã«è¡¨ç¾ããã®ãé£ãããè¦èã®çã¨ãã¦ã"key1=value1,key2=value2,..." ã®ãããªæ¸å¼æååã渡ãæ¹æ³ã«ããããã®æååãåçã«ä½ãã®ã¯ããã£ã¦é¢åãããæ°ããããããã¼ãã³ã¼ãããåã«ã¯æ¥½ã ã
TkrzwDBM *dbm = tkrzw_dbm_open("casket.tkh", true, "truncate=true,num_buckets=100");
Cã®ãªã½ã¼ã¹ç®¡çã®ç ©éãã端çã«è¡¨ãã®ãããã®é¢æ°ã ãããtkrzw_dbm_searchã¯ãã¢ã¼ãã¨ãã¿ã¼ã³ã®æååãåãåããããã«ããããããã¼ã®ãªã¹ãããã¼ã¿ãã¼ã¹å ã§æ¢ãã¦è¿ããã¢ã¼ãã«ã¯ä¸éä¸è´ãåæ¹ä¸è´ãå¾æ¹ä¸è´ãæ£è¦è¡¨ç¾ãç·¨éè·é¢ãæå®ã§ãããã¢ã«ã´ãªãºã ã®è©³ç´°ã¯å é¨ã«é è½ããããã¼ã¿ãã¼ã¹ã®ã¯ã©ã¹ã«å¿ããæåã®æ段ãèªåçã«æ¡ç¨ããããè¦ã¯ããã¡ã便å©ãªæ©è½ãªã®ã ããCã ã¨å¦ã«è¤éãªã¤ã³ã¿ã¼ãã§ã¤ã¹ã«ãªããC++ã ã¨ãçµæ㯠vector < string > ã¨ãã¦è¿ãã°ããã®ã ããCã«ã¯vectorãstringããªãããã£ã¦ãmallocã§ç¢ºä¿ãããã¤ã³ã¿ã®é åã®åã ã®è¦ç´ ã«mallocãããã¤ãåã¨ãã®ãµã¤ãºãæãããè¦ç´ æ°ãã¾ãå¥ã®å¤æ°ã§æãããã¨ãããã¨ã«ãªããããããIliffe vectorã¨ãããã¤ã ã
typedef struct { char* ptr; int32_t size; } TkrzwStr; TkrzwStr* tkrzw_dbm_search( TkrzwDBM* dbm, const char* mode, const char* pattern_ptr, int32_t pattern_size, int32_t capacity, bool utf, int32_t* num_matched); void tkrzw_free_str_array(TkrzwStr* array, int32_t size);
TkrzwStrã¯stringã®ä»£ç¨ã§ããã®é åã¯vectorã®ä»£ç¨ã ããããã使ãçµãã£ããæ示çã«ç ´æ£ããå¿ è¦ããããããå°ç¨ã®tkrzw_free_str_arrayã¨ããé¢æ°ãç¨æãã¦ããããã®æã®åçºæããã¡ãã¡ã§ããªãããããªãã¨ããã®ããCããã°ã©ãã³ã°ã®æãæ²ããã¨ããã ãéã«è¨ãã°ãC++ã®ä¾¿å©ãã®ååãããã¯STLã®ä¾¿å©ãããæ¥ã¦ããã¨è¨ãããããããªãã
Cã«ã¯é¢æ°ãªãã¸ã§ã¯ããã©ã ãå¼ããªãã®ã§ãã³ã¼ã«ããã¯ããé¢æ°ãã¤ã³ã¿ã¨ãã®ã³ã³ããã¹ãã®voidãã¤ã³ã¿ã«åãã¦æ¸¡ãå¿ è¦ããããä¾ãã°ãã¬ã³ã¼ãã®å¤ã10é²æ°ã®æååã¨ã¿ãªãã¦ã¤ã³ã¯ãªã¡ã³ãããã³ã¼ã«ããã¯é¢æ°ã¯ä»¥ä¸ã®ããã«ä½¿ãã
const char* proc_increment(void* arg, const char* key_ptr, int32_t key_size, const char* value_ptr, int32_t value_size, int32_t* new_value_size) { // æ°ããå¤ã®æ ¼ç´å ãå¼ã³åºãå´ããåãåã char* num_buf = (char*)arg; // å¤ãå¤ãæ°å¤ã«å¤æãã int64_t num_value = 0; if (value_ptr != nullptr) { num_value = atoi(value_ptr); } // æ°ããå¤ãæ ¼ç´å ã«è¨æ¶ãã *new_value_size = sprintf(num_buf, "%d", num_value + 1); // æ°ããå¤ã®å ´æããã¼ã¿ãã¼ã¹ã«æãã return num_buf; } void do_increment(TkrzwDBM* dbm, const char* key_ptr, int32_t key_size) { // å¤ã®æ ¼ç´å ãã³ã¼ã«ããã¯å¤ã«ç¢ºä¿ãã¦å¯¿å½ã管çãã char num_buf[32]; // 該å½ã®ã¬ã³ã¼ãã«ã³ã¼ã«ããã¯ãå¼ã³åºãã tkrzw_dbm_process(dbm, key_ptr, key_size, proc_increment, &num_buf, true); }
ã¨ãã£ãæãã§ãCã«ç¡çãããªãã¸ã§ã¯ãæåçãªä½æ³ãæã¡è¾¼ãã§ãã¦ã20ä¸ç´ã«æ»ã£ããã®ãããªæããããã湧ãä¸ãããã¨ããããã¤ãã¬ã¼ã¿ãCompareExchangeããã®ä»ã®ã¦ã¼ãã£ãªãã£ãå®è£ ããã®ã§ãCãããTkrzwã®ä¸»è¦æ©è½ãæ®éã«ä½¿ãããå®éã®APIã«ã¤ãã¦ã¯こちらの文書ãã覧ããã ãããã
ã¾ã¨ããTkrzwã®C++ã®APIãã©ããããCè¨èªã®ã¤ã³ã¿ã¼ãã§ã¤ã¹ãç¨æãããããã«ãã£ã¦ãCè¨èªä½¿ãã«ãæ°è»½ã«ãã¼ã¿ãã¼ã¹ããã°ã©ãã³ã°ãå§ãã¦ãããããã¾ããã·ã³ãã«ããã³ã°ãªã³ã°ãããªãã¦æ¸ãã®ã§ãä»ã®è¨èªã®ã¤ã³ã¿ã¼ãã§ã¤ã¹ãæ¸ãéã«ã楽ã«ãªãã ããã