heroku-tokyotyrant 0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (340) hide show
  1. data/COPYING +504 -0
  2. data/README.rdoc +231 -0
  3. data/Rakefile +72 -0
  4. data/benchmarks/balancer.rb +101 -0
  5. data/benchmarks/bulk_db.rb +74 -0
  6. data/benchmarks/bulk_table.rb +87 -0
  7. data/benchmarks/db.rb +114 -0
  8. data/benchmarks/table.rb +161 -0
  9. data/ext/extconf.rb +43 -0
  10. data/ext/tokyo/bin/tcamgr +0 -0
  11. data/ext/tokyo/bin/tcamttest +0 -0
  12. data/ext/tokyo/bin/tcatest +0 -0
  13. data/ext/tokyo/bin/tcbmgr +0 -0
  14. data/ext/tokyo/bin/tcbmttest +0 -0
  15. data/ext/tokyo/bin/tcbtest +0 -0
  16. data/ext/tokyo/bin/tcfmgr +0 -0
  17. data/ext/tokyo/bin/tcfmttest +0 -0
  18. data/ext/tokyo/bin/tcftest +0 -0
  19. data/ext/tokyo/bin/tchmgr +0 -0
  20. data/ext/tokyo/bin/tchmttest +0 -0
  21. data/ext/tokyo/bin/tchtest +0 -0
  22. data/ext/tokyo/bin/tcrmgr +0 -0
  23. data/ext/tokyo/bin/tcrmttest +0 -0
  24. data/ext/tokyo/bin/tcrtest +0 -0
  25. data/ext/tokyo/bin/tctmgr +0 -0
  26. data/ext/tokyo/bin/tctmttest +0 -0
  27. data/ext/tokyo/bin/tcttest +0 -0
  28. data/ext/tokyo/bin/tcucodec +0 -0
  29. data/ext/tokyo/bin/tcumttest +0 -0
  30. data/ext/tokyo/bin/tcutest +0 -0
  31. data/ext/tokyo/bin/ttserver +0 -0
  32. data/ext/tokyo/bin/ttulmgr +0 -0
  33. data/ext/tokyo/bin/ttultest +0 -0
  34. data/ext/tokyo/include/tcadb.h +548 -0
  35. data/ext/tokyo/include/tcbdb.h +1101 -0
  36. data/ext/tokyo/include/tcfdb.h +858 -0
  37. data/ext/tokyo/include/tchdb.h +871 -0
  38. data/ext/tokyo/include/tcrdb.h +801 -0
  39. data/ext/tokyo/include/tctdb.h +1086 -0
  40. data/ext/tokyo/include/tculog.h +392 -0
  41. data/ext/tokyo/include/tcutil.h +4184 -0
  42. data/ext/tokyo/include/ttutil.h +494 -0
  43. data/ext/tokyo/lib/libtokyocabinet.9.4.0.dylib +0 -0
  44. data/ext/tokyo/lib/libtokyocabinet.9.dylib +0 -0
  45. data/ext/tokyo/lib/libtokyocabinet.a +0 -0
  46. data/ext/tokyo/lib/libtokyocabinet.dylib +0 -0
  47. data/ext/tokyo/lib/libtokyotyrant.3.22.0.dylib +0 -0
  48. data/ext/tokyo/lib/libtokyotyrant.3.dylib +0 -0
  49. data/ext/tokyo/lib/libtokyotyrant.a +0 -0
  50. data/ext/tokyo/lib/libtokyotyrant.dylib +0 -0
  51. data/ext/tokyo/lib/pkgconfig/tokyocabinet.pc +14 -0
  52. data/ext/tokyo/lib/pkgconfig/tokyotyrant.pc +14 -0
  53. data/ext/tokyo/lib/ttskeldir.bundle +0 -0
  54. data/ext/tokyo/lib/ttskelmock.bundle +0 -0
  55. data/ext/tokyo/lib/ttskelnull.bundle +0 -0
  56. data/ext/tokyo/lib/ttskelproxy.bundle +0 -0
  57. data/ext/tokyo/libexec/tcawmgr.cgi +0 -0
  58. data/ext/tokyo/sbin/ttservctl +163 -0
  59. data/ext/tokyo/share/man/man1/tcamgr.1 +97 -0
  60. data/ext/tokyo/share/man/man1/tcamttest.1 +35 -0
  61. data/ext/tokyo/share/man/man1/tcatest.1 +55 -0
  62. data/ext/tokyo/share/man/man1/tcbmgr.1 +125 -0
  63. data/ext/tokyo/share/man/man1/tcbmttest.1 +81 -0
  64. data/ext/tokyo/share/man/man1/tcbtest.1 +107 -0
  65. data/ext/tokyo/share/man/man1/tcfmgr.1 +98 -0
  66. data/ext/tokyo/share/man/man1/tcfmttest.1 +62 -0
  67. data/ext/tokyo/share/man/man1/tcftest.1 +73 -0
  68. data/ext/tokyo/share/man/man1/tchmgr.1 +110 -0
  69. data/ext/tokyo/share/man/man1/tchmttest.1 +85 -0
  70. data/ext/tokyo/share/man/man1/tchtest.1 +95 -0
  71. data/ext/tokyo/share/man/man1/tcrmgr.1 +164 -0
  72. data/ext/tokyo/share/man/man1/tcrmttest.1 +55 -0
  73. data/ext/tokyo/share/man/man1/tcrtest.1 +89 -0
  74. data/ext/tokyo/share/man/man1/tctmgr.1 +140 -0
  75. data/ext/tokyo/share/man/man1/tctmttest.1 +92 -0
  76. data/ext/tokyo/share/man/man1/tcttest.1 +105 -0
  77. data/ext/tokyo/share/man/man1/tcucodec.1 +162 -0
  78. data/ext/tokyo/share/man/man1/tcumttest.1 +41 -0
  79. data/ext/tokyo/share/man/man1/tcutest.1 +81 -0
  80. data/ext/tokyo/share/man/man1/ttserver.1 +84 -0
  81. data/ext/tokyo/share/man/man1/ttulmgr.1 +40 -0
  82. data/ext/tokyo/share/man/man1/ttultest.1 +16 -0
  83. data/ext/tokyo/share/man/man3/tcadb.3 +676 -0
  84. data/ext/tokyo/share/man/man3/tcbdb.3 +1355 -0
  85. data/ext/tokyo/share/man/man3/tcfdb.3 +975 -0
  86. data/ext/tokyo/share/man/man3/tchdb.3 +898 -0
  87. data/ext/tokyo/share/man/man3/tclist.3 +1 -0
  88. data/ext/tokyo/share/man/man3/tcmap.3 +1 -0
  89. data/ext/tokyo/share/man/man3/tcmdb.3 +1 -0
  90. data/ext/tokyo/share/man/man3/tcmpool.3 +1 -0
  91. data/ext/tokyo/share/man/man3/tcrdb.3 +1309 -0
  92. data/ext/tokyo/share/man/man3/tctdb.3 +1110 -0
  93. data/ext/tokyo/share/man/man3/tctree.3 +1 -0
  94. data/ext/tokyo/share/man/man3/tculog.3 +15 -0
  95. data/ext/tokyo/share/man/man3/tcutil.3 +4518 -0
  96. data/ext/tokyo/share/man/man3/tcxstr.3 +1 -0
  97. data/ext/tokyo/share/man/man3/tokyocabinet.3 +132 -0
  98. data/ext/tokyo/share/man/man3/ttutil.3 +14 -0
  99. data/ext/tokyo/share/man/man8/ttservctl.8 +37 -0
  100. data/ext/tokyo/share/tokyocabinet/COPYING +504 -0
  101. data/ext/tokyo/share/tokyocabinet/ChangeLog +1252 -0
  102. data/ext/tokyo/share/tokyocabinet/THANKS +12 -0
  103. data/ext/tokyo/share/tokyocabinet/doc/benchmark.pdf +0 -0
  104. data/ext/tokyo/share/tokyocabinet/doc/common.css +211 -0
  105. data/ext/tokyo/share/tokyocabinet/doc/icon16.png +0 -0
  106. data/ext/tokyo/share/tokyocabinet/doc/index.html +156 -0
  107. data/ext/tokyo/share/tokyocabinet/doc/index.ja.html +197 -0
  108. data/ext/tokyo/share/tokyocabinet/doc/logo-ja.png +0 -0
  109. data/ext/tokyo/share/tokyocabinet/doc/logo.png +0 -0
  110. data/ext/tokyo/share/tokyocabinet/doc/spex-en.html +7145 -0
  111. data/ext/tokyo/share/tokyocabinet/doc/spex-ja.html +7476 -0
  112. data/ext/tokyo/share/tokyocabinet/doc/tokyoproducts.pdf +0 -0
  113. data/ext/tokyo/share/tokyocabinet/doc/tokyoproducts.ppt +0 -0
  114. data/ext/tokyo/share/tokyotyrant/COPYING +504 -0
  115. data/ext/tokyo/share/tokyotyrant/ChangeLog +578 -0
  116. data/ext/tokyo/share/tokyotyrant/THANKS +15 -0
  117. data/ext/tokyo/share/tokyotyrant/doc/common.css +211 -0
  118. data/ext/tokyo/share/tokyotyrant/doc/index.html +79 -0
  119. data/ext/tokyo/share/tokyotyrant/doc/spex.html +2264 -0
  120. data/ext/tokyo/share/tokyotyrant/ext/mapreduce.lua +57 -0
  121. data/ext/tokyo/share/tokyotyrant/ext/queue.lua +55 -0
  122. data/ext/tokyo/share/tokyotyrant/ext/senatus.lua +532 -0
  123. data/ext/tokyo/share/tokyotyrant/ext/usherette.lua +438 -0
  124. data/ext/tokyo_tyrant.c +147 -0
  125. data/ext/tokyo_tyrant.h +48 -0
  126. data/ext/tokyo_tyrant_db.c +227 -0
  127. data/ext/tokyo_tyrant_db.h +8 -0
  128. data/ext/tokyo_tyrant_module.c +453 -0
  129. data/ext/tokyo_tyrant_module.h +10 -0
  130. data/ext/tokyo_tyrant_query.c +226 -0
  131. data/ext/tokyo_tyrant_query.h +9 -0
  132. data/ext/tokyo_tyrant_table.c +319 -0
  133. data/ext/tokyo_tyrant_table.h +8 -0
  134. data/ext/tokyocabinet-1.4.41/COPYING +504 -0
  135. data/ext/tokyocabinet-1.4.41/ChangeLog +1252 -0
  136. data/ext/tokyocabinet-1.4.41/Makefile.in +825 -0
  137. data/ext/tokyocabinet-1.4.41/README +38 -0
  138. data/ext/tokyocabinet-1.4.41/THANKS +12 -0
  139. data/ext/tokyocabinet-1.4.41/bros/Makefile +133 -0
  140. data/ext/tokyocabinet-1.4.41/bros/bdbtest.c +438 -0
  141. data/ext/tokyocabinet-1.4.41/bros/cdbtest.c +219 -0
  142. data/ext/tokyocabinet-1.4.41/bros/cmpsqltctest.c +186 -0
  143. data/ext/tokyocabinet-1.4.41/bros/gdbmtest.c +216 -0
  144. data/ext/tokyocabinet-1.4.41/bros/mapreporter +72 -0
  145. data/ext/tokyocabinet-1.4.41/bros/maptest.cc +677 -0
  146. data/ext/tokyocabinet-1.4.41/bros/ndbmtest.c +204 -0
  147. data/ext/tokyocabinet-1.4.41/bros/qdbmtest.c +375 -0
  148. data/ext/tokyocabinet-1.4.41/bros/reporter +141 -0
  149. data/ext/tokyocabinet-1.4.41/bros/result.xls +0 -0
  150. data/ext/tokyocabinet-1.4.41/bros/sdbmtest.c +204 -0
  151. data/ext/tokyocabinet-1.4.41/bros/sqltest.c +404 -0
  152. data/ext/tokyocabinet-1.4.41/bros/tctest.c +748 -0
  153. data/ext/tokyocabinet-1.4.41/bros/tdbtest.c +205 -0
  154. data/ext/tokyocabinet-1.4.41/configure +7402 -0
  155. data/ext/tokyocabinet-1.4.41/configure.in +362 -0
  156. data/ext/tokyocabinet-1.4.41/doc/benchmark.pdf +0 -0
  157. data/ext/tokyocabinet-1.4.41/doc/common.css +211 -0
  158. data/ext/tokyocabinet-1.4.41/doc/icon16.png +0 -0
  159. data/ext/tokyocabinet-1.4.41/doc/index.html +156 -0
  160. data/ext/tokyocabinet-1.4.41/doc/index.ja.html +197 -0
  161. data/ext/tokyocabinet-1.4.41/doc/logo-ja.png +0 -0
  162. data/ext/tokyocabinet-1.4.41/doc/logo.png +0 -0
  163. data/ext/tokyocabinet-1.4.41/doc/spex-en.html +7145 -0
  164. data/ext/tokyocabinet-1.4.41/doc/spex-ja.html +7476 -0
  165. data/ext/tokyocabinet-1.4.41/doc/tokyoproducts.pdf +0 -0
  166. data/ext/tokyocabinet-1.4.41/doc/tokyoproducts.ppt +0 -0
  167. data/ext/tokyocabinet-1.4.41/example/Makefile +113 -0
  168. data/ext/tokyocabinet-1.4.41/example/tcadbex.c +55 -0
  169. data/ext/tokyocabinet-1.4.41/example/tcbdbex.c +64 -0
  170. data/ext/tokyocabinet-1.4.41/example/tcfdbex.c +60 -0
  171. data/ext/tokyocabinet-1.4.41/example/tchdbex.c +60 -0
  172. data/ext/tokyocabinet-1.4.41/example/tctchat.c +97 -0
  173. data/ext/tokyocabinet-1.4.41/example/tctchat.tmpl +141 -0
  174. data/ext/tokyocabinet-1.4.41/example/tctdbex.c +85 -0
  175. data/ext/tokyocabinet-1.4.41/example/tctsearch.c +95 -0
  176. data/ext/tokyocabinet-1.4.41/example/tctsearch.tmpl +122 -0
  177. data/ext/tokyocabinet-1.4.41/example/tcutilex.c +77 -0
  178. data/ext/tokyocabinet-1.4.41/f.tsv +2 -0
  179. data/ext/tokyocabinet-1.4.41/lab/calccomp +118 -0
  180. data/ext/tokyocabinet-1.4.41/lab/datechange +56 -0
  181. data/ext/tokyocabinet-1.4.41/lab/diffcheck +45 -0
  182. data/ext/tokyocabinet-1.4.41/lab/htmltotsv +102 -0
  183. data/ext/tokyocabinet-1.4.41/lab/magic +19 -0
  184. data/ext/tokyocabinet-1.4.41/lab/printenv.cgi +27 -0
  185. data/ext/tokyocabinet-1.4.41/lab/stepcount +26 -0
  186. data/ext/tokyocabinet-1.4.41/lab/stopwatch +61 -0
  187. data/ext/tokyocabinet-1.4.41/lab/tabcheck +43 -0
  188. data/ext/tokyocabinet-1.4.41/lab/wgettsv +239 -0
  189. data/ext/tokyocabinet-1.4.41/lab/widthcheck +57 -0
  190. data/ext/tokyocabinet-1.4.41/man/htmltoman +104 -0
  191. data/ext/tokyocabinet-1.4.41/man/tcadb.3 +676 -0
  192. data/ext/tokyocabinet-1.4.41/man/tcamgr.1 +97 -0
  193. data/ext/tokyocabinet-1.4.41/man/tcamttest.1 +35 -0
  194. data/ext/tokyocabinet-1.4.41/man/tcatest.1 +55 -0
  195. data/ext/tokyocabinet-1.4.41/man/tcbdb.3 +1355 -0
  196. data/ext/tokyocabinet-1.4.41/man/tcbmgr.1 +125 -0
  197. data/ext/tokyocabinet-1.4.41/man/tcbmttest.1 +81 -0
  198. data/ext/tokyocabinet-1.4.41/man/tcbtest.1 +107 -0
  199. data/ext/tokyocabinet-1.4.41/man/tcfdb.3 +975 -0
  200. data/ext/tokyocabinet-1.4.41/man/tcfmgr.1 +98 -0
  201. data/ext/tokyocabinet-1.4.41/man/tcfmttest.1 +62 -0
  202. data/ext/tokyocabinet-1.4.41/man/tcftest.1 +73 -0
  203. data/ext/tokyocabinet-1.4.41/man/tchdb.3 +898 -0
  204. data/ext/tokyocabinet-1.4.41/man/tchmgr.1 +110 -0
  205. data/ext/tokyocabinet-1.4.41/man/tchmttest.1 +85 -0
  206. data/ext/tokyocabinet-1.4.41/man/tchtest.1 +95 -0
  207. data/ext/tokyocabinet-1.4.41/man/tclist.3 +1 -0
  208. data/ext/tokyocabinet-1.4.41/man/tcmap.3 +1 -0
  209. data/ext/tokyocabinet-1.4.41/man/tcmdb.3 +1 -0
  210. data/ext/tokyocabinet-1.4.41/man/tcmpool.3 +1 -0
  211. data/ext/tokyocabinet-1.4.41/man/tctdb.3 +1110 -0
  212. data/ext/tokyocabinet-1.4.41/man/tctmgr.1 +140 -0
  213. data/ext/tokyocabinet-1.4.41/man/tctmttest.1 +92 -0
  214. data/ext/tokyocabinet-1.4.41/man/tctree.3 +1 -0
  215. data/ext/tokyocabinet-1.4.41/man/tcttest.1 +105 -0
  216. data/ext/tokyocabinet-1.4.41/man/tcucodec.1 +162 -0
  217. data/ext/tokyocabinet-1.4.41/man/tcumttest.1 +41 -0
  218. data/ext/tokyocabinet-1.4.41/man/tcutest.1 +81 -0
  219. data/ext/tokyocabinet-1.4.41/man/tcutil.3 +4518 -0
  220. data/ext/tokyocabinet-1.4.41/man/tcxstr.3 +1 -0
  221. data/ext/tokyocabinet-1.4.41/man/tokyocabinet.3 +132 -0
  222. data/ext/tokyocabinet-1.4.41/md5.c +381 -0
  223. data/ext/tokyocabinet-1.4.41/md5.h +101 -0
  224. data/ext/tokyocabinet-1.4.41/myconf.c +493 -0
  225. data/ext/tokyocabinet-1.4.41/myconf.h +549 -0
  226. data/ext/tokyocabinet-1.4.41/tcadb.c +4339 -0
  227. data/ext/tokyocabinet-1.4.41/tcadb.h +548 -0
  228. data/ext/tokyocabinet-1.4.41/tcamgr.c +1019 -0
  229. data/ext/tokyocabinet-1.4.41/tcamttest.c +542 -0
  230. data/ext/tokyocabinet-1.4.41/tcatest.c +1845 -0
  231. data/ext/tokyocabinet-1.4.41/tcawmgr.c +482 -0
  232. data/ext/tokyocabinet-1.4.41/tcbdb.c +4180 -0
  233. data/ext/tokyocabinet-1.4.41/tcbdb.h +1101 -0
  234. data/ext/tokyocabinet-1.4.41/tcbmgr.c +1012 -0
  235. data/ext/tokyocabinet-1.4.41/tcbmttest.c +1810 -0
  236. data/ext/tokyocabinet-1.4.41/tcbtest.c +2586 -0
  237. data/ext/tokyocabinet-1.4.41/tcfdb.c +2746 -0
  238. data/ext/tokyocabinet-1.4.41/tcfdb.h +858 -0
  239. data/ext/tokyocabinet-1.4.41/tcfmgr.c +786 -0
  240. data/ext/tokyocabinet-1.4.41/tcfmttest.c +1220 -0
  241. data/ext/tokyocabinet-1.4.41/tcftest.c +1695 -0
  242. data/ext/tokyocabinet-1.4.41/tchdb.c +5153 -0
  243. data/ext/tokyocabinet-1.4.41/tchdb.h +871 -0
  244. data/ext/tokyocabinet-1.4.41/tchmgr.c +842 -0
  245. data/ext/tokyocabinet-1.4.41/tchmttest.c +1757 -0
  246. data/ext/tokyocabinet-1.4.41/tchtest.c +2129 -0
  247. data/ext/tokyocabinet-1.4.41/tctdb.c +6199 -0
  248. data/ext/tokyocabinet-1.4.41/tctdb.h +1086 -0
  249. data/ext/tokyocabinet-1.4.41/tctmgr.c +1241 -0
  250. data/ext/tokyocabinet-1.4.41/tctmttest.c +1563 -0
  251. data/ext/tokyocabinet-1.4.41/tcttest.c +2062 -0
  252. data/ext/tokyocabinet-1.4.41/tcucodec.c +1357 -0
  253. data/ext/tokyocabinet-1.4.41/tcumttest.c +578 -0
  254. data/ext/tokyocabinet-1.4.41/tcutest.c +1875 -0
  255. data/ext/tokyocabinet-1.4.41/tcutil.c +10528 -0
  256. data/ext/tokyocabinet-1.4.41/tcutil.h +4184 -0
  257. data/ext/tokyocabinet-1.4.41/tokyocabinet.idl +336 -0
  258. data/ext/tokyocabinet-1.4.41/tokyocabinet.pc.in +14 -0
  259. data/ext/tokyotyrant-1.1.39/COPYING +504 -0
  260. data/ext/tokyotyrant-1.1.39/ChangeLog +578 -0
  261. data/ext/tokyotyrant-1.1.39/Makefile.in +365 -0
  262. data/ext/tokyotyrant-1.1.39/README +38 -0
  263. data/ext/tokyotyrant-1.1.39/THANKS +15 -0
  264. data/ext/tokyotyrant-1.1.39/configure +6979 -0
  265. data/ext/tokyotyrant-1.1.39/configure.in +300 -0
  266. data/ext/tokyotyrant-1.1.39/doc/common.css +211 -0
  267. data/ext/tokyotyrant-1.1.39/doc/index.html +79 -0
  268. data/ext/tokyotyrant-1.1.39/doc/spex.html +2264 -0
  269. data/ext/tokyotyrant-1.1.39/example/Makefile +68 -0
  270. data/ext/tokyotyrant-1.1.39/example/httptest.pl +88 -0
  271. data/ext/tokyotyrant-1.1.39/example/mcftest.pl +39 -0
  272. data/ext/tokyotyrant-1.1.39/example/mctest.pl +43 -0
  273. data/ext/tokyotyrant-1.1.39/example/tcrdbex +0 -0
  274. data/ext/tokyotyrant-1.1.39/example/tcrdbex.c +49 -0
  275. data/ext/tokyotyrant-1.1.39/example/tcrdbex.o +0 -0
  276. data/ext/tokyotyrant-1.1.39/example/tcrdbtblex +0 -0
  277. data/ext/tokyotyrant-1.1.39/example/tcrdbtblex.c +79 -0
  278. data/ext/tokyotyrant-1.1.39/example/tcrdbtblex.o +0 -0
  279. data/ext/tokyotyrant-1.1.39/ext/mapreduce.lua +57 -0
  280. data/ext/tokyotyrant-1.1.39/ext/queue.lua +55 -0
  281. data/ext/tokyotyrant-1.1.39/ext/senatus.lua +532 -0
  282. data/ext/tokyotyrant-1.1.39/ext/usherette.lua +438 -0
  283. data/ext/tokyotyrant-1.1.39/lab/datechange +56 -0
  284. data/ext/tokyotyrant-1.1.39/lab/diffcheck +45 -0
  285. data/ext/tokyotyrant-1.1.39/lab/fibonacci.lua +20 -0
  286. data/ext/tokyotyrant-1.1.39/lab/footprint.lua +67 -0
  287. data/ext/tokyotyrant-1.1.39/lab/highlow.lua +88 -0
  288. data/ext/tokyotyrant-1.1.39/lab/killdualmaster +12 -0
  289. data/ext/tokyotyrant-1.1.39/lab/rundualmaster +38 -0
  290. data/ext/tokyotyrant-1.1.39/lab/stepcount +26 -0
  291. data/ext/tokyotyrant-1.1.39/lab/tabcheck +43 -0
  292. data/ext/tokyotyrant-1.1.39/lab/ushrtregister.pl +55 -0
  293. data/ext/tokyotyrant-1.1.39/lab/widthcheck +57 -0
  294. data/ext/tokyotyrant-1.1.39/man/htmltoman +100 -0
  295. data/ext/tokyotyrant-1.1.39/man/tcrdb.3 +1309 -0
  296. data/ext/tokyotyrant-1.1.39/man/tcrmgr.1 +164 -0
  297. data/ext/tokyotyrant-1.1.39/man/tcrmttest.1 +55 -0
  298. data/ext/tokyotyrant-1.1.39/man/tcrtest.1 +89 -0
  299. data/ext/tokyotyrant-1.1.39/man/tculog.3 +15 -0
  300. data/ext/tokyotyrant-1.1.39/man/ttservctl.8 +37 -0
  301. data/ext/tokyotyrant-1.1.39/man/ttserver.1 +84 -0
  302. data/ext/tokyotyrant-1.1.39/man/ttulmgr.1 +40 -0
  303. data/ext/tokyotyrant-1.1.39/man/ttultest.1 +16 -0
  304. data/ext/tokyotyrant-1.1.39/man/ttutil.3 +14 -0
  305. data/ext/tokyotyrant-1.1.39/myconf.c +169 -0
  306. data/ext/tokyotyrant-1.1.39/myconf.h +408 -0
  307. data/ext/tokyotyrant-1.1.39/scrext.c +2394 -0
  308. data/ext/tokyotyrant-1.1.39/scrext.h +96 -0
  309. data/ext/tokyotyrant-1.1.39/tcrdb.c +2637 -0
  310. data/ext/tokyotyrant-1.1.39/tcrdb.h +801 -0
  311. data/ext/tokyotyrant-1.1.39/tcrmgr.c +1559 -0
  312. data/ext/tokyotyrant-1.1.39/tcrmttest.c +915 -0
  313. data/ext/tokyotyrant-1.1.39/tcrtest.c +1542 -0
  314. data/ext/tokyotyrant-1.1.39/tculog.c +1211 -0
  315. data/ext/tokyotyrant-1.1.39/tculog.h +392 -0
  316. data/ext/tokyotyrant-1.1.39/tokyotyrant.idl +143 -0
  317. data/ext/tokyotyrant-1.1.39/tokyotyrant.pc.in +14 -0
  318. data/ext/tokyotyrant-1.1.39/ttservctl +163 -0
  319. data/ext/tokyotyrant-1.1.39/ttserver.c +3583 -0
  320. data/ext/tokyotyrant-1.1.39/ttskeldir.c +141 -0
  321. data/ext/tokyotyrant-1.1.39/ttskelmock.c +64 -0
  322. data/ext/tokyotyrant-1.1.39/ttskelnull.c +79 -0
  323. data/ext/tokyotyrant-1.1.39/ttskelproxy.c +74 -0
  324. data/ext/tokyotyrant-1.1.39/ttulmgr.c +266 -0
  325. data/ext/tokyotyrant-1.1.39/ttultest.c +371 -0
  326. data/ext/tokyotyrant-1.1.39/ttutil.c +1510 -0
  327. data/ext/tokyotyrant-1.1.39/ttutil.h +494 -0
  328. data/lib/tokyo_tyrant/balancer.rb +189 -0
  329. data/spec/ext.lua +4 -0
  330. data/spec/plu_db.rb +538 -0
  331. data/spec/spec.rb +1 -0
  332. data/spec/spec_base.rb +17 -0
  333. data/spec/start_tyrants.sh +36 -0
  334. data/spec/stop_tyrants.sh +9 -0
  335. data/spec/tokyo_tyrant_balancer_db_spec.rb +160 -0
  336. data/spec/tokyo_tyrant_balancer_table_spec.rb +177 -0
  337. data/spec/tokyo_tyrant_query_spec.rb +159 -0
  338. data/spec/tokyo_tyrant_spec.rb +254 -0
  339. data/spec/tokyo_tyrant_table_spec.rb +301 -0
  340. metadata +402 -0
