redis-server 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (290) hide show
  1. data/LICENSE +38 -0
  2. data/README.md +33 -0
  3. data/bin/redis +114 -0
  4. data/redis/Makefile +5 -0
  5. data/redis/extconf.rb +3 -0
  6. data/redis/redis-2.2.11/00-RELEASENOTES +199 -0
  7. data/redis/redis-2.2.11/BUGS +1 -0
  8. data/redis/redis-2.2.11/CONTRIBUTING +13 -0
  9. data/redis/redis-2.2.11/COPYING +10 -0
  10. data/redis/redis-2.2.11/Changelog +1032 -0
  11. data/redis/redis-2.2.11/INSTALL +30 -0
  12. data/redis/redis-2.2.11/Makefile +22 -0
  13. data/redis/redis-2.2.11/README +83 -0
  14. data/redis/redis-2.2.11/TODO +4 -0
  15. data/redis/redis-2.2.11/client-libraries/README +11 -0
  16. data/redis/redis-2.2.11/deps/hiredis/COPYING +10 -0
  17. data/redis/redis-2.2.11/deps/hiredis/Makefile +115 -0
  18. data/redis/redis-2.2.11/deps/hiredis/README.md +311 -0
  19. data/redis/redis-2.2.11/deps/hiredis/TODO +2 -0
  20. data/redis/redis-2.2.11/deps/hiredis/adapters/ae.h +95 -0
  21. data/redis/redis-2.2.11/deps/hiredis/adapters/libev.h +113 -0
  22. data/redis/redis-2.2.11/deps/hiredis/adapters/libevent.h +76 -0
  23. data/redis/redis-2.2.11/deps/hiredis/async.c +321 -0
  24. data/redis/redis-2.2.11/deps/hiredis/async.h +112 -0
  25. data/redis/redis-2.2.11/deps/hiredis/example-ae.c +53 -0
  26. data/redis/redis-2.2.11/deps/hiredis/example-libev.c +47 -0
  27. data/redis/redis-2.2.11/deps/hiredis/example-libevent.c +48 -0
  28. data/redis/redis-2.2.11/deps/hiredis/example.c +67 -0
  29. data/redis/redis-2.2.11/deps/hiredis/fmacros.h +15 -0
  30. data/redis/redis-2.2.11/deps/hiredis/hiredis.c +1058 -0
  31. data/redis/redis-2.2.11/deps/hiredis/hiredis.h +170 -0
  32. data/redis/redis-2.2.11/deps/hiredis/net.c +170 -0
  33. data/redis/redis-2.2.11/deps/hiredis/net.h +43 -0
  34. data/redis/redis-2.2.11/deps/hiredis/sds.c +479 -0
  35. data/redis/redis-2.2.11/deps/hiredis/sds.h +77 -0
  36. data/redis/redis-2.2.11/deps/hiredis/test.c +479 -0
  37. data/redis/redis-2.2.11/deps/hiredis/util.h +40 -0
  38. data/redis/redis-2.2.11/deps/linenoise/Makefile +10 -0
  39. data/redis/redis-2.2.11/deps/linenoise/README.markdown +45 -0
  40. data/redis/redis-2.2.11/deps/linenoise/example.c +27 -0
  41. data/redis/redis-2.2.11/deps/linenoise/linenoise.c +609 -0
  42. data/redis/redis-2.2.11/deps/linenoise/linenoise.h +55 -0
  43. data/redis/redis-2.2.11/design-documents/REDIS-CLUSTER +214 -0
  44. data/redis/redis-2.2.11/design-documents/REDIS-CLUSTER-2 +343 -0
  45. data/redis/redis-2.2.11/doc/AppendCommand.html +48 -0
  46. data/redis/redis-2.2.11/doc/AppendOnlyFileHowto.html +41 -0
  47. data/redis/redis-2.2.11/doc/AuthCommand.html +39 -0
  48. data/redis/redis-2.2.11/doc/Benchmarks.html +129 -0
  49. data/redis/redis-2.2.11/doc/BgrewriteaofCommand.html +41 -0
  50. data/redis/redis-2.2.11/doc/BgsaveCommand.html +39 -0
  51. data/redis/redis-2.2.11/doc/BlpopCommand.html +51 -0
  52. data/redis/redis-2.2.11/doc/BrpoplpushCommand.html +39 -0
  53. data/redis/redis-2.2.11/doc/CommandReference.html +47 -0
  54. data/redis/redis-2.2.11/doc/Comparisons.html +42 -0
  55. data/redis/redis-2.2.11/doc/ConfigCommand.html +76 -0
  56. data/redis/redis-2.2.11/doc/Configuration.html +38 -0
  57. data/redis/redis-2.2.11/doc/ConnectionHandlingSidebar.html +36 -0
  58. data/redis/redis-2.2.11/doc/ControlCommandsSidebar.html +36 -0
  59. data/redis/redis-2.2.11/doc/Credits.html +38 -0
  60. data/redis/redis-2.2.11/doc/DbsizeCommand.html +38 -0
  61. data/redis/redis-2.2.11/doc/DelCommand.html +41 -0
  62. data/redis/redis-2.2.11/doc/DesignPatterns.html +37 -0
  63. data/redis/redis-2.2.11/doc/EventLibray.html +44 -0
  64. data/redis/redis-2.2.11/doc/ExistsCommand.html +42 -0
  65. data/redis/redis-2.2.11/doc/ExpireCommand.html +96 -0
  66. data/redis/redis-2.2.11/doc/FAQ.html +70 -0
  67. data/redis/redis-2.2.11/doc/Features.html +38 -0
  68. data/redis/redis-2.2.11/doc/FlushallCommand.html +39 -0
  69. data/redis/redis-2.2.11/doc/FlushdbCommand.html +39 -0
  70. data/redis/redis-2.2.11/doc/FromSqlToDataStructures.html +37 -0
  71. data/redis/redis-2.2.11/doc/GenericCommandsSidebar.html +36 -0
  72. data/redis/redis-2.2.11/doc/GetCommand.html +39 -0
  73. data/redis/redis-2.2.11/doc/GetbitCommand.html +39 -0
  74. data/redis/redis-2.2.11/doc/GetsetCommand.html +38 -0
  75. data/redis/redis-2.2.11/doc/HackingStrings.html +83 -0
  76. data/redis/redis-2.2.11/doc/HashCommandsSidebar.html +36 -0
  77. data/redis/redis-2.2.11/doc/Hashes.html +37 -0
  78. data/redis/redis-2.2.11/doc/HdelCommand.html +39 -0
  79. data/redis/redis-2.2.11/doc/HexistsCommand.html +39 -0
  80. data/redis/redis-2.2.11/doc/HgetCommand.html +39 -0
  81. data/redis/redis-2.2.11/doc/HgetallCommand.html +40 -0
  82. data/redis/redis-2.2.11/doc/HincrbyCommand.html +45 -0
  83. data/redis/redis-2.2.11/doc/HlenCommand.html +38 -0
  84. data/redis/redis-2.2.11/doc/HmgetCommand.html +40 -0
  85. data/redis/redis-2.2.11/doc/HmsetCommand.html +40 -0
  86. data/redis/redis-2.2.11/doc/HsetCommand.html +40 -0
  87. data/redis/redis-2.2.11/doc/HsetnxCommand.html +41 -0
  88. data/redis/redis-2.2.11/doc/IncrCommand.html +43 -0
  89. data/redis/redis-2.2.11/doc/InfoCommand.html +48 -0
  90. data/redis/redis-2.2.11/doc/IntroductionToRedisDataTypes.html +152 -0
  91. data/redis/redis-2.2.11/doc/KeysCommand.html +43 -0
  92. data/redis/redis-2.2.11/doc/LastsaveCommand.html +39 -0
  93. data/redis/redis-2.2.11/doc/LindexCommand.html +40 -0
  94. data/redis/redis-2.2.11/doc/ListCommandsSidebar.html +36 -0
  95. data/redis/redis-2.2.11/doc/Lists.html +42 -0
  96. data/redis/redis-2.2.11/doc/LlenCommand.html +41 -0
  97. data/redis/redis-2.2.11/doc/LpopCommand.html +41 -0
  98. data/redis/redis-2.2.11/doc/LrangeCommand.html +47 -0
  99. data/redis/redis-2.2.11/doc/LremCommand.html +41 -0
  100. data/redis/redis-2.2.11/doc/LsetCommand.html +38 -0
  101. data/redis/redis-2.2.11/doc/LtrimCommand.html +47 -0
  102. data/redis/redis-2.2.11/doc/MgetCommand.html +52 -0
  103. data/redis/redis-2.2.11/doc/MonitorCommand.html +63 -0
  104. data/redis/redis-2.2.11/doc/MoveCommand.html +42 -0
  105. data/redis/redis-2.2.11/doc/MsetCommand.html +44 -0
  106. data/redis/redis-2.2.11/doc/MultiExecCommand.html +166 -0
  107. data/redis/redis-2.2.11/doc/NonexistentCommands.html +51 -0
  108. data/redis/redis-2.2.11/doc/ObjectHashMappers.html +39 -0
  109. data/redis/redis-2.2.11/doc/Pipelining.html +36 -0
  110. data/redis/redis-2.2.11/doc/ProgrammingExamples.html +38 -0
  111. data/redis/redis-2.2.11/doc/ProtocolSpecification.html +137 -0
  112. data/redis/redis-2.2.11/doc/PublishSubscribe.html +115 -0
  113. data/redis/redis-2.2.11/doc/QuickStart.html +68 -0
  114. data/redis/redis-2.2.11/doc/QuitCommand.html +38 -0
  115. data/redis/redis-2.2.11/doc/README.html +119 -0
  116. data/redis/redis-2.2.11/doc/RandomkeyCommand.html +39 -0
  117. data/redis/redis-2.2.11/doc/Redis0100ChangeLog.html +67 -0
  118. data/redis/redis-2.2.11/doc/Redis0900ChangeLog.html +56 -0
  119. data/redis/redis-2.2.11/doc/RedisBigData.html +61 -0
  120. data/redis/redis-2.2.11/doc/RedisCLI.html +37 -0
  121. data/redis/redis-2.2.11/doc/RedisEventLibrary.html +70 -0
  122. data/redis/redis-2.2.11/doc/RedisGuides.html +37 -0
  123. data/redis/redis-2.2.11/doc/RedisInternals.html +38 -0
  124. data/redis/redis-2.2.11/doc/RedisPipelining.html +93 -0
  125. data/redis/redis-2.2.11/doc/RedisStatus.html +56 -0
  126. data/redis/redis-2.2.11/doc/Redis_1_2_0_Changelog.html +40 -0
  127. data/redis/redis-2.2.11/doc/Redis_2_0_0_Changelog.html +62 -0
  128. data/redis/redis-2.2.11/doc/Redis_2_0_Whats_new.html +59 -0
  129. data/redis/redis-2.2.11/doc/RenameCommand.html +39 -0
  130. data/redis/redis-2.2.11/doc/RenamenxCommand.html +42 -0
  131. data/redis/redis-2.2.11/doc/ReplicationHowto.html +43 -0
  132. data/redis/redis-2.2.11/doc/ReplyTypes.html +42 -0
  133. data/redis/redis-2.2.11/doc/RoadMap.html +38 -0
  134. data/redis/redis-2.2.11/doc/RpoplpushCommand.html +44 -0
  135. data/redis/redis-2.2.11/doc/RpushCommand.html +40 -0
  136. data/redis/redis-2.2.11/doc/SaddCommand.html +41 -0
  137. data/redis/redis-2.2.11/doc/SaveCommand.html +39 -0
  138. data/redis/redis-2.2.11/doc/ScardCommand.html +41 -0
  139. data/redis/redis-2.2.11/doc/SdiffCommand.html +45 -0
  140. data/redis/redis-2.2.11/doc/SdiffstoreCommand.html +38 -0
  141. data/redis/redis-2.2.11/doc/SelectCommand.html +39 -0
  142. data/redis/redis-2.2.11/doc/SetCommand.html +39 -0
  143. data/redis/redis-2.2.11/doc/SetCommandsSidebar.html +36 -0
  144. data/redis/redis-2.2.11/doc/SetbitCommand.html +45 -0
  145. data/redis/redis-2.2.11/doc/SetexCommand.html +42 -0
  146. data/redis/redis-2.2.11/doc/SetnxCommand.html +51 -0
  147. data/redis/redis-2.2.11/doc/SetrangeCommand.html +58 -0
  148. data/redis/redis-2.2.11/doc/Sets.html +36 -0
  149. data/redis/redis-2.2.11/doc/ShutdownCommand.html +39 -0
  150. data/redis/redis-2.2.11/doc/SideBar.html +36 -0
  151. data/redis/redis-2.2.11/doc/SinterCommand.html +40 -0
  152. data/redis/redis-2.2.11/doc/SinterstoreCommand.html +39 -0
  153. data/redis/redis-2.2.11/doc/SismemberCommand.html +42 -0
  154. data/redis/redis-2.2.11/doc/SlaveofCommand.html +41 -0
  155. data/redis/redis-2.2.11/doc/SmembersCommand.html +38 -0
  156. data/redis/redis-2.2.11/doc/SmoveCommand.html +44 -0
  157. data/redis/redis-2.2.11/doc/SortCommand.html +75 -0
  158. data/redis/redis-2.2.11/doc/SortedSetCommandsSidebar.html +36 -0
  159. data/redis/redis-2.2.11/doc/SortedSets.html +36 -0
  160. data/redis/redis-2.2.11/doc/Speed.html +38 -0
  161. data/redis/redis-2.2.11/doc/SponsorshipHistory.html +38 -0
  162. data/redis/redis-2.2.11/doc/SpopCommand.html +40 -0
  163. data/redis/redis-2.2.11/doc/SrandmemberCommand.html +40 -0
  164. data/redis/redis-2.2.11/doc/SremCommand.html +42 -0
  165. data/redis/redis-2.2.11/doc/StringCommandsSidebar.html +36 -0
  166. data/redis/redis-2.2.11/doc/Strings.html +37 -0
  167. data/redis/redis-2.2.11/doc/StrlenCommand.html +39 -0
  168. data/redis/redis-2.2.11/doc/SubstrCommand.html +52 -0
  169. data/redis/redis-2.2.11/doc/SunionCommand.html +40 -0
  170. data/redis/redis-2.2.11/doc/SunionstoreCommand.html +38 -0
  171. data/redis/redis-2.2.11/doc/SupportedLanguages.html +60 -0
  172. data/redis/redis-2.2.11/doc/SupportedPlatforms.html +37 -0
  173. data/redis/redis-2.2.11/doc/TemplateCommand.html +38 -0
  174. data/redis/redis-2.2.11/doc/TtlCommand.html +38 -0
  175. data/redis/redis-2.2.11/doc/TwitterAlikeExample.html +250 -0
  176. data/redis/redis-2.2.11/doc/TypeCommand.html +46 -0
  177. data/redis/redis-2.2.11/doc/UnstableSource.html +39 -0
  178. data/redis/redis-2.2.11/doc/VirtualMemorySpecification.html +156 -0
  179. data/redis/redis-2.2.11/doc/VirtualMemoryUserGuide.html +66 -0
  180. data/redis/redis-2.2.11/doc/ZaddCommand.html +43 -0
  181. data/redis/redis-2.2.11/doc/ZcardCommand.html +41 -0
  182. data/redis/redis-2.2.11/doc/ZincrbyCommand.html +42 -0
  183. data/redis/redis-2.2.11/doc/ZrangeCommand.html +42 -0
  184. data/redis/redis-2.2.11/doc/ZrangebyscoreCommand.html +77 -0
  185. data/redis/redis-2.2.11/doc/ZrankCommand.html +43 -0
  186. data/redis/redis-2.2.11/doc/ZremCommand.html +42 -0
  187. data/redis/redis-2.2.11/doc/ZremrangebyrankCommand.html +39 -0
  188. data/redis/redis-2.2.11/doc/ZremrangebyscoreCommand.html +39 -0
  189. data/redis/redis-2.2.11/doc/ZscoreCommand.html +41 -0
  190. data/redis/redis-2.2.11/doc/ZunionCommand.html +42 -0
  191. data/redis/redis-2.2.11/doc/ZunionstoreCommand.html +43 -0
  192. data/redis/redis-2.2.11/doc/index.html +43 -0
  193. data/redis/redis-2.2.11/doc/redis.png +0 -0
  194. data/redis/redis-2.2.11/doc/style.css +25 -0
  195. data/redis/redis-2.2.11/redis.conf +417 -0
  196. data/redis/redis-2.2.11/src/Makefile +177 -0
  197. data/redis/redis-2.2.11/src/adlist.c +325 -0
  198. data/redis/redis-2.2.11/src/adlist.h +92 -0
  199. data/redis/redis-2.2.11/src/ae.c +390 -0
  200. data/redis/redis-2.2.11/src/ae.h +117 -0
  201. data/redis/redis-2.2.11/src/ae_epoll.c +91 -0
  202. data/redis/redis-2.2.11/src/ae_kqueue.c +93 -0
  203. data/redis/redis-2.2.11/src/ae_select.c +72 -0
  204. data/redis/redis-2.2.11/src/anet.c +347 -0
  205. data/redis/redis-2.2.11/src/anet.h +57 -0
  206. data/redis/redis-2.2.11/src/aof.c +675 -0
  207. data/redis/redis-2.2.11/src/config.c +627 -0
  208. data/redis/redis-2.2.11/src/config.h +64 -0
  209. data/redis/redis-2.2.11/src/db.c +543 -0
  210. data/redis/redis-2.2.11/src/debug.c +314 -0
  211. data/redis/redis-2.2.11/src/dict.c +721 -0
  212. data/redis/redis-2.2.11/src/dict.h +156 -0
  213. data/redis/redis-2.2.11/src/fmacros.h +15 -0
  214. data/redis/redis-2.2.11/src/help.h +638 -0
  215. data/redis/redis-2.2.11/src/intset.c +422 -0
  216. data/redis/redis-2.2.11/src/intset.h +19 -0
  217. data/redis/redis-2.2.11/src/lzf.h +100 -0
  218. data/redis/redis-2.2.11/src/lzfP.h +159 -0
  219. data/redis/redis-2.2.11/src/lzf_c.c +295 -0
  220. data/redis/redis-2.2.11/src/lzf_d.c +150 -0
  221. data/redis/redis-2.2.11/src/mkreleasehdr.sh +9 -0
  222. data/redis/redis-2.2.11/src/multi.c +268 -0
  223. data/redis/redis-2.2.11/src/networking.c +899 -0
  224. data/redis/redis-2.2.11/src/object.c +484 -0
  225. data/redis/redis-2.2.11/src/pqsort.c +197 -0
  226. data/redis/redis-2.2.11/src/pqsort.h +15 -0
  227. data/redis/redis-2.2.11/src/pubsub.c +267 -0
  228. data/redis/redis-2.2.11/src/rdb.c +1020 -0
  229. data/redis/redis-2.2.11/src/redis-benchmark.c +530 -0
  230. data/redis/redis-2.2.11/src/redis-check-aof.c +185 -0
  231. data/redis/redis-2.2.11/src/redis-check-dump.c +681 -0
  232. data/redis/redis-2.2.11/src/redis-cli.c +773 -0
  233. data/redis/redis-2.2.11/src/redis.c +1677 -0
  234. data/redis/redis-2.2.11/src/redis.h +1022 -0
  235. data/redis/redis-2.2.11/src/release.c +13 -0
  236. data/redis/redis-2.2.11/src/replication.c +557 -0
  237. data/redis/redis-2.2.11/src/sds.c +639 -0
  238. data/redis/redis-2.2.11/src/sds.h +78 -0
  239. data/redis/redis-2.2.11/src/sha1.c +276 -0
  240. data/redis/redis-2.2.11/src/sha1.h +17 -0
  241. data/redis/redis-2.2.11/src/solarisfixes.h +22 -0
  242. data/redis/redis-2.2.11/src/sort.c +389 -0
  243. data/redis/redis-2.2.11/src/syncio.c +154 -0
  244. data/redis/redis-2.2.11/src/t_hash.c +476 -0
  245. data/redis/redis-2.2.11/src/t_list.c +986 -0
  246. data/redis/redis-2.2.11/src/t_set.c +610 -0
  247. data/redis/redis-2.2.11/src/t_string.c +438 -0
  248. data/redis/redis-2.2.11/src/t_zset.c +1084 -0
  249. data/redis/redis-2.2.11/src/testhelp.h +54 -0
  250. data/redis/redis-2.2.11/src/util.c +243 -0
  251. data/redis/redis-2.2.11/src/valgrind.sup +5 -0
  252. data/redis/redis-2.2.11/src/version.h +1 -0
  253. data/redis/redis-2.2.11/src/vm.c +1149 -0
  254. data/redis/redis-2.2.11/src/ziplist.c +1323 -0
  255. data/redis/redis-2.2.11/src/ziplist.h +15 -0
  256. data/redis/redis-2.2.11/src/zipmap.c +455 -0
  257. data/redis/redis-2.2.11/src/zipmap.h +48 -0
  258. data/redis/redis-2.2.11/src/zmalloc.c +278 -0
  259. data/redis/redis-2.2.11/src/zmalloc.h +47 -0
  260. data/redis/redis-2.2.11/tests/assets/default.conf +308 -0
  261. data/redis/redis-2.2.11/tests/integration/aof.tcl +104 -0
  262. data/redis/redis-2.2.11/tests/integration/redis-cli.tcl +208 -0
  263. data/redis/redis-2.2.11/tests/integration/replication.tcl +98 -0
  264. data/redis/redis-2.2.11/tests/support/redis.tcl +241 -0
  265. data/redis/redis-2.2.11/tests/support/server.tcl +294 -0
  266. data/redis/redis-2.2.11/tests/support/test.tcl +190 -0
  267. data/redis/redis-2.2.11/tests/support/tmpfile.tcl +15 -0
  268. data/redis/redis-2.2.11/tests/support/util.tcl +296 -0
  269. data/redis/redis-2.2.11/tests/test_helper.tcl +221 -0
  270. data/redis/redis-2.2.11/tests/unit/auth.tcl +15 -0
  271. data/redis/redis-2.2.11/tests/unit/basic.tcl +616 -0
  272. data/redis/redis-2.2.11/tests/unit/cas.tcl +135 -0
  273. data/redis/redis-2.2.11/tests/unit/expire.tcl +74 -0
  274. data/redis/redis-2.2.11/tests/unit/other.tcl +240 -0
  275. data/redis/redis-2.2.11/tests/unit/printver.tcl +6 -0
  276. data/redis/redis-2.2.11/tests/unit/protocol.tcl +62 -0
  277. data/redis/redis-2.2.11/tests/unit/pubsub.tcl +195 -0
  278. data/redis/redis-2.2.11/tests/unit/quit.tcl +40 -0
  279. data/redis/redis-2.2.11/tests/unit/sort.tcl +189 -0
  280. data/redis/redis-2.2.11/tests/unit/type/hash.tcl +300 -0
  281. data/redis/redis-2.2.11/tests/unit/type/list.tcl +819 -0
  282. data/redis/redis-2.2.11/tests/unit/type/set.tcl +334 -0
  283. data/redis/redis-2.2.11/tests/unit/type/zset.tcl +587 -0
  284. data/redis/redis-2.2.11/utils/build-static-symbols.tcl +22 -0
  285. data/redis/redis-2.2.11/utils/generate-command-help.rb +112 -0
  286. data/redis/redis-2.2.11/utils/mktarball.sh +13 -0
  287. data/redis/redis-2.2.11/utils/redis-copy.rb +78 -0
  288. data/redis/redis-2.2.11/utils/redis-sha1.rb +52 -0
  289. data/redis/redis-2.2.11/utils/redis_init_script +42 -0
  290. metadata +362 -0
