├── .github └── workflows │ └── main.yml ├── .gitignore ├── CLONE.md ├── LSM Testing.xlsx ├── License.txt ├── README.md ├── sLSM Tree White Paper.pdf └── src ├── Makefile ├── MurmurHash.cpp ├── MurmurHash.h ├── bloom.hpp ├── diskLevel.hpp ├── diskRun.hpp ├── hashMap.hpp ├── lsm.hpp ├── main.cpp ├── run.hpp └── skipList.hpp /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: GitHub Clone Count Update Everyday 2 | 3 | on: 4 | schedule: 5 | - cron: "0 */24 * * *" 6 | workflow_dispatch: 7 | 8 | jobs: 9 | build: 10 | runs-on: ubuntu-latest 11 | 12 | steps: 13 | - uses: actions/checkout@v2 14 | 15 | - name: gh login 16 | run: echo "${{ secrets.SECRET_TOKEN }}" | gh auth login --with-token 17 | 18 | - name: parse latest clone count 19 | run: | 20 | curl --user "${{ github.actor }}:${{ secrets.SECRET_TOKEN }}" \ 21 | -H "Accept: application/vnd.github.v3+json" \ 22 | https://api.github.com/repos/${{ github.repository }}/traffic/clones \ 23 | > clone.json 24 | 25 | - name: create gist and download previous count 26 | id: set_id 27 | run: | 28 | if gh secret list | grep -q "GIST_ID" 29 | then 30 | echo "GIST_ID found" 31 | echo ::set-output name=GIST::${{ secrets.GIST_ID }} 32 | curl https://gist.githubusercontent.com/${{ github.actor }}/${{ secrets.GIST_ID }}/raw/clone.json > clone_before.json 33 | if cat clone_before.json | grep '404: Not Found'; then 34 | echo "GIST_ID not valid anymore. Creating another gist..." 35 | gist_id=$(gh gist create clone.json | awk -F / '{print $NF}') 36 | echo $gist_id | gh secret set GIST_ID 37 | echo ::set-output name=GIST::$gist_id 38 | cp clone.json clone_before.json 39 | git rm --ignore-unmatch CLONE.md 40 | fi 41 | else 42 | echo "GIST_ID not found. Creating a gist..." 43 | gist_id=$(gh gist create clone.json | awk -F / '{print $NF}') 44 | echo $gist_id | gh secret set GIST_ID 45 | echo ::set-output name=GIST::$gist_id 46 | cp clone.json clone_before.json 47 | fi 48 | 49 | - name: update clone.json 50 | run: | 51 | curl https://raw.githubusercontent.com/MShawon/github-clone-count-badge/master/main.py > main.py 52 | python3 main.py 53 | 54 | - name: Update gist with latest count 55 | run: | 56 | content=$(sed -e 's/\\/\\\\/g' -e 's/\t/\\t/g' -e 's/\"/\\"/g' -e 's/\r//g' "clone.json" | sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g') 57 | echo '{"description": "${{ github.repository }} clone statistics", "files": {"clone.json": {"content": "'"$content"'"}}}' > post_clone.json 58 | curl -s -X PATCH \ 59 | --user "${{ github.actor }}:${{ secrets.SECRET_TOKEN }}" \ 60 | -H "Content-Type: application/json" \ 61 | -d @post_clone.json https://api.github.com/gists/${{ steps.set_id.outputs.GIST }} > /dev/null 2>&1 62 | 63 | if [ ! -f CLONE.md ]; then 64 | shields="https://img.shields.io/badge/dynamic/json?color=success&label=Clone&query=count&url=" 65 | url="https://gist.githubusercontent.com/${{ github.actor }}/${{ steps.set_id.outputs.GIST }}/raw/clone.json" 66 | repo="https://github.com/MShawon/github-clone-count-badge" 67 | echo ''> CLONE.md 68 | echo ' 69 | **Markdown** 70 | 71 | ```markdown' >> CLONE.md 72 | echo "[![GitHub Clones]($shields$url&logo=github)]($repo)" >> CLONE.md 73 | echo ' 74 | ``` 75 | 76 | **HTML** 77 | ```html' >> CLONE.md 78 | echo "GitHub Clones" >> CLONE.md 79 | echo '```' >> CLONE.md 80 | 81 | git add CLONE.md 82 | git config --global user.name "GitHub Action" 83 | git config --global user.email "action@github.com" 84 | git commit -m "create clone count badge" 85 | fi 86 | 87 | - name: Push 88 | uses: ad-m/github-push-action@master 89 | with: 90 | github_token: ${{ secrets.GITHUB_TOKEN }} 91 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Compiled Dynamic libraries 12 | *.so 13 | *.dylib 14 | *.dll 15 | 16 | # Fortran module files 17 | *.mod 18 | *.smod 19 | 20 | # Compiled Static libraries 21 | *.lai 22 | *.la 23 | *.a 24 | *.lib 25 | 26 | # Executables 27 | *.exe 28 | *.out 29 | *.app 30 | 31 | # MacOS files 32 | *.xcodeproj* 33 | disk_files/ 34 | cs265-sysproj/ 35 | -------------------------------------------------------------------------------- /CLONE.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | **Markdown** 4 | 5 | ```markdown 6 | [![GitHub Clones](https://img.shields.io/badge/dynamic/json?color=success&label=Clone&query=count&url=https://gist.githubusercontent.com/aronszanto/333f7e61dbf30bfce5ade0152ea293a8/raw/clone.json&logo=github)](https://github.com/MShawon/github-clone-count-badge) 7 | 8 | ``` 9 | 10 | **HTML** 11 | ```html 12 | GitHub Clones 13 | ``` 14 | -------------------------------------------------------------------------------- /LSM Testing.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aronszanto/sLSM-Tree/9bf445305d08973c301d7a788ec0c0aa737f4c00/LSM Testing.xlsx -------------------------------------------------------------------------------- /License.txt: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sLSM-Tree 2 | [![GitHub Clones](https://img.shields.io/badge/dynamic/json?color=success&label=Clone&query=count&url=https://gist.githubusercontent.com/aronszanto/333f7e61dbf30bfce5ade0152ea293a8/raw/clone.json&logo=github)](https://github.com/MShawon/github-clone-count-badge) (Since 1/31/2024) 3 | 4 | 5 | Research Abstract: 6 | Log-Structured Merge (LSM) Trees provide a tiered data storage and retrieval paradigm that is attractive for write-optimized data systems. Maintaining an efficient buffer in memory and deferring updates past their initial write-time, the structure provides quick operations over hot data. Because each layer of the structure is logically separate from the others, the structure is also conducive to opportunistic and granular optimization. In this project, I introduce the Skiplist-Based LSM Tree (sLSM), a novel system in which the memory buffer of the LSM is composed of a sequence of skiplists. I develop theoretical and experimental results that demonstrate that the breadth of tuning parameters inherent to the sLSM allows it broad flexibility for excellent performance across a wide variety of workloads. 7 | 8 | ## Notes 9 | This project was written entirely in native C and C++, with no dependencies. This means that: 1. there is no part of the code that is left to guesswork; 2. the project is modular enough to swap out components (hash tables, skiplists, etc.) for other ones if desired; and 3. the sLSM Tree is effectively limitlessly portable. 10 | 11 | ## Performance 12 | On commodity hardware, test performance was up to 1 million writes per second and 6 million reads per second. Details in the paper! 13 | 14 | -------------------------------------------------------------------------------- /sLSM Tree White Paper.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aronszanto/sLSM-Tree/9bf445305d08973c301d7a788ec0c0aa737f4c00/sLSM Tree White Paper.pdf -------------------------------------------------------------------------------- /src/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | g++ -fpermissive -std=c++11 -O3 main.cpp MurmurHash.cpp -o main.out -lpthread 3 | -------------------------------------------------------------------------------- /src/MurmurHash.cpp: -------------------------------------------------------------------------------- 1 | #include "MurmurHash.h" 2 | 3 | //----------------------------------------------------------------------------- 4 | // Platform-specific functions and macros 5 | 6 | // Microsoft Visual Studio 7 | 8 | #if defined(_MSC_VER) 9 | 10 | #define FORCE_INLINE __forceinline 11 | 12 | #include 13 | 14 | #define ROTL32(x,y) _rotl(x,y) 15 | #define ROTL64(x,y) _rotl64(x,y) 16 | 17 | #define BIG_CONSTANT(x) (x) 18 | 19 | // Other compilers 20 | 21 | #else // defined(_MSC_VER) 22 | 23 | #define FORCE_INLINE inline __attribute__((always_inline)) 24 | 25 | inline uint32_t rotl32 ( uint32_t x, int8_t r ) 26 | { 27 | return (x << r) | (x >> (32 - r)); 28 | } 29 | 30 | inline uint64_t rotl64 ( uint64_t x, int8_t r ) 31 | { 32 | return (x << r) | (x >> (64 - r)); 33 | } 34 | 35 | #define ROTL32(x,y) rotl32(x,y) 36 | #define ROTL64(x,y) rotl64(x,y) 37 | 38 | #define BIG_CONSTANT(x) (x##LLU) 39 | 40 | #endif // !defined(_MSC_VER) 41 | 42 | //----------------------------------------------------------------------------- 43 | // Block read - if your platform needs to do endian-swapping or can only 44 | // handle aligned reads, do the conversion here 45 | 46 | FORCE_INLINE uint32_t getblock32 ( const uint32_t * p, int i ) 47 | { 48 | return p[i]; 49 | } 50 | 51 | FORCE_INLINE uint64_t getblock64 ( const uint64_t * p, int i ) 52 | { 53 | return p[i]; 54 | } 55 | 56 | //----------------------------------------------------------------------------- 57 | // Finalization mix - force all bits of a hash block to avalanche 58 | 59 | FORCE_INLINE uint32_t fmix32 ( uint32_t h ) 60 | { 61 | h ^= h >> 16; 62 | h *= 0x85ebca6b; 63 | h ^= h >> 13; 64 | h *= 0xc2b2ae35; 65 | h ^= h >> 16; 66 | 67 | return h; 68 | } 69 | 70 | //---------- 71 | 72 | FORCE_INLINE uint64_t fmix64 ( uint64_t k ) 73 | { 74 | k ^= k >> 33; 75 | k *= BIG_CONSTANT(0xff51afd7ed558ccd); 76 | k ^= k >> 33; 77 | k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53); 78 | k ^= k >> 33; 79 | 80 | return k; 81 | } 82 | 83 | //----------------------------------------------------------------------------- 84 | 85 | void MurmurHash3_x86_32 ( const void * key, int len, 86 | uint32_t seed, void * out ) 87 | { 88 | const uint8_t * data = (const uint8_t*)key; 89 | const int nblocks = len / 4; 90 | 91 | uint32_t h1 = seed; 92 | 93 | const uint32_t c1 = 0xcc9e2d51; 94 | const uint32_t c2 = 0x1b873593; 95 | 96 | //---------- 97 | // body 98 | 99 | const uint32_t * blocks = (const uint32_t *)(data + nblocks*4); 100 | 101 | for(int i = -nblocks; i; i++) 102 | { 103 | uint32_t k1 = getblock32(blocks,i); 104 | 105 | k1 *= c1; 106 | k1 = ROTL32(k1,15); 107 | k1 *= c2; 108 | 109 | h1 ^= k1; 110 | h1 = ROTL32(h1,13); 111 | h1 = h1*5+0xe6546b64; 112 | } 113 | 114 | //---------- 115 | // tail 116 | 117 | const uint8_t * tail = (const uint8_t*)(data + nblocks*4); 118 | 119 | uint32_t k1 = 0; 120 | 121 | switch(len & 3) 122 | { 123 | case 3: k1 ^= tail[2] << 16; 124 | case 2: k1 ^= tail[1] << 8; 125 | case 1: k1 ^= tail[0]; 126 | k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1; 127 | }; 128 | 129 | //---------- 130 | // finalization 131 | 132 | h1 ^= len; 133 | 134 | h1 = fmix32(h1); 135 | 136 | *(uint32_t*)out = h1; 137 | } 138 | 139 | //----------------------------------------------------------------------------- 140 | 141 | void MurmurHash3_x86_128 ( const void * key, const int len, 142 | uint32_t seed, void * out ) 143 | { 144 | const uint8_t * data = (const uint8_t*)key; 145 | const int nblocks = len / 16; 146 | 147 | uint32_t h1 = seed; 148 | uint32_t h2 = seed; 149 | uint32_t h3 = seed; 150 | uint32_t h4 = seed; 151 | 152 | const uint32_t c1 = 0x239b961b; 153 | const uint32_t c2 = 0xab0e9789; 154 | const uint32_t c3 = 0x38b34ae5; 155 | const uint32_t c4 = 0xa1e38b93; 156 | 157 | //---------- 158 | // body 159 | 160 | const uint32_t * blocks = (const uint32_t *)(data + nblocks*16); 161 | 162 | for(int i = -nblocks; i; i++) 163 | { 164 | uint32_t k1 = getblock32(blocks,i*4+0); 165 | uint32_t k2 = getblock32(blocks,i*4+1); 166 | uint32_t k3 = getblock32(blocks,i*4+2); 167 | uint32_t k4 = getblock32(blocks,i*4+3); 168 | 169 | k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1; 170 | 171 | h1 = ROTL32(h1,19); h1 += h2; h1 = h1*5+0x561ccd1b; 172 | 173 | k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2; 174 | 175 | h2 = ROTL32(h2,17); h2 += h3; h2 = h2*5+0x0bcaa747; 176 | 177 | k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3; 178 | 179 | h3 = ROTL32(h3,15); h3 += h4; h3 = h3*5+0x96cd1c35; 180 | 181 | k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4; 182 | 183 | h4 = ROTL32(h4,13); h4 += h1; h4 = h4*5+0x32ac3b17; 184 | } 185 | 186 | //---------- 187 | // tail 188 | 189 | const uint8_t * tail = (const uint8_t*)(data + nblocks*16); 190 | 191 | uint32_t k1 = 0; 192 | uint32_t k2 = 0; 193 | uint32_t k3 = 0; 194 | uint32_t k4 = 0; 195 | 196 | switch(len & 15) 197 | { 198 | case 15: k4 ^= tail[14] << 16; 199 | case 14: k4 ^= tail[13] << 8; 200 | case 13: k4 ^= tail[12] << 0; 201 | k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4; 202 | 203 | case 12: k3 ^= tail[11] << 24; 204 | case 11: k3 ^= tail[10] << 16; 205 | case 10: k3 ^= tail[ 9] << 8; 206 | case 9: k3 ^= tail[ 8] << 0; 207 | k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3; 208 | 209 | case 8: k2 ^= tail[ 7] << 24; 210 | case 7: k2 ^= tail[ 6] << 16; 211 | case 6: k2 ^= tail[ 5] << 8; 212 | case 5: k2 ^= tail[ 4] << 0; 213 | k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2; 214 | 215 | case 4: k1 ^= tail[ 3] << 24; 216 | case 3: k1 ^= tail[ 2] << 16; 217 | case 2: k1 ^= tail[ 1] << 8; 218 | case 1: k1 ^= tail[ 0] << 0; 219 | k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1; 220 | }; 221 | 222 | //---------- 223 | // finalization 224 | 225 | h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len; 226 | 227 | h1 += h2; h1 += h3; h1 += h4; 228 | h2 += h1; h3 += h1; h4 += h1; 229 | 230 | h1 = fmix32(h1); 231 | h2 = fmix32(h2); 232 | h3 = fmix32(h3); 233 | h4 = fmix32(h4); 234 | 235 | h1 += h2; h1 += h3; h1 += h4; 236 | h2 += h1; h3 += h1; h4 += h1; 237 | 238 | ((uint32_t*)out)[0] = h1; 239 | ((uint32_t*)out)[1] = h2; 240 | ((uint32_t*)out)[2] = h3; 241 | ((uint32_t*)out)[3] = h4; 242 | } 243 | 244 | //----------------------------------------------------------------------------- 245 | 246 | void MurmurHash3_x64_128 ( const void * key, const int len, 247 | const uint32_t seed, void * out ) 248 | { 249 | const uint8_t * data = (const uint8_t*)key; 250 | const int nblocks = len / 16; 251 | 252 | uint64_t h1 = seed; 253 | uint64_t h2 = seed; 254 | 255 | const uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5); 256 | const uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f); 257 | 258 | //---------- 259 | // body 260 | 261 | const uint64_t * blocks = (const uint64_t *)(data); 262 | 263 | for(int i = 0; i < nblocks; i++) 264 | { 265 | uint64_t k1 = getblock64(blocks,i*2+0); 266 | uint64_t k2 = getblock64(blocks,i*2+1); 267 | 268 | k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1; 269 | 270 | h1 = ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729; 271 | 272 | k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2; 273 | 274 | h2 = ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5; 275 | } 276 | 277 | //---------- 278 | // tail 279 | 280 | const uint8_t * tail = (const uint8_t*)(data + nblocks*16); 281 | 282 | uint64_t k1 = 0; 283 | uint64_t k2 = 0; 284 | 285 | switch(len & 15) 286 | { 287 | case 15: k2 ^= ((uint64_t)tail[14]) << 48; 288 | case 14: k2 ^= ((uint64_t)tail[13]) << 40; 289 | case 13: k2 ^= ((uint64_t)tail[12]) << 32; 290 | case 12: k2 ^= ((uint64_t)tail[11]) << 24; 291 | case 11: k2 ^= ((uint64_t)tail[10]) << 16; 292 | case 10: k2 ^= ((uint64_t)tail[ 9]) << 8; 293 | case 9: k2 ^= ((uint64_t)tail[ 8]) << 0; 294 | k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2; 295 | 296 | case 8: k1 ^= ((uint64_t)tail[ 7]) << 56; 297 | case 7: k1 ^= ((uint64_t)tail[ 6]) << 48; 298 | case 6: k1 ^= ((uint64_t)tail[ 5]) << 40; 299 | case 5: k1 ^= ((uint64_t)tail[ 4]) << 32; 300 | case 4: k1 ^= ((uint64_t)tail[ 3]) << 24; 301 | case 3: k1 ^= ((uint64_t)tail[ 2]) << 16; 302 | case 2: k1 ^= ((uint64_t)tail[ 1]) << 8; 303 | case 1: k1 ^= ((uint64_t)tail[ 0]) << 0; 304 | k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1; 305 | }; 306 | 307 | //---------- 308 | // finalization 309 | 310 | h1 ^= len; h2 ^= len; 311 | 312 | h1 += h2; 313 | h2 += h1; 314 | 315 | h1 = fmix64(h1); 316 | h2 = fmix64(h2); 317 | 318 | h1 += h2; 319 | h2 += h1; 320 | 321 | ((uint64_t*)out)[0] = h1; 322 | ((uint64_t*)out)[1] = h2; 323 | } 324 | -------------------------------------------------------------------------------- /src/MurmurHash.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef _MURMURHASH3_H_ 4 | #define _MURMURHASH3_H_ 5 | 6 | //----------------------------------------------------------------------------- 7 | // Platform-specific functions and macros 8 | 9 | // Microsoft Visual Studio 10 | 11 | #if defined(_MSC_VER) && (_MSC_VER < 1600) 12 | 13 | typedef unsigned char uint8_t; 14 | typedef unsigned int uint32_t; 15 | typedef unsigned __int64 uint64_t; 16 | 17 | // Other compilers 18 | 19 | #else // defined(_MSC_VER) 20 | 21 | #include 22 | 23 | #endif // !defined(_MSC_VER) 24 | 25 | //----------------------------------------------------------------------------- 26 | 27 | void MurmurHash3_x86_32 ( const void * key, int len, uint32_t seed, void * out ); 28 | 29 | void MurmurHash3_x86_128 ( const void * key, int len, uint32_t seed, void * out ); 30 | 31 | void MurmurHash3_x64_128 ( const void * key, int len, uint32_t seed, void * out ); 32 | 33 | //----------------------------------------------------------------------------- 34 | 35 | #endif // _MURMURHASH3_H_ 36 | -------------------------------------------------------------------------------- /src/bloom.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // bloom.hpp 3 | // lsm-tree 4 | // 5 | // sLSM: Skiplist-Based LSM Tree 6 | // Copyright © 2017 Aron Szanto. All rights reserved. 7 | // 8 | // This program is free software: you can redistribute it and/or modify 9 | // it under the terms of the GNU General Public License as published by 10 | // the Free Software Foundation, either version 3 of the License, or 11 | // (at your option) any later version. 12 | // 13 | // This program is distributed in the hope that it will be useful, 14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | // GNU General Public License for more details. 17 | // 18 | // You should have received a copy of the GNU General Public License 19 | // along with this program. If not, see . 20 | // 21 | #pragma once 22 | 23 | #ifndef bloom_h 24 | #define bloom_h 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include "MurmurHash.h" 33 | 34 | using namespace std; 35 | 36 | template 37 | class BloomFilter { 38 | public: 39 | BloomFilter(uint64_t n, double fp) { 40 | 41 | double denom = 0.480453013918201; // (ln(2))^2 42 | 43 | double size = -1 * (double) n * (log(fp) / denom); 44 | 45 | m_bits = vector((int) size); 46 | 47 | double ln2 = 0.693147180559945; 48 | m_numHashes = (int) ceil( (size / n) * ln2); // ln(2) 49 | } 50 | 51 | array hash(const Key *data, size_t len) { 52 | 53 | array hashValue; 54 | 55 | MurmurHash3_x64_128(data, (int) len, 0, hashValue.data()); 56 | 57 | return hashValue; 58 | } 59 | 60 | uint64_t nthHash(uint32_t n, uint64_t hashA, uint64_t hashB, uint64_t filterSize) { 61 | return (hashA + n * hashB) % filterSize; 62 | } 63 | 64 | void add(const Key *data, size_t len) { 65 | auto hashValues = hash(data, len); 66 | 67 | for (int n = 0; n < m_numHashes; n++) { 68 | m_bits[nthHash(n, hashValues[0], hashValues[1], m_bits.size())] = true; 69 | } 70 | } 71 | 72 | bool mayContain(const Key *data, size_t len) { 73 | auto hashValues = hash(data, len); 74 | 75 | for (int n = 0; n < m_numHashes; n++) { 76 | if (!m_bits[nthHash(n, hashValues[0], hashValues[1], m_bits.size())]) { 77 | return false; 78 | } 79 | } 80 | 81 | return true; 82 | } 83 | 84 | private: 85 | uint8_t m_numHashes; 86 | vector m_bits; 87 | }; 88 | 89 | 90 | 91 | 92 | #endif /* bloom_h */ 93 | -------------------------------------------------------------------------------- /src/diskLevel.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // diskLevel.hpp 3 | // lsm-tree 4 | // 5 | // sLSM: Skiplist-Based LSM Tree 6 | // Copyright © 2017 Aron Szanto. All rights reserved. 7 | // 8 | // This program is free software: you can redistribute it and/or modify 9 | // it under the terms of the GNU General Public License as published by 10 | // the Free Software Foundation, either version 3 of the License, or 11 | // (at your option) any later version. 12 | // 13 | // This program is distributed in the hope that it will be useful, 14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | // GNU General Public License for more details. 17 | // 18 | // You should have received a copy of the GNU General Public License 19 | // along with this program. If not, see . 20 | // 21 | #pragma once 22 | 23 | #ifndef diskLevel_h 24 | #define diskLevel_h 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include "run.hpp" 30 | #include "diskRun.hpp" 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | 41 | #define LEFTCHILD(x) 2 * x + 1 42 | #define RIGHTCHILD(x) 2 * x + 2 43 | #define PARENT(x) (x - 1) / 2 44 | 45 | int TOMBSTONE = INT_MIN; 46 | 47 | using namespace std; 48 | 49 | 50 | template 51 | class DiskLevel { 52 | 53 | public: // TODO make some of these private 54 | typedef KVPair KVPair_t; 55 | typedef pair, int> KVIntPair_t; 56 | KVPair_t KVPAIRMAX; 57 | KVIntPair_t KVINTPAIRMAX; 58 | V V_TOMBSTONE = (V) TOMBSTONE; 59 | 60 | struct StaticHeap { 61 | int size ; 62 | vector arr; 63 | KVIntPair_t max; 64 | 65 | StaticHeap(unsigned sz, KVIntPair_t mx) { 66 | size = 0; 67 | arr = vector(sz, mx); 68 | max = mx; 69 | } 70 | 71 | void push(KVIntPair_t blob) { 72 | unsigned i = size++; 73 | while(i && blob < arr[PARENT(i)]) { 74 | arr[i] = arr[PARENT(i)] ; 75 | i = PARENT(i) ; 76 | } 77 | arr[i] = blob ; 78 | } 79 | void heapify(int i) { 80 | int smallest = (LEFTCHILD(i) < size && arr[LEFTCHILD(i)] < arr[i]) ? LEFTCHILD(i) : i ; 81 | if(RIGHTCHILD(i) < size && arr[RIGHTCHILD(i)] < arr[smallest]) { 82 | smallest = RIGHTCHILD(i); 83 | } 84 | if(smallest != i) { 85 | KVIntPair_t temp = arr[i]; 86 | arr[i] = arr[smallest]; 87 | arr[smallest] = temp; 88 | heapify(smallest) ; 89 | } 90 | } 91 | 92 | KVIntPair_t pop() { 93 | KVIntPair_t ret = arr[0]; 94 | arr[0] = arr[--size]; 95 | heapify(0); 96 | return ret; 97 | } 98 | }; 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | int _level; 107 | unsigned _pageSize; // number of elements per fence pointer 108 | unsigned long _runSize; // number of elts in a run 109 | unsigned _numRuns; // number of runs in a level 110 | unsigned _activeRun; // index of active run 111 | unsigned _mergeSize; // # of runs to merge downwards 112 | double _bf_fp; // bloom filter false positive 113 | vector *> runs; 114 | 115 | 116 | 117 | DiskLevel(unsigned int pageSize, int level, unsigned long runSize, unsigned numRuns, unsigned mergeSize, double bf_fp):_numRuns(numRuns), _runSize(runSize),_level(level), _pageSize(pageSize), _mergeSize(mergeSize), _activeRun(0), _bf_fp(bf_fp){ 118 | KVPAIRMAX = (KVPair_t) {INT_MAX, 0}; 119 | KVINTPAIRMAX = KVIntPair_t(KVPAIRMAX, -1); 120 | 121 | for (int i = 0; i < _numRuns; i++){ 122 | DiskRun * run = new DiskRun(_runSize, pageSize, level, i, _bf_fp); 123 | runs.push_back(run); 124 | } 125 | 126 | 127 | 128 | 129 | 130 | } 131 | 132 | ~DiskLevel(){ 133 | for (int i = 0; i< runs.size(); ++i){ 134 | delete runs[i]; 135 | } 136 | } 137 | 138 | void addRuns(vector *> &runList, const unsigned long runLen, bool lastLevel) { 139 | 140 | 141 | StaticHeap h = StaticHeap((int) runList.size(), KVINTPAIRMAX); 142 | vector heads(runList.size(), 0); 143 | for (int i = 0; i < runList.size(); i++){ 144 | KVPair_t kvp = runList[i]->map[0]; 145 | h.push(KVIntPair_t(kvp, i)); 146 | } 147 | 148 | int j = -1; 149 | K lastKey = INT_MAX; 150 | unsigned lastk = INT_MIN; 151 | while (h.size != 0){ 152 | auto val_run_pair = h.pop(); 153 | assert(val_run_pair != KVINTPAIRMAX); // TODO delete asserts 154 | if (lastKey == val_run_pair.first.key){ 155 | if( lastk < val_run_pair.second){ 156 | runs[_activeRun]->map[j] = val_run_pair.first; 157 | } 158 | } 159 | else { 160 | ++j; 161 | if ( j != -1 && lastLevel && runs[_activeRun]->map[j].value == V_TOMBSTONE){ 162 | --j; 163 | } 164 | runs[_activeRun]->map[j] = val_run_pair.first; 165 | } 166 | 167 | lastKey = val_run_pair.first.key; 168 | lastk = val_run_pair.second; 169 | 170 | unsigned k = val_run_pair.second; 171 | if (++heads[k] < runList[k]->getCapacity()){ 172 | KVPair_t kvp = runList[k]->map[heads[k]]; 173 | h.push(KVIntPair_t(kvp, k)); 174 | } 175 | 176 | } 177 | 178 | if (lastLevel && runs[_activeRun]->map[j].value == V_TOMBSTONE){ 179 | --j; 180 | } 181 | runs[_activeRun]->setCapacity(j + 1); 182 | runs[_activeRun]->constructIndex(); 183 | if(j + 1 > 0){ 184 | ++_activeRun; 185 | } 186 | 187 | } 188 | 189 | void addRunByArray(KVPair_t * runToAdd, const unsigned long runLen){ 190 | assert(_activeRun < _numRuns); 191 | assert(runLen == _runSize); 192 | runs[_activeRun]->writeData(runToAdd, 0, runLen); 193 | runs[_activeRun]->constructIndex(); 194 | _activeRun++; 195 | } 196 | 197 | 198 | vector *> getRunsToMerge(){ 199 | vector *> toMerge; 200 | for (int i = 0; i < _mergeSize; i++){ 201 | toMerge.push_back(runs[i]); 202 | } 203 | 204 | return toMerge; 205 | 206 | } 207 | 208 | void freeMergedRuns(vector *> &toFree){ 209 | assert(toFree.size() == _mergeSize); 210 | for (int i = 0; i < _mergeSize; i++){ 211 | assert(toFree[i]->_level == _level); 212 | delete toFree[i]; 213 | } 214 | runs.erase(runs.begin(), runs.begin() + _mergeSize); 215 | _activeRun -= _mergeSize; 216 | for (int i = 0; i < _activeRun; i++){ 217 | 218 | runs[i]->_runID = i; 219 | 220 | string newName = ("C_" + to_string(runs[i]->_level) + "_" + to_string(runs[i]->_runID) + ".txt"); 221 | 222 | if (rename(runs[i]->_filename.c_str(), newName.c_str())){ 223 | perror(("Error renaming file " + runs[i]->_filename + " to " + newName).c_str()); 224 | exit(EXIT_FAILURE); 225 | } 226 | runs[i]->_filename = newName; 227 | } 228 | 229 | for (int i = _activeRun; i < _numRuns; i++){ 230 | DiskRun * newRun = new DiskRun(_runSize, _pageSize, _level, i, _bf_fp); 231 | runs.push_back(newRun); 232 | } 233 | } 234 | 235 | bool levelFull(){ 236 | return (_activeRun == _numRuns); 237 | } 238 | bool levelEmpty(){ 239 | return (_activeRun == 0); 240 | } 241 | 242 | V lookup (const K &key, bool &found) { 243 | int maxRunToSearch = levelFull() ? _numRuns - 1 : _activeRun - 1; 244 | for (int i = maxRunToSearch; i >= 0; --i){ 245 | if (runs[i]->maxKey == INT_MIN || key < runs[i]->minKey || key > runs[i]->maxKey || !runs[i]->bf.mayContain(&key, sizeof(K))){ 246 | continue; 247 | } 248 | V lookupRes = runs[i]->lookup(key, found); 249 | if (found) { 250 | return lookupRes; 251 | } 252 | 253 | } 254 | 255 | return (V) NULL; 256 | 257 | } 258 | unsigned long num_elements(){ 259 | unsigned long total = 0; 260 | for (int i = 0; i < _activeRun; ++i) 261 | total += runs[i]->getCapacity(); 262 | return total; 263 | } 264 | }; 265 | #endif /* diskLevel_h */ 266 | -------------------------------------------------------------------------------- /src/diskRun.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // diskRun.hpp 3 | // lsm-tree 4 | // 5 | // sLSM: Skiplist-Based LSM Tree 6 | // Copyright © 2017 Aron Szanto. All rights reserved. 7 | // 8 | // This program is free software: you can redistribute it and/or modify 9 | // it under the terms of the GNU General Public License as published by 10 | // the Free Software Foundation, either version 3 of the License, or 11 | // (at your option) any later version. 12 | // 13 | // This program is distributed in the hope that it will be useful, 14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | // GNU General Public License for more details. 17 | // 18 | // You should have received a copy of the GNU General Public License 19 | // along with this program. If not, see . 20 | // 21 | #pragma once 22 | #ifndef diskRun_h 23 | #define diskRun_h 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include "run.hpp" 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | 39 | 40 | using namespace std; 41 | 42 | template class DiskLevel; 43 | 44 | template 45 | class DiskRun { 46 | friend class DiskLevel; 47 | public: 48 | typedef KVPair KVPair_t; 49 | 50 | 51 | static int compareKVs (const void * a, const void * b) 52 | { 53 | if ( *(KVPair*)a < *(KVPair*)b ) return -1; 54 | if ( *(KVPair*)a == *(KVPair*)b ) return 0; 55 | if ( *(KVPair*)a > *(KVPair*)b ) return 1; 56 | return 10; 57 | } 58 | 59 | 60 | KVPair_t *map; 61 | int fd; 62 | unsigned int pageSize; 63 | BloomFilter bf; 64 | 65 | K minKey = INT_MIN; 66 | K maxKey = INT_MIN; 67 | 68 | DiskRun (unsigned long capacity, unsigned int pageSize, int level, int runID, double bf_fp):_capacity(capacity),_level(level), _iMaxFP(0), pageSize(pageSize), _runID(runID), _bf_fp(bf_fp), bf(capacity, bf_fp) { 69 | 70 | _filename = "C_" + to_string(level) + "_" + to_string(runID) + ".txt"; 71 | 72 | size_t filesize = capacity * sizeof(KVPair_t); 73 | 74 | long result; 75 | 76 | fd = open(_filename.c_str(), O_RDWR | O_CREAT | O_TRUNC, (mode_t) 0600); 77 | if (fd == -1) { 78 | perror("Error opening file for writing"); 79 | exit(EXIT_FAILURE); 80 | } 81 | 82 | /* Stretch the file size to the size of the (mmapped) array of KVPairs 83 | */ 84 | result = lseek(fd, filesize - 1, SEEK_SET); 85 | if (result == -1) { 86 | close(fd); 87 | perror("Error calling lseek() to 'stretch' the file"); 88 | exit(EXIT_FAILURE); 89 | } 90 | 91 | 92 | result = write(fd, "", 1); 93 | if (result != 1) { 94 | close(fd); 95 | perror("Error writing last byte of the file"); 96 | exit(EXIT_FAILURE); 97 | } 98 | 99 | 100 | map = (KVPair*) mmap(0, filesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 101 | if (map == MAP_FAILED) { 102 | close(fd); 103 | perror("Error mmapping the file"); 104 | exit(EXIT_FAILURE); 105 | } 106 | 107 | 108 | } 109 | ~DiskRun(){ 110 | fsync(fd); 111 | doUnmap(); 112 | 113 | if (remove(_filename.c_str())){ 114 | perror(("Error removing file " + string(_filename)).c_str()); 115 | exit(EXIT_FAILURE); 116 | } 117 | } 118 | void setCapacity(unsigned long newCap){ 119 | _capacity = newCap; 120 | } 121 | unsigned long getCapacity(){ 122 | return _capacity; 123 | } 124 | void writeData(const KVPair_t *run, const size_t offset, const unsigned long len) { 125 | 126 | memcpy(map + offset, run, len * sizeof(KVPair_t)); 127 | _capacity = len; 128 | 129 | } 130 | void constructIndex(){ 131 | // construct fence pointers and write BF 132 | // _fencePointers.resize(0); 133 | _fencePointers.reserve(_capacity / pageSize); 134 | _iMaxFP = -1; // TODO IS THIS SAFE? 135 | for (int j = 0; j < _capacity; j++) { 136 | bf.add((K*) &map[j].key, sizeof(K)); 137 | if (j % pageSize == 0){ 138 | _fencePointers.push_back(map[j].key); 139 | _iMaxFP++; 140 | } 141 | } 142 | if (_iMaxFP >= 0){ 143 | _fencePointers.resize(_iMaxFP + 1); 144 | } 145 | 146 | minKey = map[0].key; 147 | maxKey = map[_capacity - 1].key; 148 | 149 | } 150 | 151 | unsigned long binary_search (const unsigned long offset, const unsigned long n, const K &key, bool &found) { 152 | if (n == 0){ 153 | found = true; 154 | return offset; 155 | } 156 | unsigned long min = offset, max = offset + n - 1; 157 | unsigned long middle = (min + max) >> 1; 158 | while (min <= max) { 159 | if (key > map[middle].key) 160 | min = middle + 1; 161 | else if (key == map[middle].key) { 162 | found = true; 163 | return middle; 164 | } 165 | else 166 | max = middle - 1; 167 | middle = (min + max) >> 1; 168 | 169 | } 170 | return min; 171 | } 172 | 173 | void get_flanking_FP(const K &key, unsigned long &start, unsigned long &end){ 174 | if (_iMaxFP == 0) { 175 | start = 0; 176 | end = _capacity; 177 | } 178 | else if (key < _fencePointers[1]){ 179 | start = 0; 180 | end = pageSize; 181 | } 182 | else if (key >= _fencePointers[_iMaxFP]) { 183 | start = _iMaxFP * pageSize; 184 | end = _capacity; 185 | } 186 | else { 187 | unsigned min = 0, max = _iMaxFP; 188 | while (min < max) { 189 | 190 | unsigned middle = (min + max) >> 1; 191 | if (key > _fencePointers[middle]){ 192 | if (key < _fencePointers[middle + 1]){ 193 | start = middle * pageSize; 194 | end = (middle + 1) * pageSize; 195 | return; // TODO THIS IS ALSO GROSS 196 | } 197 | min = middle + 1; 198 | } 199 | else if (key < _fencePointers[middle]) { 200 | if (key >= _fencePointers[middle - 1]){ 201 | start = (middle - 1) * pageSize; 202 | end = middle * pageSize; 203 | return; // TODO THIS IS ALSO GROSS. THIS WILL BREAK IF YOU DON'T KEEP TRACK OF MIN AND MAX. 204 | } 205 | 206 | max = middle - 1; 207 | } 208 | 209 | else { 210 | start = middle * pageSize; 211 | end = start; 212 | return; 213 | } 214 | 215 | } 216 | 217 | } 218 | } 219 | 220 | unsigned long get_index(const K &key, bool &found){ 221 | unsigned long start, end; 222 | get_flanking_FP(key, start, end); 223 | unsigned long ret = binary_search(start, end - start, key, found); 224 | return ret; 225 | } 226 | 227 | V lookup(const K &key, bool &found){ 228 | unsigned long idx = get_index(key, found); 229 | V ret = map[idx].value; 230 | return found ? ret : (V) NULL; 231 | } 232 | 233 | void range(const K &key1, const K &key2, unsigned long &i1, unsigned long &i2){ 234 | i1 = 0; 235 | i2 = 0; 236 | if (key1 > maxKey || key2 < minKey){ 237 | return; 238 | } 239 | if (key1 >= minKey){ 240 | bool found = false; 241 | i1 = get_index(key1, found); 242 | 243 | } 244 | if (key2 > maxKey){ 245 | i2 = _capacity; 246 | return; 247 | } 248 | else { 249 | bool found = false; 250 | i2 = get_index(key2, found); 251 | } 252 | } 253 | 254 | void printElts(){ 255 | for (int j = 0; j < _capacity; j++){ 256 | cout << map[j].key << " "; 257 | } 258 | cout << endl; 259 | } 260 | 261 | private: 262 | unsigned long _capacity; 263 | string _filename; 264 | int _level; 265 | vector _fencePointers; 266 | unsigned _iMaxFP; 267 | unsigned _runID; 268 | double _bf_fp; 269 | 270 | void doMap(){ 271 | 272 | size_t filesize = _capacity * sizeof(KVPair_t); 273 | 274 | fd = open(_filename.c_str(), O_RDWR | O_CREAT | O_TRUNC, (mode_t) 0600); 275 | if (fd == -1) { 276 | perror("Error opening file for writing"); 277 | exit(EXIT_FAILURE); 278 | } 279 | 280 | 281 | map = (KVPair*) mmap(0, filesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 282 | if (map == MAP_FAILED) { 283 | close(fd); 284 | perror("Error mmapping the file"); 285 | exit(EXIT_FAILURE); 286 | } 287 | } 288 | 289 | void doUnmap(){ 290 | size_t filesize = _capacity * sizeof(KVPair_t); 291 | 292 | 293 | if (munmap(map, filesize) == -1) { 294 | perror("Error un-mmapping the file"); 295 | } 296 | 297 | close(fd); 298 | fd = -5; 299 | } 300 | 301 | void doubleSize(){ 302 | unsigned long new_capacity = _capacity * 2; 303 | 304 | size_t new_filesize = new_capacity * sizeof(KVPair_t); 305 | int result = lseek(fd, new_filesize - 1, SEEK_SET); 306 | if (result == -1) { 307 | close(fd); 308 | perror("Error calling lseek() to 'stretch' the file"); 309 | exit(EXIT_FAILURE); 310 | } 311 | 312 | result = write(fd, "", 1); 313 | if (result != 1) { 314 | close(fd); 315 | perror("Error writing last byte of the file"); 316 | exit(EXIT_FAILURE); 317 | } 318 | 319 | map = (KVPair*) mmap(0, new_filesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 320 | if (map == MAP_FAILED) { 321 | close(fd); 322 | perror("Error mmapping the file"); 323 | exit(EXIT_FAILURE); 324 | } 325 | 326 | _capacity = new_capacity; 327 | } 328 | 329 | 330 | 331 | 332 | }; 333 | #endif /* diskRun_h */ 334 | 335 | -------------------------------------------------------------------------------- /src/hashMap.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // 4 | // hashMap.hpp 5 | // lsm-tree 6 | // 7 | // sLSM: Skiplist-Based LSM Tree 8 | // Copyright © 2017 Aron Szanto. All rights reserved. 9 | // 10 | // This program is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // This program is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with this program. If not, see . 22 | // 23 | #include "MurmurHash.h" 24 | #include 25 | #include 26 | #include 27 | 28 | #ifndef hashMap_h 29 | #define hashMap_h 30 | 31 | 32 | template 33 | class HashTable { 34 | public: 35 | unsigned long _size; 36 | unsigned long _elts; 37 | KVPair EMPTY = {INT_MIN, INT_MIN}; 38 | 39 | HashTable(unsigned long size): _size(2 * size), _elts(0) { 40 | table = new KVPair [_size](); 41 | fill(table + 0, table + _size, (KVPair) EMPTY); 42 | } 43 | 44 | ~HashTable() { 45 | // for (int i = 0; i < _size; i++) { 46 | // delete table[i]; 47 | // } 48 | delete [] table; 49 | } 50 | 51 | void resize(){ 52 | _size *= 2; 53 | auto newTable = new KVPair [_size](); 54 | fill(newTable + 0, newTable + _size, (KVPair) EMPTY); 55 | 56 | for (unsigned long i = 0; i < _size / 2; i++){ 57 | if (table[i] != EMPTY){ 58 | unsigned long newHash = hashFunc(table[i].key); 59 | 60 | for (int j = 0;; j++){ 61 | if (newTable[(newHash + j) % _size] == EMPTY){ 62 | newTable[(newHash + j) % _size] = table[i]; 63 | break; 64 | } 65 | } 66 | 67 | } 68 | } 69 | delete [] table; 70 | 71 | table = newTable; 72 | 73 | 74 | 75 | } 76 | 77 | bool get(const K &key, V &value) { 78 | unsigned long hashValue = hashFunc(key); 79 | for (int i = 0;; ++i){ 80 | if (table[(hashValue + i) % _size] == EMPTY){ 81 | return false; 82 | } 83 | else if (table[(hashValue + i) % _size].key == key){ 84 | value = table[(hashValue + i) % _size].value; 85 | return true; 86 | } 87 | } 88 | 89 | return false; 90 | } 91 | 92 | void put(const K &key, const V &value) { 93 | if (_elts * 2 > _size){ 94 | resize(); 95 | } 96 | unsigned long hashValue = hashFunc(key); 97 | KVPair node; 98 | 99 | for (unsigned long i = 0;; i++){ 100 | if (table[(hashValue + i) % _size] == EMPTY){ 101 | table[(hashValue + i) % _size].key = key; 102 | table[(hashValue + i) % _size].value = value; 103 | ++_elts; 104 | return; 105 | } 106 | else if (table[(hashValue + i) % _size].key == key){ 107 | 108 | table[(hashValue + i) % _size].value = value; 109 | return; 110 | } 111 | } 112 | } 113 | 114 | V putIfEmpty(const K &key, const V &value) { 115 | if (_elts * 2 > _size){ 116 | resize(); 117 | } 118 | unsigned long hashValue = hashFunc(key); 119 | 120 | for (unsigned long i = 0;; i++){ 121 | if (table[(hashValue + i) % _size] == EMPTY){ 122 | table[(hashValue + i) % _size].key = key; 123 | table[(hashValue + i) % _size].value = value; 124 | ++_elts; 125 | return (V) NULL; 126 | } 127 | else if (table[(hashValue + i) % _size].key == key){ 128 | // something already here, return current occupant to user 129 | return table[(hashValue + i) % _size].value; 130 | } 131 | } 132 | } 133 | 134 | 135 | unsigned long hashFunc(const K key){ 136 | array hashValue; 137 | 138 | MurmurHash3_x64_128(&key, sizeof(K), 0, hashValue.data()); 139 | return (hashValue[0] % _size); 140 | } 141 | 142 | private: 143 | KVPair *table; 144 | }; 145 | 146 | #endif /* hashMap_h */ 147 | -------------------------------------------------------------------------------- /src/lsm.hpp: -------------------------------------------------------------------------------- 1 | 2 | // lsm.hpp 3 | // lsm-tree 4 | // 5 | // Created by Aron Szanto on 3/3/17. 6 | 7 | 8 | // sLSM: Skiplist-Based LSM Tree 9 | // Copyright © 2017 Aron Szanto. All rights reserved. 10 | // 11 | // This program is free software: you can redistribute it and/or modify 12 | // it under the terms of the GNU General Public License as published by 13 | // the Free Software Foundation, either version 3 of the License, or 14 | // (at your option) any later version. 15 | // 16 | // This program is distributed in the hope that it will be useful, 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | // GNU General Public License for more details. 20 | // 21 | // You should have received a copy of the GNU General Public License 22 | // along with this program. If not, see . 23 | // 24 | #pragma once 25 | 26 | #ifndef LSM_H 27 | #define LSM_H 28 | 29 | #include "run.hpp" 30 | #include "skipList.hpp" 31 | #include "bloom.hpp" 32 | #include "diskLevel.hpp" 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | 42 | template 43 | class LSM { 44 | 45 | typedef SkipList RunType; 46 | 47 | 48 | 49 | public: 50 | V V_TOMBSTONE = (V) TOMBSTONE; 51 | mutex *mergeLock; 52 | 53 | vector *> C_0; 54 | 55 | vector *> filters; 56 | vector *> diskLevels; 57 | 58 | LSM(const LSM &other) = default; 59 | LSM(LSM &&other) = default; 60 | 61 | LSM(unsigned long eltsPerRun, unsigned int numRuns, double merged_frac, double bf_fp, unsigned int pageSize, unsigned int diskRunsPerLevel): _eltsPerRun(eltsPerRun), _num_runs(numRuns), _frac_runs_merged(merged_frac), _diskRunsPerLevel(diskRunsPerLevel), _num_to_merge(ceil(_frac_runs_merged * _num_runs)), _pageSize(pageSize){ 62 | _activeRun = 0; 63 | _bfFalsePositiveRate = bf_fp; 64 | _n = 0; 65 | 66 | 67 | DiskLevel * diskLevel = new DiskLevel(pageSize, 1, _num_to_merge * _eltsPerRun, _diskRunsPerLevel, ceil(_diskRunsPerLevel * _frac_runs_merged), _bfFalsePositiveRate); 68 | 69 | diskLevels.push_back(diskLevel); 70 | _numDiskLevels = 1; 71 | 72 | 73 | for (int i = 0; i < _num_runs; i++){ 74 | RunType * run = new RunType(INT32_MIN,INT32_MAX); 75 | run->set_size(_eltsPerRun); 76 | C_0.push_back(run); 77 | 78 | BloomFilter * bf = new BloomFilter(_eltsPerRun, _bfFalsePositiveRate); 79 | filters.push_back(bf); 80 | } 81 | mergeLock = new mutex(); 82 | } 83 | ~LSM(){ 84 | if (mergeThread.joinable()){ 85 | mergeThread.join(); 86 | } 87 | delete mergeLock; 88 | for (int i = 0; i < C_0.size(); ++i){ 89 | delete C_0[i]; 90 | delete filters[i]; 91 | } 92 | for (int i = 0; i < diskLevels.size(); ++i){ 93 | delete diskLevels[i]; 94 | } 95 | 96 | } 97 | 98 | void insert_key(K &key, V &value) { 99 | if (C_0[_activeRun]->num_elements() >= _eltsPerRun){ 100 | ++_activeRun; 101 | } 102 | 103 | if (_activeRun >= _num_runs){ 104 | do_merge(); 105 | } 106 | 107 | C_0[_activeRun]->insert_key(key,value); 108 | filters[_activeRun]->add(&key, sizeof(K)); 109 | } 110 | 111 | bool lookup(K &key, V &value){ 112 | bool found = false; 113 | for (int i = _activeRun; i >= 0; --i){ 114 | if (key < C_0[i]->get_min() || key > C_0[i]->get_max() || !filters[i]->mayContain(&key, sizeof(K))) 115 | continue; 116 | 117 | value = C_0[i]->lookup(key, found); 118 | if (found) { 119 | return value != V_TOMBSTONE; 120 | } 121 | } 122 | if (mergeThread.joinable()){ 123 | // make sure that there isn't a merge happening as you search the disk 124 | mergeThread.join(); 125 | } 126 | // it's not in C_0 so let's look at disk. 127 | for (int i = 0; i < _numDiskLevels; i++){ 128 | 129 | value = diskLevels[i]->lookup(key, found); 130 | if (found) { 131 | return value != V_TOMBSTONE; 132 | } 133 | } 134 | return false; 135 | } 136 | 137 | void delete_key(K &key){ 138 | insert_key(key, V_TOMBSTONE); 139 | } 140 | 141 | vector> range(K &key1, K &key2){ 142 | if (key2 <= key1){ 143 | return (vector> {}); 144 | } 145 | auto ht = HashTable(4096 * 1000); 146 | 147 | vector> eltsInRange = vector>(); 148 | 149 | 150 | for (int i = _activeRun; i >= 0; --i){ 151 | vector> cur_elts = C_0[i]->get_all_in_range(key1, key2); 152 | if (cur_elts.size() != 0){ 153 | eltsInRange.reserve(eltsInRange.size() + cur_elts.size()); //this over-reserves to be safe 154 | for (int c = 0; c < cur_elts.size(); c++){ 155 | V dummy = ht.putIfEmpty(cur_elts[c].key, cur_elts[c].value); 156 | if (!dummy && cur_elts[c].value != V_TOMBSTONE){ 157 | eltsInRange.push_back(cur_elts[c]); 158 | } 159 | 160 | } 161 | } 162 | 163 | } 164 | 165 | if (mergeThread.joinable()){ 166 | // make sure that there isn't a merge happening as you search the disk 167 | mergeThread.join(); 168 | } 169 | 170 | for (int j = 0; j < _numDiskLevels; j++){ 171 | for (int r = diskLevels[j]->_activeRun - 1; r >= 0 ; --r){ 172 | unsigned long i1, i2; 173 | diskLevels[j]->runs[r]->range(key1, key2, i1, i2); 174 | if (i2 - i1 != 0){ 175 | auto oldSize = eltsInRange.size(); 176 | eltsInRange.reserve(oldSize + (i2 - i1)); // also over-reserves space 177 | for (unsigned long m = i1; m < i2; ++m){ 178 | auto KV = diskLevels[j]->runs[r]->map[m]; 179 | V dummy = ht.putIfEmpty(KV.key, KV.value); 180 | if (!dummy && KV.value != V_TOMBSTONE) { 181 | eltsInRange.push_back(KV); 182 | } 183 | } 184 | } 185 | } 186 | } 187 | 188 | return eltsInRange; 189 | } 190 | 191 | 192 | 193 | void printElts(){ 194 | if (mergeThread.joinable()) 195 | mergeThread.join(); 196 | cout << "MEMORY BUFFER" << endl; 197 | for (int i = 0; i <= _activeRun; i++){ 198 | cout << "MEMORY BUFFER RUN " << i << endl; 199 | auto all = C_0[i]->get_all(); 200 | for (KVPair &c : all) { 201 | cout << c.key << ":" << c.value << " "; 202 | } 203 | cout << endl; 204 | 205 | } 206 | 207 | cout << "\nDISK BUFFER" << endl; 208 | for (int i = 0; i < _numDiskLevels; i++){ 209 | cout << "DISK LEVEL " << i << endl; 210 | for (int j = 0; j < diskLevels[i]->_activeRun; j++){ 211 | cout << "RUN " << j << endl; 212 | for (int k = 0; k < diskLevels[i]->runs[j]->getCapacity(); k++){ 213 | cout << diskLevels[i]->runs[j]->map[k].key << ":" << diskLevels[i]->runs[j]->map[k].value << " "; 214 | } 215 | cout << endl; 216 | } 217 | cout << endl; 218 | } 219 | 220 | } 221 | void printStats(){ 222 | cout << "Number of Elements: " << size() << endl; 223 | cout << "Number of Elements in Buffer (including deletes): " << num_buffer() << endl; 224 | 225 | for (int i = 0; i < diskLevels.size(); ++i){ 226 | cout << "Number of Elements in Disk Level " << i << "(including deletes): " << diskLevels[i]->num_elements() << endl; 227 | } 228 | cout << "KEY VALUE DUMP BY LEVEL: " << endl; 229 | printElts(); 230 | } 231 | 232 | //private: // TODO MAKE PRIVATE 233 | unsigned int _activeRun; 234 | unsigned long _eltsPerRun; 235 | double _bfFalsePositiveRate; 236 | unsigned int _num_runs; 237 | double _frac_runs_merged; 238 | unsigned int _numDiskLevels; 239 | unsigned int _diskRunsPerLevel; 240 | unsigned int _num_to_merge; 241 | unsigned int _pageSize; 242 | unsigned long _n; 243 | thread mergeThread; 244 | 245 | void mergeRunsToLevel(int level) { 246 | bool isLast = false; 247 | 248 | if (level == _numDiskLevels){ // if this is the last level 249 | DiskLevel * newLevel = new DiskLevel(_pageSize, level + 1, diskLevels[level - 1]->_runSize * diskLevels[level - 1]->_mergeSize, _diskRunsPerLevel, ceil(_diskRunsPerLevel * _frac_runs_merged), _bfFalsePositiveRate); 250 | diskLevels.push_back(newLevel); 251 | _numDiskLevels++; 252 | } 253 | 254 | if (diskLevels[level]->levelFull()) { 255 | mergeRunsToLevel(level + 1); // merge down one, recursively 256 | } 257 | 258 | if(level + 1 == _numDiskLevels && diskLevels[level]->levelEmpty()){ 259 | isLast = true; 260 | } 261 | 262 | 263 | vector *> runsToMerge = diskLevels[level - 1]->getRunsToMerge(); 264 | unsigned long runLen = diskLevels[level - 1]->_runSize; 265 | diskLevels[level]->addRuns(runsToMerge, runLen, isLast); 266 | diskLevels[level - 1]->freeMergedRuns(runsToMerge); 267 | 268 | 269 | 270 | 271 | 272 | } 273 | void merge_runs(vector*> runs_to_merge, vector*> bf_to_merge){ 274 | vector> to_merge = vector>(); 275 | to_merge.reserve(_eltsPerRun * _num_to_merge); 276 | for (int i = 0; i < runs_to_merge.size(); i++){ 277 | auto all = (runs_to_merge)[i]->get_all(); 278 | 279 | to_merge.insert(to_merge.begin(), all.begin(), all.end()); 280 | delete (runs_to_merge)[i]; 281 | delete (bf_to_merge)[i]; 282 | } 283 | sort(to_merge.begin(), to_merge.end()); 284 | mergeLock->lock(); 285 | if (diskLevels[0]->levelFull()){ 286 | mergeRunsToLevel(1); 287 | } 288 | diskLevels[0]->addRunByArray(&to_merge[0], to_merge.size()); 289 | mergeLock->unlock(); 290 | 291 | } 292 | 293 | void do_merge(){ 294 | if (_num_to_merge == 0) 295 | return; 296 | vector*> runs_to_merge = vector*>(); 297 | vector*> bf_to_merge = vector*>(); 298 | for (int i = 0; i < _num_to_merge; i++){ 299 | runs_to_merge.push_back(C_0[i]); 300 | bf_to_merge.push_back(filters[i]); 301 | } 302 | if (mergeThread.joinable()){ 303 | mergeThread.join(); 304 | } 305 | mergeThread = thread (&LSM::merge_runs, this, runs_to_merge,bf_to_merge); // comment for single threaded merging 306 | // merge_runs(runs_to_merge, bf_to_merge); // uncomment for single threaded merging 307 | C_0.erase(C_0.begin(), C_0.begin() + _num_to_merge); 308 | filters.erase(filters.begin(), filters.begin() + _num_to_merge); 309 | 310 | _activeRun -= _num_to_merge; 311 | for (int i = _activeRun; i < _num_runs; i++){ 312 | RunType * run = new RunType(INT32_MIN,INT32_MAX); 313 | run->set_size(_eltsPerRun); 314 | C_0.push_back(run); 315 | 316 | BloomFilter * bf = new BloomFilter(_eltsPerRun, _bfFalsePositiveRate); 317 | filters.push_back(bf); 318 | } 319 | } 320 | unsigned long num_buffer(){ 321 | if (mergeThread.joinable()) 322 | mergeThread.join(); 323 | unsigned long total = 0; 324 | for (int i = 0; i <= _activeRun; ++i) 325 | total += C_0[i]->num_elements(); 326 | return total; 327 | } 328 | unsigned long size(){ 329 | K min = INT_MIN; 330 | K max = INT_MAX; 331 | auto r = range(min, max); 332 | return r.size(); 333 | } 334 | 335 | }; 336 | 337 | 338 | 339 | 340 | #endif /* lsm_h */ 341 | 342 | -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // lsm-tree 4 | // 5 | // sLSM: Skiplist-Based LSM Tree 6 | // Copyright © 2017 Aron Szanto. All rights reserved. 7 | // 8 | // This program is free software: you can redistribute it and/or modify 9 | // it under the terms of the GNU General Public License as published by 10 | // the Free Software Foundation, either version 3 of the License, or 11 | // (at your option) any later version. 12 | // 13 | // This program is distributed in the hope that it will be useful, 14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | // GNU General Public License for more details. 17 | // 18 | // You should have received a copy of the GNU General Public License 19 | // along with this program. If not, see . 20 | // 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include "skipList.hpp" 32 | #include "bloom.hpp" 33 | #include "hashMap.hpp" 34 | #include "lsm.hpp" 35 | 36 | 37 | using namespace std; 38 | 39 | struct timespec start, finish; 40 | double elapsed; 41 | 42 | struct LSMParams { 43 | const int num_inserts; 44 | const int num_runs; 45 | const int elts_per_run; 46 | const double bf_fp; 47 | const int pageSize; 48 | const int disk_runs_per_level; 49 | const double merge_fraction; 50 | }; 51 | 52 | 53 | void bloomFilterTest(){ 54 | std::random_device rand_dev; 55 | std::mt19937 generator(rand_dev()); 56 | std::uniform_int_distribution distribution(INT32_MIN, INT32_MAX); 57 | 58 | const int num_inserts = 10; 59 | double fprate = .1; 60 | BloomFilter bf = BloomFilter(num_inserts, fprate); 61 | 62 | std::vector to_insert; 63 | for (int i = 0; i < num_inserts; i++) { 64 | int insert = distribution(generator); 65 | to_insert.push_back(insert); 66 | } 67 | clock_gettime(CLOCK_MONOTONIC, &start); std::cout << "Starting inserts" << std::endl; 68 | for (int i = 0; i < num_inserts; i++) { 69 | bf.add(&i, sizeof(i)); 70 | } 71 | clock_gettime(CLOCK_MONOTONIC, &finish); 72 | double total_insert = (finish.tv_sec - start.tv_sec); 73 | total_insert += (finish.tv_nsec - start.tv_nsec) / 1000000000.0; 74 | 75 | 76 | std::cout << "Time: " << total_insert << " s" << std::endl; 77 | std::cout << "Inserts per second: " << (int) num_inserts / total_insert << " s" << std::endl; 78 | int fp = 0; 79 | for (int i = num_inserts; i < 2 * num_inserts; i++) { 80 | bool lookup = bf.mayContain(&i, sizeof(i)); 81 | if (lookup){ 82 | // cout << i << " found but didn't exist" << endl; 83 | fp++; 84 | } 85 | } 86 | cout << fp << endl; 87 | cout << "FP rate: " << ((double) fp / double(num_inserts)) << endl; 88 | 89 | 90 | 91 | 92 | 93 | 94 | } 95 | void insertLookupTest(){ 96 | std::random_device rand_dev; 97 | std::mt19937 generator(rand_dev()); 98 | std::uniform_int_distribution distribution(INT32_MIN, INT32_MAX); 99 | // std::normal_distribution distribution(0, 10000000); 100 | 101 | 102 | const int num_inserts = 1000000; 103 | const int num_runs = 20; 104 | const int buffer_capacity = 800; 105 | const double bf_fp = .001; 106 | const int pageSize = 512; 107 | const int disk_runs_per_level = 20; 108 | const double merge_fraction = 1; 109 | LSM lsmTree = LSM(buffer_capacity, num_runs,merge_fraction, bf_fp, pageSize, disk_runs_per_level); 110 | 111 | std::vector to_insert; 112 | for (int i = 0; i < num_inserts; i++) { 113 | int insert = static_cast(distribution(generator)); 114 | to_insert.push_back(insert); 115 | } 116 | // shuffle(to_insert.begin(), to_insert.end(), generator); 117 | 118 | std::cout << "Starting inserts" << std::endl; 119 | clock_gettime(CLOCK_MONOTONIC, &start); 120 | for (int i = 0; i < num_inserts; i++) { 121 | if ( i % 100000 == 0 ) cout << "insert " << i << endl; 122 | lsmTree.insert_key(to_insert[i],i); 123 | // lsmTree.printElts(); 124 | 125 | } 126 | clock_gettime(CLOCK_MONOTONIC, &finish); 127 | double total_insert = (finish.tv_sec - start.tv_sec); 128 | total_insert += (finish.tv_nsec - start.tv_nsec) / 1000000000.0; 129 | 130 | std::cout << "Time: " << total_insert << " s" << std::endl; 131 | std::cout << "Inserts per second: " << (int) num_inserts / total_insert << " s" << std::endl; 132 | 133 | 134 | std::cout << "Starting lookups" << std::endl; 135 | clock_gettime(CLOCK_MONOTONIC, &start); 136 | int lookup; 137 | for (int i = 0 ; i < num_inserts; i++) { 138 | if ( i % 100000 == 0 ) cout << "lookup " << i << endl; 139 | 140 | lsmTree.lookup(to_insert[i], lookup); 141 | } 142 | clock_gettime(CLOCK_MONOTONIC, &finish); 143 | double total_lookup = (finish.tv_sec - start.tv_sec); 144 | total_lookup += (finish.tv_nsec - start.tv_nsec) / 1000000000.0; 145 | std::cout << "Time: " << total_lookup << " s" << std::endl; 146 | std::cout << "Lookups per second: " << (int) num_inserts / total_lookup << " s" << std::endl; 147 | } 148 | void runInOrderTest() { 149 | const int num_inserts = 1000000; 150 | const int num_runs = 16; 151 | const int buffer_capacity = 1000; 152 | const double bf_fp = .2; 153 | const int pageSize = 4096; 154 | const int disk_runs_per_level = 10; 155 | const double merge_fraction = .2; 156 | LSM lsmTree = LSM(buffer_capacity, num_runs, merge_fraction, bf_fp, pageSize, disk_runs_per_level); 157 | 158 | 159 | std::vector to_insert; 160 | for (int i = 0; i < num_inserts; i++) { 161 | to_insert.push_back(100 * i); 162 | } 163 | std::cout << "Starting inserts" << std::endl; 164 | clock_gettime(CLOCK_MONOTONIC, &start); 165 | for (int i = 0; i < num_inserts; i++) { 166 | lsmTree.insert_key(to_insert[i], i); 167 | } 168 | 169 | for (int i = 0; i < num_runs; i++){ 170 | cout << "on run " << i << endl; 171 | auto all = lsmTree.C_0[i]->get_all(); 172 | 173 | for (int j = 0; j < lsmTree._eltsPerRun; j++){ 174 | 175 | auto kv = all[j]; 176 | cout << "K: " << kv.key << ", " << "V: " << kv.value << endl; 177 | } 178 | } 179 | } 180 | 181 | void diskLevelTest(){ 182 | // REDO TEST 183 | // const int num_inserts = 10000000; 184 | // const int max_levels = 16; 185 | // const int num_runs = 10; 186 | // const int buffer_capacity = 10000; 187 | // const double bf_fp = .0001; 188 | // const int pageSize = 4096; 189 | // const int disk_run_level = 10; 190 | // LSM lsmTree = LSM(buffer_capacity, num_runs, 2,.5, bf_fp, pageSize, disk_run_level); 191 | // 192 | // 193 | // std::vector to_insert; 194 | // for (int i = 0; i < num_inserts; i++) { 195 | // to_insert.push_back(i); 196 | // } 197 | // std::clock_t start_insert; 198 | // std::cout << "Starting inserts" << std::endl; 199 | // start_insert = std::clock(); 200 | // for (int i = 0; i < num_inserts; i++) { 201 | // lsmTree.insert_key(to_insert[i], i); 202 | // } 203 | // 204 | // vector> all = lsmTree.C_0[0]->get_all(); 205 | // int capacity = num_inserts * 2; 206 | // int numElts = all.size(); 207 | // int level = 1; 208 | // auto disklevel = DiskLevel(capacity, 4096, level); 209 | // 210 | } 211 | void customTest(LSMParams &lp, double &ips, double &lps){ 212 | std::random_device rand_dev; 213 | std::mt19937 generator(rand_dev()); 214 | std::uniform_int_distribution distribution(INT32_MIN, INT32_MAX); 215 | 216 | // unsigned long eltsPerRun, unsigned int numRuns, double merged_frac, double bf_fp, unsigned int pageSize, unsigned int diskRunsPerLevel 217 | LSM lsmTree = 218 | LSM(lp.elts_per_run, lp.num_runs, lp.merge_fraction, lp.bf_fp, lp.pageSize, lp.disk_runs_per_level); 219 | 220 | std::vector to_insert; 221 | for (int i = 0; i < lp.num_inserts; i++) { 222 | to_insert.push_back(i); 223 | } 224 | shuffle(to_insert.begin(), to_insert.end(), generator); 225 | 226 | clock_gettime(CLOCK_MONOTONIC, &start); 227 | for (int i = 0; i < lp.num_inserts; i++) { 228 | lsmTree.insert_key(to_insert[i],i); 229 | } 230 | clock_gettime(CLOCK_MONOTONIC, &finish); 231 | double total_insert = (finish.tv_sec - start.tv_sec); 232 | total_insert += (finish.tv_nsec - start.tv_nsec) / 1000000000.0; 233 | 234 | clock_gettime(CLOCK_MONOTONIC, &start); 235 | int lookup; 236 | for (int i = 0 ; i < lp.num_inserts; i++) { 237 | 238 | lsmTree.lookup(to_insert[i], lookup); 239 | } 240 | clock_gettime(CLOCK_MONOTONIC, &finish); 241 | double total_lookup = (finish.tv_sec - start.tv_sec); 242 | total_lookup += (finish.tv_nsec - start.tv_nsec) / 1000000000.0; 243 | double ipersec = lp.num_inserts / total_insert; 244 | double lpersec = lp.num_inserts / total_lookup; 245 | ips = ipersec; 246 | lps = lpersec; 247 | cout << lp.num_inserts << "," << lp.num_runs << "," << lp.elts_per_run << "," << lp.bf_fp << "," << lp.merge_fraction << "," << lp.pageSize << "," << lp.disk_runs_per_level << "," << ipersec << "," << lpersec << "," << total_insert << "," << total_lookup << endl; 248 | } 249 | void cartesianTest(){ 250 | cout << "num_inserts,num_runs,elts_per_run,BF_FP,merge_fraction,page_size,disk_runs_per_level,inserts_per_sec,lookups_per_sec,total_insert,total_lookup" << endl; 251 | vector numins = {10000000}; 252 | vector numruns = {50}; 253 | vector eltspers = {800}; 254 | vector bf_fp = {.001}; 255 | vector merge_frac = {.5, .75, 1.0}; 256 | vector pss = {1024}; 257 | vector drpl = {5, 10, 20}; 258 | auto res = vector>(); 259 | 260 | for (int i = 0; i < numins.size(); i++) 261 | for(int n = 0; n < numruns.size(); n++) 262 | for(int b = 0; b < eltspers.size();b++) 263 | for(int bf = 0; bf < bf_fp.size(); bf++) 264 | for (int m = 0; m < merge_frac.size(); m++) 265 | for (int p = 0; p < pss.size(); p++) 266 | for (int d = 0; d < drpl.size(); d++){ 267 | // unsigned long eltsPerRun, unsigned int numRuns, double merged_frac, double bf_fp, unsigned int pageSize, unsigned int diskRunsPerLevel 268 | // struct LSMParams { 269 | // const int num_inserts; 270 | // const int num_runs; 271 | // const int elts_per_run; 272 | // const double bf_fp; 273 | // const int pageSize; 274 | // const int disk_runs_per_level; 275 | // const double merge_fraction; 276 | // }; 277 | LSMParams lp = {numins[i], numruns[n], eltspers[b], bf_fp[bf], pss[p], drpl[d], merge_frac[m]}; 278 | double ips, lps; 279 | customTest(lp, ips, lps); 280 | // res.push_back(tuple(ips, lps, lp)); 281 | } 282 | // sort(res.begin(), res.end()); 283 | // int p10 = ceil(.10 * res.size()); 284 | // res.erase(res.begin() + p10, res.end()); 285 | // ofstream output("binary.data"); 286 | // output.write(static_cast(&(res[0])), res.size()*sizeof(tuple)); 287 | 288 | 289 | } 290 | //void bfPerfTest(){ 291 | // vector numruns = {.00001, .000001}; 292 | // for (int i = 0; i < numruns.size(); i++) 293 | // customTest(1000000, 100, 100000, numruns[i], 0.8,1000, 50); 294 | //} 295 | void fencePointerTest(){ 296 | // TODO REDO THIS TEST 297 | // const long num_inserts = 1000 * 1000 * 1; 298 | // const int num_lookups = 10000000; 299 | // const int blocks = 16; 300 | // const long pageSize = 100; 301 | // const int num_runs = 10; 302 | // const long runSize = ceil(num_inserts / num_runs); 303 | // std::random_device rand_dev; 304 | // std::mt19937 generator(rand_dev()); 305 | // std::uniform_int_distribution distribution(0, (int) (num_inserts * 1.2)); 306 | // 307 | // std::vector> to_insert; 308 | // auto dl = DiskLevel(pageSize, 1, runSize, num_runs, 1); 309 | // // 310 | // 311 | // cout << "reserving" << endl; 312 | // to_insert.reserve(num_inserts); 313 | // cout << "pushing" << endl; 314 | // for (int b = 0; b < blocks; b++){ 315 | // for (int i = b * (num_inserts / blocks); i < (b + 1) * num_inserts / blocks; i++) { 316 | // if (i % 1000000 == 0) cout << "insert " << i << endl; 317 | // 318 | // to_insert.push_back((KVPair) {i, i}); 319 | // } 320 | // dl.merge(&to_insert[0], num_inserts / blocks); 321 | // to_insert.resize(0); 322 | // 323 | // } 324 | // 325 | // auto to_lookup = vector(); 326 | // to_lookup.reserve(num_lookups); 327 | // for (int i = 0; i< num_lookups; i++){ 328 | // to_lookup.push_back(distribution(generator)); 329 | // } 330 | // cout << "lookups" << endl; 331 | // std::clock_t start_lookup; 332 | // start_lookup = std::clock(); 333 | // 334 | // for (int i = 0 ; i < num_lookups; i++) { 335 | // if (i % 1000000 == 0) cout << "lookup " << i << endl; 336 | // int lookup = dl.lookup(to_insert[to_lookup[i]].key); 337 | // } 338 | // double total_lookup = (std::clock() - start_lookup) / (double)(CLOCKS_PER_SEC); 339 | // 340 | // double lpersec = num_lookups / total_lookup; 341 | // cout << num_inserts << "," << pageSize << "," << lpersec << "," << total_lookup << endl; 342 | // 343 | // 344 | // 345 | } 346 | 347 | void updateDeleteTest(){ 348 | const int num_inserts = 500; 349 | const int num_runs = 3; 350 | const int buffer_capacity = 50; 351 | const double bf_fp = .01; 352 | const int pageSize = 1024; 353 | const int disk_runs_per_level = 2; 354 | const double merge_fraction = 1; 355 | LSM lsmTree = LSM(buffer_capacity, num_runs,merge_fraction, bf_fp, pageSize, disk_runs_per_level); 356 | 357 | std::vector to_insert; 358 | for (int i = 0; i < num_inserts; i++) { 359 | to_insert.push_back(i); 360 | } 361 | 362 | for (int i = 0; i < num_inserts; i++) { 363 | lsmTree.insert_key(i, to_insert[i]); 364 | } 365 | int lookup; 366 | for (int i = 0; i < num_inserts; i++) { 367 | 368 | lsmTree.lookup(i, lookup); 369 | assert(to_insert[i] == lookup); 370 | } 371 | lsmTree.printStats(); // this is a good demo 372 | cout << "-----------------------------------------" << endl; 373 | for (int i = 0; i < num_inserts; i++) { 374 | to_insert[i] = num_inserts - i; 375 | } 376 | 377 | for (int i = 0; i < num_inserts; i++) { 378 | lsmTree.insert_key(i, to_insert[i]); 379 | } 380 | lsmTree.printStats(); // this is a good demo 381 | cout << "-----------------------------------------" << endl; 382 | for (int i = 0; i < num_inserts; i++) { 383 | lsmTree.lookup(i, lookup); 384 | assert(to_insert[i] == lookup); 385 | } 386 | 387 | for (int i = 0; i < num_inserts; i++) { 388 | lsmTree.delete_key(i); 389 | 390 | } 391 | lsmTree.printStats(); // this is a good demo 392 | cout << "-----------------------------------------" << endl; 393 | 394 | int negone = -1; 395 | for (int i = 0; i < num_inserts * 10; i++) { 396 | lsmTree.insert_key(i, negone); 397 | } 398 | for (int i = 0; i < num_inserts * 10; i++) { 399 | 400 | lsmTree.lookup(i, lookup); 401 | assert(lookup == -1); 402 | } 403 | lsmTree.printStats(); // this is a good demo 404 | cout << "-----------------------------------------" << endl; 405 | 406 | 407 | 408 | 409 | } 410 | void rangeTimeTest(){ 411 | const int num_inserts = 50000000; 412 | const int num_runs = 20; 413 | const int buffer_capacity = 500; 414 | const double bf_fp = .01; 415 | const int pageSize = 512; 416 | const int disk_runs_per_level = 5; 417 | const double merge_fraction = 1; 418 | LSM lsmTree = LSM(buffer_capacity, num_runs,merge_fraction, bf_fp, pageSize, disk_runs_per_level); 419 | 420 | std::vector to_insert; 421 | 422 | for (int i = 0; i < num_inserts; i++) { 423 | to_insert.push_back(i); 424 | } 425 | shuffle(to_insert.begin(), to_insert.end(), generator); 426 | 427 | for (int i = 0; i < num_inserts; i++) { 428 | lsmTree.insert_key(to_insert[i], i); 429 | } 430 | cout << "range_size time" << endl; 431 | for (int i = 20; i < 20000001; i *= 10){ 432 | 433 | int n1 = -i; 434 | int n2 = i; 435 | 436 | clock_gettime(CLOCK_MONOTONIC, &start); 437 | 438 | lsmTree.range(n1, n2); 439 | clock_gettime(CLOCK_MONOTONIC, &finish); 440 | double total_range= (finish.tv_sec - start.tv_sec); 441 | total_range += (finish.tv_nsec - start.tv_nsec) / 1000000000.0; 442 | cout << i << " " << total_range << endl; 443 | 444 | } 445 | } 446 | void rangeTest(){ 447 | const int num_inserts = 10000000; 448 | const int num_runs = 20; 449 | const int buffer_capacity = 500; 450 | const double bf_fp = .01; 451 | const int pageSize = 512; 452 | const int disk_runs_per_level = 5; 453 | const double merge_fraction = 1; 454 | LSM lsmTree = LSM(buffer_capacity, num_runs,merge_fraction, bf_fp, pageSize, disk_runs_per_level); 455 | 456 | std::vector to_insert; 457 | 458 | for (int i = 0; i < num_inserts; i++) { 459 | to_insert.push_back(i); 460 | } 461 | shuffle(to_insert.begin(), to_insert.end(), generator); 462 | 463 | for (int i = 0; i < num_inserts; i++) { 464 | lsmTree.insert_key(to_insert[i], i); 465 | } 466 | 467 | int n1 = 0; 468 | int n2 = 5000000; 469 | auto r = lsmTree.range(n1, n2); 470 | assert(r.size() == (n2 - n1)); 471 | int negone = -1; 472 | for (int i = n1; i < n2; i++) { 473 | lsmTree.insert_key(i, negone); 474 | } 475 | r = lsmTree.range(n1, n2); 476 | assert(r.size() == (n2 - n1)); 477 | int nd = 2000000; 478 | 479 | for (int i = n1; i < n1 + nd; i++) { 480 | lsmTree.delete_key(i); 481 | 482 | } 483 | 484 | r = lsmTree.range(n1, n2); 485 | assert(r.size() == (n2 - n1 - nd)); 486 | // lsmTree.printElts(); 487 | } 488 | 489 | void concurrentLookupTest(){ 490 | std::random_device rand_dev; 491 | std::mt19937 generator(rand_dev()); 492 | 493 | 494 | 495 | const int num_inserts = 3000000; 496 | const int num_lookups = 1000000; 497 | const int num_runs = 50; 498 | const int buffer_capacity = 800; 499 | const double bf_fp = .001; 500 | const int pageSize = 512; 501 | const int disk_runs_per_level = 10; 502 | const double merge_fraction = 1; 503 | cout << "iv ips" << endl; 504 | for (double d = 10; d < 1000000000; d *= 100){ 505 | std::normal_distribution distribution1(0, d); 506 | auto lsmTree = LSM(buffer_capacity, num_runs,merge_fraction, bf_fp, pageSize, disk_runs_per_level); 507 | 508 | std::vector to_insert; 509 | vector to_lookup; 510 | for (int i = 0; i < num_inserts; i++) { 511 | int insert = (int) distribution1(generator); 512 | to_insert.push_back(insert); 513 | } 514 | 515 | 516 | // shuffle(to_insert.begin(), to_insert.end(), generator); 517 | 518 | // std::cout << "Starting inserts" << std::endl; 519 | clock_gettime(CLOCK_MONOTONIC, &start); 520 | 521 | for (int i = 0; i < num_inserts; i++) { 522 | // if ( i % 100000 == 0 ) cout << "insert " << i << endl; 523 | lsmTree.insert_key(to_insert[i],i); 524 | 525 | } 526 | clock_gettime(CLOCK_MONOTONIC, &finish); 527 | double total_insert = (finish.tv_sec - start.tv_sec); 528 | total_insert += (finish.tv_nsec - start.tv_nsec) / 1000000000.0; 529 | // std::cout << "Time: " << total_insert << " s" << std::endl; 530 | // std::cout << "Inserts per second: " << (int) num_inserts / total_insert << " s" << std::endl; 531 | 532 | cout << d << " " << (int) num_inserts / total_insert << endl; 533 | } 534 | auto lsmTree = LSM(buffer_capacity, num_runs,merge_fraction, bf_fp, pageSize, disk_runs_per_level); 535 | std::uniform_int_distribution distribution(INT_MIN, INT_MAX); 536 | 537 | std::vector to_insert; 538 | vector to_lookup; 539 | for (int i = 0; i < num_inserts; i++) { 540 | int insert_late = (int) distribution(generator); 541 | to_insert.push_back(insert_late); 542 | } 543 | for (int i = 0; i < num_inserts; i++) { 544 | lsmTree.insert_key(to_insert[i],i); 545 | 546 | } 547 | 548 | 549 | sleep(2); 550 | // std::cout << "Starting lookups" << std::endl; 551 | // int nthreads = nt; 552 | cout << "variance nthreads time lookups/sec" << endl; 553 | for (double lv = 10; lv < num_lookups * 2; lv *= 1000){ 554 | std::normal_distribution distribution2(0, lv); 555 | 556 | for (int i = 0; i < num_lookups; i++) { 557 | int lookup = (int) distribution2(generator); 558 | to_lookup.push_back(lookup); 559 | } 560 | 561 | for (int i = 1; i <= 3; i += 1){ 562 | 563 | struct timespec start, finish; 564 | 565 | clock_gettime(CLOCK_MONOTONIC, &start); 566 | int nthreads = i; 567 | 568 | auto threads = vector(nthreads); 569 | 570 | 571 | for (int t = 0; t < nthreads; t++){ 572 | threads[t] = thread ([&] { 573 | unsigned m = rand(); 574 | int lookup; 575 | for (int i = 0 ; i < num_lookups; i++) { 576 | // cout << (1737119 * m * i) % to_insert.size() << endl; 577 | lsmTree.lookup(to_lookup[(1737119 * m * i) % to_lookup.size()], lookup); 578 | } 579 | 580 | }); 581 | } 582 | for (int t = 0; t < nthreads; t++) 583 | threads[t].join(); 584 | 585 | 586 | 587 | clock_gettime(CLOCK_MONOTONIC, &finish); 588 | 589 | double total_lookup = (finish.tv_sec - start.tv_sec); 590 | total_lookup += (finish.tv_nsec - start.tv_nsec) / 1000000000.0; 591 | 592 | // cout << "Number of Threads: " << nthreads << endl; 593 | // std::cout << "Time: " << total_lookup << " s" << std::endl; 594 | // std::cout << "Lookups per second: " << (int) nthreads * num_lookups / total_lookup << " s" << std::endl; 595 | std::cout << lv << " " << nthreads << " " << total_lookup << " " << (int) nthreads * num_lookups / total_lookup << endl; 596 | } 597 | } 598 | } 599 | 600 | void tailLatencyTest(){ 601 | std::random_device rand_dev; 602 | std::mt19937 generator(rand_dev()); 603 | std::uniform_int_distribution distribution(INT32_MIN, INT32_MAX); 604 | 605 | 606 | const int num_inserts = 10000000; 607 | const int num_runs = 200; 608 | const int buffer_capacity = 2000; 609 | const double bf_fp = .001; 610 | const int pageSize = 512; 611 | const int disk_runs_per_level = 2; 612 | const double merge_fraction = 1; 613 | LSM lsmTree = LSM(buffer_capacity, num_runs,merge_fraction, bf_fp, pageSize, disk_runs_per_level); 614 | 615 | std::vector to_insert; 616 | for (int i = 0; i < num_inserts; i++) { 617 | // int insert = distribution(generator); 618 | to_insert.push_back(i); 619 | } 620 | shuffle(to_insert.begin(), to_insert.end(), generator); 621 | 622 | auto times = vector(num_inserts); 623 | 624 | // std::cout << "Starting inserts" << std::endl; 625 | 626 | for (int i = 0; i < num_inserts; i++) { 627 | clock_gettime(CLOCK_MONOTONIC, &start); 628 | lsmTree.insert_key(to_insert[i],i); 629 | clock_gettime(CLOCK_MONOTONIC, &finish); 630 | times.push_back((finish.tv_sec - start.tv_sec) + (finish.tv_nsec - start.tv_nsec) / 1000000000.); 631 | } 632 | sort(times.begin(), times.end()); 633 | cout << "largest latency: " << times[times.size() - 1] << endl; 634 | cout << "smallest latency: " << times[0] << endl; 635 | 636 | } 637 | 638 | void hardCodeTest(int num_inserts, int num_runs, int elts_per_run, double bf_fp, double merge_fraction, int pageSize, int disk_runs_per_level){ 639 | std::random_device rand_dev; 640 | std::mt19937 generator(rand_dev()); 641 | std::uniform_int_distribution distribution(INT32_MIN, INT32_MAX); 642 | 643 | // unsigned long eltsPerRun, unsigned int numRuns, double merged_frac, double bf_fp, unsigned int pageSize, unsigned int diskRunsPerLevel 644 | LSM lsmTree = 645 | LSM(elts_per_run, num_runs, merge_fraction, bf_fp, pageSize, disk_runs_per_level); 646 | 647 | std::vector to_insert; 648 | for (int i = 0; i < num_inserts; i++) { 649 | to_insert.push_back(i); 650 | } 651 | shuffle(to_insert.begin(), to_insert.end(), generator); 652 | 653 | clock_gettime(CLOCK_MONOTONIC, &start); 654 | for (int i = 0; i < num_inserts; i++) { 655 | lsmTree.insert_key(to_insert[i],i); 656 | } 657 | clock_gettime(CLOCK_MONOTONIC, &finish); 658 | double total_insert = (finish.tv_sec - start.tv_sec); 659 | total_insert += (finish.tv_nsec - start.tv_nsec) / 1000000000.0; 660 | 661 | clock_gettime(CLOCK_MONOTONIC, &start); 662 | int lookup; 663 | for (int i = 0 ; i < num_inserts; i++) { 664 | 665 | lsmTree.lookup(to_insert[i], lookup); 666 | } 667 | clock_gettime(CLOCK_MONOTONIC, &finish); 668 | double total_lookup = (finish.tv_sec - start.tv_sec); 669 | total_lookup += (finish.tv_nsec - start.tv_nsec) / 1000000000.0; 670 | double ipersec = num_inserts / total_insert; 671 | double lpersec = num_inserts / total_lookup; 672 | cout << num_inserts << "," << num_runs << "," << elts_per_run << "," << bf_fp << "," << merge_fraction << "," << pageSize << "," << disk_runs_per_level << "," << ipersec << "," << lpersec << "," << total_insert << "," << total_lookup << endl; 673 | } 674 | 675 | void updateLookupSkewTest(){ 676 | std::random_device rand_dev; 677 | std::mt19937 generator(rand_dev()); 678 | 679 | 680 | 681 | const int num_total = 1000000; 682 | const int num_runs = 80; 683 | const int buffer_capacity = 800; 684 | const double bf_fp = .001; 685 | const int pageSize = 512; 686 | const int disk_runs_per_level = 20; 687 | const double merge_fraction = 1; 688 | 689 | cout << "lookup_pct total_time" << endl; 690 | for (double i = .01; i < .95; i+=.1){ 691 | LSM lsmTree = LSM(buffer_capacity, num_runs,merge_fraction, bf_fp, pageSize, disk_runs_per_level); 692 | 693 | std::uniform_int_distribution distribution(0, INT_MAX); 694 | std::vector to_query; 695 | for (int j = 0; j < num_total; j++) { 696 | 697 | int num = (int) distribution(generator); 698 | to_query.push_back(num); 699 | } 700 | int lookup; 701 | clock_gettime(CLOCK_MONOTONIC, &start); 702 | 703 | for (int j = 0; j < num_total; ++j){ 704 | if (to_query[j] < (int) floor(i * INT_MAX)){ 705 | 706 | lsmTree.lookup(to_query[j], lookup); 707 | // cout << "lookup " << to_query[j] << endl; 708 | } 709 | else { 710 | lsmTree.insert_key(to_query[j], j); 711 | // cout << "insert " << to_query[j] << endl; 712 | } 713 | } 714 | clock_gettime(CLOCK_MONOTONIC, &finish); 715 | double total = (finish.tv_sec - start.tv_sec); 716 | total += (finish.tv_nsec - start.tv_nsec) / 1000000000.0; 717 | 718 | cout << i << " " << total << endl; 719 | } 720 | 721 | 722 | } 723 | 724 | void loadFromBin(LSM &lsm, string filename){ 725 | FILE *intArrayFile; 726 | long size; 727 | 728 | 729 | intArrayFile = fopen(filename.c_str(), "rb"); 730 | fseek(intArrayFile, 0, SEEK_END); 731 | size = ftell(intArrayFile); 732 | 733 | int new_array[size / sizeof(int)]; 734 | 735 | rewind(intArrayFile); 736 | size_t num; 737 | num = fread(new_array, sizeof(int), size/sizeof(int) + 1, intArrayFile); 738 | assert(num == size / sizeof(int)); 739 | 740 | int *ptr = new_array; 741 | int read = 0; 742 | int k,v; 743 | while (read + 1 < num){ 744 | k = *ptr; 745 | v = *(ptr + 1); 746 | lsm.insert_key(k, v); 747 | ptr += 2; 748 | read += 2; 749 | } 750 | } 751 | 752 | 753 | void queryLine(LSM &lsm, const string &line, vector &strings){ 754 | unsigned long pos = line.find(' '); 755 | unsigned long ip = 0; 756 | strings.clear(); 757 | 758 | // Decompose statement 759 | while( pos != string::npos ) { 760 | strings.push_back( line.substr( ip, pos - ip + 1 ) ); 761 | ip = pos + 1; 762 | 763 | pos = line.find( ' ', ip ); 764 | } 765 | 766 | // Add the last one 767 | strings.push_back( line.substr( ip, (pos < line.size() ? pos : line.size()) - ip + 1 ) ); 768 | 769 | switch ((char) strings[0].c_str()[0]){ 770 | case 'p':{ 771 | int pk = stoi(strings[1]); 772 | int v = stoi(strings[2]); 773 | lsm.insert_key(pk, v); 774 | } 775 | break; 776 | case 'g': { 777 | int lk = stoi(strings[1]); 778 | int v; 779 | bool found = lsm.lookup(lk, v); 780 | if (found) { 781 | cout << v; 782 | } 783 | 784 | cout << endl; 785 | } 786 | break; 787 | case 'r':{ 788 | int lk1 = stoi(strings[1]); 789 | int lk2 = stoi(strings[2]); 790 | auto res = lsm.range(lk1, lk2); 791 | if (!res.empty()){ 792 | for (int i = 0; i < res.size(); ++i){ 793 | cout << res[i].key << ":" << res[i].value << " "; 794 | } 795 | } 796 | cout << endl; 797 | 798 | } 799 | break; 800 | case 'd': { 801 | int dk = stoi(strings[1]); 802 | lsm.delete_key(dk); 803 | } 804 | break; 805 | case 'l': { 806 | string ls = strings[1]; 807 | loadFromBin(lsm, ls); 808 | } 809 | break; 810 | case 's': { 811 | lsm.printStats(); 812 | } 813 | 814 | 815 | } 816 | 817 | } 818 | int main(int argc, char *argv[]){ 819 | 820 | // insertLookupTest(); 821 | // updateDeleteTest(); 822 | // rangeTest(); 823 | // rangeTimeTest(); 824 | // concurrentLookupTest(); 825 | // tailLatencyTest(); 826 | // cartesianTest(); 827 | // updateLookupSkewTest(); 828 | 829 | auto lsm = LSM(800,20,1.0,0.00100,1024,20); 830 | auto strings = vector(3); 831 | if (argc == 2){ 832 | cout << "LSM Tree DSL Interactive Mode" << endl; 833 | while (true){ 834 | cout << "> "; 835 | string input; 836 | getline(cin, input); 837 | queryLine(lsm, input, strings); 838 | } 839 | } 840 | else{ 841 | string line; 842 | ifstream f; 843 | for (int i = 1; i < argc; ++i){ 844 | f.open(argv[i]); 845 | 846 | if(!f.is_open()) { 847 | perror("Error open"); 848 | exit(EXIT_FAILURE); 849 | } 850 | while(getline(f, line)) { 851 | queryLine(lsm, line, strings); 852 | } 853 | } 854 | } 855 | 856 | 857 | 858 | 859 | 860 | 861 | 862 | 863 | 864 | 865 | return 0; 866 | 867 | } 868 | -------------------------------------------------------------------------------- /src/run.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // run.hpp 3 | // lsm-tree 4 | // 5 | // sLSM: Skiplist-Based LSM Tree 6 | // Copyright © 2017 Aron Szanto. All rights reserved. 7 | // 8 | // This program is free software: you can redistribute it and/or modify 9 | // it under the terms of the GNU General Public License as published by 10 | // the Free Software Foundation, either version 3 of the License, or 11 | // (at your option) any later version. 12 | // 13 | // This program is distributed in the hope that it will be useful, 14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | // GNU General Public License for more details. 17 | // 18 | // You should have received a copy of the GNU General Public License 19 | // along with this program. If not, see . 20 | // 21 | #pragma once 22 | 23 | #ifndef RUN_H 24 | #define RUN_H 25 | #include 26 | #include 27 | #include 28 | using namespace std; 29 | 30 | 31 | template 32 | struct KVPair { 33 | 34 | K key; 35 | V value; 36 | 37 | // bool tombstone; 38 | 39 | 40 | 41 | bool operator==(KVPair kv) const { 42 | return (kv.key == key && kv.value == value); 43 | } 44 | bool operator!=(KVPair kv) const { 45 | return (kv.key != key != kv.value != value); 46 | } 47 | 48 | bool operator<(KVPair kv) const{ 49 | return key < kv.key; 50 | } 51 | 52 | bool operator>(KVPair kv) const{ 53 | return key > kv.key; 54 | } 55 | 56 | 57 | }; 58 | 59 | 60 | 61 | template 62 | class Run { 63 | 64 | public: 65 | virtual K get_min() = 0; 66 | virtual K get_max() = 0; 67 | virtual void insert_key(const K &key, const V &value) = 0; 68 | virtual void delete_key(const K &key) = 0; 69 | virtual V lookup(const K &key, bool &found) = 0; 70 | virtual unsigned long long num_elements() = 0; 71 | virtual void set_size(const unsigned long size) = 0; 72 | virtual vector> get_all() = 0; 73 | virtual vector> get_all_in_range(const K &key1, const K &key2) = 0; 74 | virtual ~Run() { } 75 | }; 76 | 77 | 78 | 79 | 80 | #endif /* run_h */ 81 | 82 | -------------------------------------------------------------------------------- /src/skipList.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // skiplist.hpp 3 | // lsm-tree 4 | // 5 | // sLSM: Skiplist-Based LSM Tree 6 | // Copyright © 2017 Aron Szanto. All rights reserved. 7 | // 8 | // This program is free software: you can redistribute it and/or modify 9 | // it under the terms of the GNU General Public License as published by 10 | // the Free Software Foundation, either version 3 of the License, or 11 | // (at your option) any later version. 12 | // 13 | // This program is distributed in the hope that it will be useful, 14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | // GNU General Public License for more details. 17 | // 18 | // You should have received a copy of the GNU General Public License 19 | // along with this program. If not, see . 20 | // 21 | #pragma once 22 | 23 | #ifndef SKIPLIST_H 24 | #define SKIPLIST_H 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include "run.hpp" 33 | using namespace std; 34 | 35 | default_random_engine generator; 36 | uniform_real_distribution distribution(0.0,1.0); 37 | const double NODE_PROBABILITY = 0.5; 38 | 39 | 40 | template 41 | class SkipList_Node { 42 | 43 | 44 | 45 | public: 46 | const K key; 47 | V value; 48 | SkipList_Node* _forward[MAXLEVEL+1]; 49 | 50 | 51 | SkipList_Node(const K searchKey):key(searchKey) { 52 | for (int i=1; i<=MAXLEVEL; i++) { 53 | _forward[i] = NULL; 54 | } 55 | } 56 | 57 | SkipList_Node(const K searchKey,const V val):key(searchKey),value(val) { 58 | for (int i=1; i<=MAXLEVEL; i++) { 59 | _forward[i] = NULL; 60 | } 61 | } 62 | 63 | virtual ~SkipList_Node(){} 64 | }; 65 | 66 | 67 | template 68 | class SkipList : public Run 69 | { 70 | public: 71 | 72 | typedef SkipList_Node Node; 73 | 74 | const int max_level; 75 | K min; 76 | K max; 77 | 78 | SkipList(const K minKey,const K maxKey):p_listHead(NULL),p_listTail(NULL), 79 | cur_max_level(1),max_level(MAXLEVEL), min((K) NULL), max((K) NULL), 80 | _minKey(minKey),_maxKey(maxKey), _n(0) 81 | { 82 | p_listHead = new Node(_minKey); 83 | p_listTail = new Node(_maxKey); 84 | for (int i=1; i<=MAXLEVEL; i++) { 85 | p_listHead->_forward[i] = p_listTail; 86 | } 87 | } 88 | 89 | ~SkipList() 90 | { 91 | Node* currNode = p_listHead->_forward[1]; 92 | while (currNode != p_listTail) { 93 | Node* tempNode = currNode; 94 | currNode = currNode->_forward[1]; 95 | delete tempNode; 96 | } 97 | delete p_listHead; 98 | delete p_listTail; 99 | } 100 | 101 | void insert_key(const K &key, const V &value) { 102 | if (key > max){ 103 | max = key; 104 | } 105 | else if (key < min){ 106 | min = key; 107 | } 108 | Node* update[MAXLEVEL]; 109 | Node* currNode = p_listHead; 110 | for(int level = cur_max_level; level > 0; level--) { 111 | while (currNode->_forward[level]->key < key) { 112 | currNode = currNode->_forward[level]; 113 | } 114 | update[level] = currNode; 115 | } 116 | currNode = currNode->_forward[1]; 117 | if (currNode->key == key) { 118 | // update the value if the key already exists 119 | currNode->value = value; 120 | } 121 | else { 122 | // if key isn't in the list, insert a new node! 123 | int insertLevel = generateNodeLevel(); 124 | 125 | if (insertLevel > cur_max_level && insertLevel < MAXLEVEL - 1) { 126 | for (int lv = cur_max_level + 1; lv <= insertLevel; lv++) { 127 | update[lv] = p_listHead; 128 | } 129 | cur_max_level = insertLevel; 130 | } 131 | currNode = new Node(key,value); 132 | for (int level = 1; level <= cur_max_level; level++) { 133 | currNode->_forward[level] = update[level]->_forward[level]; 134 | update[level]->_forward[level] = currNode; 135 | } 136 | ++_n; 137 | 138 | } 139 | 140 | 141 | } 142 | 143 | void delete_key(const K &searchKey) { 144 | // SkipList_Node* update[MAXLEVEL]; 145 | Node* update[MAXLEVEL]; 146 | Node* currNode = p_listHead; 147 | for(int level=cur_max_level; level >=1; level--) { 148 | while (currNode->_forward[level]->key < searchKey) { 149 | currNode = currNode->_forward[level]; 150 | } 151 | update[level] = currNode; 152 | } 153 | currNode = currNode->_forward[1]; 154 | if (currNode->key == searchKey) { 155 | for (int level = 1; level <= cur_max_level; level++) { 156 | if (update[level]->_forward[level] != currNode) { 157 | break; 158 | } 159 | update[level]->_forward[level] = currNode->_forward[level]; 160 | } 161 | delete currNode; 162 | // update the max level 163 | while (cur_max_level > 1 && p_listHead->_forward[cur_max_level] == NULL) { 164 | cur_max_level--; 165 | } 166 | } 167 | _n--; 168 | } 169 | 170 | V lookup(const K &searchKey, bool &found) { 171 | Node* currNode = p_listHead; 172 | for(int level=cur_max_level; level >=1; level--) { 173 | while (currNode->_forward[level]->key < searchKey) { 174 | currNode = currNode->_forward[level]; 175 | } 176 | } 177 | currNode = currNode->_forward[1]; 178 | if (currNode->key == searchKey) { 179 | found = true; 180 | return currNode->value; 181 | } 182 | else { 183 | return (V) NULL; 184 | } 185 | } 186 | 187 | vector> get_all(){ 188 | vector> vec = vector>(); 189 | auto node = p_listHead->_forward[1]; 190 | while ( node != p_listTail){ 191 | KVPair kv = {node->key, node->value}; 192 | vec.push_back(kv); 193 | // TODO: optimize by reserving space before hand 194 | node = node->_forward[1]; 195 | } 196 | return vec; 197 | } 198 | 199 | vector> get_all_in_range(const K &key1, const K &key2){ 200 | if (key1 > max || key2 < min){ 201 | return (vector>) {}; 202 | } 203 | 204 | vector> vec = vector>(); 205 | auto node = p_listHead->_forward[1]; 206 | while ( node->key < key1){ 207 | node = node->_forward[1]; 208 | } 209 | 210 | while ( node->key < key2){ 211 | KVPair kv = {node->key, node->value}; 212 | vec.push_back(kv); 213 | node = node->_forward[1]; 214 | } 215 | return vec; 216 | 217 | 218 | } 219 | 220 | 221 | bool eltIn(K &key) { 222 | return lookup(key); 223 | } 224 | 225 | inline bool empty() { 226 | return (p_listHead->_forward[1] == p_listTail); 227 | } 228 | 229 | 230 | unsigned long long num_elements() { 231 | return _n; 232 | } 233 | 234 | K get_min(){ 235 | return min; 236 | } 237 | 238 | K get_max(){ 239 | return max; 240 | } 241 | 242 | void set_size(unsigned long size){ 243 | _maxSize = size; 244 | } 245 | 246 | size_t get_size_bytes(){ 247 | return _n * (sizeof(K) + sizeof(V)); 248 | } 249 | 250 | // private: 251 | 252 | int generateNodeLevel() { 253 | 254 | return ffs(rand() & ((1 << MAXLEVEL) - 1)) - 1; 255 | } 256 | 257 | K _minKey; 258 | K _maxKey; 259 | unsigned long long _n; 260 | size_t _maxSize; 261 | int cur_max_level; 262 | Node* p_listHead; 263 | Node* p_listTail; 264 | uint32_t _keysPerLevel[MAXLEVEL]; 265 | 266 | }; 267 | 268 | 269 | 270 | #endif /* skiplist_h */ 271 | --------------------------------------------------------------------------------