@@ -0,0 +1,1211 @@
1
+ /*************************************************************************************************
2
+ * The update log API of Tokyo Tyrant
3
+ * Copyright (C) 2006-2009 Mikio Hirabayashi
4
+ * This file is part of Tokyo Tyrant.
5
+ * Tokyo Tyrant is free software; you can redistribute it and/or modify it under the terms of
6
+ * the GNU Lesser General Public License as published by the Free Software Foundation; either
7
+ * version 2.1 of the License or any later version. Tokyo Tyrant is distributed in the hope
8
+ * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
9
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10
+ * License for more details.
11
+ * You should have received a copy of the GNU Lesser General Public License along with Tokyo
12
+ * Tyrant; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
13
+ * Boston, MA 02111-1307 USA.
14
+ *************************************************************************************************/
15
+
16
+
17
+ #include "ttutil.h"
18
+ #include "tculog.h"
19
+ #include "myconf.h"
20
+
21
+ #define TCULAIOCBNUM 64 // number of AIO tasks
22
+ #define TCULTMDEVALW 30.0 // allowed time deviance
23
+ #define TCREPLTIMEO 60.0 // timeout of the replication socket
24
+
25
+ typedef struct { // type of structure for a putshl operand
26
+ const char *vbuf; // region of the value.
27
+ int vsiz; // size of the region
28
+ int width; // the width of the record
29
+ } PUTSHLOP;
30
+
31
+
32
+ /* private function prototypes */
33
+ static bool tculogflushaiocbp(struct aiocb *aiocbp);
34
+ static void *tculogadbputshlproc(const void *vbuf, int vsiz, int *sp, PUTSHLOP *op);
35
+
36
+
37
+
38
+ /*************************************************************************************************
39
+ * API
40
+ *************************************************************************************************/
41
+
42
+
43
+ /* Create an update log object. */
44
+ TCULOG *tculognew(void){
45
+ TCULOG *ulog = tcmalloc(sizeof(*ulog));
46
+ for(int i = 0; i < TCULRMTXNUM; i++){
47
+ if(pthread_mutex_init(ulog->rmtxs + i, NULL) != 0) tcmyfatal("pthread_mutex_init failed");
48
+ }
49
+ if(pthread_rwlock_init(&ulog->rwlck, NULL) != 0) tcmyfatal("pthread_rwlock_init failed");
50
+ if(pthread_cond_init(&ulog->cnd, NULL) != 0) tcmyfatal("pthread_cond_init failed");
51
+ if(pthread_mutex_init(&ulog->wmtx, NULL) != 0) tcmyfatal("pthread_mutex_init failed");
52
+ ulog->base = NULL;
53
+ ulog->limsiz = 0;
54
+ ulog->max = 0;
55
+ ulog->fd = -1;
56
+ ulog->size = 0;
57
+ ulog->aiocbs = NULL;
58
+ ulog->aiocbi = 0;
59
+ ulog->aioend = 0;
60
+ return ulog;
61
+ }
62
+
63
+
64
+ /* Delete an update log object. */
65
+ void tculogdel(TCULOG *ulog){
66
+ assert(ulog);
67
+ if(ulog->base) tculogclose(ulog);
68
+ if(ulog->aiocbs) tcfree(ulog->aiocbs);
69
+ pthread_mutex_destroy(&ulog->wmtx);
70
+ pthread_cond_destroy(&ulog->cnd);
71
+ pthread_rwlock_destroy(&ulog->rwlck);
72
+ for(int i = TCULRMTXNUM - 1; i >= 0; i--){
73
+ pthread_mutex_destroy(ulog->rmtxs + i);
74
+ }
75
+ tcfree(ulog);
76
+ }
77
+
78
+
79
+ /* Set AIO control of an update log object. */
80
+ bool tculogsetaio(TCULOG *ulog){
81
+ #if defined(_SYS_LINUX_)
82
+ assert(ulog);
83
+ if(ulog->base || ulog->aiocbs) return false;
84
+ struct aiocb *aiocbs = tcmalloc(sizeof(*aiocbs) * TCULAIOCBNUM);
85
+ for(int i = 0; i < TCULAIOCBNUM; i++){
86
+ memset(aiocbs + i, 0, sizeof(*aiocbs));
87
+ }
88
+ ulog->aiocbs = aiocbs;
89
+ return true;
90
+ #else
91
+ assert(ulog);
92
+ return true;
93
+ #endif
94
+ }
95
+
96
+
97
+ /* Open files of an update log object. */
98
+ bool tculogopen(TCULOG *ulog, const char *base, uint64_t limsiz){
99
+ assert(ulog && base);
100
+ if(ulog->base) return false;
101
+ struct stat sbuf;
102
+ if(stat(base, &sbuf) == -1 || !S_ISDIR(sbuf.st_mode)) return false;
103
+ TCLIST *names = tcreaddir(base);
104
+ if(!names) return false;
105
+ int ln = tclistnum(names);
106
+ int max = 0;
107
+ for(int i = 0; i < ln; i++){
108
+ const char *name = tclistval2(names, i);
109
+ if(!tcstrbwm(name, TCULSUFFIX)) continue;
110
+ int id = tcatoi(name);
111
+ char *path = tcsprintf("%s/%08d%s", base, id, TCULSUFFIX);
112
+ if(stat(path, &sbuf) == 0 && S_ISREG(sbuf.st_mode) && id > max) max = id;
113
+ tcfree(path);
114
+ }
115
+ tclistdel(names);
116
+ if(max < 1) max = 1;
117
+ ulog->base = tcstrdup(base);
118
+ ulog->limsiz = (limsiz > 0) ? limsiz : INT64_MAX / 2;
119
+ ulog->max = max;
120
+ ulog->fd = -1;
121
+ ulog->size = sbuf.st_size;
122
+ struct aiocb *aiocbs = ulog->aiocbs;
123
+ if(aiocbs){
124
+ for(int i = 0; i < TCULAIOCBNUM; i++){
125
+ struct aiocb *aiocbp = aiocbs + i;
126
+ aiocbp->aio_fildes = 0;
127
+ aiocbp->aio_buf = NULL;
128
+ aiocbp->aio_nbytes = 0;
129
+ }
130
+ }
131
+ ulog->aiocbi = 0;
132
+ ulog->aioend = 0;
133
+ return true;
134
+ }
135
+
136
+
137
+ /* Close files of an update log object. */
138
+ bool tculogclose(TCULOG *ulog){
139
+ assert(ulog);
140
+ if(!ulog->base) return false;
141
+ bool err = false;
142
+ struct aiocb *aiocbs = ulog->aiocbs;
143
+ if(aiocbs){
144
+ for(int i = 0; i < TCULAIOCBNUM; i++){
145
+ if(!tculogflushaiocbp(aiocbs + i)) err = true;
146
+ }
147
+ }
148
+ if(ulog->fd != -1 && close(ulog->fd) != 0) err = true;
149
+ tcfree(ulog->base);
150
+ ulog->base = NULL;
151
+ return !err;
152
+ }
153
+
154
+
155
+ /* Get the mutex index of a record. */
156
+ int tculogrmtxidx(TCULOG *ulog, const char *kbuf, int ksiz){
157
+ assert(ulog && kbuf && ksiz >= 0);
158
+ if(!ulog->base || !ulog->aiocbs) return 0;
159
+ uint32_t hash = 19780211;
160
+ while(ksiz--){
161
+ hash = hash * 41 + *(uint8_t *)kbuf++;
162
+ }
163
+ return hash % TCULRMTXNUM;
164
+ }
165
+
166
+
167
+ /* Begin the critical section of an update log object. */
168
+ bool tculogbegin(TCULOG *ulog, int idx){
169
+ assert(ulog);
170
+ if(!ulog->base) return false;
171
+ if(idx < 0){
172
+ for(int i = 0; i < TCULRMTXNUM; i++){
173
+ if(pthread_mutex_lock(ulog->rmtxs + i) != 0){
174
+ for(i--; i >= 0; i--){
175
+ pthread_mutex_unlock(ulog->rmtxs + i);
176
+ }
177
+ return false;
178
+ }
179
+ }
180
+ return true;
181
+ }
182
+ return pthread_mutex_lock(ulog->rmtxs + idx) == 0;
183
+ }
184
+
185
+
186
+ /* End the critical section of an update log object. */
187
+ bool tculogend(TCULOG *ulog, int idx){
188
+ assert(ulog);
189
+ if(idx < 0){
190
+ bool err = false;
191
+ for(int i = TCULRMTXNUM - 1; i >= 0; i--){
192
+ if(pthread_mutex_unlock(ulog->rmtxs + i) != 0) err = true;
193
+ }
194
+ return !err;
195
+ }
196
+ return pthread_mutex_unlock(ulog->rmtxs + idx) == 0;
197
+ }
198
+
199
+
200
+ /* Write a message into an update log object. */
201
+ bool tculogwrite(TCULOG *ulog, uint64_t ts, uint32_t sid, uint32_t mid,
202
+ const void *ptr, int size){
203
+ assert(ulog && ptr && size >= 0);
204
+ if(!ulog->base) return false;
205
+ if(ts < 1) ts = (uint64_t)(tctime() * 1000000);
206
+ bool err = false;
207
+ if(pthread_rwlock_wrlock(&ulog->rwlck) != 0) return false;
208
+ pthread_cleanup_push((void (*)(void *))pthread_rwlock_unlock, &ulog->rwlck);
209
+ if(ulog->fd == -1){
210
+ char *path = tcsprintf("%s/%08d%s", ulog->base, ulog->max, TCULSUFFIX);
211
+ int fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 00644);
212
+ tcfree(path);
213
+ struct stat sbuf;
214
+ if(fd != -1 && fstat(fd, &sbuf) == 0){
215
+ ulog->fd = fd;
216
+ ulog->size = sbuf.st_size;
217
+ } else {
218
+ err = true;
219
+ }
220
+ }
221
+ int rsiz = sizeof(uint8_t) + sizeof(uint64_t) + sizeof(uint32_t) * 2 + size;
222
+ unsigned char stack[TTIOBUFSIZ];
223
+ unsigned char *buf = (rsiz < TTIOBUFSIZ) ? stack : tcmalloc(rsiz);
224
+ pthread_cleanup_push(free, (buf == stack) ? NULL : buf);
225
+ unsigned char *wp = buf;
226
+ *(wp++) = TCULMAGICNUM;
227
+ uint64_t llnum = TTHTONLL(ts);
228
+ memcpy(wp, &llnum, sizeof(llnum));
229
+ wp += sizeof(llnum);
230
+ uint16_t snum = TTHTONS(sid);
231
+ memcpy(wp, &snum, sizeof(snum));
232
+ wp += sizeof(snum);
233
+ snum = TTHTONS(mid);
234
+ memcpy(wp, &snum, sizeof(snum));
235
+ wp += sizeof(snum);
236
+ uint32_t lnum = TTHTONL(size);
237
+ memcpy(wp, &lnum, sizeof(lnum));
238
+ wp += sizeof(lnum);
239
+ memcpy(wp, ptr, size);
240
+ if(ulog->fd != -1){
241
+ struct aiocb *aiocbs = (struct aiocb *)ulog->aiocbs;
242
+ if(aiocbs){
243
+ struct aiocb *aiocbp = aiocbs + ulog->aiocbi;
244
+ if(aiocbp->aio_buf){
245
+ off_t aioend = aiocbp->aio_offset + aiocbp->aio_nbytes;
246
+ if(tculogflushaiocbp(aiocbp)){
247
+ ulog->aioend = aioend;
248
+ } else {
249
+ err = true;
250
+ }
251
+ }
252
+ aiocbp->aio_fildes = ulog->fd;
253
+ aiocbp->aio_offset = ulog->size;
254
+ aiocbp->aio_buf = tcmemdup(buf, rsiz);
255
+ aiocbp->aio_nbytes = rsiz;
256
+ while(aio_write(aiocbp) != 0){
257
+ if(errno != EAGAIN){
258
+ tcfree((char *)aiocbp->aio_buf);
259
+ aiocbp->aio_buf = NULL;
260
+ err = true;
261
+ break;
262
+ }
263
+ for(int i = 0; i < TCULAIOCBNUM; i++){
264
+ if(i == ulog->aiocbi) continue;
265
+ if(!tculogflushaiocbp(aiocbs + i)){
266
+ err = true;
267
+ break;
268
+ }
269
+ }
270
+ }
271
+ ulog->aiocbi = (ulog->aiocbi + 1) % TCULAIOCBNUM;
272
+ } else {
273
+ if(!tcwrite(ulog->fd, buf, rsiz)) err = true;
274
+ }
275
+ if(!err){
276
+ ulog->size += rsiz;
277
+ if(ulog->size >= ulog->limsiz){
278
+ if(aiocbs){
279
+ for(int i = 0; i < TCULAIOCBNUM; i++){
280
+ if(!tculogflushaiocbp(aiocbs + i)) err = true;
281
+ }
282
+ ulog->aiocbi = 0;
283
+ ulog->aioend = 0;
284
+ }
285
+ char *path = tcsprintf("%s/%08d%s", ulog->base, ulog->max + 1, TCULSUFFIX);
286
+ int fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 00644);
287
+ tcfree(path);
288
+ if(fd != 0){
289
+ if(close(ulog->fd) != 0) err = true;
290
+ ulog->fd = fd;
291
+ ulog->size = 0;
292
+ ulog->max++;
293
+ } else {
294
+ err = true;
295
+ }
296
+ }
297
+ if(pthread_cond_broadcast(&ulog->cnd) != 0) err = true;
298
+ }
299
+ } else {
300
+ err = true;
301
+ }
302
+ pthread_cleanup_pop(1);
303
+ pthread_cleanup_pop(1);
304
+ return !err;
305
+ }
306
+
307
+
308
+ /* Create a log reader object. */
309
+ TCULRD *tculrdnew(TCULOG *ulog, uint64_t ts){
310
+ assert(ulog);
311
+ if(!ulog->base) return NULL;
312
+ if(pthread_rwlock_rdlock(&ulog->rwlck) != 0) return NULL;
313
+ TCLIST *names = tcreaddir(ulog->base);
314
+ if(!names){
315
+ pthread_rwlock_unlock(&ulog->rwlck);
316
+ return NULL;
317
+ }
318
+ int ln = tclistnum(names);
319
+ int max = 0;
320
+ for(int i = 0; i < ln; i++){
321
+ const char *name = tclistval2(names, i);
322
+ if(!tcstrbwm(name, TCULSUFFIX)) continue;
323
+ int id = tcatoi(name);
324
+ char *path = tcsprintf("%s/%08d%s", ulog->base, id, TCULSUFFIX);
325
+ struct stat sbuf;
326
+ if(stat(path, &sbuf) == 0 && S_ISREG(sbuf.st_mode) && id > max) max = id;
327
+ tcfree(path);
328
+ }
329
+ tclistdel(names);
330
+ if(max < 1) max = 1;
331
+ uint64_t bts = (ts > TCULTMDEVALW * 1000000) ? ts - TCULTMDEVALW * 1000000 : 0;
332
+ int num = 0;
333
+ for(int i = max; i > 0; i--){
334
+ char *path = tcsprintf("%s/%08d%s", ulog->base, i, TCULSUFFIX);
335
+ int fd = open(path, O_RDONLY, 00644);
336
+ tcfree(path);
337
+ if(fd == -1) break;
338
+ int rsiz = sizeof(uint8_t) + sizeof(uint64_t);
339
+ unsigned char buf[rsiz];
340
+ uint64_t fts = INT64_MAX;
341
+ if(tcread(fd, buf, rsiz)){
342
+ memcpy(&fts, buf + sizeof(uint8_t), sizeof(ts));
343
+ fts = TTNTOHLL(fts);
344
+ }
345
+ close(fd);
346
+ num = i;
347
+ if(bts >= fts) break;
348
+ }
349
+ if(num < 1) num = 1;
350
+ TCULRD *urld = tcmalloc(sizeof(*urld));
351
+ urld->ulog = ulog;
352
+ urld->ts = ts;
353
+ urld->num = num;
354
+ urld->fd = -1;
355
+ urld->rbuf = tcmalloc(TTIOBUFSIZ);
356
+ urld->rsiz = TTIOBUFSIZ;
357
+ pthread_rwlock_unlock(&ulog->rwlck);
358
+ return urld;
359
+ }
360
+
361
+
362
+ /* Delete a log reader object. */
363
+ void tculrddel(TCULRD *ulrd){
364
+ assert(ulrd);
365
+ if(ulrd->fd != -1) close(ulrd->fd);
366
+ tcfree(ulrd->rbuf);
367
+ tcfree(ulrd);
368
+ }
369
+
370
+
371
+ /* Wait the next message is written. */
372
+ void tculrdwait(TCULRD *ulrd){
373
+ assert(ulrd);
374
+ TCULOG *ulog = ulrd->ulog;
375
+ if(pthread_mutex_lock(&ulog->wmtx) != 0) return;
376
+ pthread_cleanup_push((void (*)(void *))pthread_mutex_unlock, &ulog->wmtx);
377
+ int ocs = PTHREAD_CANCEL_DISABLE;
378
+ pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &ocs);
379
+ struct timeval tv;
380
+ struct timespec ts;
381
+ if(gettimeofday(&tv, NULL) == 0){
382
+ ts.tv_sec = tv.tv_sec + 1;
383
+ ts.tv_nsec = tv.tv_usec * 1000;
384
+ } else {
385
+ ts.tv_sec = (1ULL << (sizeof(time_t) * 8 - 1)) - 1;
386
+ ts.tv_nsec = 0;
387
+ }
388
+ pthread_cond_timedwait(&ulog->cnd, &ulog->wmtx, &ts);
389
+ pthread_setcancelstate(ocs, NULL);
390
+ pthread_cleanup_pop(1);
391
+ }
392
+
393
+
394
+ /* Read a message from a log reader object. */
395
+ const void *tculrdread(TCULRD *ulrd, int *sp, uint64_t *tsp, uint32_t *sidp, uint32_t *midp){
396
+ assert(ulrd && sp && tsp && sidp && midp);
397
+ TCULOG *ulog = ulrd->ulog;
398
+ if(pthread_rwlock_rdlock(&ulog->rwlck) != 0) return NULL;
399
+ if(ulrd->fd == -1){
400
+ char *path = tcsprintf("%s/%08d%s", ulog->base, ulrd->num, TCULSUFFIX);
401
+ ulrd->fd = open(path, O_RDONLY, 00644);
402
+ tcfree(path);
403
+ if(ulrd->fd == -1){
404
+ pthread_rwlock_unlock(&ulog->rwlck);
405
+ return NULL;
406
+ }
407
+ }
408
+ int rsiz = sizeof(uint8_t) + sizeof(uint64_t) + sizeof(uint32_t) * 2;
409
+ unsigned char buf[rsiz];
410
+ uint64_t ts;
411
+ uint32_t sid, mid, size;
412
+ while(true){
413
+ if(ulog->aiocbs && ulrd->num == ulog->max){
414
+ struct stat sbuf;
415
+ if(fstat(ulrd->fd, &sbuf) == -1 ||
416
+ (sbuf.st_size < ulog->size && sbuf.st_size >= ulog->aioend)){
417
+ pthread_rwlock_unlock(&ulog->rwlck);
418
+ return NULL;
419
+ }
420
+ }
421
+ if(!tcread(ulrd->fd, buf, rsiz)){
422
+ if(ulrd->num < ulog->max){
423
+ close(ulrd->fd);
424
+ ulrd->num++;
425
+ char *path = tcsprintf("%s/%08d%s", ulog->base, ulrd->num, TCULSUFFIX);
426
+ ulrd->fd = open(path, O_RDONLY, 00644);
427
+ tcfree(path);
428
+ if(ulrd->fd == -1){
429
+ pthread_rwlock_unlock(&ulog->rwlck);
430
+ return NULL;
431
+ }
432
+ continue;
433
+ }
434
+ pthread_rwlock_unlock(&ulog->rwlck);
435
+ return NULL;
436
+ }
437
+ const unsigned char *rp = buf;
438
+ if(*rp != TCULMAGICNUM){
439
+ pthread_rwlock_unlock(&ulog->rwlck);
440
+ return NULL;
441
+ }
442
+ rp += sizeof(uint8_t);
443
+ memcpy(&ts, rp, sizeof(ts));
444
+ ts = TTNTOHLL(ts);
445
+ rp += sizeof(ts);
446
+ uint16_t snum;
447
+ memcpy(&snum, rp, sizeof(snum));
448
+ sid = TTNTOHS(snum);
449
+ rp += sizeof(snum);
450
+ memcpy(&snum, rp, sizeof(snum));
451
+ mid = TTNTOHS(snum);
452
+ rp += sizeof(snum);
453
+ memcpy(&size, rp, sizeof(size));
454
+ size = TTNTOHL(size);
455
+ rp += sizeof(size);
456
+ if(ulrd->rsiz < size + 1){
457
+ ulrd->rbuf = tcrealloc(ulrd->rbuf, size + 1);
458
+ ulrd->rsiz = size + 1;
459
+ }
460
+ if(!tcread(ulrd->fd, ulrd->rbuf, size)){
461
+ pthread_rwlock_unlock(&ulog->rwlck);
462
+ return NULL;
463
+ }
464
+ if(ts < ulrd->ts) continue;
465
+ break;
466
+ }
467
+ *sp = size;
468
+ *tsp = ts;
469
+ *sidp = sid;
470
+ *midp = mid;
471
+ ulrd->rbuf[size] = '\0';
472
+ pthread_rwlock_unlock(&ulog->rwlck);
473
+ return ulrd->rbuf;
474
+ }
475
+
476
+
477
+ /* Store a record into an abstract database object. */
478
+ bool tculogadbput(TCULOG *ulog, uint32_t sid, uint32_t mid, TCADB *adb,
479
+ const void *kbuf, int ksiz, const void *vbuf, int vsiz){
480
+ assert(ulog && adb && kbuf && ksiz >= 0 && vbuf && vsiz >= 0);
481
+ bool err = false;
482
+ int rmidx = tculogrmtxidx(ulog, kbuf, ksiz);
483
+ bool dolog = tculogbegin(ulog, rmidx);
484
+ if(!tcadbput(adb, kbuf, ksiz, vbuf, vsiz)) err = true;
485
+ if(dolog){
486
+ unsigned char mstack[TTIOBUFSIZ];
487
+ int msiz = sizeof(uint8_t) * 3 + sizeof(uint32_t) * 2 + ksiz + vsiz;
488
+ unsigned char *mbuf = (msiz < TTIOBUFSIZ) ? mstack : tcmalloc(msiz + 1);
489
+ unsigned char *wp = mbuf;
490
+ *(wp++) = TTMAGICNUM;
491
+ *(wp++) = TTCMDPUT;
492
+ uint32_t lnum;
493
+ lnum = TTHTONL(ksiz);
494
+ memcpy(wp, &lnum, sizeof(lnum));
495
+ wp += sizeof(lnum);
496
+ lnum = TTHTONL(vsiz);
497
+ memcpy(wp, &lnum, sizeof(lnum));
498
+ wp += sizeof(lnum);
499
+ memcpy(wp, kbuf, ksiz);
500
+ wp += ksiz;
501
+ memcpy(wp, vbuf, vsiz);
502
+ wp += vsiz;
503
+ *(wp++) = err ? 1 : 0;
504
+ if(!tculogwrite(ulog, 0, sid, mid, mbuf, msiz)) err = true;
505
+ if(mbuf != mstack) tcfree(mbuf);
506
+ tculogend(ulog, rmidx);
507
+ }
508
+ return !err;
509
+ }
510
+
511
+
512
+ /* Store a new record into an abstract database object. */
513
+ bool tculogadbputkeep(TCULOG *ulog, uint32_t sid, uint32_t mid, TCADB *adb,
514
+ const void *kbuf, int ksiz, const void *vbuf, int vsiz){
515
+ assert(ulog && adb && kbuf && ksiz >= 0 && vbuf && vsiz >= 0);
516
+ bool err = false;
517
+ int rmidx = tculogrmtxidx(ulog, kbuf, ksiz);
518
+ bool dolog = tculogbegin(ulog, rmidx);
519
+ if(!tcadbputkeep(adb, kbuf, ksiz, vbuf, vsiz)) err = true;
520
+ if(dolog){
521
+ unsigned char mstack[TTIOBUFSIZ];
522
+ int msiz = sizeof(uint8_t) * 3 + sizeof(uint32_t) * 2 + ksiz + vsiz;
523
+ unsigned char *mbuf = (msiz < TTIOBUFSIZ) ? mstack : tcmalloc(msiz + 1);
524
+ unsigned char *wp = mbuf;
525
+ *(wp++) = TTMAGICNUM;
526
+ *(wp++) = TTCMDPUTKEEP;
527
+ uint32_t lnum;
528
+ lnum = TTHTONL(ksiz);
529
+ memcpy(wp, &lnum, sizeof(lnum));
530
+ wp += sizeof(lnum);
531
+ lnum = TTHTONL(vsiz);
532
+ memcpy(wp, &lnum, sizeof(lnum));
533
+ wp += sizeof(lnum);
534
+ memcpy(wp, kbuf, ksiz);
535
+ wp += ksiz;
536
+ memcpy(wp, vbuf, vsiz);
537
+ wp += vsiz;
538
+ *(wp++) = err ? 1 : 0;
539
+ if(!tculogwrite(ulog, 0, sid, mid, mbuf, msiz)) err = true;
540
+ if(mbuf != mstack) tcfree(mbuf);
541
+ tculogend(ulog, rmidx);
542
+ }
543
+ return !err;
544
+ }
545
+
546
+
547
+ /* Concatenate a value at the end of the existing record in an abstract database object. */
548
+ bool tculogadbputcat(TCULOG *ulog, uint32_t sid, uint32_t mid, TCADB *adb,
549
+ const void *kbuf, int ksiz, const void *vbuf, int vsiz){
550
+ assert(ulog && adb && kbuf && ksiz >= 0 && vbuf && vsiz >= 0);
551
+ bool err = false;
552
+ int rmidx = tculogrmtxidx(ulog, kbuf, ksiz);
553
+ bool dolog = tculogbegin(ulog, rmidx);
554
+ if(!tcadbputcat(adb, kbuf, ksiz, vbuf, vsiz)) err = true;
555
+ if(dolog){
556
+ unsigned char mstack[TTIOBUFSIZ];
557
+ int msiz = sizeof(uint8_t) * 3 + sizeof(uint32_t) * 2 + ksiz + vsiz;
558
+ unsigned char *mbuf = (msiz < TTIOBUFSIZ) ? mstack : tcmalloc(msiz + 1);
559
+ unsigned char *wp = mbuf;
560
+ *(wp++) = TTMAGICNUM;
561
+ *(wp++) = TTCMDPUTCAT;
562
+ uint32_t lnum;
563
+ lnum = TTHTONL(ksiz);
564
+ memcpy(wp, &lnum, sizeof(lnum));
565
+ wp += sizeof(lnum);
566
+ lnum = TTHTONL(vsiz);
567
+ memcpy(wp, &lnum, sizeof(lnum));
568
+ wp += sizeof(lnum);
569
+ memcpy(wp, kbuf, ksiz);
570
+ wp += ksiz;
571
+ memcpy(wp, vbuf, vsiz);
572
+ wp += vsiz;
573
+ *(wp++) = err ? 1 : 0;
574
+ if(!tculogwrite(ulog, 0, sid, mid, mbuf, msiz)) err = true;
575
+ if(mbuf != mstack) tcfree(mbuf);
576
+ tculogend(ulog, rmidx);
577
+ }
578
+ return !err;
579
+ }
580
+
581
+
582
+ /* Concatenate a value at the end of the existing record and shift it to the left. */
583
+ bool tculogadbputshl(TCULOG *ulog, uint32_t sid, uint32_t mid, TCADB *adb,
584
+ const void *kbuf, int ksiz, const void *vbuf, int vsiz, int width){
585
+ assert(ulog && adb && kbuf && ksiz >= 0 && vbuf && vsiz >= 0 && width >= 0);
586
+ bool err = false;
587
+ int rmidx = tculogrmtxidx(ulog, kbuf, ksiz);
588
+ bool dolog = tculogbegin(ulog, rmidx);
589
+ PUTSHLOP op;
590
+ op.vbuf = vbuf;
591
+ op.vsiz = vsiz;
592
+ op.width = width;
593
+ if(!tcadbputproc(adb, kbuf, ksiz, vbuf, vsiz, (TCPDPROC)tculogadbputshlproc, &op))
594
+ err = true;
595
+ if(dolog){
596
+ unsigned char mstack[TTIOBUFSIZ];
597
+ int msiz = sizeof(uint8_t) * 3 + sizeof(uint32_t) * 3 + ksiz + vsiz;
598
+ unsigned char *mbuf = (msiz < TTIOBUFSIZ) ? mstack : tcmalloc(msiz + 1);
599
+ unsigned char *wp = mbuf;
600
+ *(wp++) = TTMAGICNUM;
601
+ *(wp++) = TTCMDPUTSHL;
602
+ uint32_t lnum;
603
+ lnum = TTHTONL(ksiz);
604
+ memcpy(wp, &lnum, sizeof(lnum));
605
+ wp += sizeof(lnum);
606
+ lnum = TTHTONL(vsiz);
607
+ memcpy(wp, &lnum, sizeof(lnum));
608
+ wp += sizeof(lnum);
609
+ lnum = TTHTONL(width);
610
+ memcpy(wp, &lnum, sizeof(lnum));
611
+ wp += sizeof(lnum);
612
+ memcpy(wp, kbuf, ksiz);
613
+ wp += ksiz;
614
+ memcpy(wp, vbuf, vsiz);
615
+ wp += vsiz;
616
+ *(wp++) = err ? 1 : 0;
617
+ if(!tculogwrite(ulog, 0, sid, mid, mbuf, msiz)) err = true;
618
+ if(mbuf != mstack) tcfree(mbuf);
619
+ tculogend(ulog, rmidx);
620
+ }
621
+ return !err;
622
+ }
623
+
624
+
625
+ /* Remove a record of an abstract database object. */
626
+ bool tculogadbout(TCULOG *ulog, uint32_t sid, uint32_t mid, TCADB *adb,
627
+ const void *kbuf, int ksiz){
628
+ assert(ulog && adb && kbuf && ksiz >= 0);
629
+ bool err = false;
630
+ int rmidx = tculogrmtxidx(ulog, kbuf, ksiz);
631
+ bool dolog = tculogbegin(ulog, rmidx);
632
+ if(!tcadbout(adb, kbuf, ksiz)) err = true;
633
+ if(dolog){
634
+ unsigned char mstack[TTIOBUFSIZ];
635
+ int msiz = sizeof(uint8_t) * 3 + sizeof(uint32_t) + ksiz;
636
+ unsigned char *mbuf = (msiz < TTIOBUFSIZ) ? mstack : tcmalloc(msiz + 1);
637
+ unsigned char *wp = mbuf;
638
+ *(wp++) = TTMAGICNUM;
639
+ *(wp++) = TTCMDOUT;
640
+ uint32_t lnum;
641
+ lnum = TTHTONL(ksiz);
642
+ memcpy(wp, &lnum, sizeof(lnum));
643
+ wp += sizeof(lnum);
644
+ memcpy(wp, kbuf, ksiz);
645
+ wp += ksiz;
646
+ *(wp++) = err ? 1 : 0;
647
+ if(!tculogwrite(ulog, 0, sid, mid, mbuf, msiz)) err = true;
648
+ if(mbuf != mstack) tcfree(mbuf);
649
+ tculogend(ulog, rmidx);
650
+ }
651
+ return !err;
652
+ }
653
+
654
+
655
+ /* Add an integer to a record in an abstract database object. */
656
+ int tculogadbaddint(TCULOG *ulog, uint32_t sid, uint32_t mid, TCADB *adb,
657
+ const void *kbuf, int ksiz, int num){
658
+ assert(ulog && adb && kbuf && ksiz >= 0);
659
+ int rmidx = tculogrmtxidx(ulog, kbuf, ksiz);
660
+ bool dolog = tculogbegin(ulog, rmidx);
661
+ int rnum = tcadbaddint(adb, kbuf, ksiz, num);
662
+ if(dolog){
663
+ unsigned char mstack[TTIOBUFSIZ];
664
+ int msiz = sizeof(uint8_t) * 3 + sizeof(uint32_t) * 2 + ksiz;
665
+ unsigned char *mbuf = (msiz < TTIOBUFSIZ) ? mstack : tcmalloc(msiz + 1);
666
+ unsigned char *wp = mbuf;
667
+ *(wp++) = TTMAGICNUM;
668
+ *(wp++) = TTCMDADDINT;
669
+ uint32_t lnum;
670
+ lnum = TTHTONL(ksiz);
671
+ memcpy(wp, &lnum, sizeof(lnum));
672
+ wp += sizeof(lnum);
673
+ lnum = TTHTONL(num);
674
+ memcpy(wp, &lnum, sizeof(lnum));
675
+ wp += sizeof(lnum);
676
+ memcpy(wp, kbuf, ksiz);
677
+ wp += ksiz;
678
+ *(wp++) = (rnum == INT_MIN) ? 1 : 0;
679
+ if(!tculogwrite(ulog, 0, sid, mid, mbuf, msiz)) rnum = INT_MIN;
680
+ if(mbuf != mstack) tcfree(mbuf);
681
+ tculogend(ulog, rmidx);
682
+ }
683
+ return rnum;
684
+ }
685
+
686
+
687
+ /* Add a real number to a record in an abstract database object. */
688
+ double tculogadbadddouble(TCULOG *ulog, uint32_t sid, uint32_t mid, TCADB *adb,
689
+ const void *kbuf, int ksiz, double num){
690
+ assert(ulog && adb && kbuf && ksiz >= 0);
691
+ int rmidx = tculogrmtxidx(ulog, kbuf, ksiz);
692
+ bool dolog = tculogbegin(ulog, rmidx);
693
+ double rnum = tcadbadddouble(adb, kbuf, ksiz, num);
694
+ if(dolog){
695
+ unsigned char mstack[TTIOBUFSIZ];
696
+ int msiz = sizeof(uint8_t) * 3 + sizeof(uint32_t) + sizeof(uint64_t) * 2 + ksiz;
697
+ unsigned char *mbuf = (msiz < TTIOBUFSIZ) ? mstack : tcmalloc(msiz + 1);
698
+ unsigned char *wp = mbuf;
699
+ *(wp++) = TTMAGICNUM;
700
+ *(wp++) = TTCMDADDDOUBLE;
701
+ uint32_t lnum;
702
+ lnum = TTHTONL(ksiz);
703
+ memcpy(wp, &lnum, sizeof(lnum));
704
+ wp += sizeof(lnum);
705
+ ttpackdouble(num, (char *)wp);
706
+ wp += sizeof(uint64_t) * 2;
707
+ memcpy(wp, kbuf, ksiz);
708
+ wp += ksiz;
709
+ *(wp++) = isnan(rnum) ? 1 : 0;
710
+ if(!tculogwrite(ulog, 0, sid, mid, mbuf, msiz)) rnum = INT_MIN;
711
+ if(mbuf != mstack) tcfree(mbuf);
712
+ tculogend(ulog, rmidx);
713
+ }
714
+ return rnum;
715
+ }
716
+
717
+
718
+ /* Synchronize updated contents of an abstract database object with the file and the device. */
719
+ bool tculogadbsync(TCULOG *ulog, uint32_t sid, uint32_t mid, TCADB *adb){
720
+ assert(ulog && adb);
721
+ bool err = false;
722
+ bool dolog = tculogbegin(ulog, -1);
723
+ if(!tcadbsync(adb)) err = true;
724
+ if(dolog){
725
+ unsigned char mbuf[sizeof(uint8_t)*3];
726
+ unsigned char *wp = mbuf;
727
+ *(wp++) = TTMAGICNUM;
728
+ *(wp++) = TTCMDSYNC;
729
+ *(wp++) = err ? 1 : 0;
730
+ if(!tculogwrite(ulog, 0, sid, mid, mbuf, wp - mbuf)) err = true;
731
+ tculogend(ulog, -1);
732
+ }
733
+ return !err;
734
+ }
735
+
736
+
737
+ /* Optimize the storage of an abstract database object. */
738
+ bool tculogadboptimize(TCULOG *ulog, uint32_t sid, uint32_t mid, TCADB *adb, const char *params){
739
+ assert(ulog && adb);
740
+ bool err = false;
741
+ bool dolog = tculogbegin(ulog, -1);
742
+ if(!tcadboptimize(adb, params)) err = true;
743
+ if(dolog){
744
+ int psiz = strlen(params);
745
+ unsigned char mstack[TTIOBUFSIZ];
746
+ int msiz = sizeof(uint8_t) * 3 + sizeof(uint32_t) + psiz;
747
+ unsigned char *mbuf = (msiz < TTIOBUFSIZ) ? mstack : tcmalloc(msiz + 1);
748
+ unsigned char *wp = mbuf;
749
+ *(wp++) = TTMAGICNUM;
750
+ *(wp++) = TTCMDOPTIMIZE;
751
+ uint32_t lnum;
752
+ lnum = TTHTONL(psiz);
753
+ memcpy(wp, &lnum, sizeof(lnum));
754
+ wp += sizeof(lnum);
755
+ memcpy(wp, params, psiz);
756
+ wp += psiz;
757
+ *(wp++) = err ? 1 : 0;
758
+ if(!tculogwrite(ulog, 0, sid, mid, mbuf, msiz)) err = true;
759
+ if(mbuf != mstack) tcfree(mbuf);
760
+ tculogend(ulog, -1);
761
+ }
762
+ return !err;
763
+ }
764
+
765
+
766
+ /* Remove all records of an abstract database object. */
767
+ bool tculogadbvanish(TCULOG *ulog, uint32_t sid, uint32_t mid, TCADB *adb){
768
+ assert(ulog && adb);
769
+ bool err = false;
770
+ bool dolog = tculogbegin(ulog, -1);
771
+ if(!tcadbvanish(adb)) err = true;
772
+ if(dolog){
773
+ unsigned char mbuf[sizeof(uint8_t)*3];
774
+ unsigned char *wp = mbuf;
775
+ *(wp++) = TTMAGICNUM;
776
+ *(wp++) = TTCMDVANISH;
777
+ *(wp++) = err ? 1 : 0;
778
+ if(!tculogwrite(ulog, 0, sid, mid, mbuf, wp - mbuf)) err = true;
779
+ tculogend(ulog, -1);
780
+ }
781
+ return !err;
782
+ }
783
+
784
+
785
+ /* Call a versatile function for miscellaneous operations of an abstract database object. */
786
+ TCLIST *tculogadbmisc(TCULOG *ulog, uint32_t sid, uint32_t mid, TCADB *adb,
787
+ const char *name, const TCLIST *args){
788
+ assert(ulog && adb && name && args);
789
+ bool dolog = tculogbegin(ulog, -1);
790
+ TCLIST *rv = tcadbmisc(adb, name, args);
791
+ if(dolog){
792
+ int nsiz = strlen(name);
793
+ int anum = tclistnum(args);
794
+ int msiz = sizeof(uint8_t) * 3 + sizeof(uint32_t) * 2 + nsiz;
795
+ for(int i = 0; i < anum; i++){
796
+ int esiz;
797
+ tclistval(args, i, &esiz);
798
+ msiz += sizeof(uint32_t) + esiz;
799
+ }
800
+ unsigned char mstack[TTIOBUFSIZ];
801
+ unsigned char *mbuf = (msiz < TTIOBUFSIZ) ? mstack : tcmalloc(msiz + 1);
802
+ unsigned char *wp = mbuf;
803
+ *(wp++) = TTMAGICNUM;
804
+ *(wp++) = TTCMDMISC;
805
+ uint32_t lnum;
806
+ lnum = TTHTONL(nsiz);
807
+ memcpy(wp, &lnum, sizeof(lnum));
808
+ wp += sizeof(lnum);
809
+ lnum = TTHTONL(anum);
810
+ memcpy(wp, &lnum, sizeof(lnum));
811
+ wp += sizeof(lnum);
812
+ memcpy(wp, name, nsiz);
813
+ wp += nsiz;
814
+ for(int i = 0; i < anum; i++){
815
+ int esiz;
816
+ const char *ebuf = tclistval(args, i, &esiz);
817
+ lnum = TTHTONL(esiz);
818
+ memcpy(wp, &lnum, sizeof(lnum));
819
+ wp += sizeof(lnum);
820
+ memcpy(wp, ebuf, esiz);
821
+ wp += esiz;
822
+ }
823
+ *(wp++) = rv ? 0 : 1;
824
+ if(!tculogwrite(ulog, 0, sid, mid, mbuf, msiz)){
825
+ if(rv) tclistdel(rv);
826
+ rv = NULL;
827
+ }
828
+ if(mbuf != mstack) tcfree(mbuf);
829
+ tculogend(ulog, -1);
830
+ }
831
+ return rv;
832
+ }
833
+
834
+
835
+ /* Restore an abstract database object. */
836
+ bool tculogadbrestore(TCADB *adb, const char *path, uint64_t ts, bool con, TCULOG *ulog){
837
+ assert(adb && path);
838
+ bool err = false;
839
+ TCULOG *sulog = tculognew();
840
+ if(tculogopen(sulog, path, 0)){
841
+ TCULRD *ulrd = tculrdnew(sulog, ts);
842
+ if(ulrd){
843
+ const char *rbuf;
844
+ int rsiz;
845
+ uint64_t rts;
846
+ uint32_t rsid, rmid;
847
+ while((rbuf = tculrdread(ulrd, &rsiz, &rts, &rsid, &rmid)) != NULL){
848
+ bool cc;
849
+ if(!tculogadbredo(adb, rbuf, rsiz, ulog, rsid, rmid, &cc) || (con && !cc)){
850
+ err = true;
851
+ break;
852
+ }
853
+ }
854
+ tculrddel(ulrd);
855
+ } else {
856
+ err = true;
857
+ }
858
+ if(!tculogclose(sulog)) err = true;
859
+ } else {
860
+ err = true;
861
+ }
862
+ tculogdel(sulog);
863
+ return !err;
864
+ }
865
+
866
+
867
+ /* Redo an update log message. */
868
+ bool tculogadbredo(TCADB *adb, const char *ptr, int size, TCULOG *ulog,
869
+ uint32_t sid, uint32_t mid, bool *cp){
870
+ assert(adb && ptr && size >= 0);
871
+ if(size < sizeof(uint8_t) * 3) return false;
872
+ const unsigned char *rp = (unsigned char *)ptr;
873
+ int magic = *(rp++);
874
+ int cmd = *(rp++);
875
+ bool exp = (((unsigned char *)ptr)[size-1] == 0) ? true : false;
876
+ size -= sizeof(uint8_t) * 3;
877
+ if(magic != TTMAGICNUM) return false;
878
+ bool err = false;
879
+ *cp = true;
880
+ switch(cmd){
881
+ case TTCMDPUT:
882
+ if(size >= sizeof(uint32_t) * 2){
883
+ uint32_t ksiz;
884
+ memcpy(&ksiz, rp, sizeof(ksiz));
885
+ ksiz = TTNTOHL(ksiz);
886
+ rp += sizeof(ksiz);
887
+ uint32_t vsiz;
888
+ memcpy(&vsiz, rp, sizeof(vsiz));
889
+ vsiz = TTNTOHL(vsiz);
890
+ rp += sizeof(vsiz);
891
+ if(tculogadbput(ulog, sid, mid, adb, rp, ksiz, rp + ksiz, vsiz) != exp) *cp = false;
892
+ } else {
893
+ err = true;
894
+ }
895
+ break;
896
+ case TTCMDPUTKEEP:
897
+ if(size >= sizeof(uint32_t) * 2){
898
+ uint32_t ksiz;
899
+ memcpy(&ksiz, rp, sizeof(ksiz));
900
+ ksiz = TTNTOHL(ksiz);
901
+ rp += sizeof(ksiz);
902
+ uint32_t vsiz;
903
+ memcpy(&vsiz, rp, sizeof(vsiz));
904
+ vsiz = TTNTOHL(vsiz);
905
+ rp += sizeof(vsiz);
906
+ if(tculogadbputkeep(ulog, sid, mid, adb, rp, ksiz, rp + ksiz, vsiz) != exp) *cp = false;
907
+ } else {
908
+ err = true;
909
+ }
910
+ break;
911
+ case TTCMDPUTCAT:
912
+ if(size >= sizeof(uint32_t) * 2){
913
+ uint32_t ksiz;
914
+ memcpy(&ksiz, rp, sizeof(ksiz));
915
+ ksiz = TTNTOHL(ksiz);
916
+ rp += sizeof(ksiz);
917
+ uint32_t vsiz;
918
+ memcpy(&vsiz, rp, sizeof(vsiz));
919
+ vsiz = TTNTOHL(vsiz);
920
+ rp += sizeof(vsiz);
921
+ if(tculogadbputcat(ulog, sid, mid, adb, rp, ksiz, rp + ksiz, vsiz) != exp) *cp = false;
922
+ } else {
923
+ err = true;
924
+ }
925
+ break;
926
+ case TTCMDPUTSHL:
927
+ if(size >= sizeof(uint32_t) * 3){
928
+ uint32_t ksiz;
929
+ memcpy(&ksiz, rp, sizeof(ksiz));
930
+ ksiz = TTNTOHL(ksiz);
931
+ rp += sizeof(ksiz);
932
+ uint32_t vsiz;
933
+ memcpy(&vsiz, rp, sizeof(vsiz));
934
+ vsiz = TTNTOHL(vsiz);
935
+ rp += sizeof(vsiz);
936
+ uint32_t width;
937
+ memcpy(&width, rp, sizeof(width));
938
+ width = TTNTOHL(width);
939
+ rp += sizeof(width);
940
+ if(tculogadbputshl(ulog, sid, mid, adb, rp, ksiz, rp + ksiz, vsiz, width) != exp)
941
+ *cp = false;
942
+ } else {
943
+ err = true;
944
+ }
945
+ break;
946
+ case TTCMDOUT:
947
+ if(size >= sizeof(uint32_t)){
948
+ uint32_t ksiz;
949
+ memcpy(&ksiz, rp, sizeof(ksiz));
950
+ ksiz = TTNTOHL(ksiz);
951
+ rp += sizeof(ksiz);
952
+ if(tculogadbout(ulog, sid, mid, adb, rp, ksiz) != exp) *cp = false;
953
+ } else {
954
+ err = true;
955
+ }
956
+ break;
957
+ case TTCMDADDINT:
958
+ if(size >= sizeof(uint32_t) * 2){
959
+ uint32_t ksiz;
960
+ memcpy(&ksiz, rp, sizeof(ksiz));
961
+ ksiz = TTNTOHL(ksiz);
962
+ rp += sizeof(ksiz);
963
+ int32_t num;
964
+ memcpy(&num, rp, sizeof(num));
965
+ num = TTNTOHL(num);
966
+ rp += sizeof(num);
967
+ int rnum = tculogadbaddint(ulog, sid, mid, adb, rp, ksiz, num);
968
+ if(exp && rnum == INT_MIN) *cp = false;
969
+ } else {
970
+ err = true;
971
+ }
972
+ break;
973
+ case TTCMDADDDOUBLE:
974
+ if(size >= sizeof(uint32_t) + sizeof(uint64_t) * 2){
975
+ uint32_t ksiz;
976
+ memcpy(&ksiz, rp, sizeof(ksiz));
977
+ ksiz = TTNTOHL(ksiz);
978
+ rp += sizeof(ksiz);
979
+ double num = ttunpackdouble((char *)rp);
980
+ rp += sizeof(uint64_t) * 2;
981
+ double rnum = tculogadbadddouble(ulog, sid, mid, adb, rp, ksiz, num);
982
+ if(exp && isnan(rnum)) *cp = false;
983
+ } else {
984
+ err = true;
985
+ }
986
+ break;
987
+ case TTCMDSYNC:
988
+ if(size == 0){
989
+ if(tculogadbsync(ulog, sid, mid, adb) != exp) *cp = false;
990
+ } else {
991
+ err = true;
992
+ }
993
+ break;
994
+ case TTCMDOPTIMIZE:
995
+ if(size >= sizeof(uint32_t)){
996
+ uint32_t psiz;
997
+ memcpy(&psiz, rp, sizeof(psiz));
998
+ psiz = TTNTOHL(psiz);
999
+ rp += sizeof(psiz);
1000
+ char *params = tcmemdup(rp, psiz);
1001
+ if(tculogadboptimize(ulog, sid, mid, adb, params) != exp) *cp = false;
1002
+ tcfree(params);
1003
+ } else {
1004
+ err = true;
1005
+ }
1006
+ break;
1007
+ case TTCMDVANISH:
1008
+ if(size == 0){
1009
+ if(tculogadbvanish(ulog, sid, mid, adb) != exp) *cp = false;
1010
+ } else {
1011
+ err = true;
1012
+ }
1013
+ break;
1014
+ case TTCMDMISC:
1015
+ if(size >= sizeof(uint32_t) * 2){
1016
+ uint32_t nsiz;
1017
+ memcpy(&nsiz, rp, sizeof(nsiz));
1018
+ nsiz = TTNTOHL(nsiz);
1019
+ rp += sizeof(nsiz);
1020
+ uint32_t anum;
1021
+ memcpy(&anum, rp, sizeof(anum));
1022
+ anum = TTNTOHL(anum);
1023
+ rp += sizeof(anum);
1024
+ char *name = tcmemdup(rp, nsiz);
1025
+ rp += nsiz;
1026
+ TCLIST *args = tclistnew2(anum);
1027
+ for(int i = 0; i < anum; i++){
1028
+ uint32_t esiz;
1029
+ memcpy(&esiz, rp, sizeof(esiz));
1030
+ esiz = TTNTOHL(esiz);
1031
+ rp += sizeof(esiz);
1032
+ tclistpush(args, rp, esiz);
1033
+ rp += esiz;
1034
+ }
1035
+ TCLIST *res = tculogadbmisc(ulog, sid, mid, adb, name, args);
1036
+ if(res){
1037
+ if(!exp) *cp = false;
1038
+ tclistdel(res);
1039
+ } else {
1040
+ if(exp) *cp = false;
1041
+ }
1042
+ tclistdel(args);
1043
+ tcfree(name);
1044
+ } else {
1045
+ err = true;
1046
+ }
1047
+ break;
1048
+ default:
1049
+ err = true;
1050
+ break;
1051
+ }
1052
+ return !err;
1053
+ }
1054
+
1055
+
1056
+ /* Create a replication object. */
1057
+ TCREPL *tcreplnew(void){
1058
+ TCREPL *repl = tcmalloc(sizeof(*repl));
1059
+ repl->fd = -1;
1060
+ repl->sock = NULL;
1061
+ return repl;
1062
+ }
1063
+
1064
+
1065
+ /* Delete a replication object. */
1066
+ void tcrepldel(TCREPL *repl){
1067
+ assert(repl);
1068
+ if(repl->fd >= 0) tcreplclose(repl);
1069
+ tcfree(repl);
1070
+ }
1071
+
1072
+
1073
+ /* Open a replication object. */
1074
+ bool tcreplopen(TCREPL *repl, const char *host, int port, uint64_t ts, uint32_t sid){
1075
+ assert(repl && host && port >= 0);
1076
+ if(repl->fd >= 0) return false;
1077
+ if(ts < 1) ts = 1;
1078
+ if(sid < 1) sid = INT_MAX;
1079
+ char addr[TTADDRBUFSIZ];
1080
+ if(!ttgethostaddr(host, addr)) return false;
1081
+ int fd = ttopensock(addr, port);
1082
+ if(fd == -1) return false;
1083
+ unsigned char buf[TTIOBUFSIZ];
1084
+ unsigned char *wp = buf;
1085
+ *(wp++) = TTMAGICNUM;
1086
+ *(wp++) = TTCMDREPL;
1087
+ uint64_t llnum = TTHTONLL(ts);
1088
+ memcpy(wp, &llnum, sizeof(llnum));
1089
+ wp += sizeof(llnum);
1090
+ uint32_t lnum = TTHTONL(sid);
1091
+ memcpy(wp, &lnum, sizeof(lnum));
1092
+ wp += sizeof(lnum);
1093
+ repl->fd = fd;
1094
+ repl->sock = ttsocknew(fd);
1095
+ repl->rbuf = tcmalloc(TTIOBUFSIZ);
1096
+ repl->rsiz = TTIOBUFSIZ;
1097
+ if(!ttsocksend(repl->sock, buf, wp - buf)){
1098
+ tcreplclose(repl);
1099
+ return false;
1100
+ }
1101
+ repl->mid = ttsockgetint32(repl->sock);
1102
+ if(ttsockcheckend(repl->sock) || repl->mid < 1){
1103
+ tcreplclose(repl);
1104
+ return false;
1105
+ }
1106
+ return true;
1107
+ }
1108
+
1109
+
1110
+ /* Close a remote database object. */
1111
+ bool tcreplclose(TCREPL *repl){
1112
+ assert(repl);
1113
+ if(repl->fd < 0) return false;
1114
+ bool err = false;
1115
+ tcfree(repl->rbuf);
1116
+ ttsockdel(repl->sock);
1117
+ if(!ttclosesock(repl->fd)) err = true;
1118
+ repl->fd = -1;
1119
+ repl->sock = NULL;
1120
+ return !err;
1121
+ }
1122
+
1123
+
1124
+ /* Read a message from a replication object. */
1125
+ const char *tcreplread(TCREPL *repl, int *sp, uint64_t *tsp, uint32_t *sidp){
1126
+ assert(repl && sp && tsp);
1127
+ int ocs = PTHREAD_CANCEL_DISABLE;
1128
+ pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &ocs);
1129
+ ttsocksetlife(repl->sock, TCREPLTIMEO);
1130
+ int c = ttsockgetc(repl->sock);
1131
+ if(c == TCULMAGICNOP){
1132
+ *sp = 0;
1133
+ *tsp = 0;
1134
+ *sidp = 0;
1135
+ return "";
1136
+ }
1137
+ if(c != TCULMAGICNUM){
1138
+ pthread_setcancelstate(ocs, NULL);
1139
+ return NULL;
1140
+ }
1141
+ uint64_t ts = ttsockgetint64(repl->sock);
1142
+ uint32_t sid = ttsockgetint32(repl->sock);
1143
+ uint32_t rsiz = ttsockgetint32(repl->sock);
1144
+ if(repl->rsiz < rsiz + 1){
1145
+ repl->rbuf = tcrealloc(repl->rbuf, rsiz + 1);
1146
+ repl->rsiz = rsiz + 1;
1147
+ }
1148
+ if(ttsockcheckend(repl->sock) || !ttsockrecv(repl->sock, repl->rbuf, rsiz) ||
1149
+ ttsockcheckend(repl->sock)){
1150
+ pthread_setcancelstate(ocs, NULL);
1151
+ return NULL;
1152
+ }
1153
+ *sp = rsiz;
1154
+ *tsp = ts;
1155
+ *sidp = sid;
1156
+ pthread_setcancelstate(ocs, NULL);
1157
+ return repl->rbuf;
1158
+ }
1159
+
1160
+
1161
+ /* Flush a AIO task.
1162
+ `aiocbp' specifies the pointer to the AIO task object.
1163
+ If successful, the return value is true, else, it is false. */
1164
+ static bool tculogflushaiocbp(struct aiocb *aiocbp){
1165
+ assert(aiocbp);
1166
+ if(!aiocbp->aio_buf) return true;
1167
+ bool err = false;
1168
+ while(true){
1169
+ int rv = aio_error(aiocbp);
1170
+ if(rv == 0) break;
1171
+ if(rv != EINPROGRESS){
1172
+ err = true;
1173
+ break;
1174
+ }
1175
+ if(aio_suspend((void *)&aiocbp, 1, NULL) == -1) err = true;
1176
+ }
1177
+ tcfree((char *)aiocbp->aio_buf);
1178
+ aiocbp->aio_buf = NULL;
1179
+ if(aio_return(aiocbp) != aiocbp->aio_nbytes) err = true;
1180
+ return !err;
1181
+ }
1182
+
1183
+
1184
+ /* Call back function for the putshl function.
1185
+ `vbuf' specifies the pointer to the region of the value.
1186
+ `vsiz' specifies the size of the region of the value.
1187
+ `sp' specifies the pointer to the variable into which the size of the region of the return
1188
+ value is assigned.
1189
+ `op' specifies the pointer to the optional opaque object.
1190
+ The return value is the pointer to the result object. */
1191
+ static void *tculogadbputshlproc(const void *vbuf, int vsiz, int *sp, PUTSHLOP *op){
1192
+ assert(vbuf && vsiz >= 0 && sp && op);
1193
+ int rsiz = tclmin(vsiz + op->vsiz, op->width);
1194
+ char *rbuf = tcmalloc(rsiz + 1);
1195
+ char *wp = rbuf;
1196
+ int wsiz = rsiz;
1197
+
1198
+ int left = wsiz - op->vsiz;
1199
+ if(left > 0){
1200
+ memcpy(wp, (char *)vbuf + vsiz - left, left);
1201
+ wp += left;
1202
+ wsiz -= left;
1203
+ }
1204
+ if(wsiz > 0) memcpy(wp, op->vbuf + op->vsiz - wsiz, wsiz);
1205
+ *sp = rsiz;
1206
+ return rbuf;
1207
+ }
1208
+
1209
+
1210
+
1211
+ // END OF FILE