@@ -0,0 +1,1323 @@
1
+ /* The ziplist is a specially encoded dually linked list that is designed
2
+ * to be very memory efficient. It stores both strings and integer values,
3
+ * where integers are encoded as actual integers instead of a series of
4
+ * characters. It allows push and pop operations on either side of the list
5
+ * in O(1) time. However, because every operation requires a reallocation of
6
+ * the memory used by the ziplist, the actual complexity is related to the
7
+ * amount of memory used by the ziplist.
8
+ *
9
+ * ----------------------------------------------------------------------------
10
+ *
11
+ * ZIPLIST OVERALL LAYOUT:
12
+ * The general layout of the ziplist is as follows:
13
+ * <zlbytes><zltail><zllen><entry><entry><zlend>
14
+ *
15
+ * <zlbytes> is an unsigned integer to hold the number of bytes that the
16
+ * ziplist occupies. This value needs to be stored to be able to resize the
17
+ * entire structure without the need to traverse it first.
18
+ *
19
+ * <zltail> is the offset to the last entry in the list. This allows a pop
20
+ * operation on the far side of the list without the need for full traversal.
21
+ *
22
+ * <zllen> is the number of entries.When this value is larger than 2**16-2,
23
+ * we need to traverse the entire list to know how many items it holds.
24
+ *
25
+ * <zlend> is a single byte special value, equal to 255, which indicates the
26
+ * end of the list.
27
+ *
28
+ * ZIPLIST ENTRIES:
29
+ * Every entry in the ziplist is prefixed by a header that contains two pieces
30
+ * of information. First, the length of the previous entry is stored to be
31
+ * able to traverse the list from back to front. Second, the encoding with an
32
+ * optional string length of the entry itself is stored.
33
+ *
34
+ * The length of the previous entry is encoded in the following way:
35
+ * If this length is smaller than 254 bytes, it will only consume a single
36
+ * byte that takes the length as value. When the length is greater than or
37
+ * equal to 254, it will consume 5 bytes. The first byte is set to 254 to
38
+ * indicate a larger value is following. The remaining 4 bytes take the
39
+ * length of the previous entry as value.
40
+ *
41
+ * The other header field of the entry itself depends on the contents of the
42
+ * entry. When the entry is a string, the first 2 bits of this header will hold
43
+ * the type of encoding used to store the length of the string, followed by the
44
+ * actual length of the string. When the entry is an integer the first 2 bits
45
+ * are both set to 1. The following 2 bits are used to specify what kind of
46
+ * integer will be stored after this header. An overview of the different
47
+ * types and encodings is as follows:
48
+ *
49
+ * |00pppppp| - 1 byte
50
+ * String value with length less than or equal to 63 bytes (6 bits).
51
+ * |01pppppp|qqqqqqqq| - 2 bytes
52
+ * String value with length less than or equal to 16383 bytes (14 bits).
53
+ * |10______|qqqqqqqq|rrrrrrrr|ssssssss|tttttttt| - 5 bytes
54
+ * String value with length greater than or equal to 16384 bytes.
55
+ * |1100____| - 1 byte
56
+ * Integer encoded as int16_t (2 bytes).
57
+ * |1101____| - 1 byte
58
+ * Integer encoded as int32_t (4 bytes).
59
+ * |1110____| - 1 byte
60
+ * Integer encoded as int64_t (8 bytes).
61
+ */
62
+
63
+ #include <stdio.h>
64
+ #include <stdlib.h>
65
+ #include <string.h>
66
+ #include <stdint.h>
67
+ #include <assert.h>
68
+ #include <limits.h>
69
+ #include "zmalloc.h"
70
+ #include "ziplist.h"
71
+
72
+ int ll2string(char *s, size_t len, long long value);
73
+
74
+ #define ZIP_END 255
75
+ #define ZIP_BIGLEN 254
76
+
77
+ /* Different encoding/length possibilities */
78
+ #define ZIP_STR_06B (0 << 6)
79
+ #define ZIP_STR_14B (1 << 6)
80
+ #define ZIP_STR_32B (2 << 6)
81
+ #define ZIP_INT_16B (0xc0 | 0<<4)
82
+ #define ZIP_INT_32B (0xc0 | 1<<4)
83
+ #define ZIP_INT_64B (0xc0 | 2<<4)
84
+
85
+ /* Macro's to determine type */
86
+ #define ZIP_IS_STR(enc) (((enc) & 0xc0) < 0xc0)
87
+ #define ZIP_IS_INT(enc) (!ZIP_IS_STR(enc) && ((enc) & 0x30) < 0x30)
88
+
89
+ /* Utility macros */
90
+ #define ZIPLIST_BYTES(zl) (*((uint32_t*)(zl)))
91
+ #define ZIPLIST_TAIL_OFFSET(zl) (*((uint32_t*)((zl)+sizeof(uint32_t))))
92
+ #define ZIPLIST_LENGTH(zl) (*((uint16_t*)((zl)+sizeof(uint32_t)*2)))
93
+ #define ZIPLIST_HEADER_SIZE (sizeof(uint32_t)*2+sizeof(uint16_t))
94
+ #define ZIPLIST_ENTRY_HEAD(zl) ((zl)+ZIPLIST_HEADER_SIZE)
95
+ #define ZIPLIST_ENTRY_TAIL(zl) ((zl)+ZIPLIST_TAIL_OFFSET(zl))
96
+ #define ZIPLIST_ENTRY_END(zl) ((zl)+ZIPLIST_BYTES(zl)-1)
97
+
98
+ /* We know a positive increment can only be 1 because entries can only be
99
+ * pushed one at a time. */
100
+ #define ZIPLIST_INCR_LENGTH(zl,incr) { \
101
+ if (ZIPLIST_LENGTH(zl) < UINT16_MAX) ZIPLIST_LENGTH(zl)+=incr; }
102
+
103
+ typedef struct zlentry {
104
+ unsigned int prevrawlensize, prevrawlen;
105
+ unsigned int lensize, len;
106
+ unsigned int headersize;
107
+ unsigned char encoding;
108
+ unsigned char *p;
109
+ } zlentry;
110
+
111
+ /* Return the encoding pointer to by 'p'. */
112
+ static unsigned int zipEntryEncoding(unsigned char *p) {
113
+ /* String encoding: 2 MSBs */
114
+ unsigned char b = p[0] & 0xc0;
115
+ if (b < 0xc0) {
116
+ return b;
117
+ } else {
118
+ /* Integer encoding: 4 MSBs */
119
+ return p[0] & 0xf0;
120
+ }
121
+ assert(NULL);
122
+ return 0;
123
+ }
124
+
125
+ /* Return bytes needed to store integer encoded by 'encoding' */
126
+ static unsigned int zipIntSize(unsigned char encoding) {
127
+ switch(encoding) {
128
+ case ZIP_INT_16B: return sizeof(int16_t);
129
+ case ZIP_INT_32B: return sizeof(int32_t);
130
+ case ZIP_INT_64B: return sizeof(int64_t);
131
+ }
132
+ assert(NULL);
133
+ return 0;
134
+ }
135
+
136
+ /* Decode the encoded length pointed by 'p'. If a pointer to 'lensize' is
137
+ * provided, it is set to the number of bytes required to encode the length. */
138
+ static unsigned int zipDecodeLength(unsigned char *p, unsigned int *lensize) {
139
+ unsigned char encoding = zipEntryEncoding(p);
140
+ unsigned int len = 0;
141
+
142
+ if (ZIP_IS_STR(encoding)) {
143
+ switch(encoding) {
144
+ case ZIP_STR_06B:
145
+ len = p[0] & 0x3f;
146
+ if (lensize) *lensize = 1;
147
+ break;
148
+ case ZIP_STR_14B:
149
+ len = ((p[0] & 0x3f) << 8) | p[1];
150
+ if (lensize) *lensize = 2;
151
+ break;
152
+ case ZIP_STR_32B:
153
+ len = (p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4];
154
+ if (lensize) *lensize = 5;
155
+ break;
156
+ default:
157
+ assert(NULL);
158
+ }
159
+ } else {
160
+ len = zipIntSize(encoding);
161
+ if (lensize) *lensize = 1;
162
+ }
163
+ return len;
164
+ }
165
+
166
+ /* Encode the length 'l' writing it in 'p'. If p is NULL it just returns
167
+ * the amount of bytes required to encode such a length. */
168
+ static unsigned int zipEncodeLength(unsigned char *p, unsigned char encoding, unsigned int rawlen) {
169
+ unsigned char len = 1, buf[5];
170
+
171
+ if (ZIP_IS_STR(encoding)) {
172
+ /* Although encoding is given it may not be set for strings,
173
+ * so we determine it here using the raw length. */
174
+ if (rawlen <= 0x3f) {
175
+ if (!p) return len;
176
+ buf[0] = ZIP_STR_06B | rawlen;
177
+ } else if (rawlen <= 0x3fff) {
178
+ len += 1;
179
+ if (!p) return len;
180
+ buf[0] = ZIP_STR_14B | ((rawlen >> 8) & 0x3f);
181
+ buf[1] = rawlen & 0xff;
182
+ } else {
183
+ len += 4;
184
+ if (!p) return len;
185
+ buf[0] = ZIP_STR_32B;
186
+ buf[1] = (rawlen >> 24) & 0xff;
187
+ buf[2] = (rawlen >> 16) & 0xff;
188
+ buf[3] = (rawlen >> 8) & 0xff;
189
+ buf[4] = rawlen & 0xff;
190
+ }
191
+ } else {
192
+ /* Implies integer encoding, so length is always 1. */
193
+ if (!p) return len;
194
+ buf[0] = encoding;
195
+ }
196
+
197
+ /* Store this length at p */
198
+ memcpy(p,buf,len);
199
+ return len;
200
+ }
201
+
202
+ /* Decode the length of the previous element stored at "p". */
203
+ static unsigned int zipPrevDecodeLength(unsigned char *p, unsigned int *lensize) {
204
+ unsigned int len = *p;
205
+ if (len < ZIP_BIGLEN) {
206
+ if (lensize) *lensize = 1;
207
+ } else {
208
+ if (lensize) *lensize = 1+sizeof(len);
209
+ memcpy(&len,p+1,sizeof(len));
210
+ }
211
+ return len;
212
+ }
213
+
214
+ /* Encode the length of the previous entry and write it to "p". Return the
215
+ * number of bytes needed to encode this length if "p" is NULL. */
216
+ static unsigned int zipPrevEncodeLength(unsigned char *p, unsigned int len) {
217
+ if (p == NULL) {
218
+ return (len < ZIP_BIGLEN) ? 1 : sizeof(len)+1;
219
+ } else {
220
+ if (len < ZIP_BIGLEN) {
221
+ p[0] = len;
222
+ return 1;
223
+ } else {
224
+ p[0] = ZIP_BIGLEN;
225
+ memcpy(p+1,&len,sizeof(len));
226
+ return 1+sizeof(len);
227
+ }
228
+ }
229
+ }
230
+
231
+ /* Encode the length of the previous entry and write it to "p". This only
232
+ * uses the larger encoding (required in __ziplistCascadeUpdate). */
233
+ static void zipPrevEncodeLengthForceLarge(unsigned char *p, unsigned int len) {
234
+ if (p == NULL) return;
235
+ p[0] = ZIP_BIGLEN;
236
+ memcpy(p+1,&len,sizeof(len));
237
+ }
238
+
239
+ /* Return the difference in number of bytes needed to store the new length
240
+ * "len" on the entry pointed to by "p". */
241
+ static int zipPrevLenByteDiff(unsigned char *p, unsigned int len) {
242
+ unsigned int prevlensize;
243
+ zipPrevDecodeLength(p,&prevlensize);
244
+ return zipPrevEncodeLength(NULL,len)-prevlensize;
245
+ }
246
+
247
+ /* Check if string pointed to by 'entry' can be encoded as an integer.
248
+ * Stores the integer value in 'v' and its encoding in 'encoding'. */
249
+ static int zipTryEncoding(unsigned char *entry, unsigned int entrylen, long long *v, unsigned char *encoding) {
250
+ long long value;
251
+ char *eptr;
252
+ char buf[32];
253
+
254
+ if (entrylen >= 32 || entrylen == 0) return 0;
255
+ if (entry[0] == '-' || (entry[0] >= '0' && entry[0] <= '9')) {
256
+ int slen;
257
+
258
+ /* Perform a back-and-forth conversion to make sure that
259
+ * the string turned into an integer is not losing any info. */
260
+ memcpy(buf,entry,entrylen);
261
+ buf[entrylen] = '\0';
262
+ value = strtoll(buf,&eptr,10);
263
+ if (eptr[0] != '\0') return 0;
264
+ slen = ll2string(buf,32,value);
265
+ if (entrylen != (unsigned)slen || memcmp(buf,entry,slen)) return 0;
266
+
267
+ /* Great, the string can be encoded. Check what's the smallest
268
+ * of our encoding types that can hold this value. */
269
+ if (value >= INT16_MIN && value <= INT16_MAX) {
270
+ *encoding = ZIP_INT_16B;
271
+ } else if (value >= INT32_MIN && value <= INT32_MAX) {
272
+ *encoding = ZIP_INT_32B;
273
+ } else {
274
+ *encoding = ZIP_INT_64B;
275
+ }
276
+ *v = value;
277
+ return 1;
278
+ }
279
+ return 0;
280
+ }
281
+
282
+ /* Store integer 'value' at 'p', encoded as 'encoding' */
283
+ static void zipSaveInteger(unsigned char *p, int64_t value, unsigned char encoding) {
284
+ int16_t i16;
285
+ int32_t i32;
286
+ int64_t i64;
287
+ if (encoding == ZIP_INT_16B) {
288
+ i16 = value;
289
+ memcpy(p,&i16,sizeof(i16));
290
+ } else if (encoding == ZIP_INT_32B) {
291
+ i32 = value;
292
+ memcpy(p,&i32,sizeof(i32));
293
+ } else if (encoding == ZIP_INT_64B) {
294
+ i64 = value;
295
+ memcpy(p,&i64,sizeof(i64));
296
+ } else {
297
+ assert(NULL);
298
+ }
299
+ }
300
+
301
+ /* Read integer encoded as 'encoding' from 'p' */
302
+ static int64_t zipLoadInteger(unsigned char *p, unsigned char encoding) {
303
+ int16_t i16;
304
+ int32_t i32;
305
+ int64_t i64, ret = 0;
306
+ if (encoding == ZIP_INT_16B) {
307
+ memcpy(&i16,p,sizeof(i16));
308
+ ret = i16;
309
+ } else if (encoding == ZIP_INT_32B) {
310
+ memcpy(&i32,p,sizeof(i32));
311
+ ret = i32;
312
+ } else if (encoding == ZIP_INT_64B) {
313
+ memcpy(&i64,p,sizeof(i64));
314
+ ret = i64;
315
+ } else {
316
+ assert(NULL);
317
+ }
318
+ return ret;
319
+ }
320
+
321
+ /* Return a struct with all information about an entry. */
322
+ static zlentry zipEntry(unsigned char *p) {
323
+ zlentry e;
324
+ e.prevrawlen = zipPrevDecodeLength(p,&e.prevrawlensize);
325
+ e.len = zipDecodeLength(p+e.prevrawlensize,&e.lensize);
326
+ e.headersize = e.prevrawlensize+e.lensize;
327
+ e.encoding = zipEntryEncoding(p+e.prevrawlensize);
328
+ e.p = p;
329
+ return e;
330
+ }
331
+
332
+ /* Return the total number of bytes used by the entry at "p". */
333
+ static unsigned int zipRawEntryLength(unsigned char *p) {
334
+ zlentry e = zipEntry(p);
335
+ return e.headersize + e.len;
336
+ }
337
+
338
+ /* Create a new empty ziplist. */
339
+ unsigned char *ziplistNew(void) {
340
+ unsigned int bytes = ZIPLIST_HEADER_SIZE+1;
341
+ unsigned char *zl = zmalloc(bytes);
342
+ ZIPLIST_BYTES(zl) = bytes;
343
+ ZIPLIST_TAIL_OFFSET(zl) = ZIPLIST_HEADER_SIZE;
344
+ ZIPLIST_LENGTH(zl) = 0;
345
+ zl[bytes-1] = ZIP_END;
346
+ return zl;
347
+ }
348
+
349
+ /* Resize the ziplist. */
350
+ static unsigned char *ziplistResize(unsigned char *zl, unsigned int len) {
351
+ zl = zrealloc(zl,len);
352
+ ZIPLIST_BYTES(zl) = len;
353
+ zl[len-1] = ZIP_END;
354
+ return zl;
355
+ }
356
+
357
+ /* When an entry is inserted, we need to set the prevlen field of the next
358
+ * entry to equal the length of the inserted entry. It can occur that this
359
+ * length cannot be encoded in 1 byte and the next entry needs to be grow
360
+ * a bit larger to hold the 5-byte encoded prevlen. This can be done for free,
361
+ * because this only happens when an entry is already being inserted (which
362
+ * causes a realloc and memmove). However, encoding the prevlen may require
363
+ * that this entry is grown as well. This effect may cascade throughout
364
+ * the ziplist when there are consecutive entries with a size close to
365
+ * ZIP_BIGLEN, so we need to check that the prevlen can be encoded in every
366
+ * consecutive entry.
367
+ *
368
+ * Note that this effect can also happen in reverse, where the bytes required
369
+ * to encode the prevlen field can shrink. This effect is deliberately ignored,
370
+ * because it can cause a "flapping" effect where a chain prevlen fields is
371
+ * first grown and then shrunk again after consecutive inserts. Rather, the
372
+ * field is allowed to stay larger than necessary, because a large prevlen
373
+ * field implies the ziplist is holding large entries anyway.
374
+ *
375
+ * The pointer "p" points to the first entry that does NOT need to be
376
+ * updated, i.e. consecutive fields MAY need an update. */
377
+ static unsigned char *__ziplistCascadeUpdate(unsigned char *zl, unsigned char *p) {
378
+ unsigned int curlen = ZIPLIST_BYTES(zl), rawlen, rawlensize;
379
+ unsigned int offset, noffset, extra;
380
+ unsigned char *np;
381
+ zlentry cur, next;
382
+
383
+ while (p[0] != ZIP_END) {
384
+ cur = zipEntry(p);
385
+ rawlen = cur.headersize + cur.len;
386
+ rawlensize = zipPrevEncodeLength(NULL,rawlen);
387
+
388
+ /* Abort if there is no next entry. */
389
+ if (p[rawlen] == ZIP_END) break;
390
+ next = zipEntry(p+rawlen);
391
+
392
+ /* Abort when "prevlen" has not changed. */
393
+ if (next.prevrawlen == rawlen) break;
394
+
395
+ if (next.prevrawlensize < rawlensize) {
396
+ /* The "prevlen" field of "next" needs more bytes to hold
397
+ * the raw length of "cur". */
398
+ offset = p-zl;
399
+ extra = rawlensize-next.prevrawlensize;
400
+ zl = ziplistResize(zl,curlen+extra);
401
+ p = zl+offset;
402
+
403
+ /* Current pointer and offset for next element. */
404
+ np = p+rawlen;
405
+ noffset = np-zl;
406
+
407
+ /* Update tail offset when next element is not the tail element. */
408
+ if ((zl+ZIPLIST_TAIL_OFFSET(zl)) != np)
409
+ ZIPLIST_TAIL_OFFSET(zl) += extra;
410
+
411
+ /* Move the tail to the back. */
412
+ memmove(np+rawlensize,
413
+ np+next.prevrawlensize,
414
+ curlen-noffset-next.prevrawlensize-1);
415
+ zipPrevEncodeLength(np,rawlen);
416
+
417
+ /* Advance the cursor */
418
+ p += rawlen;
419
+ curlen += extra;
420
+ } else {
421
+ if (next.prevrawlensize > rawlensize) {
422
+ /* This would result in shrinking, which we want to avoid.
423
+ * So, set "rawlen" in the available bytes. */
424
+ zipPrevEncodeLengthForceLarge(p+rawlen,rawlen);
425
+ } else {
426
+ zipPrevEncodeLength(p+rawlen,rawlen);
427
+ }
428
+
429
+ /* Stop here, as the raw length of "next" has not changed. */
430
+ break;
431
+ }
432
+ }
433
+ return zl;
434
+ }
435
+
436
+ /* Delete "num" entries, starting at "p". Returns pointer to the ziplist. */
437
+ static unsigned char *__ziplistDelete(unsigned char *zl, unsigned char *p, unsigned int num) {
438
+ unsigned int i, totlen, deleted = 0;
439
+ int offset, nextdiff = 0;
440
+ zlentry first, tail;
441
+
442
+ first = zipEntry(p);
443
+ for (i = 0; p[0] != ZIP_END && i < num; i++) {
444
+ p += zipRawEntryLength(p);
445
+ deleted++;
446
+ }
447
+
448
+ totlen = p-first.p;
449
+ if (totlen > 0) {
450
+ if (p[0] != ZIP_END) {
451
+ /* Tricky: storing the prevlen in this entry might reduce or
452
+ * increase the number of bytes needed, compared to the current
453
+ * prevlen. Note that we can always store this length because
454
+ * it was previously stored by an entry that is being deleted. */
455
+ nextdiff = zipPrevLenByteDiff(p,first.prevrawlen);
456
+ zipPrevEncodeLength(p-nextdiff,first.prevrawlen);
457
+
458
+ /* Update offset for tail */
459
+ ZIPLIST_TAIL_OFFSET(zl) -= totlen;
460
+
461
+ /* When the tail contains more than one entry, we need to take
462
+ * "nextdiff" in account as well. Otherwise, a change in the
463
+ * size of prevlen doesn't have an effect on the *tail* offset. */
464
+ tail = zipEntry(p);
465
+ if (p[tail.headersize+tail.len] != ZIP_END)
466
+ ZIPLIST_TAIL_OFFSET(zl) += nextdiff;
467
+
468
+ /* Move tail to the front of the ziplist */
469
+ memmove(first.p,p-nextdiff,ZIPLIST_BYTES(zl)-(p-zl)-1+nextdiff);
470
+ } else {
471
+ /* The entire tail was deleted. No need to move memory. */
472
+ ZIPLIST_TAIL_OFFSET(zl) = (first.p-zl)-first.prevrawlen;
473
+ }
474
+
475
+ /* Resize and update length */
476
+ offset = first.p-zl;
477
+ zl = ziplistResize(zl, ZIPLIST_BYTES(zl)-totlen+nextdiff);
478
+ ZIPLIST_INCR_LENGTH(zl,-deleted);
479
+ p = zl+offset;
480
+
481
+ /* When nextdiff != 0, the raw length of the next entry has changed, so
482
+ * we need to cascade the update throughout the ziplist */
483
+ if (nextdiff != 0)
484
+ zl = __ziplistCascadeUpdate(zl,p);
485
+ }
486
+ return zl;
487
+ }
488
+
489
+ /* Insert item at "p". */
490
+ static unsigned char *__ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {
491
+ unsigned int curlen = ZIPLIST_BYTES(zl), reqlen, prevlen = 0;
492
+ unsigned int offset, nextdiff = 0;
493
+ unsigned char encoding = 0;
494
+ long long value;
495
+ zlentry entry, tail;
496
+
497
+ /* Find out prevlen for the entry that is inserted. */
498
+ if (p[0] != ZIP_END) {
499
+ entry = zipEntry(p);
500
+ prevlen = entry.prevrawlen;
501
+ } else {
502
+ unsigned char *ptail = ZIPLIST_ENTRY_TAIL(zl);
503
+ if (ptail[0] != ZIP_END) {
504
+ prevlen = zipRawEntryLength(ptail);
505
+ }
506
+ }
507
+
508
+ /* See if the entry can be encoded */
509
+ if (zipTryEncoding(s,slen,&value,&encoding)) {
510
+ /* 'encoding' is set to the appropriate integer encoding */
511
+ reqlen = zipIntSize(encoding);
512
+ } else {
513
+ /* 'encoding' is untouched, however zipEncodeLength will use the
514
+ * string length to figure out how to encode it. */
515
+ reqlen = slen;
516
+ }
517
+ /* We need space for both the length of the previous entry and
518
+ * the length of the payload. */
519
+ reqlen += zipPrevEncodeLength(NULL,prevlen);
520
+ reqlen += zipEncodeLength(NULL,encoding,slen);
521
+
522
+ /* When the insert position is not equal to the tail, we need to
523
+ * make sure that the next entry can hold this entry's length in
524
+ * its prevlen field. */
525
+ nextdiff = (p[0] != ZIP_END) ? zipPrevLenByteDiff(p,reqlen) : 0;
526
+
527
+ /* Store offset because a realloc may change the address of zl. */
528
+ offset = p-zl;
529
+ zl = ziplistResize(zl,curlen+reqlen+nextdiff);
530
+ p = zl+offset;
531
+
532
+ /* Apply memory move when necessary and update tail offset. */
533
+ if (p[0] != ZIP_END) {
534
+ /* Subtract one because of the ZIP_END bytes */
535
+ memmove(p+reqlen,p-nextdiff,curlen-offset-1+nextdiff);
536
+
537
+ /* Encode this entry's raw length in the next entry. */
538
+ zipPrevEncodeLength(p+reqlen,reqlen);
539
+
540
+ /* Update offset for tail */
541
+ ZIPLIST_TAIL_OFFSET(zl) += reqlen;
542
+
543
+ /* When the tail contains more than one entry, we need to take
544
+ * "nextdiff" in account as well. Otherwise, a change in the
545
+ * size of prevlen doesn't have an effect on the *tail* offset. */
546
+ tail = zipEntry(p+reqlen);
547
+ if (p[reqlen+tail.headersize+tail.len] != ZIP_END)
548
+ ZIPLIST_TAIL_OFFSET(zl) += nextdiff;
549
+ } else {
550
+ /* This element will be the new tail. */
551
+ ZIPLIST_TAIL_OFFSET(zl) = p-zl;
552
+ }
553
+
554
+ /* When nextdiff != 0, the raw length of the next entry has changed, so
555
+ * we need to cascade the update throughout the ziplist */
556
+ if (nextdiff != 0) {
557
+ offset = p-zl;
558
+ zl = __ziplistCascadeUpdate(zl,p+reqlen);
559
+ p = zl+offset;
560
+ }
561
+
562
+ /* Write the entry */
563
+ p += zipPrevEncodeLength(p,prevlen);
564
+ p += zipEncodeLength(p,encoding,slen);
565
+ if (ZIP_IS_STR(encoding)) {
566
+ memcpy(p,s,slen);
567
+ } else {
568
+ zipSaveInteger(p,value,encoding);
569
+ }
570
+ ZIPLIST_INCR_LENGTH(zl,1);
571
+ return zl;
572
+ }
573
+
574
+ unsigned char *ziplistPush(unsigned char *zl, unsigned char *s, unsigned int slen, int where) {
575
+ unsigned char *p;
576
+ p = (where == ZIPLIST_HEAD) ? ZIPLIST_ENTRY_HEAD(zl) : ZIPLIST_ENTRY_END(zl);
577
+ return __ziplistInsert(zl,p,s,slen);
578
+ }
579
+
580
+ /* Returns an offset to use for iterating with ziplistNext. When the given
581
+ * index is negative, the list is traversed back to front. When the list
582
+ * doesn't contain an element at the provided index, NULL is returned. */
583
+ unsigned char *ziplistIndex(unsigned char *zl, int index) {
584
+ unsigned char *p;
585
+ zlentry entry;
586
+ if (index < 0) {
587
+ index = (-index)-1;
588
+ p = ZIPLIST_ENTRY_TAIL(zl);
589
+ if (p[0] != ZIP_END) {
590
+ entry = zipEntry(p);
591
+ while (entry.prevrawlen > 0 && index--) {
592
+ p -= entry.prevrawlen;
593
+ entry = zipEntry(p);
594
+ }
595
+ }
596
+ } else {
597
+ p = ZIPLIST_ENTRY_HEAD(zl);
598
+ while (p[0] != ZIP_END && index--) {
599
+ p += zipRawEntryLength(p);
600
+ }
601
+ }
602
+ return (p[0] == ZIP_END || index > 0) ? NULL : p;
603
+ }
604
+
605
+ /* Return pointer to next entry in ziplist.
606
+ *
607
+ * zl is the pointer to the ziplist
608
+ * p is the pointer to the current element
609
+ *
610
+ * The element after 'p' is returned, otherwise NULL if we are at the end. */
611
+ unsigned char *ziplistNext(unsigned char *zl, unsigned char *p) {
612
+ ((void) zl);
613
+
614
+ /* "p" could be equal to ZIP_END, caused by ziplistDelete,
615
+ * and we should return NULL. Otherwise, we should return NULL
616
+ * when the *next* element is ZIP_END (there is no next entry). */
617
+ if (p[0] == ZIP_END) {
618
+ return NULL;
619
+ } else {
620
+ p = p+zipRawEntryLength(p);
621
+ return (p[0] == ZIP_END) ? NULL : p;
622
+ }
623
+ }
624
+
625
+ /* Return pointer to previous entry in ziplist. */
626
+ unsigned char *ziplistPrev(unsigned char *zl, unsigned char *p) {
627
+ zlentry entry;
628
+
629
+ /* Iterating backwards from ZIP_END should return the tail. When "p" is
630
+ * equal to the first element of the list, we're already at the head,
631
+ * and should return NULL. */
632
+ if (p[0] == ZIP_END) {
633
+ p = ZIPLIST_ENTRY_TAIL(zl);
634
+ return (p[0] == ZIP_END) ? NULL : p;
635
+ } else if (p == ZIPLIST_ENTRY_HEAD(zl)) {
636
+ return NULL;
637
+ } else {
638
+ entry = zipEntry(p);
639
+ assert(entry.prevrawlen > 0);
640
+ return p-entry.prevrawlen;
641
+ }
642
+ }
643
+
644
+ /* Get entry pointer to by 'p' and store in either 'e' or 'v' depending
645
+ * on the encoding of the entry. 'e' is always set to NULL to be able
646
+ * to find out whether the string pointer or the integer value was set.
647
+ * Return 0 if 'p' points to the end of the zipmap, 1 otherwise. */
648
+ unsigned int ziplistGet(unsigned char *p, unsigned char **sstr, unsigned int *slen, long long *sval) {
649
+ zlentry entry;
650
+ if (p == NULL || p[0] == ZIP_END) return 0;
651
+ if (sstr) *sstr = NULL;
652
+
653
+ entry = zipEntry(p);
654
+ if (ZIP_IS_STR(entry.encoding)) {
655
+ if (sstr) {
656
+ *slen = entry.len;
657
+ *sstr = p+entry.headersize;
658
+ }
659
+ } else {
660
+ if (sval) {
661
+ *sval = zipLoadInteger(p+entry.headersize,entry.encoding);
662
+ }
663
+ }
664
+ return 1;
665
+ }
666
+
667
+ /* Insert an entry at "p". */
668
+ unsigned char *ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {
669
+ return __ziplistInsert(zl,p,s,slen);
670
+ }
671
+
672
+ /* Delete a single entry from the ziplist, pointed to by *p.
673
+ * Also update *p in place, to be able to iterate over the
674
+ * ziplist, while deleting entries. */
675
+ unsigned char *ziplistDelete(unsigned char *zl, unsigned char **p) {
676
+ unsigned int offset = *p-zl;
677
+ zl = __ziplistDelete(zl,*p,1);
678
+
679
+ /* Store pointer to current element in p, because ziplistDelete will
680
+ * do a realloc which might result in a different "zl"-pointer.
681
+ * When the delete direction is back to front, we might delete the last
682
+ * entry and end up with "p" pointing to ZIP_END, so check this. */
683
+ *p = zl+offset;
684
+ return zl;
685
+ }
686
+
687
+ /* Delete a range of entries from the ziplist. */
688
+ unsigned char *ziplistDeleteRange(unsigned char *zl, unsigned int index, unsigned int num) {
689
+ unsigned char *p = ziplistIndex(zl,index);
690
+ return (p == NULL) ? zl : __ziplistDelete(zl,p,num);
691
+ }
692
+
693
+ /* Compare entry pointer to by 'p' with 'entry'. Return 1 if equal. */
694
+ unsigned int ziplistCompare(unsigned char *p, unsigned char *sstr, unsigned int slen) {
695
+ zlentry entry;
696
+ unsigned char sencoding;
697
+ long long zval, sval;
698
+ if (p[0] == ZIP_END) return 0;
699
+
700
+ entry = zipEntry(p);
701
+ if (ZIP_IS_STR(entry.encoding)) {
702
+ /* Raw compare */
703
+ if (entry.len == slen) {
704
+ return memcmp(p+entry.headersize,sstr,slen) == 0;
705
+ } else {
706
+ return 0;
707
+ }
708
+ } else {
709
+ /* Try to compare encoded values */
710
+ if (zipTryEncoding(sstr,slen,&sval,&sencoding)) {
711
+ if (entry.encoding == sencoding) {
712
+ zval = zipLoadInteger(p+entry.headersize,entry.encoding);
713
+ return zval == sval;
714
+ }
715
+ }
716
+ }
717
+ return 0;
718
+ }
719
+
720
+ /* Return length of ziplist. */
721
+ unsigned int ziplistLen(unsigned char *zl) {
722
+ unsigned int len = 0;
723
+ if (ZIPLIST_LENGTH(zl) < UINT16_MAX) {
724
+ len = ZIPLIST_LENGTH(zl);
725
+ } else {
726
+ unsigned char *p = zl+ZIPLIST_HEADER_SIZE;
727
+ while (*p != ZIP_END) {
728
+ p += zipRawEntryLength(p);
729
+ len++;
730
+ }
731
+
732
+ /* Re-store length if small enough */
733
+ if (len < UINT16_MAX) ZIPLIST_LENGTH(zl) = len;
734
+ }
735
+ return len;
736
+ }
737
+
738
+ /* Return size in bytes of ziplist. */
739
+ unsigned int ziplistSize(unsigned char *zl) {
740
+ return ZIPLIST_BYTES(zl);
741
+ }
742
+
743
+ void ziplistRepr(unsigned char *zl) {
744
+ unsigned char *p;
745
+ int index = 0;
746
+ zlentry entry;
747
+
748
+ printf(
749
+ "{total bytes %d} "
750
+ "{length %u}\n"
751
+ "{tail offset %u}\n",
752
+ ZIPLIST_BYTES(zl),
753
+ ZIPLIST_LENGTH(zl),
754
+ ZIPLIST_TAIL_OFFSET(zl));
755
+ p = ZIPLIST_ENTRY_HEAD(zl);
756
+ while(*p != ZIP_END) {
757
+ entry = zipEntry(p);
758
+ printf(
759
+ "{"
760
+ "addr 0x%08lx, "
761
+ "index %2d, "
762
+ "offset %5ld, "
763
+ "rl: %5u, "
764
+ "hs %2u, "
765
+ "pl: %5u, "
766
+ "pls: %2u, "
767
+ "payload %5u"
768
+ "} ",
769
+ (long unsigned)p,
770
+ index,
771
+ (unsigned long) (p-zl),
772
+ entry.headersize+entry.len,
773
+ entry.headersize,
774
+ entry.prevrawlen,
775
+ entry.prevrawlensize,
776
+ entry.len);
777
+ p += entry.headersize;
778
+ if (ZIP_IS_STR(entry.encoding)) {
779
+ if (entry.len > 40) {
780
+ if (fwrite(p,40,1,stdout) == 0) perror("fwrite");
781
+ printf("...");
782
+ } else {
783
+ if (entry.len &&
784
+ fwrite(p,entry.len,1,stdout) == 0) perror("fwrite");
785
+ }
786
+ } else {
787
+ printf("%lld", (long long) zipLoadInteger(p,entry.encoding));
788
+ }
789
+ printf("\n");
790
+ p += entry.len;
791
+ index++;
792
+ }
793
+ printf("{end}\n\n");
794
+ }
795
+
796
+ #ifdef ZIPLIST_TEST_MAIN
797
+ #include <sys/time.h>
798
+ #include "adlist.h"
799
+ #include "sds.h"
800
+
801
+ #define debug(f, ...) { if (DEBUG) printf(f, __VA_ARGS__); }
802
+
803
+ unsigned char *createList() {
804
+ unsigned char *zl = ziplistNew();
805
+ zl = ziplistPush(zl, (unsigned char*)"foo", 3, ZIPLIST_TAIL);
806
+ zl = ziplistPush(zl, (unsigned char*)"quux", 4, ZIPLIST_TAIL);
807
+ zl = ziplistPush(zl, (unsigned char*)"hello", 5, ZIPLIST_HEAD);
808
+ zl = ziplistPush(zl, (unsigned char*)"1024", 4, ZIPLIST_TAIL);
809
+ return zl;
810
+ }
811
+
812
+ unsigned char *createIntList() {
813
+ unsigned char *zl = ziplistNew();
814
+ char buf[32];
815
+
816
+ sprintf(buf, "100");
817
+ zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
818
+ sprintf(buf, "128000");
819
+ zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
820
+ sprintf(buf, "-100");
821
+ zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_HEAD);
822
+ sprintf(buf, "4294967296");
823
+ zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_HEAD);
824
+ sprintf(buf, "non integer");
825
+ zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
826
+ sprintf(buf, "much much longer non integer");
827
+ zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
828
+ return zl;
829
+ }
830
+
831
+ long long usec(void) {
832
+ struct timeval tv;
833
+ gettimeofday(&tv,NULL);
834
+ return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;
835
+ }
836
+
837
+ void stress(int pos, int num, int maxsize, int dnum) {
838
+ int i,j,k;
839
+ unsigned char *zl;
840
+ char posstr[2][5] = { "HEAD", "TAIL" };
841
+ long long start;
842
+ for (i = 0; i < maxsize; i+=dnum) {
843
+ zl = ziplistNew();
844
+ for (j = 0; j < i; j++) {
845
+ zl = ziplistPush(zl,(unsigned char*)"quux",4,ZIPLIST_TAIL);
846
+ }
847
+
848
+ /* Do num times a push+pop from pos */
849
+ start = usec();
850
+ for (k = 0; k < num; k++) {
851
+ zl = ziplistPush(zl,(unsigned char*)"quux",4,pos);
852
+ zl = ziplistDeleteRange(zl,0,1);
853
+ }
854
+ printf("List size: %8d, bytes: %8d, %dx push+pop (%s): %6lld usec\n",
855
+ i,ZIPLIST_BYTES(zl),num,posstr[pos],usec()-start);
856
+ zfree(zl);
857
+ }
858
+ }
859
+
860
+ void pop(unsigned char *zl, int where) {
861
+ unsigned char *p, *vstr;
862
+ unsigned int vlen;
863
+ long long vlong;
864
+
865
+ p = ziplistIndex(zl,where == ZIPLIST_HEAD ? 0 : -1);
866
+ if (ziplistGet(p,&vstr,&vlen,&vlong)) {
867
+ if (where == ZIPLIST_HEAD)
868
+ printf("Pop head: ");
869
+ else
870
+ printf("Pop tail: ");
871
+
872
+ if (vstr)
873
+ if (vlen && fwrite(vstr,vlen,1,stdout) == 0) perror("fwrite");
874
+ else
875
+ printf("%lld", vlong);
876
+
877
+ printf("\n");
878
+ ziplistDeleteRange(zl,-1,1);
879
+ } else {
880
+ printf("ERROR: Could not pop\n");
881
+ exit(1);
882
+ }
883
+ }
884
+
885
+ int randstring(char *target, unsigned int min, unsigned int max) {
886
+ int p, len = min+rand()%(max-min+1);
887
+ int minval, maxval;
888
+ switch(rand() % 3) {
889
+ case 0:
890
+ minval = 0;
891
+ maxval = 255;
892
+ break;
893
+ case 1:
894
+ minval = 48;
895
+ maxval = 122;
896
+ break;
897
+ case 2:
898
+ minval = 48;
899
+ maxval = 52;
900
+ break;
901
+ default:
902
+ assert(NULL);
903
+ }
904
+
905
+ while(p < len)
906
+ target[p++] = minval+rand()%(maxval-minval+1);
907
+ return len;
908
+ }
909
+
910
+ int main(int argc, char **argv) {
911
+ unsigned char *zl, *p;
912
+ unsigned char *entry;
913
+ unsigned int elen;
914
+ long long value;
915
+
916
+ /* If an argument is given, use it as the random seed. */
917
+ if (argc == 2)
918
+ srand(atoi(argv[1]));
919
+
920
+ zl = createIntList();
921
+ ziplistRepr(zl);
922
+
923
+ zl = createList();
924
+ ziplistRepr(zl);
925
+
926
+ pop(zl,ZIPLIST_TAIL);
927
+ ziplistRepr(zl);
928
+
929
+ pop(zl,ZIPLIST_HEAD);
930
+ ziplistRepr(zl);
931
+
932
+ pop(zl,ZIPLIST_TAIL);
933
+ ziplistRepr(zl);
934
+
935
+ pop(zl,ZIPLIST_TAIL);
936
+ ziplistRepr(zl);
937
+
938
+ printf("Get element at index 3:\n");
939
+ {
940
+ zl = createList();
941
+ p = ziplistIndex(zl, 3);
942
+ if (!ziplistGet(p, &entry, &elen, &value)) {
943
+ printf("ERROR: Could not access index 3\n");
944
+ return 1;
945
+ }
946
+ if (entry) {
947
+ if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
948
+ printf("\n");
949
+ } else {
950
+ printf("%lld\n", value);
951
+ }
952
+ printf("\n");
953
+ }
954
+
955
+ printf("Get element at index 4 (out of range):\n");
956
+ {
957
+ zl = createList();
958
+ p = ziplistIndex(zl, 4);
959
+ if (p == NULL) {
960
+ printf("No entry\n");
961
+ } else {
962
+ printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p-zl);
963
+ return 1;
964
+ }
965
+ printf("\n");
966
+ }
967
+
968
+ printf("Get element at index -1 (last element):\n");
969
+ {
970
+ zl = createList();
971
+ p = ziplistIndex(zl, -1);
972
+ if (!ziplistGet(p, &entry, &elen, &value)) {
973
+ printf("ERROR: Could not access index -1\n");
974
+ return 1;
975
+ }
976
+ if (entry) {
977
+ if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
978
+ printf("\n");
979
+ } else {
980
+ printf("%lld\n", value);
981
+ }
982
+ printf("\n");
983
+ }
984
+
985
+ printf("Get element at index -4 (first element):\n");
986
+ {
987
+ zl = createList();
988
+ p = ziplistIndex(zl, -4);
989
+ if (!ziplistGet(p, &entry, &elen, &value)) {
990
+ printf("ERROR: Could not access index -4\n");
991
+ return 1;
992
+ }
993
+ if (entry) {
994
+ if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
995
+ printf("\n");
996
+ } else {
997
+ printf("%lld\n", value);
998
+ }
999
+ printf("\n");
1000
+ }
1001
+
1002
+ printf("Get element at index -5 (reverse out of range):\n");
1003
+ {
1004
+ zl = createList();
1005
+ p = ziplistIndex(zl, -5);
1006
+ if (p == NULL) {
1007
+ printf("No entry\n");
1008
+ } else {
1009
+ printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p-zl);
1010
+ return 1;
1011
+ }
1012
+ printf("\n");
1013
+ }
1014
+
1015
+ printf("Iterate list from 0 to end:\n");
1016
+ {
1017
+ zl = createList();
1018
+ p = ziplistIndex(zl, 0);
1019
+ while (ziplistGet(p, &entry, &elen, &value)) {
1020
+ printf("Entry: ");
1021
+ if (entry) {
1022
+ if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1023
+ } else {
1024
+ printf("%lld", value);
1025
+ }
1026
+ p = ziplistNext(zl,p);
1027
+ printf("\n");
1028
+ }
1029
+ printf("\n");
1030
+ }
1031
+
1032
+ printf("Iterate list from 1 to end:\n");
1033
+ {
1034
+ zl = createList();
1035
+ p = ziplistIndex(zl, 1);
1036
+ while (ziplistGet(p, &entry, &elen, &value)) {
1037
+ printf("Entry: ");
1038
+ if (entry) {
1039
+ if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1040
+ } else {
1041
+ printf("%lld", value);
1042
+ }
1043
+ p = ziplistNext(zl,p);
1044
+ printf("\n");
1045
+ }
1046
+ printf("\n");
1047
+ }
1048
+
1049
+ printf("Iterate list from 2 to end:\n");
1050
+ {
1051
+ zl = createList();
1052
+ p = ziplistIndex(zl, 2);
1053
+ while (ziplistGet(p, &entry, &elen, &value)) {
1054
+ printf("Entry: ");
1055
+ if (entry) {
1056
+ if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1057
+ } else {
1058
+ printf("%lld", value);
1059
+ }
1060
+ p = ziplistNext(zl,p);
1061
+ printf("\n");
1062
+ }
1063
+ printf("\n");
1064
+ }
1065
+
1066
+ printf("Iterate starting out of range:\n");
1067
+ {
1068
+ zl = createList();
1069
+ p = ziplistIndex(zl, 4);
1070
+ if (!ziplistGet(p, &entry, &elen, &value)) {
1071
+ printf("No entry\n");
1072
+ } else {
1073
+ printf("ERROR\n");
1074
+ }
1075
+ printf("\n");
1076
+ }
1077
+
1078
+ printf("Iterate from back to front:\n");
1079
+ {
1080
+ zl = createList();
1081
+ p = ziplistIndex(zl, -1);
1082
+ while (ziplistGet(p, &entry, &elen, &value)) {
1083
+ printf("Entry: ");
1084
+ if (entry) {
1085
+ if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1086
+ } else {
1087
+ printf("%lld", value);
1088
+ }
1089
+ p = ziplistPrev(zl,p);
1090
+ printf("\n");
1091
+ }
1092
+ printf("\n");
1093
+ }
1094
+
1095
+ printf("Iterate from back to front, deleting all items:\n");
1096
+ {
1097
+ zl = createList();
1098
+ p = ziplistIndex(zl, -1);
1099
+ while (ziplistGet(p, &entry, &elen, &value)) {
1100
+ printf("Entry: ");
1101
+ if (entry) {
1102
+ if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1103
+ } else {
1104
+ printf("%lld", value);
1105
+ }
1106
+ zl = ziplistDelete(zl,&p);
1107
+ p = ziplistPrev(zl,p);
1108
+ printf("\n");
1109
+ }
1110
+ printf("\n");
1111
+ }
1112
+
1113
+ printf("Delete inclusive range 0,0:\n");
1114
+ {
1115
+ zl = createList();
1116
+ zl = ziplistDeleteRange(zl, 0, 1);
1117
+ ziplistRepr(zl);
1118
+ }
1119
+
1120
+ printf("Delete inclusive range 0,1:\n");
1121
+ {
1122
+ zl = createList();
1123
+ zl = ziplistDeleteRange(zl, 0, 2);
1124
+ ziplistRepr(zl);
1125
+ }
1126
+
1127
+ printf("Delete inclusive range 1,2:\n");
1128
+ {
1129
+ zl = createList();
1130
+ zl = ziplistDeleteRange(zl, 1, 2);
1131
+ ziplistRepr(zl);
1132
+ }
1133
+
1134
+ printf("Delete with start index out of range:\n");
1135
+ {
1136
+ zl = createList();
1137
+ zl = ziplistDeleteRange(zl, 5, 1);
1138
+ ziplistRepr(zl);
1139
+ }
1140
+
1141
+ printf("Delete with num overflow:\n");
1142
+ {
1143
+ zl = createList();
1144
+ zl = ziplistDeleteRange(zl, 1, 5);
1145
+ ziplistRepr(zl);
1146
+ }
1147
+
1148
+ printf("Delete foo while iterating:\n");
1149
+ {
1150
+ zl = createList();
1151
+ p = ziplistIndex(zl,0);
1152
+ while (ziplistGet(p,&entry,&elen,&value)) {
1153
+ if (entry && strncmp("foo",(char*)entry,elen) == 0) {
1154
+ printf("Delete foo\n");
1155
+ zl = ziplistDelete(zl,&p);
1156
+ } else {
1157
+ printf("Entry: ");
1158
+ if (entry) {
1159
+ if (elen && fwrite(entry,elen,1,stdout) == 0)
1160
+ perror("fwrite");
1161
+ } else {
1162
+ printf("%lld",value);
1163
+ }
1164
+ p = ziplistNext(zl,p);
1165
+ printf("\n");
1166
+ }
1167
+ }
1168
+ printf("\n");
1169
+ ziplistRepr(zl);
1170
+ }
1171
+
1172
+ printf("Regression test for >255 byte strings:\n");
1173
+ {
1174
+ char v1[257],v2[257];
1175
+ memset(v1,'x',256);
1176
+ memset(v2,'y',256);
1177
+ zl = ziplistNew();
1178
+ zl = ziplistPush(zl,(unsigned char*)v1,strlen(v1),ZIPLIST_TAIL);
1179
+ zl = ziplistPush(zl,(unsigned char*)v2,strlen(v2),ZIPLIST_TAIL);
1180
+
1181
+ /* Pop values again and compare their value. */
1182
+ p = ziplistIndex(zl,0);
1183
+ assert(ziplistGet(p,&entry,&elen,&value));
1184
+ assert(strncmp(v1,(char*)entry,elen) == 0);
1185
+ p = ziplistIndex(zl,1);
1186
+ assert(ziplistGet(p,&entry,&elen,&value));
1187
+ assert(strncmp(v2,(char*)entry,elen) == 0);
1188
+ printf("SUCCESS\n\n");
1189
+ }
1190
+
1191
+ printf("Create long list and check indices:\n");
1192
+ {
1193
+ zl = ziplistNew();
1194
+ char buf[32];
1195
+ int i,len;
1196
+ for (i = 0; i < 1000; i++) {
1197
+ len = sprintf(buf,"%d",i);
1198
+ zl = ziplistPush(zl,(unsigned char*)buf,len,ZIPLIST_TAIL);
1199
+ }
1200
+ for (i = 0; i < 1000; i++) {
1201
+ p = ziplistIndex(zl,i);
1202
+ assert(ziplistGet(p,NULL,NULL,&value));
1203
+ assert(i == value);
1204
+
1205
+ p = ziplistIndex(zl,-i-1);
1206
+ assert(ziplistGet(p,NULL,NULL,&value));
1207
+ assert(999-i == value);
1208
+ }
1209
+ printf("SUCCESS\n\n");
1210
+ }
1211
+
1212
+ printf("Compare strings with ziplist entries:\n");
1213
+ {
1214
+ zl = createList();
1215
+ p = ziplistIndex(zl,0);
1216
+ if (!ziplistCompare(p,(unsigned char*)"hello",5)) {
1217
+ printf("ERROR: not \"hello\"\n");
1218
+ return 1;
1219
+ }
1220
+ if (ziplistCompare(p,(unsigned char*)"hella",5)) {
1221
+ printf("ERROR: \"hella\"\n");
1222
+ return 1;
1223
+ }
1224
+
1225
+ p = ziplistIndex(zl,3);
1226
+ if (!ziplistCompare(p,(unsigned char*)"1024",4)) {
1227
+ printf("ERROR: not \"1024\"\n");
1228
+ return 1;
1229
+ }
1230
+ if (ziplistCompare(p,(unsigned char*)"1025",4)) {
1231
+ printf("ERROR: \"1025\"\n");
1232
+ return 1;
1233
+ }
1234
+ printf("SUCCESS\n\n");
1235
+ }
1236
+
1237
+ printf("Stress with random payloads of different encoding:\n");
1238
+ {
1239
+ int i,j,len,where;
1240
+ unsigned char *p;
1241
+ char buf[1024];
1242
+ int buflen;
1243
+ list *ref;
1244
+ listNode *refnode;
1245
+
1246
+ /* Hold temp vars from ziplist */
1247
+ unsigned char *sstr;
1248
+ unsigned int slen;
1249
+ long long sval;
1250
+
1251
+ for (i = 0; i < 20000; i++) {
1252
+ zl = ziplistNew();
1253
+ ref = listCreate();
1254
+ listSetFreeMethod(ref,sdsfree);
1255
+ len = rand() % 256;
1256
+
1257
+ /* Create lists */
1258
+ for (j = 0; j < len; j++) {
1259
+ where = (rand() & 1) ? ZIPLIST_HEAD : ZIPLIST_TAIL;
1260
+ if (rand() % 2) {
1261
+ buflen = randstring(buf,1,sizeof(buf)-1);
1262
+ } else {
1263
+ switch(rand() % 3) {
1264
+ case 0:
1265
+ buflen = sprintf(buf,"%lld",(0LL + rand()) >> 20);
1266
+ break;
1267
+ case 1:
1268
+ buflen = sprintf(buf,"%lld",(0LL + rand()));
1269
+ break;
1270
+ case 2:
1271
+ buflen = sprintf(buf,"%lld",(0LL + rand()) << 20);
1272
+ break;
1273
+ default:
1274
+ assert(NULL);
1275
+ }
1276
+ }
1277
+
1278
+ /* Add to ziplist */
1279
+ zl = ziplistPush(zl, (unsigned char*)buf, buflen, where);
1280
+
1281
+ /* Add to reference list */
1282
+ if (where == ZIPLIST_HEAD) {
1283
+ listAddNodeHead(ref,sdsnewlen(buf, buflen));
1284
+ } else if (where == ZIPLIST_TAIL) {
1285
+ listAddNodeTail(ref,sdsnewlen(buf, buflen));
1286
+ } else {
1287
+ assert(NULL);
1288
+ }
1289
+ }
1290
+
1291
+ assert(listLength(ref) == ziplistLen(zl));
1292
+ for (j = 0; j < len; j++) {
1293
+ /* Naive way to get elements, but similar to the stresser
1294
+ * executed from the Tcl test suite. */
1295
+ p = ziplistIndex(zl,j);
1296
+ refnode = listIndex(ref,j);
1297
+
1298
+ assert(ziplistGet(p,&sstr,&slen,&sval));
1299
+ if (sstr == NULL) {
1300
+ buflen = sprintf(buf,"%lld",sval);
1301
+ } else {
1302
+ buflen = slen;
1303
+ memcpy(buf,sstr,buflen);
1304
+ buf[buflen] = '\0';
1305
+ }
1306
+ assert(memcmp(buf,listNodeValue(refnode),buflen) == 0);
1307
+ }
1308
+ zfree(zl);
1309
+ listRelease(ref);
1310
+ }
1311
+ printf("SUCCESS\n\n");
1312
+ }
1313
+
1314
+ printf("Stress with variable ziplist size:\n");
1315
+ {
1316
+ stress(ZIPLIST_HEAD,100000,16384,256);
1317
+ stress(ZIPLIST_TAIL,100000,16384,256);
1318
+ }
1319
+
1320
+ return 0;
1321
+ }
1322
+
1323
+ #endif