ãã¼ã¿ãã¼ã¹ã®å ç¢æ§ãé«ããããã«ããã¼ã¿ãã¼ã¹æ¬ä½ã¨ã¯å¥ã«æ´æ°å 容ãä¿åãããæ´æ°ãã°ã¨ããææ³ããããããã«ãæ´æ°ãã°ãé次å¥ãµã¼ãã«è»¢éãã¦é©ç¨ããã°ãã¬ããªã±ã¼ã·ã§ã³ãæç«ããããã®è¨äºã§ã¯ãã¬ããªã±ã¼ã·ã§ã³ã«ã使ããæ´æ°ãã°ã®è¦ä»¶ã«ã¤ãã¦ã¾ã¨ããã
Kyoto Tycoonã«ã¯ã¬ããªã±ã¼ã·ã§ã³æ©è½ãããã詳ããã¯Kyoto Tycoon導入ガイドãã覧ããã ããããåºæ¬çã«ã¯ããã¨åãæ©è½ãTkrzw-RPCã«ãå®è£ ãããRDBMSã®ç¨èªã§è¨ãã°ãè¡ã¬ãã«æ´æ°ãã°ã¨ãããã¨ã«ãªãã
æ´æ°ãã°ã¨ãã¦è¨é²ãã¹ãå 容ã¯å¤§ãã2種é¡ã«åãã¦èããããã便å®ä¸ãå¤çãã°ã¨å çãã°ã¨å¼ã¼ããå¤çãã°ã¨ã¯ããµã¼ãã¹ã¬ãã«ã®ã¡ã½ããã¨ãã®ãã©ã¡ã¼ã¿ãè¨é²ãããã®ã ãä¾ãã°ã"hoge"ã¬ã³ã¼ãã®å¤ã1å¢ããã¨ããIncrementã¡ã½ãããå®è¡ãããã¨ããã¨ãå¤çãã°ã«ã¯ãIncrement("hoge", 1)ããªã©ã¨ããã¬ã³ã¼ããè¨é²ãããä¸æ¹ã§å çãã°ã¯ãã¡ã½ãããå®è¡ããçµæã¨ãã¦ã®ãã¼ã¿ãã¼ã¹å ã®ã¬ã³ã¼ãã®ææ°ç¶æ ãè¨é²ãããã®ã ãIncrementã¡ã½ããã®çµæã¨ãã¦"hoge"ã¬ã³ã¼ãã®å¤ã"8"ã«ãªã£ãã¨ããã¨ããset("hoge", "8")ããªã©ã¨ãããã¼ã¿ãè¨é²ããã
å¤çãã°ã®å©ç¹ã¯ãããã¯ã¨ã³ãã®ãã¼ã¿ãã¼ã¹å®è£ ããã®å 容ã«ä¾åããã«ããã®å©ç¨è ã§ãããµã¼ãããã°ã©ã ã®æ©è½ã ãã§å®è£ ã§ãããã¨ã§ãããæ¬ ç¹ã¯ãåªçï¼ã¹ãã¨ãï¼Idempotentï¼ã§ã¯ãªããã¨ã ãåªçã¨ã¯ãåãæä½ãä½åº¦é©ç¨ãã¦ãåãçµæãå¾ããããã¨ã ããä¸è¿°ã®Incrementãè¤æ°åé©ç¨ããã¨çµæãå¤ããã®ã¯æããã§ãããã¤ã¾ãããã¯åªçãªæä½ã§ã¯ãªããåªçã§ãªãå ´åãäºéã«ã¯é©ç¨ã§ããªãããã£ã¦ãæ´æ°ãã°ãé©ç¨ããéã«ãã©ãããã©ãã¾ã§é©ç¨ããã®ããå³å¯ã«å¤æããã°ãªããªããã¿ã¤ã ã¹ã¿ã³ãã§ç®¡çããã«ãã¦ããOSä¸ã®æéã¯æ»ããã¨ãããã®ã§ãå³å¯ã«ç®¡çããã®ã¯é£ããã®ã ã
å çãã°ã®å©ç¹ã¨æ¬ ç¹ã¯å¤çãã°ã®ããããå転ãããã®ã ãããã¯ã¨ã³ãã®ãã¼ã¿ãã¼ã¹ã®ä¸ã«æ´æ°ãã°ã®åå¾æ©è½ãæ³¨å ¥ãã¦å®è£ ããå¿ è¦ãããã¨ããã®ãæ¬ ç¹ã§ãããä¸æ¹ã§ãæ´æ°ãã°ã®å 容ãåªçã«ãªãã®ã§ãæéãé¡ã£ã¦é©ç¨ãã¦ãåé¡ãªãããã¼ã¿ãã¼ã¹ã¯æ´åæ§ãå½ã§ãåªçæ§ã¯ãã®åæã¨ãªãéè¦ãªæ§è³ªã ããã£ã¦ãã§ãããªãå çãã°ãæ¡ç¨ãã¹ãã ãããã¦ãç¾å¨ã®ã¨ããããã¯ã¨ã³ããç§èªèº«ãæ¸ãã¦ãããããªã®ã§ãå çãã°ã®å®è£ ã¯å¯è½ã§ãããKyoto Tycoonãå çãã°ã ããTkrzw-RPCãå çãã°ãæ¡ç¨ããã
æ´æ°ãã°ã®å³æ転éï¼ã¬ããªã±ã¼ã·ã§ã³ï¼ãå®è£ ããªãã¦ãããã·ã³åä½ã§å ç¢æ§ãé«ããã®ã«æ´æ°ãã°ã¯å½¹ç«ã¤ããã¼ã¿ãã¼ã¹ãã¡ã¤ã«ãå£ããå ´åã«ãããã¯ã¢ãããã¡ã¤ã«ã ããã復å ããã¨ãããã¯ã¢ãããä½æãã¦ããç¾å¨ã¾ã§ã®æ´æ°ã失ããã¦ãã¾ããæ´æ°ãã°ãåå¾ãã¦ããã°ãããã¯ã¢ãããã¡ã¤ã«ã«æ´æ°ãã°ãé©ç¨ãã¦ãææ°ç¶æ ã«å¾©å ã§ããã®ã ã
å çãã°ã®æ´æ°ãã°ãå®è£ ãããã¨ã決æãããã¨ãªãã¨ãTkrzwã®å ¨7ã¤ã®ãã¼ã¿ãã¼ã¹ã¯ã©ã¹ã«å çãã°ã®å®è£ ãæ³¨å ¥ããå¿ è¦ããããããã¯æ£ç´ã ãããããããæ´æ°ãã°ãè¨é²ããæ©è½ã¯RPCã§ä½¿ãå ´å以å¤ã§ãå ç¢æ§ãé«ããã®ã«æç¨ãªã®ã§ãTkrzwæ¬ä½ã®æ©è½ã¨ãã¦å®è£ ãã¦ããã¦æã¯ãªãã ãããé½åã®è¯ããã¨ã«ãåãã¼ã¿ãã¼ã¹å®è£ ã§ã¯ãä»»æã®ã³ã¼ã«ããã¯ã§ã¬ã³ã¼ããã¼ã¿ã®åç §ãæ´æ°ãã§ããããã«æè»æ§ãæããã¦ããã®ã§ããããåå©ç¨ããã°ãããã¤ã¾ãããæ´æ°ãã¬ã¼ãã®ã³ã¼ã«ããã¯ãæ³¨å ¥ãã¦ããã¦ãæ´æ°ç¨ã®ã³ã¼ã«ããã¯ã«æ¸¡ãããã©ã¡ã¼ã¿ã¨è¿ããããã¼ã¿ããã®ã¾ã¾æ´æ°ãã¬ã¼ã®ã³ã¼ã«ããã¯ã«ã渡ãã°è¯ããæ´æ°ç¨ã®ã³ã¼ã«ããã¯ã®ã¤ã³ã¿ã¼ãã§ã¤ã¹ã¯ä»¥ä¸ã®ã¯ã©ã¹ã§ããã
class RecordProcessor { public: // æ¢åã®ã¬ã³ã¼ããããå ´åã«å¼ã°ããã // è¿ããå¤ãæ°ããªå¤ã«ãªããREMOVEã¨ããç¹æ®å¤ãè¿ãã¨ãã¬ã³ã¼ããæ¶ããã std::string_view ProcessFull(std::string_view key, std::string_view value); // æ¢åã®ã¬ã³ã¼ãããªãå ´åã«å¼ã°ãããæ»ãå¤ã®æå³ã¯åãã std::string_view ProcessEmpty(std::string_view key); };
ã³ã¼ã«ããã¯ã®æ»ãå¤ãä¿åããã®ã ããå½ç¶ãã©ã¡ã¼ã¿ã®ãã¼ãä¿åãããåªçãåæã¨ããã¨ããã©ã¡ã¼ã¿ã®å¤ã¯ä¿åããªãã¦ããããã¾ããå ¨ã¬ã³ã¼ããæ¶ãClearæä½å°ç¨ã®ã³ã¼ã«ããã¯ãç¨æããã
class UpdateLogger { public: // ã¬ã³ã¼ãã®æ°ããå¤ãè¿ãããå ´åã«å¼ã°ããã Status WriteSet(std::string_view key, std::string_view value); // ç¹æ®å¤Removeãè¿ãããå ´åã«å¼ã°ããã Status WriteRemove(std::string_view key); // Clearã¡ã½ãããå¼ã°ããå ´åã«å¼ã°ããã Status WriteClear(); };
ãã®ãããªDependency Injectionæ¹å¼ã«ãã¦ãããã¨ã§ããã¹ããç°¡åã«ãªãããTkrzw-RPCã®æ´æ°ãã°ã®å ·ä½çãªå½¢å¼ãTkrzwã®ãã¼ã¿ãã¼ã¹ãç¥ãå¿ è¦ããªããªããããã«ãTkrzw以å¤ã®ãã¼ã¿ãã¼ã¹ã«ããUpdateLoggerãæ³¨å ¥ã§ããã©ããã¼ãæ¸ãã°ã対å¿ã§ããããã«ãªãã
Writeãå¼ã°ããéã«è¨é²ãããã¼ã¿ã®å½¢å¼ãè¨è¨ããã以ä¸ã®å±æ§ãã·ãªã¢ã©ã¤ãºãããã¼ã¿ãä¿åãããã¨ã«ãªãã ããã
- ã¿ã¤ã ã¹ã¿ã³ã
- ãªãªã¸ã³ãµã¼ãIDï¼ãµã¼ãåºæã®å¤ï¼
- ãã¼ã¿ãã¼ã¹IDï¼ãµã¼ãä¸ã®ä½çªç®ã®ãã¼ã¿ãã¼ã¹ãï¼
- æä½ã¿ã¤ãï¼SetãRemoveãClearï¼
- ãã¼ï¼ãµã¤ãºã¨ãã¼ã¿ï¼
- å¤ï¼ãµã¤ãºã¨ãã¼ã¿ï¼
å ¨ã¦ã®ãµã¼ãã«ã¯IDãå²ãå½ã¦ã¦ããããªãªã¸ã³ãµã¼ãIDã¨ã¯ã該å½ã®æ´æ°æä½ãã¯ã©ã¤ã¢ã³ãããåãä»ãããµã¼ãã®IDã§ãããããããããã¦ããã¨ããã¥ã¢ã«ãã¹ã¿ãªã©ã®ã循ç°æ§é ã®ã¬ããªã±ã¼ã·ã§ã³ãå¯è½ã«ãªããã¬ããªã±ã¼ã·ã§ã³æã«åãåã£ãæ´æ°ãã°ã®ãªãªã¸ã³ãµã¼ãIDãèªåã¨åãã ã£ãå ´åã«ç¡è¦ãããã¨ã§ãç¡éã«ã¼ããæã¡åããã¨ãã§ãããå®éã«ã¯ã転ééãæ¸ããããã«ãæ´æ°ãã°ã®éä¿¡æã«ç¸æã®ãµã¼ãIDã調ã¹ã¦éä¿¡ãçç¥ãããã¨ã«ãªãã
Tkrzw-RPCã®ãµã¼ãã¯è¤æ°ã®ãã¼ã¿ãã¼ã¹ãåæã«éç¨ã§ããã®ã§ããã®IDãå¿ è¦ã«ãªãã0ãèµ·ç¹ã¨ããã¤ã³ããã¯ã¹ã ãã¬ããªã±ã¼ã·ã§ã³ã«åå ããå ¨ã¦ã®ãµã¼ãã¯åãæ§æã§ãã¼ã¿ãã¼ã¹ãéç¨ãã¦ãããã¨ãåæã¨ãããã¨ã¯ãããå®éã«ã¯ãã¥ã¼ã¤ã³ã°ãå¤ãã¦ããããããã¼ã¿ãã¼ã¹ã¯ã©ã¹ããå¤ãã¦ããããã¢ã¼ã«ã¤ãå°ç¨ã®ã¹ã¬ã¼ãã§ã¯å§ç¸®ãæå¹ã«ããã¨ããã§ããã
æä½ã¿ã¤ãã¯SetãRemoveãClearã§ãããSetã®å ´åããã¼ã¨å¤ãå¾ç¶ã«åããRemoveã®å ´åããã¼ã®ã¿ãå¾ç¶ã«åããClearã®å ´åãå¾ç¶ã«ã¯ä½ãåããªãããã®ä»ã®æä½ã¿ã¤ãã追ã£ã¦è¿½å ãããããããªãããå½é¢ã¯ãã®3種ã§è¯ãã ããã
ã·ãªã¢ã©ã¤ãºãã©ãããããèããããããã³ã«ãããã¡ã§ã·ãªã¢ã©ã¤ãºããã®ã楽ãªã®ã ããããã§ã¯ç©ºéå¹çãåªå ãã¦ç¬èªå½¢å¼ãæ¡ç¨ããã以ä¸ã®ãã¼ã¿ã並ã¹ãã
- ãã¸ãã¯ãã¼ã¿ : åºå®é·1ãã¤ãã0xFFã
- æä½ã¿ã¤ã : åºå®é·1ãã¤ãã0xA1=Setã0xA2=Removeã0xA3=Clearã
- ã¿ã¤ã ã¹ã¿ã³ã : åºå®é·5ãã¤ããUNIXæéã®ã»ã³ãç§ã
- ãªãªã¸ã³ãµã¼ãID : ãã¤ããã«ã¿ã¨ã³ã³ã¼ããå ¸åçã«ã¯1ãã¤ãã
- ãã¼ã¿ãã¼ã¹ID : ãã¤ããã«ã¿ã¨ã³ã³ã¼ããå ¸åçã«ã¯1ãã¤ãã
- ãã¼ : ãã¤ããã«ã¿ã¨ã³ã³ã¼ãã®ãµã¤ãºã¨ãä»»æã®ãã¤ãå
- å¤ : ãã¤ããã«ã¿ã¨ã³ã³ã¼ãã®ãµã¤ãºã¨ãä»»æã®ãã¤ãå
- ãã§ãã¯ãµã : åºå®é·1ãã¤ãã
ãã¸ãã¯ãã¼ã¿ã¨æä½ã¿ã¤ãã®çµã¿åããã¯ãä¸ãä¸ãã¼ã¿ãå£ããå ´åã®æ¤åºã«æç¨ã ãæ«å°¾ã«ç½®ããã§ãã¯ãµã ã«ã¯ã251ãæ³ã¨ãããã§ãã¯ãµã ã使ããæå¾ã«1ã足ãããã§ãã¯ãµã ã®ãããã§ã©ã³ãã ãã¤ãºã確ççã«æ¤åºã§ããã®ã¨ããã®å¤åã1以ä¸ã§ãããã¨ã§ãæ¸ãè¾¼ã¿éä¸ã§åå°½ãããã°ã確å®ã«æ¤åºã§ãããæ´æ°ãã°ãä½ããã®çç±ã§éä¸ã§å£ãã¦ããå ´åããã以éå ¨ã¦ãç¡è¦ããã®ã§ã¯ãªãã次ã®ã¬ã³ã¼ãã¾ã§èªã¿é£ã°ãã¦å¦çãç¶ããããªããããã®éã®èª¤æ¤åºã®ç¢ºçã¯ããã¸ãã¯ãã¼ã¿ã§1/256ã«ãªããæä½ã¿ã¤ãã§3/256ã«ãªãããã§ãã¯ãµã ã§1/251ã«ãªãã®ã§ã0.0000182%ã ãå®éã«ã¯ããã次ã®ã¬ã³ã¼ããè¦ã¤ããã¾ã§ã®ãã¤ãæ°ã ãè¡ãã®ã§ãå¹³å256ãã¤ããããã ã¨ããã¨ã0.0046%ãããã¡ã¤ã«ã®æ«å°¾ä»¥å¤ã§ã¯ããããã¦çºè¦ããã次ã®ã¬ã³ã¼ãã®åè£ã®æ¬¡ã®ã¬ã³ã¼ããåæ§ã«æ¤æ»ã§ããã®ã§ãå®è³ªã®èª¤æ¤åºçã¯ã¼ãã¨è¨ã£ã¦è¯ãã
ã¿ã¤ã ã¹ã¿ã³ãã5ãã¤ããªã®ã¯ã4ãã¤ãã§UNIXæéã表ãã¨2106å¹´ã«ãªã¼ãã¼ããã¼ããããã§ããã5ãã¤ãã«ããã°36812å¹´ã¾ã§åé¡ãå éãã«ã§ããããããã«ããã¯ãªã¼ãã¼ã¹ããã¯ãªã®ã§ã100ã§å²ã£ãã»ã³ãç§ã§æ±ã£ã¦ããã¬ã¼ãµããªãã£ãåä¸ãããã2318å¹´ãä¸éã«ãªãããç¾è¡ã·ã¹ãã ã®å¯¿å½ã¨ãã¦ã¯ååã ããã
ã¨ããã§ã人éã¨æ©æ¢°ã®ä¸¡æ¹ã«ã¨ã£ã¦æ±ãããããã¼ã¿ç³»åã¨ãã¦ã¯ãUNIXæéãåºæºã«ããã®ãç¾å®çã ããUNIXæéãæ»ãå¯è½æ§ã«ã¯æ³¨æãè¦ãããæåãããã¯ITPçã«ããèªå調æ´ã§ã·ã¹ãã ã®æéã¯æ»ããéç§ã®éã«ã¯åãæå»ã2ç§ç¶ããããã®éã«ç§æªæºã®æ å ±ãã©ããªããã¯å®è£ ä¾åã ãæ´æ°ãã°ã®ã¿ã¤ã ã¹ã¿ã³ãã¯å¸¸ã«å¢å ãç¶ããããå°ãªãã¨ãæ¸ããªããã¨ãå¿ è¦ã ããUNIXæéããã®ã¾ã¾è¨é²ããã¨ããã«éåãããã¨ã«ãªãããã£ã¦ãæ´æ°ãã°ãè¨é²ããéã«ã¯ãç´åã«ä½¿ã£ãã¿ã¤ã ã¹ã¿ã³ãã¨ç¾å¨åå¾ããã¿ã¤ã ã¹ã¿ã³ãã®å¤§ããæ¹ã使ããã¨ã«ãªããã¤ã¾ããæéãæ»ã£ãå ´åã«ã¯ãåãã¿ã¤ã ã¹ã¿ã³ãã®ãã°ãé£ç¶ãããã¨ã«ãªããæéã®éè¡ã大ããå ´åãä½ç¾ä¸åãç¶ããããããªãããããã¯ã¿ã¤ã ã¹ã¿ã³ãä¸ã§ã¯åºå¥ã§ããªãããæ´æ°ãçºçããé åºã¨è¨é²ã®é åºã«ä¸è²«æ§ãããããããã°ãåé¡ãªããæ´æ°ãã°ãé©ç¨ããéã«ã¯ãæéã®éè¡ãèãã¦ååã«ç¶äºãæã£ãã¨ããããå§ããã°ããã®ã ãåªçæ§ãããã§éè¦ã¨ãªãã
çæããæ´æ°ãã°ãã©ããã£ã¦ãã¡ã¤ã«ã«æ¸ãè¾¼ããããã¼ãã¼ã·ã§ã³ã¯ã©ãããããã¾ããã®æ´æ°ãèªã¿åºãå´ã«ã©ããã£ã¦éç¥ããããªã©ã®å®è£ ã®è©³ç´°ã¯ãã¬ããªã±ã¼ã·ã§ã³ã®å®è£ æã«è¿½ã£ã¦è©°ãããã¨ããããä»ã¯ãæ´æ°ãã°ã®ä»æ§ã«åé¡ããªããã ãã確èªãããã
æ´æ°ãã°ãèªã¿è¾¼ãã«ã¯ããããããã¼ãã¼ã·ã§ã³ã«ãã£ã¦è¤æ°ã«åå²ãããæ´æ°ãã°ã®ãã¡ã¤ã«ã®æ¥é è¾ãæå®ããã¨ã¨ãã«ããªãã·ã§ã³ã¨ãã¦ãèµ·ç¹ã¨ãªãã¿ã¤ã ã¹ã¿ã³ãã¨ãµã¼ãIDãæå®ãããèªã¿è¾¼ãã æ´æ°ãã°ãé©ç¨ããéã«ã¯ããã¼ã¿ãã¼ã¹IDã使ã£ã¦å¯¾è±¡ã®ãã¼ã¿ãã¼ã¹ãç¹å®ããä¸ã§ããã°ã®åã ã®ã¬ã³ã¼ããèªã¿è¾¼ãã§ããã®æä½ã¿ã¤ãã«å¿ãã¦SetãRemoveãClearãå¼ã¹ã°è¯ããããã¯ã¢ãããã¡ã¤ã«ã«æ´æ°ãã°ãé©ç¨ããã¦ã¼ãã£ãªãã£ã³ãã³ãã¯ä»¥ä¸ã®ããã«å®è¡ããã ããã
$ ls tkrzw-ulog.* casket-ulog.1631289371 casket-ulog.1631292834 casket-ulog.1631219232 $ tkrzw_dbm_util --db_id 0 --since 1631209232 casket-backup-20210819.tkh casket-ulog set=2342342, remove=54829, elapsed_time=18.752
Tkrzw-RPCã§ãä»ã®ãµã¼ãå´ããæ´æ°ãã°ãåå¾ããã¬ããªã±ã¼ã·ã§ã³æ©è½ãæå¹ã«ãã¤ã¤ããã¤èªåãæ´æ°ãã°ãè¨é²ããã«ã¯ã以ä¸ã®ããã«è¨å®ããã®ããªã詳細ã¯å¤ããã¨æããã©ã
$ tkrzw_server --server_id 1 --replication "master=host01:1978,since=1631209232" \ --ulog_prefix casket-ulog --ulog_max_size 100Mi \ "casket.tkh#num_buckets=10M"
ã¨ããã§ããµã¼ãã®æ®éã®ã¢ã¯ã»ã¹ãã°ã«ã¯ãã¼ãã¼ã·ã§ã³æ©è½ãã¤ããªãã®ã«ãæ´æ°ãã°ã«ã¯ãã¼ãã¼ã·ã§ã³æ©è½ãã¤ããã¨ããã®ã¯ãä¸è²«æ§ã«æ¬ ããæ 度ã ã¨æããããããããªããã¨ããããç§ãæããããããæ´æ°ãã°ã¯ããã°ã¨ããååã¯ä»ãã¦ããããå®éã«ã¯ãã¼ã¿ãã¼ã¹ã®ä¸é¨ãªã®ã§ããã¼ãã¼ã·ã§ã³ã¾ã§é¢åãè¦ã¦ãããªãã¨ã©ããããããªããããã¦ãæ®éã®ãã°ã¯æ¨æºåºåãªã©ã®ãã¡ã¤ã«ä»¥å¤ã®åºåå ãæ³å®ããªãã¨ãããªãã®ã§ããããããã¼ãã¼ã·ã§ã³ã§ããã¨ã¯éããªããæ¨æºåºåããã¼ãã¼ã·ã§ã³ãããã¡ã¤ã«ã«æ¸ãè¾¼ãApacheã®rotatelogã¨ããçµã¿åããããããããªãããæ¨æºåºåã§ã§ããäºã«ã¯ä½è¨ãªãã¨ãããªãæ¹ãUNIXçãªã®ã ã
ã¾ã¨ãããã¼ã¿ãã¼ã¹ã®æ´æ°ãã°ã®ä»æ§ãèãããæ´æ°ãã°ãããã¨ãããã¯ã¢ãããã¡ã¤ã«ããã®é害復æ§æã«ææ°ç¶æ ã¾ã§å¾©å ã§ããã¨ããå©ç¹ããããæ´æ°ãã°ãä»ã®ãµã¼ãã«è»¢éããã°ãã¬ããªã±ã¼ã·ã§ã³ãå®ç¾ã§ãããKyoto CabinetãKyoto Tycoonã§ãå®è£ ããæ©è½ãªã®ã§ããã®è¨è¨ã§åé¡ãªãã ããã